| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Copyright (c) MediaTek USA Inc., 2020 |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
# your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, see |
| 17 |
# <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
# |
| 20 |
# p4udiff |
| 21 |
# |
| 22 |
# This script extracts a unified-diff between two Perforce changelists. |
| 23 |
|
| 24 |
use strict; |
| 25 |
use DateTime; |
| 26 |
use Getopt::Long; |
| 27 |
|
| 28 |
package P4File; |
| 29 |
|
| 30 |
sub new |
| 31 |
{ |
| 32 |
my $class = shift; |
| 33 |
my $description = shift; |
| 34 |
if ($description =~ |
| 35 |
m/([^#]+)#([0-9]+)\s+-\s+(\S+)\s+change\s+([0-9]+)\s+\(([^)]+)\).*$/) { |
| 36 |
my $self = {}; |
| 37 |
bless $self, $class; |
| 38 |
|
| 39 |
$self->{path} = $1; |
| 40 |
$self->{rev} = $2; |
| 41 |
$self->{action} = $3; |
| 42 |
$self->{changelist} = $4; |
| 43 |
$self->{type} = $5; |
| 44 |
|
| 45 |
return $self; |
| 46 |
} |
| 47 |
return undef; |
| 48 |
} |
| 49 |
|
| 50 |
sub path |
| 51 |
{ |
| 52 |
my $self = shift; |
| 53 |
return $self->{path}; |
| 54 |
} |
| 55 |
|
| 56 |
sub rev |
| 57 |
{ |
| 58 |
my $self = shift; |
| 59 |
return $self->{rev}; |
| 60 |
} |
| 61 |
|
| 62 |
sub action |
| 63 |
{ |
| 64 |
my $self = shift; |
| 65 |
return $self->{action}; |
| 66 |
} |
| 67 |
|
| 68 |
sub changelist |
| 69 |
{ |
| 70 |
my $self = shift; |
| 71 |
return $self->{changelist}; |
| 72 |
} |
| 73 |
|
| 74 |
sub type |
| 75 |
{ |
| 76 |
my $self = shift; |
| 77 |
return $self->{type}; |
| 78 |
} |
| 79 |
|
| 80 |
package P4FileList; |
| 81 |
|
| 82 |
sub new |
| 83 |
{ |
| 84 |
my ($class, $include, $exclude) = @_; |
| 85 |
my $self = [{}, $include, $exclude]; |
| 86 |
bless $self, $class; |
| 87 |
|
| 88 |
return $self; |
| 89 |
} |
| 90 |
|
| 91 |
sub include_me |
| 92 |
{ |
| 93 |
my ($path, $include, $exclude) = @_; |
| 94 |
|
| 95 |
if (defined($exclude)) { |
| 96 |
foreach my $pat (@$exclude) { |
| 97 |
return 0 |
| 98 |
if $path =~ /$pat/; |
| 99 |
} |
| 100 |
} |
| 101 |
return 1 |
| 102 |
if (!defined($include) || 0 == scalar(@$include)); |
| 103 |
|
| 104 |
foreach my $pat (@$include) { |
| 105 |
return 1 |
| 106 |
if ($path =~ /$pat/); |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
sub append |
| 112 |
{ |
| 113 |
my $self = shift; |
| 114 |
my $entry = shift; |
| 115 |
|
| 116 |
return $self if (!defined($entry)); |
| 117 |
my ($hash, $include, $exclude) = @$self; |
| 118 |
my $key = $entry->path(); |
| 119 |
if (defined($hash->{$key})) { |
| 120 |
warn("WARNING: skipping duplicated path $key\n"); |
| 121 |
return $self; |
| 122 |
} |
| 123 |
$hash->{$key} = $entry |
| 124 |
if include_me($key, $include, $exclude); |
| 125 |
return $self; |
| 126 |
} |
| 127 |
|
| 128 |
sub files |
| 129 |
{ |
| 130 |
my $self = shift; |
| 131 |
return sort keys %{$self->[0]}; |
| 132 |
} |
| 133 |
|
| 134 |
sub get |
| 135 |
{ |
| 136 |
my $self = shift; |
| 137 |
my $key = shift; |
| 138 |
|
| 139 |
if (!defined($self->[0]->{$key})) { |
| 140 |
return undef; |
| 141 |
} |
| 142 |
return $self->[0]->{$key}; |
| 143 |
} |
| 144 |
|
| 145 |
sub remove |
| 146 |
{ |
| 147 |
my $self = shift; |
| 148 |
my $key = shift; |
| 149 |
delete $self->[0]->{$key}; |
| 150 |
return $self; |
| 151 |
} |
| 152 |
|
| 153 |
package main; |
| 154 |
|
| 155 |
my @exclude_patterns; |
| 156 |
my @include_patterns; |
| 157 |
my $suppress_unchanged; |
| 158 |
my $ignore_whitespace; |
| 159 |
|
| 160 |
if (!GetOptions("exclude=s" => \@exclude_patterns, |
| 161 |
"include=s" => \@include_patterns, |
| 162 |
'b|blank' => \$ignore_whitespace, |
| 163 |
'no-unchanged' => \$suppress_unchanged) || |
| 164 |
3 != scalar(@ARGV) |
| 165 |
) { |
| 166 |
print(STDERR<<EOF |
| 167 |
usage: [(--exclude|include) regexp[,regexp]] [-b] [--no-unchanged] sandbox_directory base_changelist current_changelist |
| 168 |
'exclude' wins if both exclude and include would match. |
| 169 |
'current_changelist' may be a changelist number or "sandbox". |
| 170 |
"sandbox" indicates that there may be changes in 'sandbox_directory' which have not ben committed to perforce yet. |
| 171 |
'-b' ignores whitespace changes |
| 172 |
EOF |
| 173 |
); |
| 174 |
exit(1); |
| 175 |
} |
| 176 |
@exclude_patterns = split(',', join(',', @exclude_patterns)); |
| 177 |
@include_patterns = split(',', join(',', @include_patterns)); |
| 178 |
|
| 179 |
my $top_directory = shift @ARGV; |
| 180 |
my $base_changelist = shift @ARGV; |
| 181 |
my $curr_changelist = shift @ARGV; |
| 182 |
my $base_files = P4FileList->new(\@include_patterns, \@exclude_patterns); |
| 183 |
my $curr_files = P4FileList->new(\@include_patterns, \@exclude_patterns); |
| 184 |
|
| 185 |
# need "/..." on the pathname if this is a directory. |
| 186 |
# - depot_path may not be the same as the client workspace path. |
| 187 |
# - If the name is local, then append if it is a directory - otherwise, ask P4 |
| 188 |
# Don't go to P4 unless necessary - as the interaction is pretty slow. |
| 189 |
|
| 190 |
# Ask the Perforce server to lookup the path and append a directory recursion |
| 191 |
# pattern if the depot_path is not a file_type. |
| 192 |
my $p4_path = $top_directory; |
| 193 |
if (-e $p4_path) { |
| 194 |
$p4_path .= "/..." |
| 195 |
if (-d $p4_path); |
| 196 |
} else { |
| 197 |
system("p4 fstat $top_directory|grep depotFile >/dev/null 2>&1"); |
| 198 |
if (($? >> 8) != 0) { |
| 199 |
$p4_path .= "/..."; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (open(HANDLE, "-|", "p4 files ${p4_path}\@$base_changelist")) { |
| 204 |
DIFF_FILE: |
| 205 |
while (my $line = <HANDLE>) { |
| 206 |
chomp $line; |
| 207 |
s/\015$//; |
| 208 |
|
| 209 |
$base_files->append(P4File->new($line)); |
| 210 |
} |
| 211 |
close(HANDLE) or die("unable to close p4 files (baseline) pipe: $!\n"); |
| 212 |
if (0 != $?) { |
| 213 |
$? & 0x7F & |
| 214 |
die("p4 files (baseline) died from signal ", ($? & 0x7F), "\n"); |
| 215 |
die("p4 files (baseline) exited with error ", ($? >> 8), "\n"); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
my $curr = $curr_changelist eq "sandbox" ? "" : "\@$curr_changelist"; |
| 220 |
if (open(HANDLE, "-|", "p4 files ${p4_path}$curr")) { |
| 221 |
DIFF_FILE: |
| 222 |
while (my $line = <HANDLE>) { |
| 223 |
chomp $line; |
| 224 |
s/\015$//; |
| 225 |
$curr_files->append(P4File->new($line)); |
| 226 |
} |
| 227 |
close(HANDLE) or die("unable to close p4 files (current) pipe: $!\n"); |
| 228 |
if (0 != $?) { |
| 229 |
$? & 0x7F & |
| 230 |
die("p4 files (current) died from signal ", ($? & 0x7F), "\n"); |
| 231 |
die("p4 files (current) exited with error ", ($? >> 8), "\n"); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
my @unchanged; |
| 236 |
|
| 237 |
# prune files at the same rev; no difference to report |
| 238 |
foreach my $f ($base_files->files()) { |
| 239 |
my $b = $base_files->get($f); |
| 240 |
my $c = $curr_files->get($f); |
| 241 |
|
| 242 |
if (defined($c) && |
| 243 |
$b->rev() eq $c->rev() && |
| 244 |
$b->action() eq $c->action() && |
| 245 |
$b->changelist() eq $c->changelist()) { |
| 246 |
$curr_files->remove($f); |
| 247 |
$base_files->remove($f); |
| 248 |
push(@unchanged, $c) |
| 249 |
unless ($c->action() eq 'delete' || |
| 250 |
$c->type() eq 'binary'); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
# prune files already deleted in base list |
| 255 |
foreach my $f (grep { |
| 256 |
$base_files->get($_)->action() eq "delete" || |
| 257 |
$base_files->get($_)->action() eq "move/delete" |
| 258 |
} $base_files->files() |
| 259 |
) { |
| 260 |
my $b = $base_files->get($f); |
| 261 |
my $c = $curr_files->get($f); |
| 262 |
|
| 263 |
if (defined($c) && $b->action() eq $c->action()) { |
| 264 |
# deleted again in curr with a different rev |
| 265 |
$curr_files->remove($f); |
| 266 |
} |
| 267 |
|
| 268 |
$base_files->remove($f); |
| 269 |
} |
| 270 |
|
| 271 |
# prune files deleted in curr list |
| 272 |
foreach my $f (grep { |
| 273 |
$curr_files->get($_)->action() eq "delete" || |
| 274 |
$curr_files->get($_)->action() eq "move/delete" |
| 275 |
} $curr_files->files() |
| 276 |
) { |
| 277 |
my $c = $curr_files->get($f); |
| 278 |
|
| 279 |
$curr_files->remove($f); |
| 280 |
} |
| 281 |
|
| 282 |
my %union; |
| 283 |
|
| 284 |
foreach my $k ($base_files->files()) { |
| 285 |
#my $b = $base_files->get($k); |
| 286 |
#printf("base: %s#%d %s change %d\n", $k, $b->rev(), $b->action(), $b->changelist()); |
| 287 |
$union{$k} = 1; |
| 288 |
} |
| 289 |
foreach my $k ($curr_files->files()) { |
| 290 |
#my $b = $curr_files->get($k); |
| 291 |
#printf("curr: %s#%d %s change %d\n", $k, $b->rev(), $b->action(), $b->changelist()); |
| 292 |
$union{$k} = 1; |
| 293 |
} |
| 294 |
#exit; |
| 295 |
|
| 296 |
#my $workspace = `p4 -F \%clientRoot\% -ztag info`; |
| 297 |
my $where = |
| 298 |
`p4 where $p4_path`; # need the "..." in the path or p4 gets confused |
| 299 |
$where =~ s/\/\.\.\.//g; |
| 300 |
my ($depot_path, $workspace_path, $sandbox_path) = split(' ', $where); |
| 301 |
|
| 302 |
sub reloc |
| 303 |
{ |
| 304 |
my $path = shift; |
| 305 |
$path =~ s/$depot_path/$sandbox_path/; |
| 306 |
return $path; |
| 307 |
} |
| 308 |
|
| 309 |
foreach my $f (sort keys %union) { |
| 310 |
my $b = $base_files->get($f); |
| 311 |
my $c = $curr_files->get($f); |
| 312 |
|
| 313 |
if (defined($b) && !defined($c)) { |
| 314 |
# deleted |
| 315 |
next if ($b->type() eq "binary"); |
| 316 |
printf("p4 diff $f#%d $f\n", $b->rev()); |
| 317 |
printf("index %d..0\n", $base_changelist); |
| 318 |
printf("--- %s\n", reloc($f)); |
| 319 |
printf("+++ /dev/null\n"); |
| 320 |
# p4 print -q $b->path() . '#' . $b->rev() |sed -e 's/^/-/' |
| 321 |
my @lines; |
| 322 |
open(HANDLE, "-|", "p4", "print", "-q", $b->path() . '#' . $b->rev()) |
| 323 |
or |
| 324 |
die("p4 print failed: $!\n"); |
| 325 |
while (my $line = <HANDLE>) { |
| 326 |
chomp $line; |
| 327 |
$line =~ s/^/-/; |
| 328 |
push @lines, $line; |
| 329 |
} |
| 330 |
close(HANDLE) or die("unable to close p4 print pipe: $!\n"); |
| 331 |
if (0 != $?) { |
| 332 |
$? & 0x7F & die("p4 print died from signal ", ($? & 0x7F), "\n"); |
| 333 |
die("p4 print exited with error ", ($? >> 8), "\n"); |
| 334 |
} |
| 335 |
printf("@@ 1,%d 0,0 @@\n", scalar(@lines)); |
| 336 |
printf("%s\n", join("\n", @lines)); |
| 337 |
} elsif (!defined($b) && defined($c)) { |
| 338 |
# added |
| 339 |
next if ($c->type() eq "binary"); |
| 340 |
printf("p4 diff $f $f#%d\n", $c->rev()); |
| 341 |
printf("new file mode\n"); |
| 342 |
printf("index 0..%d\n", $curr_changelist); |
| 343 |
printf("--- /dev/null\n"); |
| 344 |
printf("+++ %s\n", reloc($f)); |
| 345 |
my @lines; |
| 346 |
open(HANDLE, "-|", "p4", "print", "-q", $c->path() . '#' . $c->rev()) |
| 347 |
or |
| 348 |
die("p4 print failed: $!\n"); |
| 349 |
|
| 350 |
while (my $line = <HANDLE>) { |
| 351 |
chomp $line; |
| 352 |
$line =~ s/^/+/; |
| 353 |
push @lines, $line; |
| 354 |
} |
| 355 |
close(HANDLE) or die("unable to close p4 print pipe: $!\n"); |
| 356 |
if (0 != $?) { |
| 357 |
$? & 0x7F & die("p4 print died from signal ", ($? & 0x7F), "\n"); |
| 358 |
die("p4 print exited with error ", ($? >> 8), "\n"); |
| 359 |
} |
| 360 |
printf("@@ 0,0 1,%d @@\n", scalar(@lines)); |
| 361 |
printf("%s\n", join("\n", @lines)); |
| 362 |
} elsif (defined($b) && defined($c)) { |
| 363 |
# check diff |
| 364 |
next if ($b->type() eq "binary" || $c->type() eq "binary"); |
| 365 |
# "p4 diff $ignore_whitespace -du ". $c->path() . '#' . $c->rev() . " " . $b->path() . '#' . $b->rev() |
| 366 |
printf("p4 diff $f#%d $f#%d\n", $b->rev(), $c->rev()); |
| 367 |
printf("index %d..%d\n", $base_changelist, $curr_changelist); |
| 368 |
my @lines; |
| 369 |
my @cmd = ("p4", "diff", "-du", |
| 370 |
$b->path() . '#' . $b->rev(), |
| 371 |
$c->path() . '#' . $c->rev()); |
| 372 |
splice(@cmd, 2, 0, '-db') if $ignore_whitespace; |
| 373 |
open(HANDLE, "-|", @cmd) or |
| 374 |
die("p4 diff failed: $!\n"); |
| 375 |
while (my $line = <HANDLE>) { |
| 376 |
chomp $line; |
| 377 |
if ($line =~ m/^(---|\+\+\+)/) { |
| 378 |
$line =~ s/^(---\s+\S+).*$/$1/; |
| 379 |
$line =~ s/^(\+\+\+\s+\S+).*$/$1/; |
| 380 |
$line = reloc($line); |
| 381 |
} |
| 382 |
printf("%s\n", $line); |
| 383 |
} |
| 384 |
close(HANDLE) or die("unable to close p4 diff pipe: $!\n"); |
| 385 |
if (0 != $?) { |
| 386 |
$? & 0x7F & die("p4 diff died from signal ", ($? & 0x7F), "\n"); |
| 387 |
die("p4 diff exited with error ", ($? >> 8), "\n"); |
| 388 |
} |
| 389 |
} else { |
| 390 |
warn("WARNING: not in base or current for $f\n"); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
exit 0 if defined($suppress_unchanged); |
| 395 |
|
| 396 |
foreach my $f (@unchanged) { |
| 397 |
my $name = $f->path(); |
| 398 |
next |
| 399 |
unless P4FileList::include_me($name, \@include_patterns, |
| 400 |
\@exclude_patterns); |
| 401 |
printf("p4 diff $name#%d $name\n", $f->rev()); |
| 402 |
printf("=== %s\n", reloc($name)); |
| 403 |
} |