threads / discuss / 49949

Any way to make git-log to enumerate commits?

Subject: Any way to make git-log to enumerate commits?

## tl;dr

7 messages between Dec 5, 2018 and Dec 7, 2018.

replies: 6people: 5as markdown or json

Konstantin Kharlamov· Dec 5, 2018, 14:22 UTC · lore
It would be great if git-log has a formatting option to insert an index of the current commit since HEAD.
It would allow after quitting the git-log to immediately fire up "git rebase -i HEAD~index" instead of "git rebase -i go-copy-paste-this-long-number-id".
Konstantin Khomoutov· Dec 5, 2018, 14:54 UTC · re: Konstantin Kharlamov · lore

Re: Any way to make git-log to enumerate commits?

On Wed, Dec 05, 2018 at 05:22:14PM +0300, Konstantin Kharlamov wrote:
Show 6 quoted lines
> It would be great if git-log has a formatting option to insert an
> index of the current commit since HEAD.
> 
> It would allow after quitting the git-log to immediately fire up "git
> rebase -i HEAD~index" instead of "git rebase -i
> go-copy-paste-this-long-number-id".

This may have little sense in a general case as the history maintained by Git is a graph, not a single line. Hence your prospective approach would only work for cases like `git log` called with the "--first-parent" command-line option.

Still, for a simple approach you may code it right away yourself. Say, let's create an alias:

  $ git config alias.foo '!git log "$@" --pretty=oneline --source | {
      n=0;
      while read sha ref rest; do
        printf "%s\t%s~%s\t%s\n" "$sha" "$ref" $n "$rest"
		n=$((n+1))
	  done
    }'
Now calling `git foo --abbrev=commit` would output something like
9be8e297d        HEAD~7       Frobincated fizzle
where "7" is what you're looking for.
A more roubst solution may need to use the `git rev-list` command.
Elijah Newren· Dec 5, 2018, 15:31 UTC · re: Konstantin Khomoutov · lore

Re: Any way to make git-log to enumerate commits?

On Wed, Dec 5, 2018 at 6:56 AM Konstantin Khomoutov <kostix@bswap.ru> wrote:
Show 33 quoted lines
>
> On Wed, Dec 05, 2018 at 05:22:14PM +0300, Konstantin Kharlamov wrote:
>
> > It would be great if git-log has a formatting option to insert an
> > index of the current commit since HEAD.
> >
> > It would allow after quitting the git-log to immediately fire up "git
> > rebase -i HEAD~index" instead of "git rebase -i
> > go-copy-paste-this-long-number-id".
>
> This may have little sense in a general case as the history maintained
> by Git is a graph, not a single line. Hence your prospective approach
> would only work for cases like `git log` called with the
> "--first-parent" command-line option.
>
> Still, for a simple approach you may code it right away yourself.
> Say, let's create an alias:
>
>   $ git config alias.foo '!git log "$@" --pretty=oneline --source | {
>       n=0;
>       while read sha ref rest; do
>         printf "%s\t%s~%s\t%s\n" "$sha" "$ref" $n "$rest"
>                 n=$((n+1))
>           done
>     }'
>
> Now calling `git foo --abbrev=commit` would output something like
>
> 9be8e297d        HEAD~7       Frobincated fizzle
>
> where "7" is what you're looking for.
>
> A more roubst solution may need to use the `git rev-list` command.
Or, just use name-rev so it works with non-linear histories too:
git log | git name-rev --refs=$(git symbolic-ref HEAD) --stdin | less

That'll add things like "master~7" for stuff in the first parent history and output like "master~7^2~3" for commits on side-branches, either of which can be fed to other git commands.

Andreas Schwab· Dec 5, 2018, 15:43 UTC · re: Elijah Newren · lore

Re: Any way to make git-log to enumerate commits?

On Dez 05 2018, Elijah Newren <newren@gmail.com> wrote:
> Or, just use name-rev so it works with non-linear histories too:
>
> git log | git name-rev --refs=$(git symbolic-ref HEAD) --stdin | less

That wouldn't work for a detached HEAD, though, and you need to use --no-abbrev.

Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Junio C Hamano· Dec 6, 2018, 00:31 UTC · re: Konstantin Khomoutov · lore

Re: Any way to make git-log to enumerate commits?

Konstantin Khomoutov <kostix@bswap.ru> writes:
Show 13 quoted lines
> On Wed, Dec 05, 2018 at 05:22:14PM +0300, Konstantin Kharlamov wrote:
>
>> It would be great if git-log has a formatting option to insert an
>> index of the current commit since HEAD.
>> 
>> It would allow after quitting the git-log to immediately fire up "git
>> rebase -i HEAD~index" instead of "git rebase -i
>> go-copy-paste-this-long-number-id".
>
> This may have little sense in a general case as the history maintained
> by Git is a graph, not a single line. Hence your prospective approach
> would only work for cases like `git log` called with the
> "--first-parent" command-line option.

I do not see why the "name each rev relative to HEAD" formatting option cannot produce HEAD^2~2 etc.

It would be similar to "git log | git name-rev --stdin" but I do not offhand recall if we had a way to tell name-rev to use only HEAD as the anchoring point.

Konstantin Khomoutov· Dec 6, 2018, 11:48 UTC · re: Junio C Hamano · lore

Re: Any way to make git-log to enumerate commits?

On Thu, Dec 06, 2018 at 09:31:36AM +0900, Junio C Hamano wrote:
Show 18 quoted lines
> >> It would be great if git-log has a formatting option to insert an
> >> index of the current commit since HEAD.
> >> 
> >> It would allow after quitting the git-log to immediately fire up "git
> >> rebase -i HEAD~index" instead of "git rebase -i
> >> go-copy-paste-this-long-number-id".
> >
> > This may have little sense in a general case as the history maintained
> > by Git is a graph, not a single line. Hence your prospective approach
> > would only work for cases like `git log` called with the
> > "--first-parent" command-line option.
> 
> I do not see why the "name each rev relative to HEAD" formatting
> option cannot produce HEAD^2~2 etc.
> 
> It would be similar to "git log | git name-rev --stdin" but I do not
> offhand recall if we had a way to tell name-rev to use only HEAD as
> the anchoring point.

My reading was that the OP explicitly wanted to just glance at a single integer number and use it right away in a subsequent rebase command.

I mean, use one's own short memory instead of copying and pasting.

The way I decided to format the reference in my sketch script — using HEAD~<n> — is just a byproduct of the fact I was aware both of the "gitrevisions" manual page and the fact `git name-rev` exists (though I regretfully was not aware it's able to process a stream of `git log`).

Hence while getting fancy names for revisions would be technically correct but less error-prone for retyping from memory ;-)

Junio C Hamano· Dec 7, 2018, 00:12 UTC · re: Konstantin Khomoutov · lore

Re: Any way to make git-log to enumerate commits?

Konstantin Khomoutov <kostix@bswap.ru> writes:
Show 7 quoted lines
>> I do not see why the "name each rev relative to HEAD" formatting
>> option cannot produce HEAD^2~2 etc.
>>  ...
> My reading was that the OP explicitly wanted to just glance at a single
> integer number and use it right away in a subsequent rebase command.
>
> I mean, use one's own short memory instead of copying and pasting.

As everybody pointed out, a single integer number will fundamentally unworkable with distributed nature of Git that inherently makes the history with forks and merges. Besides, there is no way to feed "git log" with "a single integer number", without which "making git-log to enumerate" would not be all that useful ("git show HEAD~4" works but "git show 4" does not).

All of these name-rev based suggestions were about using anchoring point with memorable name plus a short path to reach from there, which I think is the closest thing to "a single integer number" and still is usable for the exact purpose. "HEAD~48^2" on an integration branch would be "the tip of the side branch that was merged some 48 commits ago", for example.

← back to recent threads