git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Konstantin Khomoutov <kostix@bswap.ru>
Cc: wuzhouhui <wuzhouhui14@mails.ucas.ac.cn>, git@vger.kernel.org
Subject: Re: How to display "HEAD~*" in "git log"
Date: Wed, 6 Jul 2022 10:38:37 -0400	[thread overview]
Message-ID: <YsWebbaOJbaOZ6i7@coredump.intra.peff.net> (raw)
In-Reply-To: <20220705092514.hsm7cou5bqvajvgq@carbon>

On Tue, Jul 05, 2022 at 12:25:14PM +0300, Konstantin Khomoutov wrote:

> > because SHA sum is hard to memorize, so I have to use copy and past,
> > which is too boring. So, I wonder if there is a way to let "git log"
> > display commits like following:
> > 
> >   HEAD   <one line commit message>
> >   HEAD~1 <one line commit message>
> > HEAD~2 <one line commit message>
> > HEAD~3 <one line commit message>
> >   ...
> > 
> > With these "HEAD~*", I can easily directly type them and no need to
> > move my fingers out of keyboard.
> 
> You can script this. Provided you have a POSIX-compatible shell (such as
> Bash), the encantation would read something like
> 
>  $ git log --oneline | { n=0; while read line; do printf '%d\t%s\n' $n "$line"; done; }

That will just number the commits linearly as they are printed. If your
history has any branches or merges, eventually it will get out of sync.

You can use "name-rev" to annotate commits with names that respect the
history. It only matches full oids, so try:

  git log --oneline --no-abbrev | git name-rev --stdin

If you want shorter hex oids, you can work around it with "--name-only"
and a custom format:

  git log --format='%h (%H) %s' | git name-rev --stdin --name-only

Note that the names will be based on the nearest branches/tags. You can
use "--refs" to limit it, but sadly there doesn't seem to be a way to
specify just HEAD. That might be a fun and easy feature to add.

And finally, if you really like this, you can configure git-log's pager
to always pipe through name-rev, like this:

  [pager]
        log = "git name-rev --stdin | less"

Then the usual "medium" output from git-log will have the annotations on
the "commit" lines. Arguably git-log ought to be able to do this
internally, but I don't think anybody has ever implemented it (and I
wouldn't be surprised if it's a little challenging, just because you'd
have two traversals going on at once in the same program).

Note that I don't use any of those myself. Long ago I taught my terminal
to do keyboard selection of object ids for cut-and-paste. I think there
are probably solutions for tmux and other programs you can find online.
I shared mine for urxvt a while ago:

  https://lore.kernel.org/git/20170120192539.7jts6xqzx46unn7y@sigill.intra.peff.net/

though if anybody is interested in it, let me know because I can share a
new version with some bug fixes and improvements since then.

-Peff

  parent reply	other threads:[~2022-07-06 14:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-05  7:11 How to display "HEAD~*" in "git log" wuzhouhui
2022-07-05  9:25 ` Konstantin Khomoutov
2022-07-05  9:30   ` Konstantin Khomoutov
2022-07-05  9:36     ` wuzhouhui
2022-07-06 12:38       ` kostix
2022-07-06 14:38   ` Jeff King [this message]
2022-07-06 16:28     ` Konstantin Khomoutov
2022-07-06 16:49       ` Jeff King
2022-07-06 18:21         ` Konstantin Khomoutov
2022-07-07 17:06           ` Jeff King
2022-07-06 17:31     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YsWebbaOJbaOZ6i7@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=kostix@bswap.ru \
    --cc=wuzhouhui14@mails.ucas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).