On 30/11/2026 11:31, Toon Claes wrote:
> Aniket Gupta <anik…@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;
>