-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-diff.pl
executable file
·168 lines (141 loc) · 4.02 KB
/
image-diff.pl
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
#!/usr/bin/perl
use strict;
my ($a, $b) = @ARGV;
die "Usage: image-diff old_revision new_revision" unless $a && $b;
my %ignore_toplevel_dirs = ();
sub git_call {
my $GIT;
my $child_pid = open($GIT, "-|") // die "can't fork: $!";
if (!$child_pid) {
exec(@_);
}
my $data = join("", <$GIT>);
close $GIT or die (($! ? "error closing pipe ($!) for " :
"git returned with status $? for ") .
join(" ", @_));
return $data;
}
sub diff {
# Creates and parses a `git diff-tree $a:$_[0] $b:$_[0]`
my $diff = &git_call("git", "diff-tree", "-z", "$a:$_[0]", "$b:$_[0]");
my @changes;
while ($diff =~ /\G:(\d+) (\d+) (\w+) (\w+) (?|([ADMTUX])()\x00([^\x00]+)()|([CR])(\d*)\x00([^\x00]+)\x00([^\x00]+))\x00/sg) {
push @changes, {
src_mode => $1,
dst_mode => $2,
src_sha1 => $3,
dst_sha1 => $4,
status => $5,
score => $6,
src_filename => $7,
dst_filename => $8,
filename => $7 # For convenience in case of ADMTUX
};
}
return @changes;
}
sub is_tree {
my $object = $_[0];
my $type = &git_call("git", "cat-file", "-t", $object);
chomp $type;
return ($type eq "tree");
}
sub encode {
my $string = $_[0];
$string =~ s/([^\w\/.])/'\x{'.ord($1).'}'/ge;
return $string;
}
sub add {
my ($path, $change) = @_;
my $filename = $change->{'filename'};
if (&is_tree("$b:$path/$filename")) {
print("rput $change->{'dst_sha1'} ".&encode("$path/$filename")."\n");
}
else {
print("put $change->{'dst_sha1'} ".&encode("$path/$filename")."\n");
}
}
sub delete {
my ($path, $change) = @_;
my $filename = $change->{'filename'};
if (&is_tree("$a:$path/$filename")) {
print("rdel $change->{'src_sha1'} ".&encode("$path/$filename")."\n");
}
else {
print("del ".&encode("$path/$filename")."\n");
}
}
sub modify {
my ($path, $change) = @_;
my $filename = $change->{'filename'};
if (&is_tree("$a:$path/$filename")) {
die "Modify must not be called for a directory!";
}
print("put $change->{'dst_sha1'} ".&encode("$path/$filename")."\n");
}
sub changetype {
my ($path, $change) = @_;
my $filename = $change->{'filename'};
# We simply remove the old file and add the new one.
&delete(@_);
&add(@_);
}
sub copy {
my ($path, $change) = @_;
# Note that we could have a score, so this need not be an exact
# copy. Therefore delete and put
&delecte(@_);
&add(@_);
}
sub rename {
# Note that we could have a score, so this need not be an exact
# copy. Therefore delete and put
&delecte(@_);
&add(@_);
}
sub diff_error {
my ($rev, $path, $change) = @_;
# This is a problem severe enough to die, because we are either in
# an unmerged stage or git has a bug.
die "Invalid change encountered: " . join " ", @$change;
}
my %actions = (
A => \&add,
D => \&delete,
M => \&modify,
T => \&changetype,
U => \&diff_error,
X => \&diff_error,
C => \©,
R => \&rename,
);
sub changed_toplevel {
# Determines which top level files have changed
# (Maybe doesn't do what it should.)
my @changes = &diff("");
@changes = grep { $_->{'status'} =~ /A|M|T|C|R/ } @changes;
@changes = grep { &is_tree("$b:" . ($_->{"dst_filename"} || $_->{"src_filename"})) } @changes;
return map { $_->{"dst_filename"} || $_->{"src_filename"} } @changes;
}
sub process {
my $path = $_[0];
my @changes = &diff($path);
for (@changes) {
if ($_->{'status'} eq 'M' and &is_tree("$a:$path/$_->{'filename'}")) {
&process("$path/$_->{'filename'}");
}
else {
&{$actions{$_->{'status'}}}($path, $_);
}
}
}
my @toplevel = grep { !$ignore_toplevel_dirs{$_} } &changed_toplevel;
print "$a $b\n\n";
for (@toplevel) {
print(&encode($_)."\n");
}
for (@toplevel) {
print "\n";
&process($_);
}
print("\n");