# rev-parse, unknown arguments and extended sha1's

5 messages from 2005-06-24 to 2005-06-24. Participants: Sven Verdoolaege, Linus Torvalds.
Thread: https://gitlist.dev/t/1018

## Sven Verdoolaege, 2005-06-24 12:24

Subject: rev-parse, unknown arguments and extended sha1's
Message-ID: <20050624122436.GA15182@pc117b.liacs.nl>
URL: https://gitlist.dev/e/20050624122436.GA15182%40pc117b.liacs.nl

```
Is git-rev-parse supposed to echo arguments it doesn't understand ?
It currently does, but git-checkout-script seems to think it doesn't:

                rev=$(git-rev-parse "$arg")   
                if [ -z "$rev" ]; then
                        echo "unknown flag $arg"
                        exit 1
                fi

Also, is there any reason why the extended sha1 notation
is restricted to the scripts and not used in the actual
plumbing ?

skimo

```

## Linus Torvalds, 2005-06-24 16:09

Subject: Re: rev-parse, unknown arguments and extended sha1's
Message-ID: <Pine.LNX.4.58.0506240904240.11175@ppc970.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0506240904240.11175%40ppc970.osdl.org
In-Reply-To: <20050624122436.GA15182@pc117b.liacs.nl>

```


On Fri, 24 Jun 2005, Sven Verdoolaege wrote:
>
> Is git-rev-parse supposed to echo arguments it doesn't understand ?
> It currently does, but git-checkout-script seems to think it doesn't:
> 
>                 rev=$(git-rev-parse "$arg")   
>                 if [ -z "$rev" ]; then
>                         echo "unknown flag $arg"
>                         exit 1
>                 fi

Argh, there's a "--revs-only" thing that I was planning to use there, some 
remnant of an ealy plan (unimplemented) to use that to determine if it is 
a filename to be checked out, or a revision.

But then I never got around to do the "specify individual filenames" part 
of "git checkout".

Anyway, add the --revs-only, and the test against empty should make a bit 
more sense. In fact, it _should_ test that it's a SHA1 (and not a rev 
argument), but I couldn't come up with a good way to test that in shell.

		Linus

```

## Sven Verdoolaege, 2005-06-24 16:17

Subject: Re: rev-parse, unknown arguments and extended sha1's
Message-ID: <20050624161718.GA14909@pc117b.liacs.nl>
URL: https://gitlist.dev/e/20050624161718.GA14909%40pc117b.liacs.nl
In-Reply-To: <Pine.LNX.4.58.0506240904240.11175@ppc970.osdl.org>

```
On Fri, Jun 24, 2005 at 09:09:23AM -0700, Linus Torvalds wrote:
> Anyway, add the --revs-only, and the test against empty should make a bit 
> more sense. In fact, it _should_ test that it's a SHA1 (and not a rev 
> argument), but I couldn't come up with a good way to test that in shell.
> 

This is what I use:

rev2=`echo $rev | sed -e 's/[^0-9a-f]//'`
rev3=`echo $rev2 | sed -e 's/........................................//'`
if test "x$rev3" = "x" -a "x$rev" = "x$rev2"; then
	echo "valid SHA1"
fi

skimo

```

## Linus Torvalds, 2005-06-24 16:43

Subject: Re: rev-parse, unknown arguments and extended sha1's
Message-ID: <Pine.LNX.4.58.0506240941520.11175@ppc970.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0506240941520.11175%40ppc970.osdl.org
In-Reply-To: <20050624161718.GA14909@pc117b.liacs.nl>

```


On Fri, 24 Jun 2005, Sven Verdoolaege wrote:
>
> This is what I use:
> 
> rev2=`echo $rev | sed -e 's/[^0-9a-f]//'`
> rev3=`echo $rev2 | sed -e 's/........................................//'`
> if test "x$rev3" = "x" -a "x$rev" = "x$rev2"; then
> 	echo "valid SHA1"
> fi

Oooh. UGGHLEE.

This is one reason I don't do much shell.

Anyway, I think I'll make git-rev-parse have some option to "error out if 
the result is anything but a single revision number", since this is really 
the only reason for git-rev-parse in the first place: to make readable 
scripts.

		Linus

```

## Linus Torvalds, 2005-06-24 17:32

Subject: Re: rev-parse, unknown arguments and extended sha1's
Message-ID: <Pine.LNX.4.58.0506241026490.11175@ppc970.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0506241026490.11175%40ppc970.osdl.org
In-Reply-To: <Pine.LNX.4.58.0506240941520.11175@ppc970.osdl.org>

```


On Fri, 24 Jun 2005, Linus Torvalds wrote:
> 
> Anyway, I think I'll make git-rev-parse have some option to "error out if 
> the result is anything but a single revision number", since this is really 
> the only reason for git-rev-parse in the first place: to make readable 
> scripts.

How about something like

	rev=$(git-rev-parse --default HEAD --revs-only --verify "$@") || exit 1

which now should work correctly.

In particular, the "--default" field is now expanded properly as a
revision, instead of just output raw, so the "HEAD" actually gets
translated into its SHA1 representation (and this also means that you can
now use SHA1 expressions in "default", ie you don't need to do two
git-rev-parse phases to do "--default HEAD^" etc).

The "--verify" thing then checks that the end result isn't a revision
argument (ie "--max-age=.." isn't accepted), and that there's exactly one
revision in the result (ie no ranges or multiple revisions some other
way).

So now you shouldn't need to check the result any more. You know that if
it worked, "rev" will be a nice SHA1 (of course, it might still be an
_invalid_ SHA1, that's a different issue. But it's at least syntactically
ok)

		Linus

```
