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.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 12:14:03 -0800	[thread overview]
Message-ID: <xmqqa4mz5mr8.fsf@gitster.g> (raw)
In-Reply-To: <20261123103911.98860-1-aniketgupta3001@gmail.com> (Aniket Gupta's message of "Mon, 23 Nov 2026 16:09:11 +0530")

Aniket Gupta <aniketgupta3001@gmail.com> writes:

> +/*
> + * 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.
> + */

The intent is good.

> +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)) {
> +		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;

Imagine we have a bare repository repo.git whose primary branch is
'main'.  Its HEAD records refs/heads/main (which is a common state
after you run "git clone --bare" from a modern project whose primary
branch is 'main').  Then you can add a worktree ../wt to check out the
'main' branch, because branch_checked_out() will say "no" when you
create and check out 'main' in that new worktree.

In that setup, what would running "git history" in the bare repository
to, say, drop a commit, do?  The 'main' branch is checked out
elsewhere in the worktree at ../wt; we run "git history" in the bare
repository, and head_target above is resolved to "refs/heads/main".
This 'continue' triggers for the 'main' branch, so we return ret == 0.

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

Now, the following is all outside the context of this patch, but
looking at cmd_history_drop(), it calls setup_revwalk() which in turn
calls the above function, which does not complain in this case.

And is_bare_repository() is true.  We skip update_worktree() and
update the 'main' branch via its HEAD, making the index and the
worktree of the worktree at ../wt out of sync, no?

Perhaps head_target should be initialized to NULL and assigned the
resolution result of HEAD only in a non-bare repository, or something?
I didn't trace all other callers, so there may be other glitches I did
not notice.

The proposed log message claims to "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", but as we saw above, it is
inaccurate for bare repositories.  It probably is also worth noting in
the log message that 'drop' updates the worktree while 'reword',
'split', and 'squash' preserve the tip tree and 'fixup' incorporates
staged changes from the index.

Thanks, I think this patch still needs some work.


  parent reply	other threads:[~2026-11-23 20:14 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 [this message]
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=xmqqa4mz5mr8.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=aniketgupta3001@gmail.com \
    --cc=git@vger.kernel.org \
    --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