[1/2] Make die_if_checked_out() ignore missing worktree checkouts.
diff mbox series

Message ID 20191017162826.1064257-1-pjones@redhat.com
State New
Headers show
Series
  • [1/2] Make die_if_checked_out() ignore missing worktree checkouts.
Related show

Commit Message

Peter Jones Oct. 17, 2019, 4:28 p.m. UTC
Currently if you do, for example:

$ git worktree add path foo

And "foo" has already been checked out at some other path, but the user
has removed it without pruning, you'll get an error that the branch is
already checked out.  It isn't meaningfully checked out, the repo's
data is just stale and no longer reflects reality.

This makes it so that if nothing is present where a worktree is
supposedly checked out, we ignore that the worktree exists, and let it
get cleaned up the next time worktrees are pruned.

(I would prune it instead, but prune isn't available from libgit
currently.)

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 branch.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

SZEDER Gábor Oct. 17, 2019, 4:44 p.m. UTC | #1
On Thu, Oct 17, 2019 at 12:28:25PM -0400, Peter Jones wrote:
> Currently if you do, for example:
> 
> $ git worktree add path foo
> 
> And "foo" has already been checked out at some other path, but the user
> has removed it without pruning, you'll get an error that the branch is
> already checked out.  It isn't meaningfully checked out, the repo's
> data is just stale and no longer reflects reality.
> 
> This makes it so that if nothing is present where a worktree is
> supposedly checked out, we ignore that the worktree exists, and let it
> get cleaned up the next time worktrees are pruned.
> 
> (I would prune it instead, but prune isn't available from libgit
> currently.)
> 
> Signed-off-by: Peter Jones <pjones@redhat.com>
> ---
>  branch.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/branch.c b/branch.c
> index 579494738a7..60322ded953 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -360,6 +360,9 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree)
>  	wt = find_shared_symref("HEAD", branch);
>  	if (!wt || (ignore_current_worktree && wt->is_current))
>  		return;
> +	if (access(wt->path, F_OK) < 0 &&
> +	    (errno == ENOENT || errno == ENOTDIR))
> +		return;

I think this check is insuffient: even if the directory of the working
tree is not present, the working tree might still exist, and should
not be ignored (or deleted/pruned in the second patch).

See the description of 'git worktree lock' for details.

>  	skip_prefix(branch, "refs/heads/", &branch);
>  	die(_("'%s' is already checked out at '%s'"),
>  	    branch, wt->path);
> -- 
> 2.23.0
>

Patch
diff mbox series

diff --git a/branch.c b/branch.c
index 579494738a7..60322ded953 100644
--- a/branch.c
+++ b/branch.c
@@ -360,6 +360,9 @@  void die_if_checked_out(const char *branch, int ignore_current_worktree)
 	wt = find_shared_symref("HEAD", branch);
 	if (!wt || (ignore_current_worktree && wt->is_current))
 		return;
+	if (access(wt->path, F_OK) < 0 &&
+	    (errno == ENOENT || errno == ENOTDIR))
+		return;
 	skip_prefix(branch, "refs/heads/", &branch);
 	die(_("'%s' is already checked out at '%s'"),
 	    branch, wt->path);