-
Notifications
You must be signed in to change notification settings - Fork 6
/
git-log-compact
executable file
·467 lines (443 loc) · 13.6 KB
/
git-log-compact
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env perl
# git-log-compact.pl -- compact git log --oneline alternative with dates, times and initials
# Copyright (C) 2015,2016,2020 Kyle J. McKay <mackyle@gmail.com>. All rights reserved.
# License GPL v2
# Version 1.0.1
use 5.008;
use strict;
use warnings;
use File::Basename qw(basename);
use POSIX qw(strftime _exit);
use Encode;
my $USAGE = <<'USAGE';
usage: git%slog-compact [<options>] [<revision-range>] [[--] <path>...]
-h Show this help
--seconds Use HH:MM:SS instead of just the default HH:MM
--minutes Use just HH:MM (default) for times not HH:MM:SS
--no-times Omit the time field entirely
--two-initials Use maximum of two initials instead of default three
--three-initials Use maximum of three initials (default)
--no-initials Omit the initials field entirely
--commit-message Show the commit message when using --walk-reflogs
--author-date Use author dates and times
--committer-date Use committer dates and times (default)
--initials=author Use author initials (default)
--initials=committer Use committer initials
--initials=author,committer
Use author/committer initials and --two-initials
--initials=committer,author
Use committer/author initials and --two-initials
--time-zone=<zone> Set TZ environment variable to <zone>
--weekday Show the weekday with the date
--no-weekday Do not show the weekday with the date (default)
other log options See `git help log` for more information
Default colors for dates, times and initials may be changed by setting
`color.log-compact.date`, `color.log-compact.time` and/or
`color.log-compact.initials` config values. Dates and times are shown in the
local time zone if TZ is not set in the environment and the `--time-zone`
option has not been used. Default options may be set in the
`log-compact.defaults` config value and they will be treated as though they were
listed first in the command line options list (e.g.
`git config log-compact.defaults "--abbrev=8 --seconds"`)
USAGE
my $timeformat = "%H:%M";
$SIG{PIPE} = sub {_exit 1};
sub dodie {
my $msg = join(" ", @_);
chomp $msg;
die basename($0).": fatal: ".$msg."\n";
}
my ($setusedecorate, $usedecorate);
sub use_decorate {
return $usedecorate if $setusedecorate;
my $do = qx(git config --get log.decorate 2>/dev/null) || "0";
chomp $do;
return 0 if $do eq "0" || $do eq "false" || $do eq "off";
return 0 if $do eq "auto" && ! -t STDOUT;
return 1;
}
my $iw;
sub get_initials($$) {
my $initials = shift;
my $width = shift;
my $wasutf8 = utf8::decode($initials);
$initials = lc($initials)." ";
$initials =~ s/[.]/ /g;
$initials =~ s/ iii? / /g;
$initials =~ s/ iv / /g;
$initials =~ s/ [js]r / /g;
$initials =~ s/[,;:'\042+_-]//g;
$initials =~ s/\([^(]*?\)/ /g;
$initials =~ s/\[[^[]*?\]/ /g;
$initials =~ s/\s+/ /g;
$initials =~ s/^ //g;
return "jc" if $iw == 2 && $initials eq "junio c hamano ";
$initials =~ s/([^ ])[^ ]* /$1/g;
if ($iw == 2) {
$initials =~ s/^(.).+(.)$/$1$2/;
} else {
$initials =~ s/^(..).+(.)$/$1$2/;
}
$initials .= " " x ($width - length($initials))
if length($initials) < $width;
utf8::encode($initials) if $wasutf8;
return $initials;
}
sub get_nocolor_indent {
my $indent = shift;
$indent =~ s/\033[^m]*m//g;
$indent =~ s/\s+$//;
$indent =~ s/-+\.$//;
return $indent;
}
sub get_blank_graph_indent {
my $indent = shift;
chomp $indent;
$indent =~ s/\033[^m]*m//g;
$indent =~ s/^[\s|]+//;
return $indent;
}
sub get_first_indent {
my $indent = shift;
$indent =~ s/\033[^m]*m//g;
$indent =~ s/./ /gs;
return $indent;
}
my $nobar;
my $barcolor;
my $resetcolor = "";
sub get_bar_color {
my ($prefix, $index) = @_;
my $c = (split(m{[-=^<>*+o /|\\_]}, $prefix))[$index];
$c =~ s/\Q$resetcolor\E//g if $resetcolor;
return $c;
}
sub get_indent {
my $indent = shift;
if ($nobar) {
$indent =~ tr/\-=^<>*+o./ /;
} else {
$indent =~ s/[-=^<>*+o]/$barcolor ? $barcolor."|".$resetcolor : "|"/e;
$indent =~ tr/\-./ /;
}
return $indent;
}
sub get_prefix {
my $indent = shift;
$indent =~ tr'\/'||';
return $indent;
}
sub get_defaults {
# defaults are cumulative, but an empty setting resets
my @defaults = ();
my $opts = qx(git config --get-all log-compact.defaults 2>/dev/null);
chomp($opts);
foreach (split(/\r\n|\r|\n/, $opts, -1)) {
s/^\s+//; s/\s+$//;
if ($_ eq "") {
@defaults = ();
next;
}
push(@defaults, $_);
}
return split(" ", join(" ", @defaults));
}
system("git rev-parse --git-dir >/dev/null") == 0 or exit(1);
my ($usemark, $usegraph, $usereflog, $useboundary, $useleftright, $usecherry, $setusecolor, $usecolor, $usecad);
my @args = ();
my $lastwasgrep;
my $dateopt = "%ct";
my $usewkday;
my $reflogsubj = "%gs";
my $sawdashdash;
$iw = undef;
my $iw2 = "";
my ($committer, $author, $ivar, $ivar2);
$ivar = \$author;
foreach my $arg (get_defaults(), @ARGV) {
my $nextisgrep;
if ($sawdashdash || $lastwasgrep) {
push(@args, $arg);
$lastwasgrep = $nextisgrep;
next;
}
if ($arg eq "-h") {
my $dash = "-";
my $exec_path = qx(git --exec-path 2>/dev/null);
chomp $exec_path;
$dash = " " if $ENV{PATH} =~ /^\Q$exec_path\E:/;
printf "$USAGE\n", $dash;
exit 0;
} elsif ($arg eq "--oneline") {
# silently ignore --oneline as we are always in a one line format
next;
} elsif ($arg eq "--seconds") {
# extra option
$timeformat = "%H:%M:%S";
next;
} elsif ($arg eq "--minutes") {
# extra option
$timeformat = "%H:%M";
next;
} elsif ($arg eq "--no-times") {
# extra option
$timeformat = "";
next;
} elsif ($arg eq "--two-initials") {
# extra option
$iw = 2;
next;
} elsif ($arg eq "--three-initials") {
# extra option
$iw = 3;
next;
} elsif ($arg eq "--no-initials") {
# extra option
$iw = 0;
next;
} elsif ($arg eq "--two-initials") {
# extra option
$iw = 3;
next;
} elsif ($arg eq "--commit-message") {
# extra option
$reflogsubj = "%s";
next;
} elsif ($arg eq "--author-date") {
# extra option
$dateopt = "%at";
$usecad = 1;
next;
} elsif ($arg eq "--committer-date") {
# extra option
$dateopt = "%ct";
$usecad = 1;
next;
} elsif ($arg eq "--weekday") {
# extra option
$usewkday = 1;
next;
} elsif ($arg eq "--no-weekday") {
# extra option
$usewkday = undef;
next;
} elsif ($arg =~ /^--initials=/) {
# extra option
$arg =~ s/^--initials=//;
if ($arg eq "author") {
$ivar = \$author;
$ivar2 = undef;
} elsif ($arg eq "committer") {
$ivar = \$committer;
$ivar2 = undef;
} elsif ($arg eq "committer,author" || $arg eq "committer/author") {
$ivar = \$committer;
$ivar2 = \$author;
} elsif ($arg eq "author,committer" || $arg eq "author/committer") {
$ivar = \$author;
$ivar2 = \$committer;
} else {
dodie "--initials= requires 'author', 'committer' or 'committer,author'";
}
next;
} elsif ($arg =~ /^--time-zone=/) {
# extra option
$arg =~ s/^--time-zone=//;
$ENV{TZ} = $arg;
next;
} elsif ($arg eq "--date-order" || $arg eq "--topo-order") {
$dateopt = "%ct" unless $usecad;
} elsif ($arg eq "--author-date-order") {
$dateopt = "%at";
} elsif ($arg =~ /^--(pretty|pretty=.*|format=.*|notes|show-notes|show-notes=.*|standard-notes)$/) {
dodie "formatting/notes option not allowed: $arg";
} elsif ($arg eq "--no-decorate" || $arg eq "--decorate=no") {
$setusedecorate = 1;
$usedecorate = undef;
} elsif ($arg eq "--decorate=auto") {
$setusedecorate = 1;
$usedecorate = -t STDOUT ? 1 : undef;
} elsif ($arg eq "--decorate" || $arg =~ /^--decorate=/) {
$setusedecorate = 1;
$usedecorate = 1;
} elsif ($arg eq "--color" || $arg eq "--color=always") {
$setusecolor = 1;
$usecolor = 1;
} elsif ($arg eq "--no-color" || $arg eq "--color=never") {
$setusecolor = 1;
$usecolor = undef;
} elsif ($arg eq "--color=auto") {
$setusecolor = 1;
$usecolor = -t STDOUT ? 1 : undef;
} elsif ($arg eq "-g" || $arg eq "--walk-reflogs") {
$usereflog = 1;
} elsif ($arg eq "--boundary") {
$useboundary = 1;
$usemark = 1;
} elsif ($arg eq "--cherry-mark" || $arg eq "--cherry") {
$usecherry = 1;
$usemark = 1;
} elsif ($arg eq "--left-right") {
$useleftright = 1;
$usemark = 1;
} elsif ($arg eq "--graph") {
$usegraph = 1;
} elsif ($arg =~ /^(--grep|--grep-reflog|-S|-G)$/) {
$nextisgrep = 1;
} elsif ($arg eq "--") {
$sawdashdash = 1;
}
push(@args, $arg);
$lastwasgrep = $nextisgrep;
}
$iw = defined($ivar2) ? 2 : 3 unless defined($iw);
$iw = "" if !$iw;
$iw2 = $iw if defined($ivar2);
my ($mark, $fixmark) = ("");
$mark = "%m " unless $usegraph || !$usemark;
if ($mark && !$useleftright) {
$fixmark = " ";
$fixmark = "+" if $usecherry;
}
my $color = "never";
my ($hashcolor, $datecolor, $timecolor, $initialscolor, $autocolor) = ("", "", "", "", "");
$usecolor = 1 if !$setusecolor && system("git", "config", "--get-colorbool", "color.diff") == 0;
if ($usecolor) {
$color = "always";
$autocolor = "%C(auto)";
$hashcolor= qx(git config --get-color color.diff.commit "yellow");
$datecolor= qx(git config --get-color color.log-compact.date "bold blue");
$timecolor= qx(git config --get-color color.log-compact.time "green") if $timeformat;
$initialscolor = qx(git config --get-color color.log-compact.initials "red") if $iw;
$resetcolor = qx(git config --get-color "" "reset");
}
my $decopt = "";
$decopt = "$autocolor%d" if use_decorate;
my $pager = qx(git var GIT_PAGER);
defined($pager) and chomp $pager;
$ENV{LESS} = "-FRX" unless exists $ENV{LESS};
$ENV{LV} = "-c" unless exists $ENV{LV};
my ($lastdate, $lastprefix, $lastplainprefix) = ("");
my $msgopt = "%s";
$msgopt = "%gd: $reflogsubj" if $usereflog;
my $lastwasroot = 1;
open(LOG, '-|', "git", "log", "--color=$color",
"--format=tformat:$mark%x1fCOMMIT %H %h $dateopt%x1f%cn%x1f%an%x1f%P%x1f$decopt $msgopt%x1f",
@args) or exit(1);
if (defined($pager) && $pager ne "cat") {
open OUT, "|$pager" or dodie "could not run pager \"$pager\": $!\n";
} else {
open OUT, '>&STDOUT' or die "could not dupe STDOUT: $!";
}
select((select(OUT),$|=1)[0]);
my $delblank;
my @lastparents = ();
my $lastwascommit;
my ($prefix, $data, $parentlist, $subject);
while (my $logline = <LOG>) {
($prefix, $data, $committer, $author, $parentlist, $subject) = split(/\x1f/, $logline, -1);
$subject =~ s/ // if $subject;
my ($flag, $fullhash, $hash, $timestamp) = split(" ", $data, 4) if defined($data);
if (!defined($flag) || $flag ne "COMMIT") {
chomp $prefix;
$delblank = 0, next if $delblank && !$usegraph && $prefix =~ /^\s*$/;
$delblank = 0, next if $delblank && $usegraph && !get_blank_graph_indent($prefix);
print OUT "$prefix\n";
$lastprefix = $prefix;
$lastplainprefix = undef;
@lastparents = ();
$lastwascommit = undef;
next;
}
my $isroot = !$parentlist;
my @parents = split(' ', $parentlist) if $usegraph;
my $initials = $iw ? get_initials($$ivar, $iw) : "";
my $initials2 = $iw2 ? get_initials($$ivar2, $iw2) : "";
my ($newdate, $newday, $newtime) = split(" ", strftime("%Y-%m-%d %a $timeformat", localtime($timestamp)));
$newdate .= " " . $newday if $usewkday;
my $mightneedbreak = $lastwascommit && !$lastwasroot && $usegraph && !grep($_ eq $fullhash, @lastparents);
if ($lastdate ne $newdate || $mightneedbreak) {
my $indent = "";
if (!$lastdate || $mark) {
$indent = get_first_indent($prefix);
$lastprefix = $prefix;
$lastplainprefix = undef;
} elsif ($prefix ne "") {
my $newplainprefix = get_nocolor_indent($prefix);
defined($lastplainprefix) or $lastplainprefix = get_nocolor_indent($lastprefix);
$nobar = undef;
$barcolor = undef;
if ($newplainprefix =~ /^(.*?[-=^<>*+o])/) {{
my $marklen = length($1);
my $difflen = length($lastplainprefix) - length($1);
$nobar = 1;
if ($difflen >= 0) {
my $lastmark = substr($lastplainprefix, $marklen-1, 1);
$lastmark =~ /[-=^<>*+o]/ and $nobar = $lastwasroot || $mightneedbreak, last;
$lastmark eq "|" && $lastdate ne $newdate and
$nobar = 0,
$barcolor = get_bar_color($lastprefix, $marklen - 1),
last;
}
if ($lastdate eq $newdate) {
$lastprefix = $prefix;
$lastplainprefix = $newplainprefix;
goto NOBREAKNEEDED;
}
$difflen >= -1 or last;
substr($lastplainprefix, $marklen-2, 1) eq "\\" and
$nobar = 0,
$barcolor = get_bar_color($lastprefix, $marklen - 2),
last;
$difflen >= 1 &&
substr($lastplainprefix, $marklen, 1) eq "/" and
$nobar = 0,
$barcolor = get_bar_color($lastprefix, $marklen);
}}
$indent = get_indent($prefix);
$lastprefix = $prefix;
$prefix = get_prefix($prefix);
$lastplainprefix = $newplainprefix;
}
if ($lastdate ne $newdate) {
printf OUT "%s%s=== %s ===%s\n", $indent,
$datecolor, $newdate, $resetcolor;
$lastdate = $newdate;
} else {
printf OUT "%s%s %s%s%-${iw}s%s%-${iw2}s %s\n", $indent,
' ' x length($hash), ' ' x length($newtime),
($iw ? " " : ""), "", ($iw2 ? " " : ""), "",
"..........";
}
} else {
$lastprefix = $prefix;
$lastplainprefix = undef;
}
NOBREAKNEEDED:
$lastwasroot = $isroot;
@lastparents = @parents;
$lastwascommit = 1;
my $rootflag = " ";
if ($isroot) {
$rootflag = "_";
$prefix = substr($prefix, 0, length($prefix) - 1) . "_"
if length($prefix);
if ($prefix =~ /^(.*?[-=^<>*+o])(.+)$/) {
my ($initial, $trail) = ($1, $2);
$trail =~ tr/ /_/;
$prefix = $initial . $trail;
}
}
if ($fixmark) {
$prefix = $fixmark . substr($prefix, 1)
if $prefix =~ /^[<>]/;
}
printf OUT "%s%s%s%s%s%s%s%s%s%s\n", $prefix,
"$hashcolor$hash$resetcolor",
$rootflag, ($timeformat ? "$timecolor$newtime$resetcolor " : ""),
$initialscolor, $initials, ($iw2 ? "/" : ""), $initials2,
($iw ? "$resetcolor " : ""), $subject;
$delblank = 1;
}
close LOG;
close OUT;