# [Buglet] "git show <random-40-hexdigit>" gives an unclear error message

3 messages from 2011-03-11 to 2011-03-14. Participants: Junio C Hamano, Jakob Pfender.
Thread: https://gitlist.dev/t/26714

## Junio C Hamano, 2011-03-11 18:28

Subject: [Buglet] "git show <random-40-hexdigit>" gives an unclear error message
Message-ID: <7vzkp1y0jr.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzkp1y0jr.fsf%40alter.siamese.dyndns.org

```
Here is one who wants to tackle potentially a low-hanging fruit.

  $ git cat-file -p 4cd96ce446ab653325a0e47fad73e94c99c57dd2
  error: unable to find 4cd96ce446ab653325a0e47fad73e94c99c57dd2
  fatal: Not a valid object name 4cd96ce446ab653325a0e47fad73e94c99c57dd2

This looks sensible and understandable, even though you could argue that
we don't have to say the same thing twice.  In the same repository:

  $ git show 4cd96ce446ab653325a0e47fad73e94c99c57dd3
  fatal: bad object 4cd96ce446ab653325a0e47fad73e94c99c57dd3

This is bad, as it sounds as if we found the named object but it is
corrupt.

```

## Jakob Pfender, 2011-03-14 15:11

Subject: [PATCH] revision.c: Clarify error message for missing objects
Message-ID: <4D7E303A.5090605@elegosoft.com>
URL: https://gitlist.dev/e/4D7E303A.5090605%40elegosoft.com
In-Reply-To: <7vzkp1y0jr.fsf@alter.siamese.dyndns.org>

```
[Sorry Junio, accidentally sent this to just you the first time around]

If "git show" is invoked on a hash for a file that doesn't exist, the
produced error message was "fatal: bad object <hash>". This is
misleading as the object isn't bad, but missing. As a matter of fact, if
an object is bad, this error message is never produced, because
parse_object() terminates with its own error message if the object is
corrupt.

This patch introduces a check to see whether an object exists (with the
appropriate error message if it doesn't) and removes the unnecessary and
misleading original error message if parse_object() fails.

Signed-off-by: Jakob Pfender <jpfender@elegosoft.com>
---
  revision.c |    4 ++--
  1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/revision.c b/revision.c
index 86d2470..085aac2 100644
--- a/revision.c
+++ b/revision.c
@@ -173,9 +173,9 @@ static struct object *get_reference(struct rev_info 
*revs, const char *name, con
  {
      struct object *object;

+    if (sha1_object_info(sha1, NULL) < 0)
+        die("Not a valid object name %s", name);
      object = parse_object(sha1);
-    if (!object)
-        die("bad object %s", name);
      object->flags |= flags;
      return object;
  }
-- 
1.7.0.4

```

## Junio C Hamano, 2011-03-14 17:40

Subject: Re: [PATCH] revision.c: Clarify error message for missing objects
Message-ID: <7vsjupeh20.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vsjupeh20.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4D7E303A.5090605@elegosoft.com>

```
Jakob Pfender <jpfender@elegosoft.com> writes:

> This patch introduces a check to see whether an object exists (with the
> appropriate error message if it doesn't) and removes the unnecessary and
> misleading original error message if parse_object() fails.

How does this interact with object replacement?

I suspect the real issue is in read_sha1_file_repl() that says:

    /*
     * This function dies on corrupt objects; the callers who want to
     * deal with them should arrange to call read_object() and give error
     * messages themselves.
     */

without giving sufficient support for the callers that wish to use
read_object() themselves to implement the object replacement logic.


> Signed-off-by: Jakob Pfender <jpfender@elegosoft.com>
> ---
>  revision.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/revision.c b/revision.c
> index 86d2470..085aac2 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -173,9 +173,9 @@ static struct object *get_reference(struct
> rev_info *revs, const char *name, con
>  {
>      struct object *object;
>
> +    if (sha1_object_info(sha1, NULL) < 0)
> +        die("Not a valid object name %s", name);
>      object = parse_object(sha1);
> -    if (!object)
> -        die("bad object %s", name);
>      object->flags |= flags;
>      return object;
>  }

```
