Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Aniket Gupta <aniketgupta3001@gmail.com>
Cc: git@vger.kernel.org,  Patrick Steinhardt <ps@pks.im>,
	 Toon Claes <toon@iotcl.com>,
	 Phillip Wood <phillip.wood123@gmail.com>,
	 Nikita Bobko <nikita@nbobko.com>
Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees
Date: Fri, 27 Nov 2026 16:12:24 -0800	[thread overview]
Message-ID: <xmqqse0l3jbr.fsf@gitster.g> (raw)
In-Reply-To: <20261127173252.7794-1-aniketgupta3001@gmail.com> (Aniket Gupta's message of "Fri, 27 Nov 2026 23:02:52 +0530")

Aniket Gupta <aniketgupta3001@gmail.com> writes:

> ... A bare repository
> has no such worktree, and branch_checked_out() records only one worktree
> per branch, so ask the other worktrees whether they use the branch
> instead of skipping it by name. --update-refs=head refuses in the same
> cases.

Checking the other worktrees for the current HEAD target instead of
skipping it by name is a neat way to handle both bare repositories and
branches forced into multiple worktrees.  

I had to read "--update-refs=head refuses in the same cases" above a few
times to unconfuse myself.  I initially took it to mean that it rejects
whenever "--update-refs=branches" would reject, but "--update-refs=head"
specifically leaves unrelated branches in other worktrees alone and only
refuses when HEAD's target branch itself is used elsewhere (either
checked out or when a rebase or bisect is in progress).

Spelling that out directly would make the paragraph easier to follow.

> 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, to tell the
> +		user about `--update-refs=head`.

As tested in t/t3450-history.sh, when a branch is being rebased in the
current worktree, this advice is also shown when the current worktree is
rebasing or bisecting the branch with HEAD detached, not only when it is
used by another worktree.

> 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"

What new things from log-tree.h are referenced by this patch?
The file seems to compile without this new inclusion.

> @@ -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;
> +}

This separation makes sense, given how branch_checked_out() works in
branch.c.  Because branch.c:prepare_checked_out_branches() populates the
current_checked_out_branches strmap, a branch checked out in two
worktrees only keeps one path in the map, so branch_checked_out() cannot
tell whether another worktree also uses head_target.

Asking non-current worktrees via is_shared_symref() covers symrefs,
detached rebases, and bisects in other worktrees, and also works when
run in a bare repository where the current worktree is bare.

> +/* "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;
> +		}
> +	}

Instead of enumerating every worktree with get_worktrees() just to pick
out '.is_current', we could call get_current_worktree(repo) and
free_worktree(), as wt-status.c does in wt_status_get_state().  It would
avoid reading HEAD for all linked worktrees, no?

> @@ -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);
> +	}

Merging the merge-commit check and the branch/HEAD check into a single
walk in validate_revwalk() is clean.

In setup_revwalk(), the revision args include --ancestry-path=<original>
and ^<original>, so original is excluded from get_revision() and
checking it once before the loop will not visit any branch decoration
twice.  All five subcommands also normalize REF_ACTION_DEFAULT to
REF_ACTION_BRANCHES and call setup_revwalk() before opening an editor or
modifying refs or the index, and moving reset_revision_walk() to the
'out:' label keeps object flags clean on error paths too.

>  out:
> +	reset_revision_walk();
>  	release_revisions(&revs);
> +	string_list_clear(&used, 1);
>  	strvec_clear(&args);
> +	free(head_target);
>  	return ret;
>  }


> 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
>  '

The new tests in t/t3450-history.sh cover the worktree, bare
repository, shared checkout, rebase, bisect, advice, --dry-run,
--update-refs=head, and pre-editor refusal cases well.

Overall this looks solid to me.

  reply	other threads:[~2026-11-28  0:12 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
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 [this message]
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=xmqqse0l3jbr.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=aniketgupta3001@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=nikita@nbobko.com \
    --cc=phillip.wood123@gmail.com \
    --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