From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796049590; b=OSkm20tXWHCqZl5cvHX2awHCE3y6PcGZCk1exwANOXuLMbwHXh4e8IQ9Y2Usa5wGHMO2 XipUCSBN8GBLp9QY+fnO/ZeItZI5DzOGh86jpdOXqb0pnXV+5WlE+Z+uMF7S0JAXqpzfg hYS3AaOUmOreSUt922XNiire/ZnoU7XFn03ZjaeJGig/YxPfCu95DaZBDE2EHmnVUI60c bttMnpM4cD4z26wbSyms7TSA0f84RJ1oini0+J4CVwPWyRtFlIJYWYN83/XsA5XMPyedM tHqvDkYW/HzFIseDwH85QDwKjWWxNujs4+2Cn1YnxMDPzJizTD48xQ4tn//HzH1yhdA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796049590; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:From:Reply-To:Subject:To:Cc: References:Content-Language:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=yc3vPpOSWzLh6NarlVF4C2p7tssjFjGZozjFQUzTvwA=; b=pVwPdQqm1ANV954xaGnadf91gfrhu3YsV93sMTs8F6hnx1pCTKy9s3yxqLrOaJBIvuRh NERgnOjNn0sVvu51L7LstyaxijqEnUqIU3qweXS21kqeIVjRFTeJfZLWKI2swLaZqCKqH nL9MHgphOix8meFLuN8fXeh4maQ2MHk7TkJrQu4bPMr0p/22CpgYxb/VqUSnZ65wPvsBT FX+2JTWojaEvdI5dvk4XTLvClpuGrmWitHCOI3WjTS7LvpgkZgyB90tIsHz9GxE9Fum7k xR5h07mWFZB8rIU21X90/rtyO16D2/wgnp0VtPdZl5saOR676txiAmbwzjSy1OVeQjA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="z0Fxu/SL"; arc=none smtp.remote-ip=209.85.208.177 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C0D8606A32F for ; Mon, 30 Nov 2026 14:39:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2C0D8606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=z0Fxu/SL Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-3ae85e3685cso74451651fa.2 for ; Mon, 30 Nov 2026 06:39:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796049587; x=1796654387; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:content-language :references:cc:to:subject:reply-to:from:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=yc3vPpOSWzLh6NarlVF4C2p7tssjFjGZozjFQUzTvwA=; b=z0Fxu/SLWfJx26JmzQP32KhpOAwwDhRRmBdxdXJE/fqeD2H616lM6B/hR2FyZ8kaMt HE+1dEeaKHWHDUxw0hWFzgIFC/OG14t6xGNllMyRQdzWeompjh+5r7omAzESdNF7WJ0L MhyNcQtKTLaNd6W2tjdyAvOgcGDA4nRQRF1bFJkuKSwVF7OG2GmIHFkDd8B7usSsoJKG E2OmNGoQFrBRwJ1FPqXPHVvpwBH/YyVD/5cb7x8d4w81GIa5WrM1oJHU5cLqY3wB3gEZ WHKXGW0c8PYAadrdnSn09oLk8KnqwGXjO7GheWXST3ZGHFx/8eYjUa7nByMPyoav4TtC LfeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796049587; x=1796654387; h=content-transfer-encoding:content-type:in-reply-to:content-language :references:cc:to:subject:reply-to:from:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yc3vPpOSWzLh6NarlVF4C2p7tssjFjGZozjFQUzTvwA=; b=ohpJMe24CH6jrj4nPl9qluS/a/+PzRIvwbk8IxQfVg3RKEMlXCdJhtxMvtKN7sck9v 37qoeCvnEH/ztqhsNiIaLJTjtGgqXHhkL9EPFu7Cllf6extXAPgbePE0eqe8KNtC0aBD JayVHTgyhSKClSxBh34pGJrgDz/P4/QtfwGPw3P2yi7mcV8YpD+LeRh/lnd8O9Itwlha WoOUqqJcYXFsfJst0ehh2cls+yqHczO9vy8a1kTracGVMLH+ejgO1mOfbrF7g9UqlbOC nPqfBLW5F6OgwVifgbrjPTfBkc2wfj5g9QxMLbUFsZbMxSAs2GA96lYrkJ3IdGmlWzlZ ALCQ== X-Forwarded-Encrypted: i=1; AL+hErH49wfbTXqnmc1+sApa4kWzj9Wh8LiAPTCieHUWMlwCvag1wYptCr4ljWYLvPl3Z5c1iyE=@vger.kernel.org X-Gm-Message-State: APuU4Z/xQbWpand2OBgKRVBNtKvB/csVtVlnjv1LTSQBCOINkjW/vz65 kxiX35ULnACnC8fxt1Uw6h0dqhgS2qtNov7K9vgaMlOyF4qWdtR4tBjV X-Gm-Gg: AZFdTZeRNQ4fBF7RRhU6gdSslJnlke66rAvW6uFcm8k7fSJsXwGPrXiPa/1bApVFuew NwGAxDpY+kflunV1c3Bqt4iAcCcmlHe5GeOOuYZ8YsXsZiz2jIALlX4aVSBQEZWJLxpCyjb5A8W GnhHxuYMcuU87rz8/+XnU1z5W4B7vlwgVnAExrCdmTDZBILoe1RFG1JaTcHENv8HkpMjdbU1JFN kgc7svb4gN5IzsBxihnx4nPoQWjpuFJBNKcNaeTLnFoVdzCyDAD1Fxuulr66GcTBnir/kA+yRf5 To6/lE9kHO4hXuyDyzbxZW+/Bi0PpVQ6vuDqSOIp+xgCBpQBs6M/b+U1S5YCsqpfzhqFvaWh2Rc fbqrNeImdTyaiyrtg3csehgmr2ZwM2rTPU/4H6/a1U2USTp4JfUVBQQbhjKB5UrAoPzOxyN4T0F Eg50/iApe5+/BeM2Ppn8apvrollYRFjrxX1/R9QSbuZHeNM3i0lxulkLpFqq8Lp5UV7A4JSlDDL wodwReufCwMIRptDpYIis7i7rMCcIf3m/guE12nOySfsAEe75PeA8s1j2GRZiTAcRTyzmbFMvSL eUzA+bNx+I0i2Fk8n9jI7qwDvpHP6WthOJkNuLla7Pj+K8ceQRkgb2NGqFX2+G28i/3Wd379ycA AqOi5aOXvvJ5PVEb8lu+KB06fmEJW X-Received: by 2002:a05:651c:a210:10b0:3af:acfe:6ca6 with SMTP id 38308e7fff4ca-3afacfe6e0bmr28139941fa.23.1796049586889; Mon, 30 Nov 2026 06:39:46 -0800 (PST) Received: from ?IPV6:2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f? ([2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ad644140b5sm61815094f8f.17.2026.11.30.06.39.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 06:39:46 -0800 (PST) Message-ID: <644b3ff8-7443-4887-9cea-b649a9382fe1@gmail.com> Date: Mon, 30 Nov 2026 14:39:41 +0000 X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Phillip Wood Reply-To: phillip.wood@dunelm.org.uk Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees To: Toon Claes , Aniket Gupta , git@vger.kernel.org Cc: Patrick Steinhardt , Junio C Hamano , Nikita Bobko References: <20261123103911.98860-1-aniketgupta3001@gmail.com> <20261127173252.7794-1-aniketgupta3001@gmail.com> <87qzg2r1wj.fsf@dev.null.iotcl.com.invalid> Content-Language: en-US In-Reply-To: <87qzg2r1wj.fsf@dev.null.iotcl.com.invalid> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 30/11/2026 11:31, Toon Claes wrote: > Aniket Gupta 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; >