diff options
-rw-r--r-- | branch.c | 4 | ||||
-rw-r--r-- | branch.h | 2 | ||||
-rw-r--r-- | builtin/checkout.c | 2 | ||||
-rw-r--r-- | builtin/worktree.c | 4 | ||||
-rwxr-xr-x | t/t2025-worktree-add.sh | 38 | ||||
-rw-r--r-- | worktree.c | 32 |
6 files changed, 76 insertions, 6 deletions
@@ -334,12 +334,12 @@ void remove_branch_state(void) unlink(git_path_squash_msg()); } -void die_if_checked_out(const char *branch) +void die_if_checked_out(const char *branch, int ignore_current_worktree) { const struct worktree *wt; wt = find_shared_symref("HEAD", branch); - if (!wt) + if (!wt || (ignore_current_worktree && wt->is_current)) return; skip_prefix(branch, "refs/heads/", &branch); die(_("'%s' is already checked out at '%s'"), @@ -58,7 +58,7 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name); * worktree and die (with a message describing its checkout location) if * it is. */ -extern void die_if_checked_out(const char *branch); +extern void die_if_checked_out(const char *branch, int ignore_current_worktree); /* * Update all per-worktree HEADs pointing at the old ref to point the new ref. diff --git a/builtin/checkout.c b/builtin/checkout.c index efcbd8f6b5..6041718783 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -1111,7 +1111,7 @@ static int checkout_branch(struct checkout_opts *opts, char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag); if (head_ref && (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path))) - die_if_checked_out(new->path); + die_if_checked_out(new->path, 1); free(head_ref); } diff --git a/builtin/worktree.c b/builtin/worktree.c index d8e3795dc4..12c0af723e 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -205,7 +205,7 @@ static int add_worktree(const char *path, const char *refname, if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && ref_exists(symref.buf)) { /* it's a branch */ if (!opts->force) - die_if_checked_out(symref.buf); + die_if_checked_out(symref.buf, 0); } else { /* must be a commit */ commit = lookup_commit_reference_by_name(refname); if (!commit) @@ -349,7 +349,7 @@ static int add(int ac, const char **av, const char *prefix) if (!opts.force && !strbuf_check_branch_ref(&symref, opts.new_branch) && ref_exists(symref.buf)) - die_if_checked_out(symref.buf); + die_if_checked_out(symref.buf, 0); strbuf_release(&symref); } diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh index 3acb9926f2..da54327f5d 100755 --- a/t/t2025-worktree-add.sh +++ b/t/t2025-worktree-add.sh @@ -4,6 +4,8 @@ test_description='test git worktree add' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-rebase.sh + test_expect_success 'setup' ' test_commit init ' @@ -225,4 +227,40 @@ test_expect_success '"add" worktree with --checkout' ' test_cmp init.t swamp2/init.t ' +test_expect_success 'put a worktree under rebase' ' + git worktree add under-rebase && + ( + cd under-rebase && + set_fake_editor && + FAKE_LINES="edit 1" git rebase -i HEAD^ && + git worktree list | grep "under-rebase.*detached HEAD" + ) +' + +test_expect_success 'add a worktree, checking out a rebased branch' ' + test_must_fail git worktree add new-rebase under-rebase && + ! test -d new-rebase +' + +test_expect_success 'checking out a rebased branch from another worktree' ' + git worktree add new-place && + test_must_fail git -C new-place checkout under-rebase +' + +test_expect_success 'not allow to delete a branch under rebase' ' + ( + cd under-rebase && + test_must_fail git branch -D under-rebase + ) +' + +test_expect_success 'check out from current worktree branch ok' ' + ( + cd under-rebase && + git checkout under-rebase && + git checkout - && + git rebase --abort + ) +' + test_done diff --git a/worktree.c b/worktree.c index 927905b7a1..5043756663 100644 --- a/worktree.c +++ b/worktree.c @@ -3,6 +3,7 @@ #include "strbuf.h" #include "worktree.h" #include "dir.h" +#include "wt-status.h" void free_worktrees(struct worktree **worktrees) { @@ -215,6 +216,30 @@ const char *get_worktree_git_dir(const struct worktree *wt) return git_common_path("worktrees/%s", wt->id); } +static int is_worktree_being_rebased(const struct worktree *wt, + const char *target) +{ + struct wt_status_state state; + int found_rebase; + + memset(&state, 0, sizeof(state)); + found_rebase = wt_status_check_rebase(wt, &state) && + ((state.rebase_in_progress || + state.rebase_interactive_in_progress) && + state.branch && + starts_with(target, "refs/heads/") && + !strcmp(state.branch, target + strlen("refs/heads/"))); + free(state.branch); + free(state.onto); + return found_rebase; +} + +/* + * note: this function should be able to detect shared symref even if + * HEAD is temporarily detached (e.g. in the middle of rebase or + * bisect). New commands that do similar things should update this + * function as well. + */ const struct worktree *find_shared_symref(const char *symref, const char *target) { @@ -231,6 +256,13 @@ const struct worktree *find_shared_symref(const char *symref, for (i = 0; worktrees[i]; i++) { struct worktree *wt = worktrees[i]; + if (wt->is_detached && !strcmp(symref, "HEAD")) { + if (is_worktree_being_rebased(wt, target)) { + existing = wt; + break; + } + } + strbuf_reset(&path); strbuf_reset(&sb); strbuf_addf(&path, "%s/%s", |