Re: [PATCH] history: refuse to rewrite branches used by other worktrees
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 23, 2026, 20:14 UTC
- Message-ID
- <xmqqa4mz5mr8.fsf@gitster.g>
- In-Reply-To
- <20261123103911.98860-1-aniketgupta3001@gmail.com>
Aniket Gupta <aniketgupta3001@gmail.com> writes:
Show 7 quoted lines
> +/* > + * 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.
Show 24 quoted lines
> +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.
Show 23 quoted lines
> + 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.