forked from jwiegley/git-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-forest
executable file
·641 lines (567 loc) · 13.1 KB
/
git-forest
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/perl
#
# git-森林
# text-based tree visualisation
# Copyright © Jan Engelhardt <jengelh [at] medozas de>, 2008
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 or 3 of the license.
#
use Getopt::Long;
use Git;
use strict;
use utf8;
use Encode qw(encode);
my $Repo = Git->repository($ENV{"GIT_DIR"} || ".");
my $Pretty_fmt = "format:%s";
my $Reverse_order = 0;
my $Show_all = 0;
my $Show_rebase = 1;
my $Style = 1;
my $Subvine_depth = 2;
my $With_sha = 0;
my %Color = (
"default" => "\e[0m", # ]
"at" => "\e[1;30m", # ]
"hhead" => "\e[1;31m", # ]
"head" => "\e[1;32m", # ]
"ref" => "\e[1;34m", # ]
"remote" => "\e[1;35m", # ]
"sha" => "\e[0;31m", # ]
"tag" => "\e[1;33m", # ]
"tree" => "\e[0;33m", # ]
);
&main();
sub main
{
&Getopt::Long::Configure(qw(bundling pass_through));
&GetOptions(
"all" => \$Show_all,
"no-color" => sub { %Color = (); },
"no-rebase" => sub { $Show_rebase = 0; },
"a" => sub { $Pretty_fmt = "format:\e[1;30m(\e[0;32m%an\e[1;30m)\e[0m %s"; }, # ]]]]
"pretty=s" => \$Pretty_fmt,
"reverse" => \$Reverse_order,
"svdepth=i" => \$Subvine_depth,
"style=i" => \$Style,
"sha" => \$With_sha,
);
++$Subvine_depth;
if (substr($Pretty_fmt, 0, 7) ne "format:") {
die "If you use --pretty, it must be in the form of --pretty=format:";
}
$Pretty_fmt = substr($Pretty_fmt, 7);
while ($Pretty_fmt =~ /\%./g) {
if ($& eq "\%b" || $& eq "\%n" || ($&.$') =~ /^\%x0a/i) {
die "Cannot use \%b, \%n or \%x0a in --pretty=format:";
}
}
if ($Show_all) {
#
# Give --all back. And add HEAD to include commits
# in the rev list that belong to a detached HEAD.
#
unshift(@ARGV, "--all", "HEAD");
}
if ($Reverse_order) {
tie(*STDOUT, "ReverseOutput");
}
&process();
if ($Reverse_order) {
untie *STDOUT;
}
}
sub get_line_block
{
my($fh, $max) = @_;
while (scalar(@h::ist) < $max) {
my $x;
$x = <$fh>;
if (!defined($x)) {
last;
}
push(@h::ist, $x);
}
my @ret = (shift @h::ist);
foreach (2..$max) {
push(@ret, $h::ist[$_-2]);
}
return @ret;
}
sub process
{
my(@vine);
my $refs = &get_refs();
my($fh, $fhc) = $Repo->command_output_pipe("log", "--date-order",
"--pretty=format:<%H><%h><%P>$Pretty_fmt", @ARGV);
while (my($line, @next_sha) = get_line_block($fh, $Subvine_depth)) {
if (!defined($line)) {
last;
}
chomp $line;
my($sha, $mini_sha, $parents, $msg) =
($line =~ /^<(.*?)><(.*?)><(.*?)>(.*)/s);
my @next_sha = map { ($_) = /^<(.*?)>/ } @next_sha;
my @parents = split(" ", $parents);
&vine_branch(\@vine, $sha);
my $ra = &vine_commit(\@vine, $sha, \@parents);
if (exists($refs->{$sha})) {
print &vis_post(&vis_commit($ra),
$Color{at}."m".$Color{default});
&ref_print($refs->{$sha});
} else {
print &vis_post(&vis_commit($ra, " "));
}
if ($With_sha) {
print $msg, $Color{at}, encode('UTF-8', "──("), $Color{sha}, $mini_sha,
$Color{at}, ")", $Color{default}, "\n";
} else {
print $msg, "\n";
}
&vine_merge(\@vine, $sha, \@next_sha, \@parents);
}
$Repo->command_close_pipe($fh, $fhc);
}
sub get_refs
{
my($fh, $c) = $Repo->command_output_pipe("show-ref");
my $ret = {};
while (defined(my $ln = <$fh>)) {
chomp $ln;
if (length($ln) == 0) {
next;
}
my($sha, $name) = ($ln =~ /^(\S+)\s+(.*)/s);
if (!exists($ret->{$sha})) {
$ret->{$sha} = [];
}
push(@{$ret->{$sha}}, $name);
if ($name =~ m{^refs/tags/}) {
my $sub_sha = $Repo->command("log", "-1",
"--pretty=format:%H", $name);
chomp $sub_sha;
if ($sha ne $sub_sha) {
push(@{$ret->{$sub_sha}}, $name);
}
}
}
$Repo->command_close_pipe($fh, $c);
my $rebase = -e $Repo->repo_path()."/.dotest-merge/git-rebase-todo" &&
$Show_rebase;
if ($rebase) {
if (open(my $act_fh, $Repo->repo_path().
"/.dotest-merge/git-rebase-todo")) {
my($curr) = (<$act_fh> =~ /^\S+\s+(\S+)/);
$curr = $Repo->command("rev-parse", $curr);
chomp $curr;
unshift(@{$ret->{$curr}}, "rebase/cn");
close $act_fh;
}
chomp(my $up = $Repo->command("rev-parse", ".dotest-merge/upstream"));
chomp(my $onto = $Repo->command("rev-parse", ".dotest-merge/onto"));
chomp(my $old = $Repo->command("rev-parse", ".dotest-merge/head"));
unshift(@{$ret->{$up}}, "rebase/upstream");
unshift(@{$ret->{$onto}}, "rebase/onto");
unshift(@{$ret->{$old}}, "rebase/saved-HEAD");
}
my $head = $Repo->command("rev-parse", "HEAD");
chomp $head;
if ($rebase) {
unshift(@{$ret->{$head}}, "rebase/new");
}
unshift(@{$ret->{$head}}, "HEAD");
return $ret;
}
#
# ref_print - print a ref with color
# @s: ref name
#
sub ref_print
{
foreach my $symbol (@{shift @_}) {
print $Color{at}, "[";
if ($symbol eq "HEAD" || $symbol =~ m{^rebase/}) {
print $Color{hhead}, $symbol;
} elsif ($symbol =~ m{^refs/(remotes/[^/]+)/(.*)}s) {
print $Color{remote}, $1, $Color{head}, "/$2";
} elsif ($symbol =~ m{^refs/heads/(.*)}s) {
print $Color{head}, $1;
} elsif ($symbol =~ m{^refs/tags/(.*)}s) {
print $Color{tag}, $1;
} elsif ($symbol =~ m{^refs/(.*)}s) {
print $Color{ref}, $1;
}
print $Color{at}, encode('UTF-8', "]──"), $Color{default};
}
}
#
# vine_branch -
# @vine: column array containing the expected parent IDs
# @rev: commit ID
#
# Draws the branching vine matrix between a commit K and K^ (@rev).
#
sub vine_branch
{
my($vine, $rev) = @_;
my $idx;
my($matched, $master) = (0, 0);
my $ret;
# Transform array into string
for ($idx = 0; $idx <= $#$vine; ++$idx) {
if (!defined($vine->[$idx])) {
$ret .= " ";
next;
} elsif ($vine->[$idx] ne $rev) {
$ret .= "I";
next;
}
if (!$master && $idx % 2 == 0) {
$ret .= "S";
$master = 1;
} else {
$ret .= "s";
$vine->[$idx] = undef;
}
++$matched;
}
if ($matched < 2) {
return;
}
&remove_trailing_blanks($vine);
print &vis_post(&vis_fan($ret, "branch")), "\n";
}
#
# vine_commit -
# @vine: column array containing the expected IDs
# @rev: commit ID
# @parents: array of parent IDs
#
sub vine_commit
{
my($vine, $rev, $parents) = @_;
my $ret;
for (my $i = 0; $i <= $#$vine; ++$i) {
if (!defined($vine->[$i])) {
$ret .= " ";
} elsif ($vine->[$i] eq $rev) {
$ret .= "C";
} else {
$ret .= "I";
}
}
if ($ret !~ /C/) {
# Not having produced a C before means this is a tip
my $i;
for ($i = &round_down2($#$vine); $i >= 0; $i -= 2) {
if (substr($ret, $i, 1) eq " ") {
substr($ret, $i, 1) = "t";
$vine->[$i] = $rev;
last;
}
}
if ($i < 0) {
if (scalar(@$vine) % 2 != 0) {
push(@$vine, undef);
$ret .= " ";
}
$ret .= "t";
push(@$vine, $rev);
}
}
&remove_trailing_blanks($vine);
if (scalar(@$parents) == 0) {
# tree root
$ret =~ tr/C/r/;
}
return $ret;
}
#
# vine_merge -
# @vine: column array containing the expected parent IDs
# @rev: commit ID
# @next_rev: next commit ID in the revision list
# @parents: parent IDs of @rev
#
# Draws the merging vine matrix between a commit K (@rev) and K^ (@parents).
#
sub vine_merge
{
my($vine, $rev, $next_rev, $parents) = @_;
my $orig_vine = -1;
my @slot;
my($ret, $max);
for (my $i = 0; $i <= $#$vine; ++$i) {
if ($vine->[$i] eq $rev) {
$orig_vine = $i;
last;
}
}
if ($orig_vine == -1) {
die "vine_commit() did not add this vine.";
}
if (scalar(@$parents) <= 1) {
#
# A single parent does not need a visual. Update and return.
#
$vine->[$orig_vine] = $parents->[0];
&remove_trailing_blanks($vine);
return;
}
#
# Put previously seen branches in the vine subcolumns
# Need to keep at least one parent for the slot algorithm below.
#
for (my $j = 0; $j <= $#$parents && $#$parents > 0; ++$j) {
for (my $idx = 0; $idx <= $#$vine; ++$idx) {
if ($vine->[$idx] ne $parents->[$j] ||
!grep { my $z = $vine->[$idx]; /^\Q$z\E$/ }
@$next_rev) {
next;
}
if ($idx == $orig_vine) {
die "Should not really happen";
}
if ($idx < $orig_vine) {
my $p = $idx + 1;
if (defined($vine->[$p])) {
$p = $idx - 1;
}
if (defined($vine->[$p])) {
last;
}
$vine->[$p] = $parents->[$j];
str_expand(\$ret, $p + 1);
substr($ret, $p, 1) = "s";
} else {
my $p = $idx - 1;
if (defined($vine->[$p]) || $p < 0) {
$p = $idx + 1;
}
if (defined($vine->[$p])) {
last;
}
$vine->[$p] = $parents->[$j];
str_expand(\$ret, $p + 1);
substr($ret, $p, 1) = "s";
}
splice(@$parents, $j, 1);
--$j; # outer loop
last; # inner loop
}
}
#
# Find some good spots to split out into and record columns
# that will be used soon in the @slot list.
#
push(@slot, $orig_vine);
my $parent = 0;
for (my $seeker = 2; $parent < $#$parents &&
$seeker < 2 + $#$vine; ++$seeker)
{
my $idx = ($seeker % 2 == 0) ? -1 : 1;
$idx *= int($seeker / 2);
$idx *= 2;
$idx += $orig_vine;
if ($idx >= 0 && $idx <= $#$vine && !defined($vine->[$idx])) {
push(@slot, $idx);
$vine->[$idx] = "0" x 40;
++$parent;
}
}
for (my $idx = $orig_vine + 2; $parent < $#$parents; $idx += 2) {
if (!defined($vine->[$idx])) {
push(@slot, $idx);
++$parent;
}
}
if (scalar(@slot) != scalar(@$parents)) {
die "Serious internal problem";
}
@slot = sort { $a <=> $b } @slot;
$max = scalar(@$vine) + 2 * scalar(@slot);
for (my $i = 0; $i < $max; ++$i) {
str_expand(\$ret, $i + 1);
if ($#slot >= 0 && $i == $slot[0]) {
shift @slot;
$vine->[$i] = shift @$parents;
substr($ret, $i, 1) = ($i == $orig_vine) ? "S" : "s";
} elsif (substr($ret, $i, 1) eq "s") {
; # keep existing fanouts
} elsif (defined($vine->[$i])) {
substr($ret, $i, 1) = "I";
} else {
substr($ret, $i, 1) = " ";
}
}
print &vis_post(&vis_fan($ret, "merge")), "\n";
}
#
# vis_* - transform control string into usable graphic
#
# To cut down on code, the three vine_* functions produce only a dumb,
# but already unambiguous, control string which needs some processing
# before it is ready for public display.
#
sub vis_commit
{
my $s = shift @_;
my $f = shift @_;
$s =~ s/ +$//gs;
if (defined $f) {
$s .= $f;
}
return $s;
}
sub vis_fan
{
my $s = shift @_;
my $b = shift(@_) eq "branch";
$s =~ s{s.*s}{
$_ = $&;
$_ =~ tr/ I/DO/;
$_;
}ei;
# Transform an ODODO.. sequence into a contiguous overpass.
$s =~ s{O[DO]+O}{"O" x length($&)}eg;
# Do left/right edge transformation
$s =~ s{(s.*)S(.*s)}{&vis_fan3($1, $2)}es ||
$s =~ s{(s.*)S}{&vis_fan2L($1)."B"}es ||
$s =~ s{S(.*s)}{"A".&vis_fan2R($1)}es ||
die "Should not come here";
if ($b) {
$s =~ tr/efg/xyz/;
}
return $s;
}
sub vis_fan2L
{
my $l = shift @_;
$l =~ s/^s/e/;
$l =~ s/s/f/g;
return $l;
}
sub vis_fan2R
{
my $r = shift @_;
$r =~ s/s$/g/;
$r =~ s/s/f/g;
return $r;
}
sub vis_fan3
{
my($l, $r) = @_;
$l =~ s/^s/e/;
$l =~ s/s/f/g;
$r =~ s/s$/g/;
$r =~ s/s/f/g;
return "${l}K$r";
}
sub vis_xfrm
{
# A: branch to right
# B: branch to right
# C: commit
# D:
# e: merge visual left (╔)
# f: merge visual center (╦)
# g: merge visual right (╗)
# I: straight line (║)
# K: branch visual split (╬)
# m: single line (─)
# O: overpass (≡)
# r: root (╙)
# t: tip (╓)
# x: branch visual left (╚)
# y: branch visual center (╩)
# z: branch visual right (╝)
# *: filler
my $s = shift @_;
my $spc = shift @_;
if ($spc) {
$s =~ s{[Ctr].*}{
$_ = $&;
$_ =~ s{ }{\*}g;
$_;
}esg;
}
if ($Reverse_order) {
$s =~ tr/efg.rt.xyz/xyz.tr.efg/;
}
if ($Style == 1) {
$s =~ tr/ABCD.efg.IKO.mrt.xyz/├┤├─.┌┬┐.│┼≡.─└┌.└┴┘/;
} elsif ($Style == 2) {
$s =~ tr/ABCD.efg.IKO.mrt.xyz/╠╣╟═.╔╦╗.║╬═.─╙╓.╚╩╝/;
} elsif ($Style == 10) {
$s =~ tr/ABCD.efg.IKO.mrt.xyz/├┤├─.╭┬╮.│┼≡.─└┌.╰┴╯/;
} elsif ($Style == 15) {
$s =~ tr/ABCD.efg.IKO.mrt.xyz/┣┫┣━.┏┳┓.┃╋☰.━┗┏.┗┻┛/;
}
return $s;
}
#
# vis_post - post-process/transform vine graphic
# Apply user-specific style transformation.
#
sub vis_post
{
my $s = shift @_;
my $f = shift @_;
$s = vis_xfrm($s, defined $f);
$f =~ s/^([^\x1b]+)/vis_xfrm($1)/e;
$f =~ s/(\x1b.*?m)([^\x1b]+)/$1.vis_xfrm($2)/eg;
if (defined $f) {
$s =~ s/\*/$f/g;
$s =~ s{\Q$Color{default}\E}{$&.$Color{tree}}egs;
$s .= $f;
}
return $Color{tree}, encode('UTF-8', $s), $Color{default};
}
sub remove_trailing_blanks
{
my $a = shift @_;
while (scalar(@$a) > 0 && !defined($a->[$#$a])) {
pop(@$a);
}
}
sub round_down2
{
my $i = shift @_;
if ($i < 0) {
return $i;
}
return $i & ~1;
}
sub str_expand
{
my $r = shift @_;
my $l = shift @_;
if (length($$r) < $l) {
$$r .= " " x ($l - length($$r));
}
}
package ReverseOutput;
require Tie::Handle;
@ReverseOutput::ISA = qw(Tie::Handle);
sub TIEHANDLE
{
my $class = shift @_;
my $self = {};
open($self->{fh}, ">&STDOUT");
return bless $self, $class;
}
sub PRINT
{
my $self = shift @_;
my $fh = $self->{fh};
$self->{saved} .= join($\, @_);
}
sub UNTIE
{
my $self = shift @_;
my $fh = $self->{fh};
print $fh join($/, reverse split(/\n/s, $self->{saved}));
print $fh "\n";
undef $self->{saved};
}