From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796209191; b=lIptH+i03VDhDh8yVhZqLxFIVFuKizLvMl3Iy+CVR/5DdX1YQplgxb2ILNZSavsiu50W h0auRPqzzHGCVu0SWlBxRGq/hwHnLY0xCn71w/4EGRyE5PdYlJdN2lIj8ZsFboNXfop/n Q12smg2ZrbxE56ljeFHVngW4sPvzM3YZag69fkTocOiELWS3eFsXsrrp/PuHdUQ1Wxp2Z C0UyS26/WCHb43cyETDnJvCZQaBG7KtG0NPUl6IoMpo+/iY94GW1oCJaUsevyvZ66kxiF 4V+K/0Y3+P0ny+QOCuagimnnPT5xclI7K7sPdp6XaTIGVcObhqfCjHVBn036vvQQzow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796209191; 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:Reply-To:Subject:To:Cc: References:Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=cXDNu86uitsojFriLbyosGZqcN1hUubHWvw+VFB35ak=; b=wB9L3v6bbiJcKhQrtT33uWfCgj9eSJUlDJkG5BdoEs/Hfzp5aICvM4pBHkUqatOe+rFp a4v2sVFMdvci67kEpm/ZTXugK0ui3hiKkJS0C03Hpp8uds35pLKWeH4yWn/kZ58wXK46J ZlnBVBh4e70KzT5Lxig1ipkPdyaRv02UOn1ZDigxB/Ao6AUsyJucveyFmLS2HLS3kytn+ qzYlKwetNuJ6bw2coWIqSrM2uZPAb7uxJOQ4ckagwheG2oQQi+KtA6cbSd0qaAHGW8WTc Zcoh78dT1GOo1AbVc0XgVEKUOe57jCSPJAjcjEMBHpcs+YWM47nFaSgOSYE/ie3ifyw== 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=IT7vA3Yv; arc=none smtp.remote-ip=209.85.221.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 BCA04606A31A for ; Wed, 2 Dec 2026 10:59:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BCA04606A31A 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=IT7vA3Yv Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4ad6219f7d8so11842169f8f.0 for ; Wed, 02 Dec 2026 02:59:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796209189; x=1796813989; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:reply-to:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=cXDNu86uitsojFriLbyosGZqcN1hUubHWvw+VFB35ak=; b=IT7vA3YvC1PlAAJOa0N4jAP4oClP6MDGmb6NxMycNBM3vv0w/tropMU/OnSvA1zpbp WUwItw849kVV7xZ63oYMvCPu0JbrwNH3gnOpWCvROubNJImdrqrKXr2eZ+K6BEc7yAHr h5Mi6gDmCVDJdOp8giWSN1mRuqad2QGYxJ026gtDunUIu+4hOLZOIeN+wqCIy7LSfVHZ R4bWJNyD7LnKyN+0Rqm4pGBgjINg6F8cRQ4hu+fWLU8YvFLrWH+0CGHFJYiWhCRQMNnj sAM25BQmAAWYsoMFZSLMXcrVNppxktp+ddbKU4albDBChOcR0XjyzkFKYg2XjaJAVXvI 9FOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796209189; x=1796813989; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:reply-to: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=cXDNu86uitsojFriLbyosGZqcN1hUubHWvw+VFB35ak=; b=0/sJey7k9GngnPw9Mq/iqAxu0qqED8OAi3uSv4rO63qaS5zKA45gqo9gZSeCnQpUjI hjUXVe4eTktgMWwtoCBZs/NkDFrEe9+d6/7MPknIKkKtYWgtXgP/I4eiwsIarJpxgVXc 7SACQRH0in1SQjKdEpLotAQ6PofbqcX4BKAUdEG4vNNCYQRkJpeSBXwywcYmCcjvm7oP NAUL8wcmA+2mw3Np6nxsgRSbCV1wOH/6aPbotU/fliGzfXCeBV2zWFb+r8wFgKYsYxmG e0hOJAXKznFQgZe/Y1zGAeI4nzvGHJ3wkUqRDViUgVa7Ch52LdC6KDeH5I75Vh4nge0K ckJQ== X-Forwarded-Encrypted: i=1; AL+hErFTBythkoAZDzthT5dMfpMGdM/MbFphgaT8d2fEULAWN0X6FDm8S4SCzHRM430mmw5APtg=@vger.kernel.org X-Gm-Message-State: APuU4Z90XFQRDoERbyq3L6zZfQyYpwWzBxBxTD4oTe9j795KLhV8q5pU uwB3BSQiDj8LHvAExR1IXbADgdWSr5l1cmu/Myf32iupEzRvKqd91Rwd X-Gm-Gg: AZFdTZfgEt8DZINT7x5IJ1woofcT3D8hwpBwLo+/TZ4hzY35q8sJ3DhhSRZ6OWkgw1c Hiz/9bs35djMbm7ARLOcunza8TmXnnxRkbtA7P5+RXmSmVUemTQ5LkhoeqZZGQ0WT0vnPSdGz14 GVfL/iBqj8aaFIMM5XcCURjgVqC65lt9aqgkEyV2z6DA7+XpJKu+XnU81h58etnlZXkXCzn9I7h lA+rgE18rSo83cbVfrAUW1/J51KLzh6y6DS1LKJZBUHWEBCmos+0EQSCZ3UE8BI/DOMcMB0cVCR 7MQhA3D/jDBegXO72yyB3G5lNX05A/7Gt5XeFqt17Uzia9V4j08zPyy+9v1a2isjF0gFYNkOyKQ QpYjlzKs4P4aJiqgJF6MoclARI/PML3RgPNIspkvAesakcVPEwzT4KfmxXEPQ6OMMYxM+N7DJeI LmZXufQGuJcvkxOW36BEWEhgOWKQ94KN8W6g6+SaOIX0Sf+JEVsgTvNfsazPEHuT9jR1KkKj9j7 Du5JRNlUa5KjuDWDdmOHeyZTs8GUMWEJdpaCjq1EAr73+ub6NozC16A5+VhJ/mfN2bx9QClowy3 sWta2N/JHCs26d7geJUbHSPh129K8HvG1VCtVW9yH4AX/8ahztESb6H35EvHCaBld5xCxh2ZR+Y ac8krhQ== X-Received: by 2002:a5d:5888:0:b0:4a9:4dfa:c151 with SMTP id ffacd0b85a97d-4abb683b6c2mr71017440f8f.26.1796209188835; Wed, 02 Dec 2026 02:59:48 -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-4ab658e2e6dsm73724264f8f.6.2026.12.02.02.59.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Dec 2026 02:59:48 -0800 (PST) Message-ID: Date: Wed, 2 Dec 2026 10:59:47 +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 Reply-To: phillip.wood@dunelm.org.uk Subject: Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees To: Patrick Steinhardt , phillip.wood@dunelm.org.uk Cc: Aniket Gupta , git@vger.kernel.org, Toon Claes , Junio C Hamano , Nikita Bobko References: <20261123103911.98860-1-aniketgupta3001@gmail.com> <20261127173252.7794-1-aniketgupta3001@gmail.com> <3d2e9653-87ce-48f4-a154-2b5a1a31a84c@gmail.com> Content-Language: en-US From: Phillip Wood In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 02/12/2026 06:23, Patrick Steinhardt wrote: > On Tue, Dec 01, 2026 at 04:37:37PM +0000, Phillip Wood wrote: >> On 01/12/2026 13:18, Patrick Steinhardt wrote: >>> >>> 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? >> >> It's confusing if HEAD gets updated in one worktree because a branch was >> rewritten in a different worktree. That's why we have checks in "git rebase" >> to avoid rewriting branches that are checked out in other worktrees. > > Yes, but we resolve `head_target` only for the current worktree, we > don't ever resolve it for any of the other worktrees. That's what's > confusing me. Each worktree has its own HEAD, so we'd need to verify > each of them separately, don't we? After all, rewriting history may > cause us to modify an arbitrary branch, and if such an arbitrary branch > is what HEAD points to in the other worktree then we suffer from the > same problem If a branch that we want to rewrite isn't checked out in the current worktree then we call branch_checked_out() which tells us if it is in use in another worktree. The reason we special case HEAD is the when a branch is in use in more than one worktree there is guarantee which worktree it will return, so it could return the current worktree and we'd miss that it is checked out in another worktree as well. >>> 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. >> >> We need to do the check before rewriting so that a user running "git history >> reword" does not spend time editing the commit message, only for us to >> refuse to update any refs. > > The only point in time where we _need_ to check is when doing the actual > update, because this is where we may end up breaking the user's working > trees. But I agree that we _should_ also check before performing any > rewrites because that's simply a better UX. > >>> 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. In the current code, we do at least two revision walks (one to look for merge commits and the other in replay_revisions(), the "squash" subcommand walks the revs a few more times) with "--branches". There is no guarantee that those revision walks will see the same set of commits so that problem already exists. We'd be better to collect all the refs that need to be updated and all the leaf commits in the first walk, and then pass the leaf commits to replay_revisions() so that we know we're replaying the same set of commits that we've just checked. We'd also need replay_revisions() to give us a mapping of rewritten commits so we can figure out the ref updates. Having a list of rewritten commits would make it easy to support note copying as well. >> Ideally we'd find all the branches that we want to rewrite, write those to >> "$GIT_DIR/history/ref-updates", then walk all the other worktrees checking >> which branch is checked out, being rebased etc. and read each worktree's >> "$GIT_DIR/history/ref-updates" to see if there is an active "git history" >> command rewriting the same branch. If a branch that we want to rewrite is >> being rewritten by a command running in another worktree we'd remove >> "$GIT_DIR/history/ref-updates" and error out. "git checkout", "git rebase" >> etc. would also check "$GIT_DIR/history/ref-updates" as well. By writing the >> list of refs we want to update before checking if any other worktree is >> updating them and removing the file after we've rewritten them, I think we >> avoid races where two commands started at the same time try and update the >> same branch. Unfortunately it is not that simple [1] > I don't think we necessarily need to implement that all at once >> though, the checks added in this patch are a good first step. > > We can fully avoid the race by doing the checks when the ref transaction > is prepared though, can't we? Well, with reftables at least its trivial. > With the files backend it's a bit of a different story though, > unfortunately. To prepare a ref transaction you need to know the new object id. If we want to check we're not going to try and update the same ref as another process before we rewrite the commits then we cannot prepare a transaction. If we want to handle this entirely in the ref backend (which I agree would be ideal) we'd need to be able to say "check this ref points at this object and lock it. I'll tell you what to update it to later" before we rewrite the commits and then "here are the new values for those refs I asked to to lock, please update them now" after we've rewritten the commits. Thanks Phillip [1] If we have a history that looks like - A - B - C (topic) and run "git history drop A" and "git history reword B" at the same time we can have git history drop A git history reword B collect ref updates collect ref updates write $GIT_DIR/history/ref-updates check other worktrees rewrite commits update refs remove $GIT_DIR/history/ref-updates write "$GIT_DIR/history/ref- updates" # At this point the refs we read # have been updated but there # are other rewrites in # progress. check other worktrees rewrite commits update refs # This will either fail if we # use the old value we read at # the start, or overwrite the # changes made by dropping A. We # need to re-read the refs after # checking the other worktrees # to check they are unchanged, # before rewriting the commits. Another thing to think about is what happens if we run GIT_EDITOR="git branch feature B;:" git history reword A should "feature" be rewritten?