Skip to content

Commit b0ddc79

Browse files
mmontalbogitster
authored andcommitted
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>
1 parent f368df4 commit b0ddc79

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5139,6 +5139,8 @@ static int diff_opt_find_object(const struct option *option,
51395139
struct object_id oid;
51405140

51415141
BUG_ON_OPT_NEG(unset);
5142+
if (!startup_info->have_repository)
5143+
return error(_("--find-object requires a git repository"));
51425144
if (repo_get_oid(the_repository, arg, &oid))
51435145
return error(_("unable to resolve '%s'"), arg);
51445146

t/t4053-diff-no-index.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err
5959
)
6060
'
6161

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+
6272
test_expect_success 'diff D F and diff F D' '
6373
(
6474
cd repo &&

0 commit comments

Comments
 (0)