# Weird behavior of git rev-parse

5 messages from 2009-07-14 to 2009-07-14. Participants: Yakup Akbay, Santi Béjar, Junio C Hamano, Jakub Narebski.
Thread: https://gitlist.dev/t/20108

## Yakup Akbay, 2009-07-14 07:20

Subject: Weird behavior of git rev-parse
Message-ID: <4A5C31CF.2090204@ubicom.com>
URL: https://gitlist.dev/e/4A5C31CF.2090204%40ubicom.com

```
Hi,

instead of `git rev-list -2 HEAD` I've tried `git rev-parse -2 HEAD` 
just to see the effect of -N in rev-parse, but I've got this output:

    -2
    0294cdd1e2c5535f5b87eff4a1aff3390e03af39

Then I've tried

    $ git rev-parse -'hi, this is a test!'

the output is:

    -hi, this is a test!

Is this an expected behavior?


Another questions is, usage is printed if you omit the commit id in git 
rev-list (E.g. `git rev-list -2`). Is there a reason why HEAD is not 
taken as the default?

Regards,
Yakup

```

## Santi Béjar, 2009-07-14 09:06

Subject: Re: Weird behavior of git rev-parse
Message-ID: <adf1fd3d0907140206v691cc27awf4f62397c5a491f8@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0907140206v691cc27awf4f62397c5a491f8%40mail.gmail.com
In-Reply-To: <4A5C31CF.2090204@ubicom.com>

```
2009/7/14 Yakup Akbay <yakbay@ubicom.com>:
> Hi,
>
> instead of `git rev-list -2 HEAD` I've tried `git rev-parse -2 HEAD` just to
> see the effect of -N in rev-parse, but I've got this output:
>
>   -2
>   0294cdd1e2c5535f5b87eff4a1aff3390e03af39
>
> Then I've tried
>
>   $ git rev-parse -'hi, this is a test!'
>
> the output is:
>
>   -hi, this is a test!
>
> Is this an expected behavior?

There is no "-N" flag to "git rev-parse", neither "-'hi, this is a
test!'". The purpuse of git rev-parse is for porcelainish commands to
parse their argument, as the man page says:

git-rev-parse - Pick out and massage parameters

Many git porcelainish commands take mixture of flags
(i.e. parameters that begin with a dash '-') and parameters
meant for the underlying 'git-rev-list' command they use internally
and flags and parameters for the other commands they use
downstream of 'git-rev-list'.  This command is used to
distinguish between them.

>
>
> Another questions is, usage is printed if you omit the commit id in git
> rev-list (E.g. `git rev-list -2`). Is there a reason why HEAD is not taken
> as the default?

You can say what is the default with --default <arg>.

HTH,
Santi

```

## Junio C Hamano, 2009-07-14 09:07

Subject: Re: Weird behavior of git rev-parse
Message-ID: <7vvdlvhbtg.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vvdlvhbtg.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4A5C31CF.2090204@ubicom.com>

```
Yakup Akbay <yakbay@ubicom.com> writes:

> Then I've tried
>
>    $ git rev-parse -'hi, this is a test!'
>
> the output is:
>
>    -hi, this is a test!
>
> Is this an expected behavior?

Absolutely.  rev-parse was originally written as a way for Porcelain
scripts to sift parameters into four different categories.

 * options and non-options (that's two)

 * args meant for rev-list and others (that's another two)

Multiplying two x two gives you four combinations.

Because you are not giving options like --revs-only, --no-revs, --flags,
nor --no-flags, rev-parse outputs everything.  You can try these:

$ git rev-parse --no-flags -'Hi'
$ git rev-parse --no-revs HEAD
$ git rev-parse --flags --no-revs -Hi HEAD

> Another questions is, usage is printed if you omit the commit id in
> git rev-list (E.g. `git rev-list -2`). Is there a reason why HEAD is
> not taken as the default?

The reason is because that is the way it has been, that is the way it is,
and changing it will break existing behaviour and scripts.  In other
words, it is a historical accident without any deep logic.

Besides, "git rev-list" is a plumbing and didn't necessarily want a
user-friendliness niceties such as "defaulting to X when nothing is
given".

```

## Yakup Akbay, 2009-07-14 10:01

Subject: Re: Weird behavior of git rev-parse
Message-ID: <4A5C5792.3050704@ubicom.com>
URL: https://gitlist.dev/e/4A5C5792.3050704%40ubicom.com
In-Reply-To: <7vvdlvhbtg.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Yakup Akbay <yakbay@ubicom.com> writes:
>
>   
>> Then I've tried
>>
>>    $ git rev-parse -'hi, this is a test!'
>>
>> the output is:
>>
>>    -hi, this is a test!
>>
>> Is this an expected behavior?
>>     
>
> Absolutely.  rev-parse was originally written as a way for Porcelain
> scripts to sift parameters into four different categories.
>
>  * options and non-options (that's two)
>
>  * args meant for rev-list and others (that's another two)
>
> Multiplying two x two gives you four combinations.
>
> Because you are not giving options like --revs-only, --no-revs, --flags,
> nor --no-flags, rev-parse outputs everything.  You can try these:
>
> $ git rev-parse --no-flags -'Hi'
> $ git rev-parse --no-revs HEAD
> $ git rev-parse --flags --no-revs -Hi HEAD
>   
Got it!

>> Another questions is, usage is printed if you omit the commit id in
>> git rev-list (E.g. `git rev-list -2`). Is there a reason why HEAD is
>> not taken as the default?
>>     
>
> The reason is because that is the way it has been, that is the way it is,
> and changing it will break existing behaviour and scripts.  In other
> words, it is a historical accident without any deep logic.
>
> Besides, "git rev-list" is a plumbing and didn't necessarily want a
> user-friendliness niceties such as "defaulting to X when nothing is
> given".
>
>   
Makes sense, thanks!

Yakup

```

## Jakub Narebski, 2009-07-14 11:34

Subject: Re: Weird behavior of git rev-parse
Message-ID: <m3iqhvv6ph.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3iqhvv6ph.fsf%40localhost.localdomain
In-Reply-To: <7vvdlvhbtg.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> Yakup Akbay <yakbay@ubicom.com> writes:
> 
> > Then I've tried
> >
> >    $ git rev-parse -'hi, this is a test!'
> >
> > the output is:
> >
> >    -hi, this is a test!
> >
> > Is this an expected behavior?
> 
> Absolutely.  rev-parse was originally written as a way for Porcelain
> scripts to sift parameters into four different categories.
> 
>  * options and non-options (that's two)
> 
>  * args meant for rev-list and others (that's another two)
> 
> Multiplying two x two gives you four combinations.
> 
> Because you are not giving options like --revs-only, --no-revs, --flags,
> nor --no-flags, rev-parse outputs everything.

git-rev-parse can also be used to bring parseopt to shell scripts.

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```
