From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796050188; b=N3x28aniTonapcMjMnvVt20M0CEKkomcTMUDCp3wNkqZKhJwlORl6MXV1Vlw5eyl/p+h 88i110HzOmlpELnHCxUEB8jD3gfqnT4ME2DsdWmOxK2u2PcgXFWzk8WGN+6JAvLu0XR4w d8x0GlDnMyzfjfUvNyy276jLIUa7wil+XzLdM1Z/ozZs9ywgj1cvz5QbZWY/L/3DTzMW/ paQde2IXIRZHNuIHmxMbbg7OcVQYOjPWbIuiUpcJfDlwvYQJ/3lwgN3UfGh7nmJbHOeUi vnrXFZ9dI51iVURaYVKgbbPz9VyN1Jk5apNK+zssenlQ1CkqWIpn5cgjUsgiAC+qhuQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050188; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:From:Reply-To:Subject:To:Cc: References:Content-Language:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=Wlc7h84loUWLjcvrXOcjZ49Xojgijmb5t51egDM4JaM=; b=Hww40jZsTrm3IYvyZypOGUqSE+WZ9YGBdExzcwYeQ/PtOqijCWSeI0CKgVrZvOeAXiPT lRYKGDtfncdDdut5W+5m68/qGVdAAxaLQeXu20C2N0E8yNKnOXK/VmN9d56STDt+GP5E6 c8NsIETBRZcDj5j+m6SuZQhdUJmMkYXFFbQRCC8/Dz3ke4nNK2SPI+l2qN3cfeB6Q8Uek NZbp2VOSrXsDZKzZyoml7+oItAWUwBfOVx9bbJlV6mZDfivVxqEBM158SWIpF3/4iCMDn ryLZVSOI4BdjoQQyzvTyz/oA8bxLT9MrfW6HgQNroFA/Os809FT2sv69sx4GUIwU+8A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="Nygjg/Nn"; arc=none smtp.remote-ip=209.85.128.54 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90B53606A332 for ; Mon, 30 Nov 2026 14:49:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90B53606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Nygjg/Nn Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4b79502c27aso106028055e9.1 for ; Mon, 30 Nov 2026 06:49:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796050186; x=1796654986; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:content-language :references:cc:to:subject:reply-to:from:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Wlc7h84loUWLjcvrXOcjZ49Xojgijmb5t51egDM4JaM=; b=Nygjg/NnOexfSAqxwBefTWWu4KOzLRllVXZm2W39Fu8FqsxE9+BcwpSRG0vSz/hcBL 3Ao8rLWlLg7G2AMHnwg37VOWtdBuJdWz3EGBKl7NrZFqvClkPGKXroAQK10/4buUN6GT EdWOAAAy+7jItZB4kdrC2EOkXa8auT/ozIcay42tQrb0w1y1Z5Batcf/75BmlD1AY7d5 1X4JuBz9A/lm+LVO7jCghTVgtCX33FtpqI6UUdRz2nac6dRwALPZ2ctOcWH4PYrgPYT3 VVQL8jocNhqc8R0iMr3Nb65YKEMht5c8np8CqqSp1uM3P8NsPhVwqjoMD25eU1w6rf6J TgIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796050186; x=1796654986; h=content-transfer-encoding:content-type:in-reply-to:content-language :references:cc:to:subject:reply-to:from:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Wlc7h84loUWLjcvrXOcjZ49Xojgijmb5t51egDM4JaM=; b=oAozFzL19XUQGmFIbIrVUOYqzkutn/cdIKl5rY9FxI58bIjncTH2bWQ9RH1Iv44Wcn LPCrM6LqlcTFlggmGOQg/m2kB1hN20GiJ1M2jhuLuJdj8SOdqePWlVcRYmndToYtvsAi W2fKLQEUaj7t4G0X3Bk2wbUhO+Pm18lOF259khT2h44b4eZ/83LKpEb6Okkj0kraDtZJ Z1vzpriNXoiuhvyz+u6nCqqaXccJQP/yyWGS9lPVCmfVk6o+QZ1m36d81Y4XcjyBKATs dqsCTRSVA5YdbzqKwJtszUtX2oJGxIWGW7YzN3rfmiXpUycil4HFku+qTunxuR6o3L/L 0DIw== X-Forwarded-Encrypted: i=1; AL+hErHpNA7H+kxt2lcc+nKuJw0fsuB1zNnldDTwGwLO8jFnbtKk6hdTlw1Q1Zcuis9+1GX6fBQ=@vger.kernel.org X-Gm-Message-State: APuU4Z9msGfcY8C0O5oLsINqcTObvlxuxv27oZoTTDOt3evLwM2K8VSQ Yz2W2ZMRE8I+0xUYlmtl2lhXv5k45+cpb441e3rutSb5HuB2PEiZGqb9 X-Gm-Gg: AZFdTZf6FkdYPbsbTrVBTfRhTreoSAnmY2kXwnBFnMw+wrBjazqBK3a1jZ/Hc2rM9ls /NjPuKmugdaI7C6+cz4zHxHsxu6UTxqwk0Jn8XniuMnV8FoRyxGwie24ZHvY/L2muFoXIOHl6mF dymHWkdxGjFUoX86r1NQ4L7LRXO02hBLLwDto3bERDTO3+ygYfymQ69OCS+D/OXYbf/3TbgX2Ja kZ6RGPxZa6rd768l2MJuqX58Yu/8Pz3vczznXHk2RgM51tB5Z8L+1Kgfr4y/C0Wofnt67lItYyj ekGbaOfO1LF9Z+JiqayDkaONUuegU5HN++ZhwuGXUgk6lVqMjVs7pxj6RgO4ELXMhkCEMtdMlJ3 wE+J5+bGfHoTwhCijPMdmuBO9jDdBa4uc26PScDnb6b3KQyngsaiau5z0j7Qw54/B8eu2I7xH+S U8IjGFWuGO4FENgNqwzNu5/T1Ra7NE9bMRk2H0BZ9xisipmgWEat5/4wqsgrQ4w+NTV0eLobg27 b0nsJQf1iF4V4Xx4Bdmn/hTtbgpBPThxJlIJEeSkI9uo9ig4NKUISElXpWn9refa2vvtI483TU8 4IECirvlce1Tv7pGC1D5qQ26HI5aFG/xSk3+7V2LPxTzEZRC/Qi850kKaaTRl3ZNJvOYDkCBgi/ ibGA/ZXg= X-Received: by 2002:a05:600c:3b83:b0:4b7:6b96:e431 with SMTP id 5b1f17b1804b1-4bb88822242mr773705515e9.28.1796050185547; Mon, 30 Nov 2026 06:49:45 -0800 (PST) Received: from ?IPV6:2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f? ([2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8526cbd0sm1262259775e9.0.2026.11.30.06.49.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 06:49:45 -0800 (PST) Message-ID: <03c0d1e1-83c5-4324-b1c9-8023edc4b09e@gmail.com> Date: Mon, 30 Nov 2026 14:49:42 +0000 X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Phillip Wood Reply-To: phillip.wood@dunelm.org.uk Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees To: Aniket Gupta , git@vger.kernel.org Cc: Patrick Steinhardt , Toon Claes , Junio C Hamano , Nikita Bobko References: <20261123103911.98860-1-aniketgupta3001@gmail.com> <20261127173252.7794-1-aniketgupta3001@gmail.com> Content-Language: en-US In-Reply-To: <20261127173252.7794-1-aniketgupta3001@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi Aniket On 27/11/2026 17:32, Aniket Gupta wrote: > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc > index 3d91c90eda..65f3ff0e02 100644 > --- a/Documentation/config/advice.adoc > +++ b/Documentation/config/advice.adoc > @@ -60,9 +60,10 @@ all advice messages. > Shown when the user tries to delete a not fully merged > branch without the force option set. > historyUpdateRefs:: > - Shown when `git history squash` refuses because a local branch > - cannot be rewritten as a descendant of the squashed commit, to > - tell the user about `--update-refs=head`. > + Shown when `git history` refuses to run because a local branch is > + used by another worktree, or, for `squash`, because it cannot be > + rewritten as a descendant of the squashed commit, I'd finish the sentence here. > to tell the user about `--update-refs=head`. This ends up being confusing when it is tacked on to the end of the sentance explaining when the advice is shown. We don't describe the advice in the documentation of the other options so I'd drop it. > ignoredHook:: > Shown when a hook is ignored because the hook is not > set as executable. > diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc > index 1fcc30150a..243817d28a 100644 > --- a/Documentation/git-history.adoc > +++ b/Documentation/git-history.adoc > @@ -215,6 +215,13 @@ empty. > `branches`, all local branches that point to commits which are > descendants of the original commit will be rewritten. With `head`, only > the current `HEAD` reference will be rewritten. Defaults to `branches`. > ++ > +A branch that is used by another worktree, or that the current worktree is > +in the middle of rebasing or bisecting, cannot be rewritten, as the index and > +working tree of that worktree would not match the rewritten branch. That's true for other worktrees, but not the current worktree where the reason is that we'll end up losing work. I'd split it up A branch that is used by another worktree cannot be rewritten because the index and working tree of that worktree would not match the rewritten branch. A branch that is being rebased in the current worktree cannot be rewritten because the rewrite cause the rebase to fail. A branch that is being bisected in the current worktree cannot be rewritten because ... (is there actually a good reason?) > With > +`branches` the command refuses in that case, so use `head` to rewrite only > +the current branch. The command refuses to rewrite `HEAD` while a rebase > +is in progress in the current worktree. This is looking good, I don't have anything else to add to what Junio and Toon have said. Thanks Phillip > EXAMPLES > -------- > diff --git a/builtin/history.c b/builtin/history.c > index 43ba76f5a1..d0a3287c1d 100644 > --- a/builtin/history.c > +++ b/builtin/history.c > @@ -2,6 +2,7 @@ > > #include "builtin.h" > #include "advice.h" > +#include "branch.h" > #include "cache-tree.h" > #include "commit.h" > #include "commit-reach.h" > @@ -11,6 +12,7 @@ > #include "gettext.h" > #include "hex.h" > #include "lockfile.h" > +#include "log-tree.h" > #include "merge-ort.h" > #include "oidmap.h" > #include "parse-options.h" > @@ -27,6 +29,7 @@ > #include "tree.h" > #include "tree-walk.h" > #include "unpack-trees.h" > +#include "worktree.h" > #include "wt-status.h" > > #define GIT_HISTORY_DROP_USAGE \ > @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns > return 0; > } > > -static int revwalk_contains_merges(struct repository *repo, > - const struct strvec *revwalk_args) > +/* > + * Return the path of the worktree that uses "refname", if rewriting it would > + * leave the index and working tree of that worktree behind. The branch that > + * the current worktree has checked out is fine, unless another worktree has > + * it checked out as well. > + */ > +static char *branch_used_elsewhere(struct repository *repo, > + const char *refname, > + const char *head_target) > +{ > + char *path = NULL; > + > + if (head_target && !strcmp(refname, head_target)) { > + struct worktree **worktrees = get_worktrees(repo); > + > + for (int i = 0; worktrees[i]; i++) { > + if (!worktrees[i]->is_current && > + is_shared_symref(worktrees[i], "HEAD", refname)) { > + path = xstrdup(worktrees[i]->path); > + break; > + } > + } > + free_worktrees(worktrees); > + } else { > + const char *checked_out = branch_checked_out(refname); > + > + if (checked_out) > + path = xstrdup(checked_out); > + } > + > + return path; > +} > + > +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ > +static bool rebase_in_progress(struct repository *repo) > +{ > + struct worktree **worktrees = get_worktrees(repo); > + struct wt_status_state state = { 0 }; > + bool ret = false; > + > + for (int i = 0; worktrees[i]; i++) { > + if (worktrees[i]->is_current) { > + wt_status_check_rebase(worktrees[i], &state); > + ret = state.rebase_in_progress || > + state.rebase_interactive_in_progress; > + break; > + } > + } > + > + wt_status_state_free_buffers(&state); > + free_worktrees(worktrees); > + return ret; > +} > + > +static void collect_used_branches(struct repository *repo, > + const struct commit *commit, > + const char *head_target, > + struct string_list *used, > + bool *rewrites_head) > { > + const struct name_decoration *decoration; > + > + for (decoration = get_name_decoration(&commit->object); > + decoration; > + decoration = decoration->next) { > + char *path; > + > + if (decoration->type == DECORATION_REF_HEAD) > + *rewrites_head = true; > + if (decoration->type != DECORATION_REF_LOCAL) > + continue; > + path = branch_used_elsewhere(repo, decoration->name, head_target); > + if (path) > + string_list_append(used, decoration->name)->util = path; > + } > +} > + > +static int validate_revwalk(struct repository *repo, > + enum ref_action action, > + struct commit *original, > + const struct strvec *revwalk_args) > +{ > + struct string_list used = STRING_LIST_INIT_DUP; > struct strvec args = STRVEC_INIT; > struct rev_info revs; > + struct commit *commit; > + bool rewrites_head = action == REF_ACTION_HEAD; > + char *head_target; > int ret; > > + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", > + 0, NULL, NULL); > + > strvec_pushv(&args, revwalk_args->v); > - strvec_push(&args, "--min-parents=2"); > > repo_init_revisions(repo, &revs, NULL); > > @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, > goto out; > } > > - if (get_revision(&revs)) { > - ret = error(_("replaying merge commits is not supported yet!")); > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, original, head_target, &used, > + &rewrites_head); > + > + while ((commit = get_revision(&revs))) { > + if (commit->parents && commit->parents->next) { > + ret = error(_("replaying merge commits is not supported yet!")); > + goto out; > + } > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, commit, head_target, &used, > + &rewrites_head); > + } > + > + if (action == REF_ACTION_HEAD && head_target && > + starts_with(head_target, "refs/heads/")) { > + char *path = branch_used_elsewhere(repo, head_target, head_target); > + if (path) > + string_list_append(&used, head_target)->util = path; > + } > + > + if (used.nr) { > + string_list_sort(&used); > + for (size_t i = 0; i < used.nr; i++) { > + const char *branch; > + > + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) > + BUG("branch decoration without refs/heads/ prefix"); > + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), > + branch, (const char *)used.items[i].util); > + } > + if (action == REF_ACTION_BRANCHES) > + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, > + _("Use --update-refs=head to rewrite only " > + "the current branch and leave such branches " > + "untouched.")); > + ret = -1; > + goto out; > + } > + > + if (rewrites_head && rebase_in_progress(repo)) { > + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); > goto out; > } > > - reset_revision_walk(); > ret = 0; > > out: > + reset_revision_walk(); > release_revisions(&revs); > + string_list_clear(&used, 1); > strvec_clear(&args); > + free(head_target); > return ret; > } > > @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, > strvec_push(&args, "HEAD"); > } > > - ret = revwalk_contains_merges(repo, &args); > + ret = validate_revwalk(repo, action, original, &args); > if (ret < 0) > goto out; > > diff --git a/t/t3450-history.sh b/t/t3450-history.sh > index f513463b92..03bd375ef8 100755 > --- a/t/t3450-history.sh > +++ b/t/t3450-history.sh > @@ -14,4 +14,269 @@ test_expect_success 'raises an error with unknown argument' ' > test_grep "unknown subcommand: .garbage." err > ' > > +test_expect_success 'refuses to rewrite a branch checked out in another worktree' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add ../wt -b other && > + > + git refs list >refs-expect && > + test_must_fail git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && > + test_grep "^hint: .* --update-refs=head" err && > + test_grep ! "branch .main." err && > + test_must_fail git history drop --dry-run second >out 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at" err && > + test_must_be_empty out && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +test_expect_success 'advice.historyUpdateRefs silences the hint for checked out branches' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add ../wt -b other && > + > + test_must_fail git -c advice.historyUpdateRefs=false \ > + history drop second 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at" err && > + test_grep ! "hint:" err > + ) > +' > + > +test_expect_success '--update-refs=head leaves branches of other worktrees alone' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add ../wt -b other && > + git rev-parse other >other-expect && > + > + git history drop --update-refs=head second && > + > + git rev-parse other >other-actual && > + test_cmp other-expect other-actual && > + git log --format=%s main >actual && > + test_write_lines third first >expect && > + test_cmp expect actual > + ) > +' > + > +test_expect_success 'rewrites the branch of the current worktree' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + git worktree add ../wt -b other && > + test_commit -C ../wt second && > + test_commit -C ../wt third && > + > + git -C ../wt history drop second && > + > + git log --format=%s other >actual && > + test_write_lines third first >expect && > + test_cmp expect actual > + ) > +' > + > +test_expect_success 'refuses to rewrite a branch that is being rebased' ' > + test_when_finished "rm -rf repo" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + test_must_fail git rebase -x false first && > + > + git refs list >refs-expect && > + test_must_fail git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at" err && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual && > + > + git rebase --abort > + ) > +' > + > +test_expect_success 'refuses to rewrite a branch that is being bisected' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add ../wt -b other && > + git -C ../wt bisect start HEAD first && > + > + test_must_fail git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at" err > + ) > +' > + > +test_expect_success 'refuses to rewrite HEAD while a rebase is in progress' ' > + test_when_finished "rm -rf repo" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit base && > + git checkout -b topic && > + test_commit topic-one && > + test_commit topic-two && > + git checkout main && > + test_commit main-new && > + git checkout topic && > + git rev-parse topic >orig && > + test_must_fail git rebase -x false main && > + git rev-parse HEAD >head-expect && > + > + test_must_fail git history drop HEAD~1 2>err && > + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && > + test_must_fail git history drop --update-refs=head HEAD 2>err && > + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && > + git rev-parse HEAD >head-actual && > + test_cmp head-expect head-actual && > + > + git rebase --abort && > + git rev-parse topic >actual && > + test_cmp orig actual > + ) > +' > + > +test_expect_success 'rewrites commits unrelated to HEAD while a rebase is in progress' ' > + test_when_finished "rm -rf repo" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit base && > + git checkout -b other && > + test_commit other-one && > + test_commit other-two && > + git checkout -b topic base && > + test_commit topic-one && > + git checkout main && > + test_commit main-new && > + git checkout topic && > + test_must_fail git rebase -x false main && > + git rev-parse HEAD >head-expect && > + > + git history drop other-one && > + git log --format=%s other >actual && > + test_write_lines other-two base >expect && > + test_cmp expect actual && > + git rev-parse HEAD >head-actual && > + test_cmp head-expect head-actual && > + git rebase --abort > + ) > +' > + > +test_expect_success 'refuses to rewrite the branch a bare repository names in HEAD when a worktree uses it' ' > + test_when_finished "rm -rf repo repo.git wt" && > + git init repo --initial-branch=main && > + test_commit -C repo first && > + test_commit -C repo second && > + test_commit -C repo third && > + git clone --bare repo repo.git && > + git -C repo.git worktree add ../wt main && > + > + git -C repo.git refs list >refs-expect && > + test_must_fail git -C repo.git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + test_must_fail git -C repo.git history drop --update-refs=head second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + git -C repo.git refs list >refs-actual && > + test_cmp refs-expect refs-actual > +' > + > +test_expect_success 'refuses to rewrite a branch that two worktrees have checked out' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add --force ../wt main && > + > + git refs list >refs-expect && > + test_must_fail git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + test_must_fail git -C ../wt history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*repo" err && > + test_must_fail git history drop --update-refs=head second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + test_grep ! "^hint:" err && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +test_expect_success 'refuses to rewrite the commit that a branch of another worktree points at' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + git worktree add ../wt -b other second && > + > + git refs list >refs-expect && > + test_must_fail git history reword second 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && > + test_grep ! "branch .main." err && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +test_expect_success 'every subcommand refuses before it does anything' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + test_commit fourth && > + git worktree add ../wt -b other && > + echo changed >first.t && > + git add first.t && > + write_script fail-editor <<-\EOF && > + >editor-ran > + exit 1 > + EOF > + test_set_editor "$(pwd)/fail-editor" && > + > + git refs list >refs-expect && > + git diff --cached >index-expect && > + for cmd in "drop second" "reword second" "split second" \ > + "fixup second" "squash first..third" > + do > + test_must_fail git history $cmd err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at" err && > + test_path_is_missing editor-ran && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual && > + git diff --cached >index-actual && > + test_cmp index-expect index-actual || return 1 > + done > + ) > +' > + > test_done