]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: fix crash with --find-object outside repository
authorMichael Montalbo <mmontalbo@gmail.com>
Sat, 28 Feb 2026 20:31:16 +0000 (20:31 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Mar 2026 16:29:48 +0000 (08:29 -0800)
When "git diff --find-object=<oid>" is run outside a git repository,
the option parsing callback eagerly resolves the OID via
repo_get_oid(), which reaches get_main_ref_store() and hits a BUG()
assertion because no repository has been set up.

Check startup_info->have_repository before attempting to resolve the
OID, and return a user-friendly error instead.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4053-diff-no-index.sh

diff --git a/diff.c b/diff.c
index 90e8003dd11e4df314ac8cff73a842164d663c42..fd9aedbec73b43b49bdef04a602f1e5ed320500b 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -5139,6 +5139,8 @@ static int diff_opt_find_object(const struct option *option,
        struct object_id oid;
 
        BUG_ON_OPT_NEG(unset);
+       if (!startup_info->have_repository)
+               return error(_("--find-object requires a git repository"));
        if (repo_get_oid(the_repository, arg, &oid))
                return error(_("unable to resolve '%s'"), arg);
 
index 5e5bad61ca1ed8098dacd3e5573f192adc44ef06..d5fcfbe40735f8b8fc801ab042da94652e6efb86 100755 (executable)
@@ -59,6 +59,16 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err
        )
 '
 
+test_expect_success 'git diff --find-object outside repo fails gracefully' '
+       (
+               GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
+               export GIT_CEILING_DIRECTORIES &&
+               cd non/git &&
+               test_must_fail git diff --find-object=abc123 2>err &&
+               test_grep "find-object requires a git repository" err
+       )
+'
+
 test_expect_success 'diff D F and diff F D' '
        (
                cd repo &&