-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcg-Xfollowrenames
More file actions
executable file
·261 lines (220 loc) · 6.09 KB
/
Copy pathcg-Xfollowrenames
File metadata and controls
executable file
·261 lines (220 loc) · 6.09 KB
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
#!/usr/bin/env perl
#
# git-rev-list | git-diff-tree --stdin following renames
# Copyright (c) Petr Baudis, 2006
# Uses bits of git-annotate.perl by Ryan Anderson.
#
# This script will efficiently show output as of the
#
# git-rev-list --remove-empty ARGS -- FILE... |
# git-diff-tree -M -r -m --stdin --pretty=raw ARGS
#
# pipeline, except that it follows renames of individual files listed
# in the FILE... set.
#
# Usage:
#
# cg-Xfollowrenames revlistargs -- difftreeargs -- revs -- files
# Testsuite: TODO
# TODO: BROKEN - if a file is removed (that is, added) on one branch,
# we will stop watching for the renames on all the other branches. We need
# to separate the heads and pipes.
# TODO: Does not work on multiple files properly yet - most probably
# (I didn't test it!). We want git-rev-list to stop traversing the history
# when _any_ file disappears while now it probably stops traversing when
# _all_ files disappear.
use warnings;
use strict;
$| = 1;
our (@revlist_args, @difftree_args, @revs, @files);
{ # Load arguments
my @argp = (\@revlist_args, \@difftree_args, \@revs, \@files);
my $argi = 0;
for my $arg (@ARGV) {
if ($arg eq '--' and $argi < $#argp) {
$argi++;
next;
}
push(@{$argp[$argi]}, $arg);
}
}
# The heads we watch (sorted by commit time)
our @heads;
# Each head is: {
# # Persistent for the whole line of development:
# pipe => $pipe,
# files => \@files, # to watch for
#
# id => $sha1, # useful actually only for debugging
# time => $timestamp,
# str => $prettyoutput,
# parents => \@sha1s,
#
# # When the commit is processed, spawn these extra heads:
# recurse => {$sha1id => \@files, ...},
# }
# To avoid printing duplicate commits
# FIXME: Currently, we will not handle merge commits properly since
# we hit them multiple times.
our %commits;
sub open_pipe($@) {
my ($stdin, @execlist) = @_;
my $pid = open my $kid, "-|";
defined $pid or die "Cannot fork: $!";
unless ($pid) {
if (defined $stdin) {
open STDIN, "<&", $stdin or die "Cannot dup(): $!";
}
exec @execlist;
die "Cannot exec @execlist: $!";
}
return $kid;
}
sub revlist($@) {
my ($rev, @files) = @_;
open_pipe(undef, "git-rev-list", "--remove-empty",
@revlist_args, $rev, "--", @files)
or die "Failed to exec git-rev-list: $!";
}
sub difftree($) {
my ($revlist) = @_;
open_pipe($revlist, "git-diff-tree", "-r", "-m", "--stdin", "-M",
"--pretty=raw", @difftree_args)
or die "Failed to exec git-diff-tree: $!";
}
sub revdiffpipe($@) {
my ($rev, @files) = @_;
my $pipe = difftree(revlist($rev, @files));
}
sub read_commit($$) {
my ($head, $tolerant) = @_;
my $pipe = $head->{'pipe'};
my $against;
my @oldset = @{$head->{'files'}};
my @newset;
my $rename;
# Load header
while (my $line = <$pipe>) {
$head->{'str'} .= $line;
chomp $line;
$line eq '' and goto header_loaded;
if ($line =~ /^diff-tree (\S+) \(from (root|\S+)\)/) {
$head->{'id'} = $1;
if (not $tolerant and $commits{$1}++) {
close $pipe;
return undef;
}
# The 'root' case is harmless since there'll be no renames.
$against = $2;
} elsif ($line =~ /^parent (\S+)/) {
push (@{$head->{'parents'}}, $1);
} elsif ($line =~ /^committer .*?> (\d+)/) {
$head->{'time'} = $1;
}
}
return undef;
header_loaded:
# Load message
while (my $line = <$pipe>) {
$head->{'str'} .= $line;
chomp $line;
$line eq '' and goto message_loaded;
}
return undef;
message_loaded:
# Load delta
# Note that we must interpret the patch we are seeing _reverse_.
while (my $line = <$pipe>) {
$head->{'str'} .= $line;
chomp $line;
$line eq '' and goto delta_loaded;
$line =~ /^:/ or return undef;
my ($info, $newfile, $oldfile) = split("\t", $line);
if ($info =~ /[RC]\d*$/) {
# Behold, a rename!
# (Or a copy, it's all the same for us.)
my $i;
for ($i = 0; $i <= $#oldset; $i++) {
$oldfile eq $oldset[$i] or next;
$rename = 1;
splice(@oldset, $i, 1);
push(@newset, $newfile);
last;
}
# In case of multiple candidates, follow
# all of them:
# (TODO: This might be a policy decision
# best left on the user.)
if ($i > $#oldset and grep { $oldfile eq $_ } @newset) {
$rename = 1;
push(@newset, $newfile);
}
} elsif ($info =~ /A$/) {
# Not weeding out deleted files (the patch is reversed
# so they appear as added to us) might cause bizarre
# results when following multiple files since
# git-rev-list weeds them out too (probably?).
#print STDERR "grepping - @oldset, @{$head->{files}} > MINUS $newfile <\n";
@oldset = grep { $newfile ne $_ } @oldset;
@{$head->{'files'}} = grep { $newfile ne $_ } @{$head->{'files'}};
#print STDERR "post-grepping - @oldset, @{$head->{files}} <\n";
}
}
$head->{'str'} .= "\n";
delta_loaded:
if ($rename) {
$head->{'recurse'}->{$against} = [@newset, @oldset];
}
return 1;
}
sub load_commit($) {
my ($head) = @_;
$head->{'time'} = undef;
$head->{'str'} = '';
$head->{'parents'} = ();
read_commit($head, 0) or return undef;
# In case there was a merge, the commit will be multiple times
# here, each time with a different delta section. Read them all.
for (1 .. $#{$head->{'parents'}}) { # stupid vim syntax highlighting
read_commit($head, 1) or return undef;
}
# Cut the last \n. We don't want it for the last commit.
substr($head->{'str'}, -1, 1, '');
return 1;
}
# Add head at the proper position
sub add_head($) {
my ($head) = @_;
my $i;
for ($i = 0; $i <= $#heads; $i++) {
last if ($head->{'time'} > $heads[$i]->{'time'})
}
splice(@heads, $i, 0, $head);
}
# Create new head
sub init_head($@) {
my ($rev, @files) = @_;
my $head = { files => \@files, 'pipe' => revdiffpipe($rev, @files) };
load_commit($head) or return;
add_head($head);
}
{ # Seed the heads list
for my $rev (@revs) {
init_head($rev, @files);
}
}
# Process the heads
{
my $first = 1;
while (@heads) {
my $head = shift(@heads);
print "\n" unless $first; $first = 0;
print $head->{'str'};
foreach my $parent (keys %{$head->{'recurse'}}) {
init_head($parent, @{$head->{'recurse'}->{$parent}});
}
$head->{'recurse'} = undef;
load_commit($head) or next;
add_head($head);
}
}