git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/4] libgit: Expose more worktree functionality.

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 21, 2019, 01:59 UTC
Message-ID
<xmqqpniqn9ju.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20191018194542.1316981-2-pjones@redhat.com>
Peter Jones <pjones@redhat.com> writes:

Same comment on the commit title as 1/4; also, we tend not to upcase the first word after the <area>: word and omit the full-stop on the title (see "git shortlog -32 --no-merges" on our project for examples).

> Add delete_worktrees_dir_if_empty() and prune_worktree() to the public
> API, so they can be used from more places.  Also add a new function,
> prune_worktree_if_missing(), which prunes unlocked worktrees if they
> aren't present on the filesystem.

It probably is cleaner to do the "also" part as a separate step, as that allows readers to skip this step without reading it deeply, but let's see how it is done.

Show 15 quoted lines
> @@ -144,7 +73,7 @@ static void prune_worktrees(void)
>  		if (is_dot_or_dotdot(d->d_name))
>  			continue;
>  		strbuf_reset(&reason);
> -		if (!prune_worktree(d->d_name, &reason))
> +		if (!prune_worktree(d->d_name, &reason, expire))
>  			continue;
>  		if (show_only || verbose)
>  			printf("%s\n", reason.buf);
> diff --git a/worktree.c b/worktree.c
> index 4924805c389..08454a4e65d 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -608,3 +608,91 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
> +int prune_worktree(const char *id, struct strbuf *reason, timestamp_t expire)

This is not a mere code movement, because the original relied on the file-scope static "expire", and the public version wants to give callers control over the expiration value. That is a good change that deserves to be advertised and explained in the proposed log message.

Show 11 quoted lines
> +int prune_worktree_if_missing(const struct worktree *wt)
> +{
> +	struct strbuf reason = STRBUF_INIT;
> +	int ret;
> +
> +	if (is_worktree_locked(wt) ||
> +	    access(wt->path, F_OK) >= 0 ||
> +	    (errno != ENOENT && errno == ENOTDIR)) {
> +		errno = EEXIST;
> +		return -1;
> +	}

When access() failed but not because the named path did not exist (i.e. the directory may still exist---it is just this invocation of the process happened to fail to see it---or it may not exist but we cannot see far enough to notice that it does not exist) then we play safe, assume it does exist, and refrain from calling prune_worktree() on it. Which makes sense, but do we need to set errno to EEXIST here? Does prune_worktree() ensure the value left in errno when it returns failure in a similar way to allow the caller of this new helper make effective and reliable use of errno?

> +	strbuf_addf(&reason, _("Removing worktrees/%s: worktree directory is not present"), wt->id);
> +	ret = prune_worktree(wt->id, &reason, TIME_MAX);
> +	return ret;
> +}
Previous: Peter JonesNext: Peter Jones
Message 7 of 15 in “Make die_if_checked_out() ignore missing worktree checkouts.”
  1. 1/2 Make die_if_checked_out() ignore missing worktree checkouts.Peter Jones, Oct 17, 2019
  2. 2/2 Make "git branch -d" prune missing worktrees automatically.Peter Jones, Oct 17, 2019
  3. Eric SunshineOct 17, 2019
  4. Peter JonesOct 18, 2019
  5. 1/4 libgit: Add a read-only helper to test the worktree lockPeter Jones, Oct 18, 2019
  6. 2/4 libgit: Expose more worktree functionality.Peter Jones, Oct 18, 2019
  7. Junio C HamanoOct 21, 2019
  8. 4/4 Make "git branch -d" prune missing worktrees automatically.Peter Jones, Oct 18, 2019
  9. 3/4 Make die_if_checked_out() prune missing checkouts of unlocked worktrees.Peter Jones, Oct 18, 2019
  10. Junio C HamanoOct 21, 2019
  11. Junio C HamanoOct 21, 2019
  12. Eric SunshineNov 8, 2019
  13. Phillip WoodNov 8, 2019
  14. Eric SunshineNov 9, 2019
  15. SZEDER GáborOct 17, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.