threads / bug / 57137

bug: git name-rev --stdin --no-undefined on detached head

Subject: bug: git name-rev --stdin --no-undefined on detached head

## tl;dr

8 messages between Dec 22, 2021 and Dec 31, 2021.

replies: 7people: 5as markdown or json

Erik Cervin Edin· Dec 22, 2021, 10:05 UTC · lore
Hey all!

I ran into a situation that I think may be a bug using git name-rev for detached heads.

Steps to reproduce:
Create a detached head
  git checkout --detached
  git commit --allow-empty -m foo
Expected results:
My understanding is that
  git name-rev $(git rev-list -1 HEAD)
  git rev-list -1 HEAD | git name-rev --stdin
should yield the same result.

As well as combining with other flags like --name-only / --no-undefined

Actual results:
Where this fails as expected
  git name-rev --no-undefined $(git rev-list HEAD)
this just prints the SHA wo failing
  git rev-list -1 HEAD |  git name-rev --stdin --no-undefined
"name-only" is also affected
  git rev-list -1 HEAD |  git name-rev --stdin --name-only
returns the SHA and not the name

Tested on git version 2.34.1.windows.1

-- 
Erik Cervin-Edin
John Cai· Dec 23, 2021, 18:39 UTC · re: Erik Cervin Edin · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

It seems like this bug can be generalized to “git name-rev --stdin” does not work with --no-undefined nor --name-only
The --name-only case seems clear to me that we should fix it. It’s misleading to return the sha instead of “undefined” for a rev without a symbolic name, as a sha could be a symbolic name.
I think we can also make the argument that --no-undefined should also die in --stdin mode when given a rev without any symbolic names.
Show 35 quoted lines
> On Dec 22, 2021, at 2:05 AM, Erik Cervin Edin <erik@cervined.in> wrote:
> 
> Hey all!
> 
> I ran into a situation that I think may be a bug
> using git name-rev for detached heads.
> 
> Steps to reproduce:
> Create a detached head
>  git checkout --detached
>  git commit --allow-empty -m foo
> 
> Expected results:
> My understanding is that
>  git name-rev $(git rev-list -1 HEAD)
>  git rev-list -1 HEAD | git name-rev --stdin
> should yield the same result.
> 
> As well as combining with other flags
> like --name-only / --no-undefined
> 
> Actual results:
> Where this fails as expected
>  git name-rev --no-undefined $(git rev-list HEAD)
> this just prints the SHA wo failing
>  git rev-list -1 HEAD |  git name-rev --stdin --no-undefined
> 
> "name-only" is also affected
>  git rev-list -1 HEAD |  git name-rev --stdin --name-only
> returns the SHA and not the name
> 
> Tested on
> git version 2.34.1.windows.1
> -- 
> Erik Cervin-Edin
John Cai· Dec 24, 2021, 06:09 UTC · re: John Cai · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

Show 7 quoted lines
> On Dec 23, 2021, at 10:39 AM, John Cai <jcai@gitlab.com> wrote:
> 
> It seems like this bug can be generalized to “git name-rev --stdin” does not work with --no-undefined nor --name-only
> 
> The --name-only case seems clear to me that we should fix it. It’s misleading to return the sha instead of “undefined” for a rev without a symbolic name, as a sha could be a symbolic name.
> 
> I think we can also make the argument that --no-undefined should also die in --stdin mode when given a rev without any symbolic names.
While I think this would make name-rev more consistent, I’d be interested in hearing what others think about changing the behavior of this command. This would have the potential of breaking scripts that rely on the current behavior. Since I’m a bit new, I’m wondering how we generally handle these cases?
Show 38 quoted lines
> 
> 
>> On Dec 22, 2021, at 2:05 AM, Erik Cervin Edin <erik@cervined.in> wrote:
>> 
>> Hey all!
>> 
>> I ran into a situation that I think may be a bug
>> using git name-rev for detached heads.
>> 
>> Steps to reproduce:
>> Create a detached head
>> git checkout --detached
>> git commit --allow-empty -m foo
>> 
>> Expected results:
>> My understanding is that
>> git name-rev $(git rev-list -1 HEAD)
>> git rev-list -1 HEAD | git name-rev --stdin
>> should yield the same result.
>> 
>> As well as combining with other flags
>> like --name-only / --no-undefined
>> 
>> Actual results:
>> Where this fails as expected
>> git name-rev --no-undefined $(git rev-list HEAD)
>> this just prints the SHA wo failing
>> git rev-list -1 HEAD |  git name-rev --stdin --no-undefined
>> 
>> "name-only" is also affected
>> git rev-list -1 HEAD |  git name-rev --stdin --name-only
>> returns the SHA and not the name
>> 
>> Tested on
>> git version 2.34.1.windows.1
>> -- 
>> Erik Cervin-Edin
> 
Erik Cervin Edin· Dec 24, 2021, 11:44 UTC · re: John Cai · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

> On Fri, Dec 24, 2021 at 7:09 AM John Cai <johncai86@gmail.com> wrote:
> This would have the potential of breaking scripts that rely on the current behavior.

Presumably, using --no-undefined scripts rely on presumed behavior that doesn't exist. Similarly, using --name-only should expect to return a symbolic name While it's possible, I think it's rare that this would break scripts in an unacceptable way.

> On Fri, Dec 24, 2021 at 7:09 AM John Cai <johncai86@gmail.com> wrote:
> Since I’m a bit new, I’m wondering how we generally handle these cases?

I'm also new so I can't really comment on this :) Though I believe the git suite in general is quite adamant on backwards compatibility

Junio C Hamano· Dec 24, 2021, 19:42 UTC · re: John Cai · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

John Cai <jcai@gitlab.com> writes:
Show 11 quoted lines
> It seems like this bug can be generalized to “git name-rev
> --stdin” does not work with --no-undefined nor --name-only
>
> The --name-only case seems clear to me that we should fix
> it. It’s misleading to return the sha instead of “undefined”
> for a rev without a symbolic name, as a sha could be a symbolic
> name.
>
> I think we can also make the argument that --no-undefined should
> also die in --stdin mode when given a rev without any symbolic
> names.
Hmph, the manual page documents:
    --stdin::
            Transform stdin by substituting all the 40-character SHA-1
            hexes (say $hex) with "$hex ($rev_name)".  When used with
            --name-only, substitute with "$rev_name", omitting $hex
            altogether.  Intended for the scripter's use.

It is unfortunate that the way this option works is confusingly a bit different from what we learned to expect from the --stdin option other subcommands like "git pack-objects --stdin" takes. In short, these are not equivalent:

	git name-rev [<options>] $string
        printf "%s" "$string" | xargs git name-rev [<options>]
        printf "%s" "$string" | git name-rev --stdin [<options>]

The first two are supposed to be the equivalent, but the third one is different by design. Its `--stdin` mode is expected to read something like this [*]:

	$ cat sample.txt
        A revision that exists 2ae0a9cb82 is shown here,
        and its full name is 2ae0a9cb8298185a94e5998086f380a355dd8907
        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
        which probably is undescribable hexdigits.

and its designed use is to annotate its input into a more reader friendly from with refnames where possible. Here is what we get:

        $ git name-rev --stdin <sample.txt
        A revision that exists 2ae0a9cb82 is shown here,
        and its full name is 2ae0a9cb8298185a94e5998086f380a355dd8907 (master)
        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
        which probably is undescribable hexdigits.
I notice a few things.
 * An abbreviated commit object name is not affected;
 * A 40-digit string that cannot be described with a reference is
   left alone, without "undefined".

It might be debatable that the latter may want to be annotated with "undefined", but as the command does not molest other noise strings like "its" "full" name" in the input, I think the current behaviour is preferred over appending "(undefined)" after a string we do not recognize that happens to be 40-hex.

When used with --name-only, we see this:
	$ git name-rev --name-only --stdin <sample.txt
        A revision that exists 2ae0a9cb82 is shown here,
        and its full name is master
        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
        which probably is undescribable hexdigits.

So, as far as I can see, it is working as described. If there is any bug in the things I saw and shown here, it is that it is misleading to claim that this behaviour is intended for scripter's use. It clearly is not scripter friendly when you want to run "name-rev" on unbounded number of object names you have, which may not fit on the command line, as that is not how it was designed to be used.

Two possible things we can do to improve are
 * Fix the documentation; it is not for scripters but for annotating
   text with object names.
 * Possibly add --names-from-standard-input option that would behave
   more like "we cannot afford to stuff all object names on the
   command line, so we feed them one by one from the standard input"
   mode the "--stdin" option of other subcommands use.

I do not think the latter is so important, as it is perfectly OK to use xargs to split the large input into multiple invocations of name-rev. This is unlike "pack-objects --stdin" where the command needs to see _all_ input in a single invocation.

[Footnote]
* The sample input was produced with
        $ cat >sample.txt <<EOF
        A revision that exists $(git rev-parse --short HEAD) is shown here,
        and its full name is $(git rev-parse HEAD)
        while its tree object is $(git rev-parse HEAD:)
        which probably is undescribable hexdigits.
        EOF
if you want to try it at home ;-)
John Cai· Dec 24, 2021, 20:09 UTC · re: Junio C Hamano · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

Show 45 quoted lines
> On Dec 24, 2021, at 11:42 AM, Junio C Hamano <gitster@pobox.com> wrote:
> 
> John Cai <jcai@gitlab.com> writes:
> 
>> It seems like this bug can be generalized to “git name-rev
>> --stdin” does not work with --no-undefined nor --name-only
>> 
>> The --name-only case seems clear to me that we should fix
>> it. It’s misleading to return the sha instead of “undefined”
>> for a rev without a symbolic name, as a sha could be a symbolic
>> name.
>> 
>> I think we can also make the argument that --no-undefined should
>> also die in --stdin mode when given a rev without any symbolic
>> names.
> 
> Hmph, the manual page documents:
> 
>    --stdin::
>            Transform stdin by substituting all the 40-character SHA-1
>            hexes (say $hex) with "$hex ($rev_name)".  When used with
>            --name-only, substitute with "$rev_name", omitting $hex
>            altogether.  Intended for the scripter's use.
> 
> It is unfortunate that the way this option works is confusingly a
> bit different from what we learned to expect from the --stdin option
> other subcommands like "git pack-objects --stdin" takes.  In short,
> these are not equivalent:
> 
> 	git name-rev [<options>] $string
>        printf "%s" "$string" | xargs git name-rev [<options>]
>        printf "%s" "$string" | git name-rev --stdin [<options>]
> 
> The first two are supposed to be the equivalent, but the third one
> is different by design.  Its `--stdin` mode is expected to read
> something like this [*]:
> 
> 	$ cat sample.txt
>        A revision that exists 2ae0a9cb82 is shown here,
>        and its full name is 2ae0a9cb8298185a94e5998086f380a355dd8907
>        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
>        which probably is undescribable hexdigits.
> 
> and its designed use is to annotate its input into a more reader
> friendly from with refnames where possible.  

No wonder! This elucidates why I found the user experience of the --stdin mode a bit unexpected, as it would simply echo back the input when it couldn’t find a valid object or refname rather than return an error message. I think I totally missed the verbiage in the documentation that states its meant to **substitute** text in stdin. Makes sense to echo back the input if nothing useful could be found.

Show 41 quoted lines
> Here is what we get:
> 
>        $ git name-rev --stdin <sample.txt
>        A revision that exists 2ae0a9cb82 is shown here,
>        and its full name is 2ae0a9cb8298185a94e5998086f380a355dd8907 (master)
>        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
>        which probably is undescribable hexdigits.
> 
> I notice a few things.
> 
> * An abbreviated commit object name is not affected;
> 
> * A 40-digit string that cannot be described with a reference is
>   left alone, without "undefined".
> 
> It might be debatable that the latter may want to be annotated with
> "undefined", but as the command does not molest other noise strings
> like "its" "full" name" in the input, I think the current behaviour
> is preferred over appending "(undefined)" after a string we do not
> recognize that happens to be 40-hex.
> 
> When used with --name-only, we see this:
> 
> 	$ git name-rev --name-only --stdin <sample.txt
>        A revision that exists 2ae0a9cb82 is shown here,
>        and its full name is master
>        while its tree object is 70d105cc79e63b81cfdcb08a15297c23e60b07ad
>        which probably is undescribable hexdigits.
> 
> So, as far as I can see, it is working as described.  If there is
> any bug in the things I saw and shown here, it is that it is
> misleading to claim that this behaviour is intended for scripter's
> use.  It clearly is not scripter friendly when you want to run
> "name-rev" on unbounded number of object names you have, which may
> not fit on the command line, as that is not how it was designed to
> be used.
> 
> Two possible things we can do to improve are
> 
> * Fix the documentation; it is not for scripters but for annotating
>   text with object names.

Makes sense to update the documentation to make it clear what --stdin mode is meant to do.

Show 24 quoted lines
> 
> * Possibly add --names-from-standard-input option that would behave
>   more like "we cannot afford to stuff all object names on the
>   command line, so we feed them one by one from the standard input"
>   mode the "--stdin" option of other subcommands use.
> 
> I do not think the latter is so important, as it is perfectly OK to
> use xargs to split the large input into multiple invocations of
> name-rev.  This is unlike "pack-objects --stdin" where the command
> needs to see _all_ input in a single invocation.
> 
> 
> [Footnote]
> 
> * The sample input was produced with
> 
>        $ cat >sample.txt <<EOF
>        A revision that exists $(git rev-parse --short HEAD) is shown here,
>        and its full name is $(git rev-parse HEAD)
>        while its tree object is $(git rev-parse HEAD:)
>        which probably is undescribable hexdigits.
>        EOF
> 
> if you want to try it at home ;-)
Junio C Hamano· Dec 25, 2021, 00:35 UTC · re: Junio C Hamano · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

Junio C Hamano <gitster@pobox.com> writes:
Show 14 quoted lines
> Two possible things we can do to improve are
>
>  * Fix the documentation; it is not for scripters but for annotating
>    text with object names.
>
>  * Possibly add --names-from-standard-input option that would behave
>    more like "we cannot afford to stuff all object names on the
>    command line, so we feed them one by one from the standard input"
>    mode the "--stdin" option of other subcommands use.
>
> I do not think the latter is so important, as it is perfectly OK to
> use xargs to split the large input into multiple invocations of
> name-rev.  This is unlike "pack-objects --stdin" where the command
> needs to see _all_ input in a single invocation.

This is primarily to teach newer developers how incompatible changes are done in this project. I am not suggesting that introducing such an incompatible change and following through these steps to the end is a good idea in this case.

Hypothetically, if name-rev were so important a command that the xargs based workaround were unacceptable, what we would probably do is to make the following changes over time.

 1. Introduce a new `--annotate-text` option, which is a synonym to
    the current `--stdin` option.  `--stdin` will work the same way
    as today, but using it will issue a warning() to the standard
    error to tell people to use the `--annotate-text` option
    instead.
 2. After a while, change `--stdin` to die() with a suggestion that
    people should instead pipe into xargs to invoke name-rev
    instead.
 3. After the above two steps, users and scripts of `--stdin` will
    go extinct.  Reintroduce `--stdin` but with a behaviour more in
    line with the `--stdin` option of other subcommands, i.e. take
    one arg per line from the standard input, and behave as if they
    came in the argv[] array.

The idea is to give users an early warning that is annoying enough to encourage migration, ample time to adjust to the new world order, and to ensure that there are no stragglers that gets hurt when the name gets reused for an option with totally different behaviour.

It may not be a bad idea to do steps #1 and #2, though. I do not think xargs name-rev is bad enough to require going to the step #3, but `--stdin` that exists and does a wrong thing is much worse than `--stdin` that does not exist, and finishing upto step #2 is enough to rectify that.

Philip Oakley· Dec 31, 2021, 17:16 UTC · re: Junio C Hamano · lore

Re: bug: git name-rev --stdin --no-undefined on detached head

Thanks you for the example..
On 24/12/2021 19:42, Junio C Hamano wrote:
Show 10 quoted lines
> * The sample input was produced with
>
>         $ cat >sample.txt <<EOF
>         A revision that exists $(git rev-parse --short HEAD) is shown here,
>         and its full name is $(git rev-parse HEAD)
>         while its tree object is $(git rev-parse HEAD:)
>         which probably is undescribable hexdigits.
>         EOF
>
> if you want to try it at home ;-)
The ` $(git rev-parse HEAD:) ` technique is serendipitous. 

I'd forgotten that was how to get the commit object's tree  for further investigation, as I'd need it for my `deadhead` query [1], as used regularly in the Git-for-Windows merging-rebase (see also [2]).

Philip [1] https://lore.kernel.org/git/be7ec330-2b2c-a01f-c7ca-e5e752493ee0@iee.email/ [2] https://lore.kernel.org/git/nycvar.QRO.7.76.6.2112101528200.90@tvgsbejvaqbjf.bet/

← back to recent threads