All of lore.kernel.org
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Toon Claes <toon@iotcl.com>,
	Aniket Gupta <aniketgupta3001@gmail.com>,
	git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>,
	Junio C Hamano <gitster@pobox.com>,
	Nikita Bobko <nikita@nbobko.com>
Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees
Date: Mon, 30 Nov 2026 14:39:41 +0000	[thread overview]
Message-ID: <644b3ff8-7443-4887-9cea-b649a9382fe1@gmail.com> (raw)
In-Reply-To: <87qzg2r1wj.fsf@dev.null.iotcl.com.invalid>

On 30/11/2026 11:31, Toon Claes wrote:
> Aniket Gupta <aniketgupta3001@gmail.com> writes:
> 
>> +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)) {
> 
> By reading the comment above is_shared_symref() I've noticed this leaves
> a gap open. When a branch is checked out in another worktree, but has an
> ongoing rebase or bisect, it is not detected.

Good catch.

I wonder if branch_used_elsewhere() would better in branch.c and based 
around register_checked_out_branch(). For example "git checkout" calls 
branch_checked_out() but I think it really wants to know if a branch is 
in use elsewhere.

Thanks

Phillip

> 
>> +				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;
>> +}
>> +
>> +/* "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;
>> +		}
>> +	}
>> +
>> +	wt_status_state_free_buffers(&state);
>> +	free_worktrees(worktrees);
>> +	return ret;
>> +}
>> +
>> +static void collect_used_branches(struct repository *repo,
>> +				  const struct commit *commit,
>> +				  const char *head_target,
>> +				  struct string_list *used,
>> +				  bool *rewrites_head)
>>   {
>> +	const struct name_decoration *decoration;
>> +
>> +	for (decoration = get_name_decoration(&commit->object);
>> +	     decoration;
>> +	     decoration = decoration->next) {
>> +		char *path;
>> +
>> +		if (decoration->type == DECORATION_REF_HEAD)
>> +			*rewrites_head = true;
>> +		if (decoration->type != DECORATION_REF_LOCAL)
>> +			continue;
>> +		path = branch_used_elsewhere(repo, decoration->name, head_target);
>> +		if (path)
>> +			string_list_append(used, decoration->name)->util = path;
>> +	}
>> +}
>> +
>> +static int validate_revwalk(struct repository *repo,
>> +			    enum ref_action action,
>> +			    struct commit *original,
>> +			    const struct strvec *revwalk_args)
>> +{
>> +	struct string_list used = STRING_LIST_INIT_DUP;
>>   	struct strvec args = STRVEC_INIT;
>>   	struct rev_info revs;
>> +	struct commit *commit;
>> +	bool rewrites_head = action == REF_ACTION_HEAD;
>> +	char *head_target;
>>   	int ret;
>>   
>> +	head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD",
>> +					  0, NULL, NULL);
>> +
>>   	strvec_pushv(&args, revwalk_args->v);
>> -	strvec_push(&args, "--min-parents=2");
>>   
>>   	repo_init_revisions(repo, &revs, NULL);
>>   
>> @@ -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);
>> +	}
>> +
>> +	if (action == REF_ACTION_HEAD && head_target &&
>> +	    starts_with(head_target, "refs/heads/")) {
>> +		char *path = branch_used_elsewhere(repo, head_target, head_target);
>> +		if (path)
>> +			string_list_append(&used, head_target)->util = path;
>> +	}
>> +
>> +	if (used.nr) {
>> +		string_list_sort(&used);
>> +		for (size_t i = 0; i < used.nr; i++) {
>> +			const char *branch;
>> +
>> +			if (!skip_prefix(used.items[i].string, "refs/heads/", &branch))
>> +				BUG("branch decoration without refs/heads/ prefix");
>> +			error(_("cannot rewrite branch '%s' used by worktree at '%s'"),
>> +			      branch, (const char *)used.items[i].util);
>> +		}
>> +		if (action == REF_ACTION_BRANCHES)
>> +			advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,
>> +					  _("Use --update-refs=head to rewrite only "
>> +					    "the current branch and leave such branches "
>> +					    "untouched."));
> 
> There are situations where using '--update-refs=head' would not help.
> For example when a branch is checked out in another worktree (using
> --force), or when you're operating on a bare repository.
> 
>> +		ret = -1;
>> +		goto out;
>> +	}
>> +
>> +	if (rewrites_head && rebase_in_progress(repo)) {
>> +		ret = error(_("cannot rewrite HEAD while a rebase is in progress"));
>>   		goto out;
>>   	}
>>   
>> -	reset_revision_walk();
>>   	ret = 0;
>>   
>>   out:
>> +	reset_revision_walk();
>>   	release_revisions(&revs);
>> +	string_list_clear(&used, 1);
>>   	strvec_clear(&args);
>> +	free(head_target);
>>   	return ret;
>>   }
>>   
>> @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo,
>>   		strvec_push(&args, "HEAD");
>>   	}
>>   
>> -	ret = revwalk_contains_merges(repo, &args);
>> +	ret = validate_revwalk(repo, action, original, &args);
>>   	if (ret < 0)
>>   		goto out;
> 


  parent reply	other threads:[~2026-11-30 14:39 UTC|newest]

Thread overview: 23+ 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 [this message]
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
2026-12-01 16:37     ` Phillip Wood
2026-12-02  6:23       ` Patrick Steinhardt
2026-12-02 10:59         ` Phillip Wood
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=644b3ff8-7443-4887-9cea-b649a9382fe1@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 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.