Skip to content

Commit 3a93dd9

Browse files
committed
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>
1 parent 67ad421 commit 3a93dd9

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
@@ -5215,6 +5215,8 @@ static int diff_opt_find_object(const struct option *option,
52155215
struct object_id oid;
52165216

52175217
BUG_ON_OPT_NEG(unset);
5218+
if (!startup_info->have_repository)
5219+
return error(_("--find-object requires a git repository"));
52185220
if (repo_get_oid(the_repository, arg, &oid))
52195221
return error(_("unable to resolve '%s'"), arg);
52205222

t/t4053-diff-no-index.sh

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

79+
test_expect_success 'git diff --find-object outside repo fails gracefully' '
80+
(
81+
GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
82+
export GIT_CEILING_DIRECTORIES &&
83+
cd non/git &&
84+
test_must_fail git diff --find-object=abc123 2>err &&
85+
test_grep "find-object requires a git repository" err
86+
)
87+
'
88+
7989
test_expect_success 'diff D F and diff F D' '
8090
(
8191
cd repo &&

0 commit comments

Comments
 (0)