public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "Mathias Rav" <m@git.strova.dk>
To: "Junio C Hamano" <gitster@pobox.com>
Cc: "Phillip Wood" <phillip.wood@dunelm.org.uk>,
	"John Cai" <johncai86@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	"Patrick Steinhardt" <ps@pks.im>,
	git@vger.kernel.org
Subject: [PATCH v2] merge-file: fix BUG when --object-id is used in a worktree
Date: Wed, 11 Mar 2026 06:44:06 +0000	[thread overview]
Message-ID: <c076edd0-9057-443b-ba37-33aacde2eede@app.fastmail.com> (raw)
In-Reply-To: <xmqqh5qntpvy.fsf@gitster.g>

The `--object-id` option was added in commit e1068f0ad4
(merge-file: add an option to process object IDs, 2023-11-01)
together with a call to setup_git_directory() to avoid crashing
when run outside a repository.

However, the call to setup_git_directory() is redundant when run inside
a repository, as merge-file runs with RUN_SETUP_GENTLY, so the
repository has already been set up. The redundant call is harmless
when linked worktrees are not used, but in a linked worktree,
the repo_set_gitdir() function ends up being called twice.

Calling repo_set_gitdir() used to be silently accepted, but commit
2816b748e5 (odb: handle changing a repository's commondir, 2025-11-19)
changed this to a BUG in repository.c with the error message:
"cannot reinitialize an already-initialized object directory".

Guard the redundant call to setup_git_directory() behind a repo pointer
check, to ensure that we continue to give the correct "not a git repo"
error whilst avoiding the BUG when running in a linked worktree.

Signed-off-by: Mathias Rav <m@git.strova.dk>
---
Thanks Karthik, Patrick, Kristoffer and Junio for your feedback.
I've incorporated the sum of it all in this PATCH v2:

- Check !repo before object_id and add a comment
- Use term "linked worktree" instead of just "worktree" throughout
- Use git -C instead of a subshell in test
- Remove gitk's quotes from the commit references in the commit message

As for the quotes in the commit references, I use gitk's "Copy commit
reference" daily and am personally used to the quotes. Since
SubmittingPatches seems to give equal preference to --pretty=reference and
"Copy commit reference" I didn't think that the quotes were a problem.
(I wonder how controversial it would be to remove the quotes in gitk.)

 builtin/merge-file.c  | 5 +++--
 t/t6403-merge-file.sh | 9 +++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/merge-file.c b/builtin/merge-file.c
index 46775d0c79..cc8fda3b5b 100644
--- a/builtin/merge-file.c
+++ b/builtin/merge-file.c
@@ -60,7 +60,7 @@ static int diff_algorithm_cb(const struct option *opt,
 int cmd_merge_file(int argc,
 		   const char **argv,
 		   const char *prefix,
-		   struct repository *repo UNUSED)
+		   struct repository *repo)
 {
 	const char *names[3] = { 0 };
 	mmfile_t mmfs[3] = { 0 };
@@ -110,7 +110,8 @@ int cmd_merge_file(int argc,
 			return error_errno("failed to redirect stderr to /dev/null");
 	}
 
-	if (object_id)
+	if (!repo && object_id)
+		/* emit the correct "not a git repo" error in this case */
 		setup_git_directory();
 
 	for (i = 0; i < 3; i++) {
diff --git a/t/t6403-merge-file.sh b/t/t6403-merge-file.sh
index 06ab4d7aed..ed7eec8f93 100755
--- a/t/t6403-merge-file.sh
+++ b/t/t6403-merge-file.sh
@@ -506,6 +506,15 @@ test_expect_success '--object-id fails without repository' '
 	grep "not a git repository" err
 '
 
+test_expect_success 'run in a linked worktree with --object-id' '
+	empty="$(test_oid empty_blob)" &&
+	git worktree add work &&
+	git -C work merge-file --object-id $empty $empty $empty >actual &&
+	git worktree remove work &&
+	git merge-file --object-id $empty $empty $empty >expected &&
+	test_cmp actual expected
+'
+
 test_expect_success 'merging C files with "myers" diff algorithm creates some spurious conflicts' '
 	cat >expect.c <<-\EOF &&
 	int g(size_t u)
-- 
2.53.0

  reply	other threads:[~2026-03-11  6:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10 11:46 [PATCH] merge-file: fix BUG when --object-id is used in a worktree Mathias Rav
2026-03-10 12:35 ` Karthik Nayak
2026-03-10 12:49 ` Patrick Steinhardt
2026-03-10 20:01   ` Junio C Hamano
2026-03-11  6:44     ` Mathias Rav [this message]
2026-03-11  7:18       ` [PATCH v2] " Kristoffer Haugsbakk
2026-03-11 11:14       ` Kristoffer Haugsbakk
2026-03-11 17:26         ` Junio C Hamano
2026-03-11 20:16           ` Kristoffer Haugsbakk
2026-03-18 19:40             ` Junio C Hamano
2026-03-18 19:16       ` Mathias Rav
2026-03-18 19:45         ` Junio C Hamano
2026-03-10 13:34 ` [PATCH] " Kristoffer Haugsbakk

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=c076edd0-9057-443b-ba37-33aacde2eede@app.fastmail.com \
    --to=m@git.strova.dk \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johncai86@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=ps@pks.im \
    --cc=sandals@crustytoothpaste.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox