worktree.c: check whether branch is rebased in another worktree

This function find_shared_symref() is used in a couple places: 1) in builtin/branch.c: it's used to detect if a branch is checked out elsewhere and refuse to delete the branch. 2) in builtin/notes.c: it's used to detect if a note is being merged in another worktree 3) in branch.c, the function die_if_checked_out() is actually used by "git checkout" and "git worktree add" to see if a branch is already checked out elsewhere and refuse the operation. In cases 1 and 3, if a rebase is ongoing, "HEAD" will be in detached mode, find_shared_symref() fails to detect it and declares "no branch is checked out here", which is not really what we want. This patch tightens the test. If the given symref is "HEAD", we try to detect if rebase is ongoing. If so return the branch being rebased. This makes checkout and branch delete operations safer because you can't checkout a branch being rebased in another place, or delete it. Special case for checkout. If the current branch is being rebased, git-rebase.sh may use "git checkout" to abort and return back to the original branch. The updated test in find_shared_symref() will prevent that and "git rebase --abort" will fail as a result. find_shared_symref() and die_if_checked_out() have to learn a new option ignore_current_worktree to loosen the test a bit. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 22, 2016 at 20:01 UTC 8d9fdd7087d0c2700a56e20043d5d79c9951f64f
6 files changed +76 -6
branch.c
+2 -2
@@ -334,12 +334,12 @@ void remove_branch_state(void)
334 unlink(git_path_squash_msg());
335 }
336
337 -void die_if_checked_out(const char *branch)
337 +void die_if_checked_out(const char *branch, int ignore_current_worktree)
338 {
339 const struct worktree *wt;
340
341 wt = find_shared_symref("HEAD", branch);
342 - if (!wt)
342 + if (!wt || (ignore_current_worktree && wt->is_current))
343 return;
344 skip_prefix(branch, "refs/heads/", &branch);
345 die(_("'%s' is already checked out at '%s'"),
branch.h
+1 -1
@@ -58,7 +58,7 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
58 * worktree and die (with a message describing its checkout location) if
59 * it is.
60 */
61 -extern void die_if_checked_out(const char *branch);
61 +extern void die_if_checked_out(const char *branch, int ignore_current_worktree);
62
63 /*
64 * Update all per-worktree HEADs pointing at the old ref to point the new ref.
builtin/checkout.c
+1 -1
@@ -1111,7 +1111,7 @@ static int checkout_branch(struct checkout_opts *opts,
1111 char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
1112 if (head_ref &&
1113 (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
1114 - die_if_checked_out(new->path);
1114 + die_if_checked_out(new->path, 1);
1115 free(head_ref);
1116 }
1117
builtin/worktree.c
+2 -2
@@ -205,7 +205,7 @@ static int add_worktree(const char *path, const char *refname,
205 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
206 ref_exists(symref.buf)) { /* it's a branch */
207 if (!opts->force)
208 - die_if_checked_out(symref.buf);
208 + die_if_checked_out(symref.buf, 0);
209 } else { /* must be a commit */
210 commit = lookup_commit_reference_by_name(refname);
211 if (!commit)
@@ -349,7 +349,7 @@ static int add(int ac, const char **av, const char *prefix)
349 if (!opts.force &&
350 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
351 ref_exists(symref.buf))
352 - die_if_checked_out(symref.buf);
352 + die_if_checked_out(symref.buf, 0);
353 strbuf_release(&symref);
354 }
355
t/t2025-worktree-add.sh
+38
@@ -4,6 +4,8 @@ test_description='test git worktree add'
4
5 . ./test-lib.sh
6
7 +. "$TEST_DIRECTORY"/lib-rebase.sh
8 +
9 test_expect_success 'setup' '
10 test_commit init
11 '
@@ -225,4 +227,40 @@ test_expect_success '"add" worktree with --checkout' '
227 test_cmp init.t swamp2/init.t
228 '
229
230 +test_expect_success 'put a worktree under rebase' '
231 + git worktree add under-rebase &&
232 + (
233 + cd under-rebase &&
234 + set_fake_editor &&
235 + FAKE_LINES="edit 1" git rebase -i HEAD^ &&
236 + git worktree list | grep "under-rebase.*detached HEAD"
237 + )
238 +'
239 +
240 +test_expect_success 'add a worktree, checking out a rebased branch' '
241 + test_must_fail git worktree add new-rebase under-rebase &&
242 + ! test -d new-rebase
243 +'
244 +
245 +test_expect_success 'checking out a rebased branch from another worktree' '
246 + git worktree add new-place &&
247 + test_must_fail git -C new-place checkout under-rebase
248 +'
249 +
250 +test_expect_success 'not allow to delete a branch under rebase' '
251 + (
252 + cd under-rebase &&
253 + test_must_fail git branch -D under-rebase
254 + )
255 +'
256 +
257 +test_expect_success 'check out from current worktree branch ok' '
258 + (
259 + cd under-rebase &&
260 + git checkout under-rebase &&
261 + git checkout - &&
262 + git rebase --abort
263 + )
264 +'
265 +
266 test_done
worktree.c
+32
@@ -3,6 +3,7 @@
3 #include "strbuf.h"
4 #include "worktree.h"
5 #include "dir.h"
6 +#include "wt-status.h"
7
8 void free_worktrees(struct worktree **worktrees)
9 {
@@ -215,6 +216,30 @@ const char *get_worktree_git_dir(const struct worktree *wt)
216 return git_common_path("worktrees/%s", wt->id);
217 }
218
219 +static int is_worktree_being_rebased(const struct worktree *wt,
220 + const char *target)
221 +{
222 + struct wt_status_state state;
223 + int found_rebase;
224 +
225 + memset(&state, 0, sizeof(state));
226 + found_rebase = wt_status_check_rebase(wt, &state) &&
227 + ((state.rebase_in_progress ||
228 + state.rebase_interactive_in_progress) &&
229 + state.branch &&
230 + starts_with(target, "refs/heads/") &&
231 + !strcmp(state.branch, target + strlen("refs/heads/")));
232 + free(state.branch);
233 + free(state.onto);
234 + return found_rebase;
235 +}
236 +
237 +/*
238 + * note: this function should be able to detect shared symref even if
239 + * HEAD is temporarily detached (e.g. in the middle of rebase or
240 + * bisect). New commands that do similar things should update this
241 + * function as well.
242 + */
243 const struct worktree *find_shared_symref(const char *symref,
244 const char *target)
245 {
@@ -231,6 +256,13 @@ const struct worktree *find_shared_symref(const char *symref,
256 for (i = 0; worktrees[i]; i++) {
257 struct worktree *wt = worktrees[i];
258
259 + if (wt->is_detached && !strcmp(symref, "HEAD")) {
260 + if (is_worktree_being_rebased(wt, target)) {
261 + existing = wt;
262 + break;
263 + }
264 + }
265 +
266 strbuf_reset(&path);
267 strbuf_reset(&sb);
268 strbuf_addf(&path, "%s/%s",