public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Michael Montalbo via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Michael Montalbo <mmontalbo@gmail.com>
Subject: Re: [PATCH] diff: fix crash with --find-object outside repository
Date: Mon, 02 Mar 2026 08:29:25 -0800	[thread overview]
Message-ID: <xmqqfr6i42l6.fsf@gitster.g> (raw)
In-Reply-To: <pull.2057.git.1772310676438.gitgitgadget@gmail.com> (Michael Montalbo via GitGitGadget's message of "Sat, 28 Feb 2026 20:31:16 +0000")

"Michael Montalbo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Michael Montalbo <mmontalbo@gmail.com>
>
> 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>
> ---
>     diff: fix crash with --find-object outside repository
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2057%2Fmmontalbo%2Ffix-find-object-crash-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2057/mmontalbo/fix-find-object-crash-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/2057
>
>  diff.c                   |  2 ++
>  t/t4053-diff-no-index.sh | 10 ++++++++++
>  2 files changed, 12 insertions(+)
>
> diff --git a/diff.c b/diff.c
> index a68ddd2168..8f0f141be7 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -5215,6 +5215,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);

Makes sense.

> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
> index 69599279e9..15076dfe0d 100755
> --- a/t/t4053-diff-no-index.sh
> +++ b/t/t4053-diff-no-index.sh
> @@ -76,6 +76,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
> +	)
> +'

Looks good, too.

Will queue.  Thanks.



>  test_expect_success 'diff D F and diff F D' '
>  	(
>  		cd repo &&
>
> base-commit: 67ad42147a7acc2af6074753ebd03d904476118f

      reply	other threads:[~2026-03-02 16:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-28 20:31 [PATCH] diff: fix crash with --find-object outside repository Michael Montalbo via GitGitGadget
2026-03-02 16:29 ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqfr6i42l6.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=mmontalbo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox