{"thread":{"id":"65099","subject":"[PATCH] diff: fix crash with --find-object outside repository","startedAt":"2026-02-28T20:31:19Z","lastAt":"2026-03-02T16:29:27Z","messageCount":2,"participants":["Michael Montalbo via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"537407","messageId":"pull.2057.git.1772310676438.gitgitgadget@gmail.com","threadId":"65099","inReplyTo":null,"subject":"[PATCH] diff: fix crash with --find-object outside repository","fromName":"Michael Montalbo via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-02-28T20:31:16Z","receivedAt":"2026-02-28T20:31:19Z","isPatch":true,"sender":{"key":"mmontalbo@gmail.com","avatar":"https://avatars.githubusercontent.com/u/498667?v=4"},"body":"From: Michael Montalbo <mmontalbo@gmail.com>\n\nWhen \"git diff --find-object=<oid>\" is run outside a git repository,\nthe option parsing callback eagerly resolves the OID via\nrepo_get_oid(), which reaches get_main_ref_store() and hits a BUG()\nassertion because no repository has been set up.\n\nCheck startup_info->have_repository before attempting to resolve the\nOID, and return a user-friendly error instead.\n\nSigned-off-by: Michael Montalbo <mmontalbo@gmail.com>\n---\n    diff: fix crash with --find-object outside repository\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2057%2Fmmontalbo%2Ffix-find-object-crash-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2057/mmontalbo/fix-find-object-crash-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2057\n\n diff.c                   |  2 ++\n t/t4053-diff-no-index.sh | 10 ++++++++++\n 2 files changed, 12 insertions(+)\n\ndiff --git a/diff.c b/diff.c\nindex a68ddd2168..8f0f141be7 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5215,6 +5215,8 @@ static int diff_opt_find_object(const struct option *option,\n \tstruct object_id oid;\n \n \tBUG_ON_OPT_NEG(unset);\n+\tif (!startup_info->have_repository)\n+\t\treturn error(_(\"--find-object requires a git repository\"));\n \tif (repo_get_oid(the_repository, arg, &oid))\n \t\treturn error(_(\"unable to resolve '%s'\"), arg);\n \ndiff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh\nindex 69599279e9..15076dfe0d 100755\n--- a/t/t4053-diff-no-index.sh\n+++ b/t/t4053-diff-no-index.sh\n@@ -76,6 +76,16 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err\n \t)\n '\n \n+test_expect_success 'git diff --find-object outside repo fails gracefully' '\n+\t(\n+\t\tGIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&\n+\t\texport GIT_CEILING_DIRECTORIES &&\n+\t\tcd non/git &&\n+\t\ttest_must_fail git diff --find-object=abc123 2>err &&\n+\t\ttest_grep \"find-object requires a git repository\" err\n+\t)\n+'\n+\n test_expect_success 'diff D F and diff F D' '\n \t(\n \t\tcd repo &&\n\nbase-commit: 67ad42147a7acc2af6074753ebd03d904476118f\n-- \ngitgitgadget\n"},{"id":"537547","messageId":"xmqqfr6i42l6.fsf@gitster.g","threadId":"65099","inReplyTo":"pull.2057.git.1772310676438.gitgitgadget@gmail.com","subject":"Re: [PATCH] diff: fix crash with --find-object outside repository","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-03-02T16:29:25Z","receivedAt":"2026-03-02T16:29:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Michael Montalbo via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Michael Montalbo <mmontalbo@gmail.com>\n>\n> When \"git diff --find-object=<oid>\" is run outside a git repository,\n> the option parsing callback eagerly resolves the OID via\n> repo_get_oid(), which reaches get_main_ref_store() and hits a BUG()\n> assertion because no repository has been set up.\n>\n> Check startup_info->have_repository before attempting to resolve the\n> OID, and return a user-friendly error instead.\n>\n> Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>\n> ---\n>     diff: fix crash with --find-object outside repository\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2057%2Fmmontalbo%2Ffix-find-object-crash-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2057/mmontalbo/fix-find-object-crash-v1\n> Pull-Request: https://github.com/gitgitgadget/git/pull/2057\n>\n>  diff.c                   |  2 ++\n>  t/t4053-diff-no-index.sh | 10 ++++++++++\n>  2 files changed, 12 insertions(+)\n>\n> diff --git a/diff.c b/diff.c\n> index a68ddd2168..8f0f141be7 100644\n> --- a/diff.c\n> +++ b/diff.c\n> @@ -5215,6 +5215,8 @@ static int diff_opt_find_object(const struct option *option,\n>  \tstruct object_id oid;\n>  \n>  \tBUG_ON_OPT_NEG(unset);\n> +\tif (!startup_info->have_repository)\n> +\t\treturn error(_(\"--find-object requires a git repository\"));\n>  \tif (repo_get_oid(the_repository, arg, &oid))\n>  \t\treturn error(_(\"unable to resolve '%s'\"), arg);\n\nMakes sense.\n\n> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh\n> index 69599279e9..15076dfe0d 100755\n> --- a/t/t4053-diff-no-index.sh\n> +++ b/t/t4053-diff-no-index.sh\n> @@ -76,6 +76,16 @@ test_expect_success 'git diff --no-index executed outside repo gives correct err\n>  \t)\n>  '\n>  \n> +test_expect_success 'git diff --find-object outside repo fails gracefully' '\n> +\t(\n> +\t\tGIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&\n> +\t\texport GIT_CEILING_DIRECTORIES &&\n> +\t\tcd non/git &&\n> +\t\ttest_must_fail git diff --find-object=abc123 2>err &&\n> +\t\ttest_grep \"find-object requires a git repository\" err\n> +\t)\n> +'\n\nLooks good, too.\n\nWill queue.  Thanks.\n\n\n\n>  test_expect_success 'diff D F and diff F D' '\n>  \t(\n>  \t\tcd repo &&\n>\n> base-commit: 67ad42147a7acc2af6074753ebd03d904476118f\n"}]}