]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge-file: fix BUG when --object-id is used in a worktree
authorMathias Rav <m@git.strova.dk>
Wed, 11 Mar 2026 06:44:06 +0000 (06:44 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 18 Mar 2026 19:46:26 +0000 (12:46 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/merge-file.c
t/t6403-merge-file.sh

index 46775d0c79f4547a23b9b7e9abdef976a2005832..cc8fda3b5b7c15e5cfb404b08a936a631d48cf29 100644 (file)
@@ -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++) {
index 06ab4d7aede08139165bfa27c1bbddb97cc06aad..ed7eec8f93cd020659b9b5336798fa769ff77e64 100755 (executable)
@@ -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)