# rev-parse: -- is sometimes a flag and sometimes an arg?

8 messages from 2022-08-20 to 2022-08-23. Participants: Tim Hockin, Junio C Hamano, brian m. carlson.
Thread: https://gitlist.dev/t/58338

## Tim Hockin, 2022-08-20 22:39

Subject: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <CAO_RewbD+BJd1hPKCmCNM8wYBSUmZ2TuOoy6t1up1CT-rbn4DA@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_RewbD%2BBJd1hPKCmCNM8wYBSUmZ2TuOoy6t1up1CT-rbn4DA%40mail.gmail.com

```
ˋˋˋ
$ git rev-parse unknown-tag
unknown-tag
fatal: ambiguous argument 'unknown-tag': unknown revision or path not
in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
ˋˋˋ

OK

ˋˋˋ
$ git rev-parse unknown-tag --
fatal: bad revision 'unknown-tag'
ˋˋˋ

Much nicer error.  But:

ˋˋˋ
$ git rev-parse HEAD --
113a928239196d0d9f70671517ce917071ceecf6
--
ˋˋˋ

That's not very nice.  Why is "--" treated as an arg?

ˋˋˋ
$ git rev-parse HEAD
113a928239196d0d9f70671517ce917071ceecf6
ˋˋˋ

Looking at rev-parse code it SEEMS to be intentional, but I can't
comprehend why.

Tim

```

## Junio C Hamano, 2022-08-20 22:59

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <xmqq8rniikrg.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq8rniikrg.fsf%40gitster.g
In-Reply-To: <CAO_RewbD+BJd1hPKCmCNM8wYBSUmZ2TuOoy6t1up1CT-rbn4DA@mail.gmail.com>

```
Tim Hockin <thockin@google.com> writes:

> ˋˋˋ
> $ git rev-parse unknown-tag
> unknown-tag
> fatal: ambiguous argument 'unknown-tag': unknown revision or path not
> in the working tree.
> Use '--' to separate paths from revisions, like this:
> 'git <command> [<revision>...] -- [<file>...]'
> ˋˋˋ
>
> OK
>
> ˋˋˋ
> $ git rev-parse unknown-tag --
> fatal: bad revision 'unknown-tag'
> ˋˋˋ
>
> Much nicer error.  But:
>
> ˋˋˋ
> $ git rev-parse HEAD --
> 113a928239196d0d9f70671517ce917071ceecf6
> --
> ˋˋˋ
>
> That's not very nice.  Why is "--" treated as an arg?
>
> ˋˋˋ
> $ git rev-parse HEAD
> 113a928239196d0d9f70671517ce917071ceecf6
> ˋˋˋ
>
> Looking at rev-parse code it SEEMS to be intentional, but I can't
> comprehend why.

Because the first function of "rev-parse" (among many others) is to
be a helper usable in scripted Porcelains to "parse" command line
arguments, shifting them into revs and "files".  Without options
like --revs-only, --no-revs, --flags etc., it dumps everything, while
translating "revs" into raw object names, to its output.  "--" is a
valid command line option for whatever scripted Porcelain you'd be
writing using "rev-parse" to help parsing its command line arguments.

So everything we see above is very much working as designed.


```

## Tim Hockin, 2022-08-21 00:31

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <CAO_RewZPXP1EUP90kU6uEQ3_CqqC7yWedjfNd7RaVASFSfrg0Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_RewZPXP1EUP90kU6uEQ3_CqqC7yWedjfNd7RaVASFSfrg0Q%40mail.gmail.com
In-Reply-To: <xmqq8rniikrg.fsf@gitster.g>

```
Thanks.  Is there a more friendly way to do what I'm trying to
express?  Should I just use `rev-list -n1` instead?

On Sat, Aug 20, 2022 at 3:59 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Tim Hockin <thockin@google.com> writes:
>
> > ˋˋˋ
> > $ git rev-parse unknown-tag
> > unknown-tag
> > fatal: ambiguous argument 'unknown-tag': unknown revision or path not
> > in the working tree.
> > Use '--' to separate paths from revisions, like this:
> > 'git <command> [<revision>...] -- [<file>...]'
> > ˋˋˋ
> >
> > OK
> >
> > ˋˋˋ
> > $ git rev-parse unknown-tag --
> > fatal: bad revision 'unknown-tag'
> > ˋˋˋ
> >
> > Much nicer error.  But:
> >
> > ˋˋˋ
> > $ git rev-parse HEAD --
> > 113a928239196d0d9f70671517ce917071ceecf6
> > --
> > ˋˋˋ
> >
> > That's not very nice.  Why is "--" treated as an arg?
> >
> > ˋˋˋ
> > $ git rev-parse HEAD
> > 113a928239196d0d9f70671517ce917071ceecf6
> > ˋˋˋ
> >
> > Looking at rev-parse code it SEEMS to be intentional, but I can't
> > comprehend why.
>
> Because the first function of "rev-parse" (among many others) is to
> be a helper usable in scripted Porcelains to "parse" command line
> arguments, shifting them into revs and "files".  Without options
> like --revs-only, --no-revs, --flags etc., it dumps everything, while
> translating "revs" into raw object names, to its output.  "--" is a
> valid command line option for whatever scripted Porcelain you'd be
> writing using "rev-parse" to help parsing its command line arguments.
>
> So everything we see above is very much working as designed.
>

```

## Junio C Hamano, 2022-08-21 00:36

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <xmqq4jy6igb2.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq4jy6igb2.fsf%40gitster.g
In-Reply-To: <CAO_RewZPXP1EUP90kU6uEQ3_CqqC7yWedjfNd7RaVASFSfrg0Q@mail.gmail.com>

```
Tim Hockin <thockin@google.com> writes:

> Thanks.  Is there a more friendly way to do what I'm trying to
> express?  Should I just use `rev-list -n1` instead?

That's hard to answer, as you didn't exactly say what you are trying
to express.  All you asked was: why is "--" treated as an arg.

```

## Tim Hockin, 2022-08-21 00:39

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <CAO_RewaZHL34WBaoXVyXh6ZNVuG+fi5uGUbpRMsc9N=orHzx1Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_RewaZHL34WBaoXVyXh6ZNVuG%2Bfi5uGUbpRMsc9N%3DorHzx1Q%40mail.gmail.com
In-Reply-To: <xmqq4jy6igb2.fsf@gitster.g>

```
Sorry, I assumed it was clear, but that was dumb of me.

I have a string - might be a tag name, might be a branch name, might
be a SHA (complete or partial).  I want to resolve that into a
canonical SHA.  My finger-memory is `git rev-parse`, but I can't say
if that's just a bad habit I picked up somewhere...

Tim

On Sat, Aug 20, 2022 at 5:36 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Tim Hockin <thockin@google.com> writes:
>
> > Thanks.  Is there a more friendly way to do what I'm trying to
> > express?  Should I just use `rev-list -n1` instead?
>
> That's hard to answer, as you didn't exactly say what you are trying
> to express.  All you asked was: why is "--" treated as an arg.

```

## Junio C Hamano, 2022-08-21 00:54

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <xmqqzgfyh0vj.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqzgfyh0vj.fsf%40gitster.g
In-Reply-To: <CAO_RewaZHL34WBaoXVyXh6ZNVuG+fi5uGUbpRMsc9N=orHzx1Q@mail.gmail.com>

```
Tim Hockin <thockin@google.com> writes:

> Sorry, I assumed it was clear, but that was dumb of me.
>
> I have a string - might be a tag name, might be a branch name, might
> be a SHA (complete or partial).  I want to resolve that into a
> canonical SHA.

"git rev-parse --verify string" would insist that 'string' is an
object name and show it as an object name to the standard output, or
gives an error message and exits with a non-zero status.

"git rev-parse --help" has some more elaborate examples in its
EXAMPLES section.




```

## brian m. carlson, 2022-08-23 00:51

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <YwQki4nAEOp4wvv9@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/YwQki4nAEOp4wvv9%40tapette.crustytoothpaste.net
In-Reply-To: <xmqqzgfyh0vj.fsf@gitster.g>

```
On 2022-08-21 at 00:54:40, Junio C Hamano wrote:
> Tim Hockin <thockin@google.com> writes:
> 
> > Sorry, I assumed it was clear, but that was dumb of me.
> >
> > I have a string - might be a tag name, might be a branch name, might
> > be a SHA (complete or partial).  I want to resolve that into a
> > canonical SHA.
> 
> "git rev-parse --verify string" would insist that 'string' is an
> object name and show it as an object name to the standard output, or
> gives an error message and exits with a non-zero status.

Note that this will print a full hex object ID whether it exists in the
repo or not if one is given (for example, the all-zeros object ID).  If
you want to verify that the object exists, write this:

git rev-parse --verify string^{object}
-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA

```

## Tim Hockin, 2022-08-23 17:45

Subject: Re: rev-parse: -- is sometimes a flag and sometimes an arg?
Message-ID: <CAO_RewZCjdGcC3fx7pPZEZ9iTk8_Xs7eBqaf8208da3fD=40mA@mail.gmail.com>
URL: https://gitlist.dev/e/CAO_RewZCjdGcC3fx7pPZEZ9iTk8_Xs7eBqaf8208da3fD%3D40mA%40mail.gmail.com
In-Reply-To: <YwQki4nAEOp4wvv9@tapette.crustytoothpaste.net>

```
Thanks!  That helps a lot.

On Mon, Aug 22, 2022 at 5:51 PM brian m. carlson
<sandals@crustytoothpaste.net> wrote:
>
> On 2022-08-21 at 00:54:40, Junio C Hamano wrote:
> > Tim Hockin <thockin@google.com> writes:
> >
> > > Sorry, I assumed it was clear, but that was dumb of me.
> > >
> > > I have a string - might be a tag name, might be a branch name, might
> > > be a SHA (complete or partial).  I want to resolve that into a
> > > canonical SHA.
> >
> > "git rev-parse --verify string" would insist that 'string' is an
> > object name and show it as an object name to the standard output, or
> > gives an error message and exits with a non-zero status.
>
> Note that this will print a full hex object ID whether it exists in the
> repo or not if one is given (for example, the all-zeros object ID).  If
> you want to verify that the object exists, write this:
>
> git rev-parse --verify string^{object}
> --
> brian m. carlson (he/him or they/them)
> Toronto, Ontario, CA

```
