From: Patrick Steinhardt <ps@pks.im>
To: Aniket Gupta <aniketgupta3001@gmail.com>
Cc: git@vger.kernel.org, Toon Claes <toon@iotcl.com>,
Junio C Hamano <gitster@pobox.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: Tue, 1 Dec 2026 14:18:08 +0100 [thread overview]
Message-ID: <aw7I-t56lb5S1Z49@pks.im> (raw)
In-Reply-To: <20261127173252.7794-1-aniketgupta3001@gmail.com>
On Fri, Nov 27, 2026 at 11:02:52PM +0530, Aniket Gupta wrote:
> diff --git a/builtin/history.c b/builtin/history.c
> index 43ba76f5a1..d0a3287c1d 100644
> --- a/builtin/history.c
> +++ b/builtin/history.c
What follows is a bunch of additional logic to verify whether we'd end
up rewriting any commit that is checked out anywhere. Would it make
sense to maybe split this up into a sequence of commits, where each of
the commits introduces one such new check?
This is not necessarily a blocking comment, especially not in the case
where this change may already be good enough.
> @@ -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++) {
s/int/size_t/
> + if (!worktrees[i]->is_current &&
> + is_shared_symref(worktrees[i], "HEAD", refname)) {
This function name confused me quite a bit at first. What the function
does is to check whether the given refname belongs to the worktree,
which makes sense I guess.
But it also checks whether this worktree is currently in the process of
being rebased or bisected:
if (wt->is_detached && !strcmp(symref, "HEAD")) {
if (is_worktree_being_rebased(wt, target))
return 1;
if (is_worktree_being_bisected(wt, target))
return 1;
}
So doesn't this make `rebase_in_progress()` obsolete, at least for
worktrees other than the current one?
What this function doesn't check is whether any of the worktrees is
detached on a specific commit ID that we'd end up rewriting. But I think
that's actually intentional, as it doesn't really matter for our usecase
of git-history(1).
> + 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);
> + }
I'm probably missing something, but the way we handle `head_target`
seems weird to me. It is getting resolved to whatever we have checked
out in the current worktree. But do we even care about whether the
currently checked out branch is used anywhere else?
What I would have expected us to do is that we take the list of pending
ref updates that we compute in `handle_reference_updates()` and verify
that none of those refs that we're updating is checked out or being
rebased/bisected. But as far as I can see, this check here comes before
we have even computed those ref updates in the first place.
So we're verifying the pre-rewrite state, not the state after already
having performed the history rewrite where the ref updates are now
pending. And consequently, I think we don't reject some updates that may
happen in worktrees.
But even if we detected all of them, we would now be racy as we should
perform the validation at the point in time where we have already
gathered expected old and new commit IDs for the ref updates, shouldn't
we?
I may be missing something very obvious, but I think as-is these checks
are somewhat broken?
Thanks!
Patrick
next prev parent reply other threads:[~2026-12-01 13:18 UTC|newest]
Thread overview: 22+ 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
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
2026-12-01 16:39 ` Phillip Wood
2026-12-01 17:36 ` Aniket Gupta
2026-12-01 13:18 ` Patrick Steinhardt [this message]
2026-12-01 16:37 ` Phillip Wood
2026-12-02 6:23 ` Patrick Steinhardt
2026-12-01 18:11 ` 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=aw7I-t56lb5S1Z49@pks.im \
--to=ps@pks.im \
--cc=aniketgupta3001@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=nikita@nbobko.com \
--cc=phillip.wood123@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.