Git development
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Aniket Gupta <aniketgupta3001@gmail.com>, git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>, Toon Claes <toon@iotcl.com>,
	Junio C Hamano <gitster@pobox.com>,
	Phillip Wood <phillip.wood@dunelm.org.uk>,
	Nikita Bobko <nikita@nbobko.com>
Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Date: Mon, 23 Nov 2026 13:10:50 +0000	[thread overview]
Message-ID: <a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com> (raw)
In-Reply-To: <20261123103911.98860-1-aniketgupta3001@gmail.com>

Hi Aniket

Thanks for working on this, it is a useful improvement.

On 23/11/2026 10:39, Aniket Gupta wrote:
> By default "git history" also rewrites every local branch that descends

"also" is unnecessary here

> from the commit it changes. If another worktree has such a branch
> checked out, the branch moves but the index and working tree of that
> worktree stay behind. So it shows staged changes that nobody made.

The staged changes it shows were made by "git history" - they did not 
appear from nowhere. An alternative to refusing to run would be to 
update those worktrees if they are clean, but failing is the simpler 
option to implement.

> If
> the current worktree is in the middle of rebasing the branch, "git
> rebase --continue" later fails because the branch is not where the
> rebase expects it.
> 
> "git branch -f" refuses to move such branches and "git rebase
> --update-refs" skips them. Do the same here: before replaying anything
> look for local branches that contain the commit and are used by a
> worktree and refuse to go on if there is one. Use branch_checked_out()
> so that a branch that is being rebased or bisected counts as used.

Good. One thing to note is that I was recently looking at 
branch_checked_out() and it only stores a single worktree per ref, so I 
think if there are multiple checkouts of the same branch and we happen 
to record the current worktree as the one where it is checked out we'll 
fail to notice it is checked out elsewhere. That's not a problem that 
needs solving here though.

> Skip
> the branch that the current worktree has checked out, since the command
> already takes care of the index and working tree of that worktree.

What happens if a rebase is running in the same worktree as the user 
tries to run "git history"?

> Refuse with --dry-run as well because it will show what a real run will
> do.

Good

> Mention --update-refs=head, which leaves the other branches alone, with
> the existing advice.historyUpdateRefs hint.

Does --update-refs=head try to update the current branch? If so we 
should check that is not being written somewhere else.

> 
> diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc
> index 3d91c90eda..bb0e24b745 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 because a local branch is used

This is copied from the existing wording, so it is not a new problem but 
we should say what "git history" refuses to do. e.g.

	Shown when 'git history' refuses to run because ...

> +		by another worktree, or, for `squash`, because it cannot be
> +		rewritten as a descendant of the squashed commit, to tell the
> +		user about `--update-refs=head`.
>   	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..ce7c0a88c8 100644
> --- a/Documentation/git-history.adoc
> +++ b/Documentation/git-history.adoc
> @@ -215,6 +215,12 @@ 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. With> +`branches` the command refuses in that case, so use `head` to 
rewrite only
> +the current branch.

We should also prevent rebase from rewriting a branch that is being 
rewritten by "git history" but that can come later.

> +/*
> + * Rewriting a branch that is used by another worktree moves the branch
> + * without updating the index and working tree of that worktree. The same
> + * goes for the branch that the current worktree is rebasing. Refuse to
> + * rewrite such branches, but not the branch that the current worktree has
> + * checked out, as we update that one ourselves.
> + */
> +static int reject_checked_out_branches(struct repository *repo,
> +				       struct commit *original)
> +{
> +	struct ref_filter filter = REF_FILTER_INIT;
> +	struct ref_array refs = { 0 };
> +	char *head_target;
> +	int ret = 0;
> +
> +	head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD",
> +					  RESOLVE_REF_NO_RECURSE, NULL, NULL);
> +
> +	commit_list_insert(original, &filter.with_commit);
> +	filter.kind = FILTER_REFS_BRANCHES;
> +	if (filter_refs(&refs, &filter, filter.kind)) {

This is correct, but it duplicates the revision walk we're already doing 
when looking for merge commits. It would be more efficient to

(1) modify setup_revwalk() to load decorations (we already load them
     when replaying the commits so there is no extra work, we're just
     loading them earlier)
(2) modify and rename revwalk_contains_merges() to
   (a) drop"--min-parents" and check for merges by looking at
       commit->parents->next.
   (b) check DECORATION_REF_LOCAL decorations are not checked out
       elsewhere.

For an existing example of using decorations, look at 
replay.c:replay_revisions().

Thanks

Phillip

> +		ret = error(_("could not filter refs"));
> +		goto out;
> +	}
> +
> +	for (int i = 0; i < refs.nr; i++) {
> +		const char *refname = refs.items[i]->refname;
> +		const char *branch, *path;
> +
> +		if (head_target && !strcmp(refname, head_target))
> +			continue;
> +
> +		path = branch_checked_out(refname);
> +		if (!path)
> +			continue;
> +
> +		if (!skip_prefix(refname, "refs/heads/", &branch))
> +			BUG("branch filter returned '%s'", refname);
> +		error(_("cannot rewrite branch '%s' used by worktree at '%s'"),
> +		      branch, path);
> +		ret = -1;
> +	}
> +
> +	if (ret)
> +		advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,
> +				  _("Use --update-refs=head to rewrite only "
> +				    "the current branch and leave such branches "
> +				    "untouched."));
> +
> +out:
> +	free(head_target);
> +	ref_filter_clear(&filter);
> +	ref_array_clear(&refs);
> +	return ret;
> +}
> +
>   static int setup_revwalk(struct repository *repo,
>   			 enum ref_action action,
>   			 struct commit *original,
> @@ -350,6 +407,10 @@ static int setup_revwalk(struct repository *repo,
>   
>   		strvec_push(&args, "HEAD");
>   	} else {
> +		ret = reject_checked_out_branches(repo, original);
> +		if (ret < 0)
> +			goto out;
> +
>   		strvec_push(&args, "--branches");
>   		strvec_push(&args, "HEAD");
>   	}
> diff --git a/t/t3450-history.sh b/t/t3450-history.sh
> index f513463b92..f7ca66edc6 100755
> --- a/t/t3450-history.sh
> +++ b/t/t3450-history.sh
> @@ -14,4 +14,132 @@ 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 &&
> +		git refs list >refs-actual &&
> +		test_cmp refs-expect refs-actual
> +	)
> +'
> +
> +test_expect_success 'refusing to rewrite checked out branches honors --dry-run' '
> +	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 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
> +	)
> +'
> +
> +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_done
> 
> base-commit: f7de81456940dd6f3274e461149bba12965de609


  reply	other threads:[~2026-11-23 13:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta
2026-11-23 13:10 ` Phillip Wood [this message]
2026-11-23 21:54   ` Aniket Gupta
2026-11-24 10:49     ` Phillip Wood
2026-11-24 14:09       ` Aniket Gupta
2026-11-23 20:14 ` Junio C Hamano
2026-11-23 21:54   ` Aniket Gupta
2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta
2026-11-28  0:12   ` Junio C Hamano
2026-11-28  7:37     ` Aniket Gupta
2026-11-30 11:31   ` Toon Claes
2026-11-30 12:15     ` Aniket Gupta
2026-11-30 14:39     ` Phillip Wood
2026-11-30 17:13       ` Aniket Gupta
2026-11-30 14:49   ` Phillip Wood
2026-11-30 17:13     ` Aniket Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=aniketgupta3001@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=nikita@nbobko.com \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=ps@pks.im \
    --cc=toon@iotcl.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox