zoloto
zoloto

Reputation: 13

Add tag name to "git log --graph --format=format:'%h - %s'" output

How do I add the output of

# Get tag distance $(TAGDIST) 
git describe --tags | \
awk '{split($0,TagNameWithTagDist,"-g");
     i=split(TagNameWithTagDist[1],TagDist,"-");
     print(TagDist[i]);}'

to the output of the following?

git log --graph --format=format:'%h - %s + $TAGDIST'

I don’t see in the “Pretty Formats” section of the git-log documentation a placeholders that corresponds to this value. Placeholder %d shows the tag name on the commit where it was created only.

Upvotes: 1

Views: 435

Answers (1)

Greg Bacon
Greg Bacon

Reputation: 139461

Copy the code below as a file named git-logtagdist in some directory in your path. Note that it runs slowly because it forks a new git-describe process for each commit. You could speed it up by batching the git-describe runs.

#! /usr/bin/env perl

use strict;
use warnings;

use Git;

my $repo = Git->repository;

my @logcmd = (
  "log", "--color=always", "--graph",
  "--format=format:%h - %s - !!!DISTANCE!!!",
);

my($fh,$ctx) = $repo->command_output_pipe(@logcmd);

if (-t STDOUT) {
  my(@pager) = $ENV{GIT_PAGER} || $ENV{PAGER} || ("less", "-R");
  open STDOUT, "|-", @pager or die "$0: open: $!";
}

while (<$fh>) {
  # e.g., 797166c - Merge branch 'maint' - !!!DISTANCE!!!
  s<([0-9a-f]{7,})\b(.+ - )!!!DISTANCE!!!> {
    my($sha1,$msg) = ($1,$2);
    my $distance;

    # e.g., v1.7.9.2-334-g797166c
    chomp(my $describe = $repo->command("describe", "--tags", $sha1));
    if ($describe =~ /^(.+?)-([0-9]+)-g/) {
      $distance = "$2 past $1";
    }
    else {
      $distance = "tagged: $describe";
    }

    $sha1 . $msg . $distance;
  }e;

  print;
}

close STDOUT or warn "$0: close: $!";

Sample output:

$ git logtagdist
* 9bea2b5 - Git 1.7.11-rc3 - tagged: v1.7.11-rc3
*   3a2c135 - Merge git://github.com/git-l10n/git-po - 17 past v1.7.11-rc2
|\
| *   3482b14 - Merge git://github.com/ralfth/git-po-de - 5 past v1.7.11-rc2
| |\
| | * d7f22ed - l10n: de.po: translate 27 new messages - 3 past v1.7.11-rc2
| * | 6cb4571 - l10n: Update  po/vi.po to v1.7.11.rc2.2.gb694fbb - 3 past v1.7.11-rc2
| |/
| * b694fbb - l10n: zh_CN.po: translate 27 new messages - 2 past v1.7.11-rc2
| * 7256fd7 - l10n: Update git.pot (27 new, 1 removed messages) - 1 past v1.7.11-rc2
* |   73a6e3c - Merge branch 'mm/api-credentials-doc' - 11 past v1.7.11-rc2

Upvotes: 1

Related Questions