diff: fix crash with --find-object outside repository

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>

Michael Montalbo committed Feb 28, 2026 at 20:31 UTC b0ddc7947cc6f0a077543204a96710c53daa48a5
2 files changed +12
diff.c
+2
@@ -5139,6 +5139,8 @@ static int diff_opt_find_object(const struct option *option,
5139 struct object_id oid;
5140
5141 BUG_ON_OPT_NEG(unset);
5142 + if (!startup_info->have_repository)
5143 + return error(_("--find-object requires a git repository"));
5144 if (repo_get_oid(the_repository, arg, &oid))
5145 return error(_("unable to resolve '%s'"), arg);
5146
t/t4053-diff-no-index.sh
+10
@@ -59,6 +59,16 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err
59 )
60 '
61
62 +test_expect_success 'git diff --find-object outside repo fails gracefully' '
63 + (
64 + GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
65 + export GIT_CEILING_DIRECTORIES &&
66 + cd non/git &&
67 + test_must_fail git diff --find-object=abc123 2>err &&
68 + test_grep "find-object requires a git repository" err
69 + )
70 +'
71 +
72 test_expect_success 'diff D F and diff F D' '
73 (
74 cd repo &&