Skip to content

Commit 57246b7

Browse files
Mortalgitster
authored andcommitted
merge-file: fix BUG when --object-id is used in a worktree
The `--object-id` option was added in commit e1068f0 (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 2816b74 (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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 67ad421 commit 57246b7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

builtin/merge-file.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int diff_algorithm_cb(const struct option *opt,
6060
int cmd_merge_file(int argc,
6161
const char **argv,
6262
const char *prefix,
63-
struct repository *repo UNUSED)
63+
struct repository *repo)
6464
{
6565
const char *names[3] = { 0 };
6666
mmfile_t mmfs[3] = { 0 };
@@ -110,7 +110,8 @@ int cmd_merge_file(int argc,
110110
return error_errno("failed to redirect stderr to /dev/null");
111111
}
112112

113-
if (object_id)
113+
if (!repo && object_id)
114+
/* emit the correct "not a git repo" error in this case */
114115
setup_git_directory();
115116

116117
for (i = 0; i < 3; i++) {

t/t6403-merge-file.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,15 @@ test_expect_success '--object-id fails without repository' '
506506
grep "not a git repository" err
507507
'
508508

509+
test_expect_success 'run in a linked worktree with --object-id' '
510+
empty="$(test_oid empty_blob)" &&
511+
git worktree add work &&
512+
git -C work merge-file --object-id $empty $empty $empty >actual &&
513+
git worktree remove work &&
514+
git merge-file --object-id $empty $empty $empty >expected &&
515+
test_cmp actual expected
516+
'
517+
509518
test_expect_success 'merging C files with "myers" diff algorithm creates some spurious conflicts' '
510519
cat >expect.c <<-\EOF &&
511520
int g(size_t u)

0 commit comments

Comments
 (0)