git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [Question] git rev-parse verify for non-existent object

From
Christian Couder <christian.couder@gmail.com>
Date
Aug 2, 2023, 10:24 UTC
Message-ID
<CAP8UFD02tYrz+bxoR_GQfYuxxrmv5=BXjcZuiqXbX3m7N2dPaQ@mail.gmail.com>
In-Reply-To
<CAOLTT8RvAbmS1SwFja8_+NSBBoL0gTH1RgpSN5CYtDWZscMYZA@mail.gmail.com>
On Wed, Aug 2, 2023 at 9:50 AM ZheNing Hu <adlternative@gmail.com> wrote:
Show 20 quoted lines
>
> In my case, I often need to use "git rev-parse --verify" to verify
> if a revision exists in the repository, and this usually works:
>
> git rev-parse --verify HEAD
> afa5ff0f56bc60d1c9abe839726e7fb2105a9ca3
>
> git rev-parse --verify afa5ff0f56bc60d1c9abe839726e7fb2105a9ca3
> afa5ff0f56bc60d1c9abe839726e7fb2105a9ca3
>
> git rev-parse --verify aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> fatal: Needed a single revision
>
> However, when I started checking for a non-existent OID that happened
> to be exactly 40 characters long, something unexpected happened.
>  "git rev-parse --verify" did not produce any error messages.
> I consider this to be a BUG.
>
> git rev-parse --verify aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The doc says:
"
      --verify
          Verify that exactly one parameter is provided, and that it
can be turned into a raw 20-byte SHA-1 that can be used to access the
object database. If so, emit it to the standard output;
          otherwise, error out.
          If you want to make sure that the output actually names an
object in your object database and/or can be used as a specific type
of object you require, you can add the ^{type} peeling
          operator to the parameter. For example, git rev-parse
"$VAR^{commit}" will make sure $VAR names an existing object that is a
commit-ish (i.e. a commit, or an annotated tag that points
          at a commit). To make sure that $VAR names an existing
object of any type, git rev-parse "$VAR^{object}" can be used.
"

So "--verify <argument>" only checks that the <argument> provided to it can be turned into a raw 20-byte SHA-1 (or perhaps a raw 32-byte SHA256 in case the repo is using SHA256). "^{object}" should be added at the end of the <argument> if you want to verify that the raw 20-byte SHA-1 (or the raw 32-byte SHA256) also corresponds to an actual object in the repo.

$ git rev-parse --verify aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa^{object} fatal: Needed a single revision

Hope this helps.
Previous: ZheNing HuNext: ZheNing Hu
Message 2 of 5 in “[Question] git rev-parse verify for non-existent object”
  1. ZheNing HuAug 2, 2023
  2. Christian CouderAug 2, 2023
  3. ZheNing HuAug 2, 2023
  4. Junio C HamanoAug 2, 2023
  5. ZheNing HuAug 3, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.