threads / discuss / 58338

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

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

## tl;dr

8 messages between Aug 20, 2022 and Aug 23, 2022.

replies: 7people: 3as markdown or json

Tim Hockin· Aug 20, 2022, 22:39 UTC · lore

``` $ 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· Aug 20, 2022, 22:59 UTC · re: Tim Hockin · lore

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

Tim Hockin <thockin@google.com> writes:
Show 33 quoted lines
> ```
> $ 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· Aug 21, 2022, 00:31 UTC · re: Junio C Hamano · lore

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

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:
Show 47 quoted lines
>
> 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· Aug 21, 2022, 00:36 UTC · re: Tim Hockin · lore

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

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· Aug 21, 2022, 00:39 UTC · re: Junio C Hamano · lore

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

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:
Show 8 quoted lines
>
> 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· Aug 21, 2022, 00:54 UTC · re: Tim Hockin · lore

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

Tim Hockin <thockin@google.com> writes:
Show 5 quoted lines
> 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· Aug 23, 2022, 00:51 UTC · re: Junio C Hamano · lore

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

On 2022-08-21 at 00:54:40, Junio C Hamano wrote:
Show 11 quoted lines
> 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· Aug 23, 2022, 17:45 UTC · re: brian m. carlson · lore

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

Thanks!  That helps a lot.

On Mon, Aug 22, 2022 at 5:51 PM brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 22 quoted lines
>
> 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

← back to recent threads