Debian Codegraph @

scripts/gitdiff

raw
1 #!/usr/bin/env perl
2
3 use Getopt::Long;
4 use strict;
5 use Cwd /realpath/;
6
7 my $verbose = 0;
8
9 sub include_me
10 {
11 my ($path, $includes, $excludes) = @_;
12
13 foreach my $pat (@$excludes) {
14 if ($path =~ /$pat/) {
15 if ($verbose) {
16 print(STDERR "exclude '$path', pattern '$pat'\n");
17 }
18 return 0;
19 }
20 }
21 return 1
22 if 0 == scalar(@$includes);
23
24 foreach my $pat (@$includes) {
25 if ($path =~ /$pat/) {
26 if ($verbose) {
27 print(STDERR "include '$path', pattern '$pat'\n");
28 }
29 return 1;
30 }
31 }
32 if ($verbose > 1) {
33 print(STDERR "exclude '$path': no match\n");
34 }
35 return 0;
36 }
37
38 my @exclude_patterns;
39 my @include_patterns;
40 my $suppress_unchanged;
41 my $prefix = '';
42 my $ignore_whitespace;
43 my $repo = '.'; # cwd
44
45 if (!GetOptions("exclude=s" => \@exclude_patterns,
46 "include=s" => \@include_patterns,
47 'no-unchanged' => \$suppress_unchanged,
48 'prefix=s' => \$prefix,
49 'b|blank' => \$ignore_whitespace,
50 'repo=s' => \$repo,
51 'verbose+' => \$verbose) ||
52 (2 != scalar(@ARGV) &&
53 3 != scalar(@ARGV))
54 ) {
55 print(STDERR
56 "usage: [(--exclude|include) regexp[,regexp]] [--repo repo] [-b] [dir] base_changelist current_changelist\n'exclude' wins if both exclude and include would match.\n"
57 );
58 exit(1);
59 }
60
61 @exclude_patterns = split(',', join(',', @exclude_patterns));
62 @include_patterns = split(',', join(',', @include_patterns));
63 $ignore_whitespace = '-b' if $ignore_whitespace;
64
65 if ($verbose) {
66 print(STDERR join(' ', @ARGV));
67 if (scalar(@exclude_patterns)) {
68 print(STDERR " --exclude " . join(" --exclude ", @exclude_patterns));
69 }
70 if (scalar(@include_patterns)) {
71 print(STDERR " --include " . join(" --include ", @include_patterns));
72 }
73 print(STDERR "\n");
74 }
75
76 $prefix .= '/' if $prefix;
77 if (3 == scalar(@ARGV)) {
78 my $dir = shift @ARGV;
79 push(@include_patterns, "$dir");
80 }
81 my $base_sha = shift @ARGV;
82 my $current_sha = shift @ARGV;
83
84 $repo = Cwd::realpath($repo);
85
86 my $cmd = "cd $repo ; git diff $ignore_whitespace $base_sha $current_sha";
87
88 open(HANDLE, "-|", $cmd) or
89 die("failed to exec git diff: $!");
90
91 my $includeCurrentFile;
92 my %allFiles;
93
94 while (<HANDLE>) {
95 chomp($_);
96 s/\r//g;
97 my $line = $_;
98
99 if ($line =~ /(^diff|\+\+\+|---) /) {
100 # remove the a/b leader from the
101 $line =~ s# [ab]/# $prefix#g;
102 }
103
104 if ($line =~ /^diff --git (\S+) (\S+)/) {
105 # git diff header
106 my $fileA = $1;
107 my $fileB = $2;
108 $includeCurrentFile =
109 (include_me($fileA, \@include_patterns, \@exclude_patterns) ||
110 include_me($fileB, \@include_patterns, \@exclude_patterns));
111 if ($includeCurrentFile) {
112 $allFiles{$fileB} = $fileA;
113 $line =~ s/($fileA|$fileB)/$1/g;
114 }
115 }
116
117 printf("%s\n", $line)
118 if $includeCurrentFile;
119 }
120 close(HANDLE) or die("failed to close git diff pipe: $!");
121 if (0 != $?) {
122 $? & 0x7F & die("git diff died from signal ", ($? & 0x7F), "\n");
123 die("git diff exited with error ", ($? >> 8), "\n");
124 }
125
126 exit 0 if defined($suppress_unchanged);
127
128 # now find the list of files in the current SHA - we can process that
129 # to find the list of all current files which were unchanged since the
130 # baseline SHA
131 die("failed to exec git ls-tree")
132 unless (
133 open(HANDLE, "-|", "cd $repo ; git ls-tree -r --name-only $current_sha"));
134
135 while (<HANDLE>) {
136 chomp($_);
137 s/\r//g;
138 my $filename = $repo . '/' . $_;
139 my $path = $prefix . $filename;
140 if (!exists($allFiles{$path}) &&
141 include_me($path, \@include_patterns, \@exclude_patterns)) {
142 printf("diff --git $prefix$filename $prefix$filename\n");
143 printf("=== $path\n");
144 }
145 }
146 close(HANDLE) or die("failed to close git ls-tree pipe: $!");
147 if (0 != $?) {
148 $? & 0x7F & die("git ls-tree died from signal ", ($? & 0x7F), "\n");
149 die("git ls-tree exited with error ", ($? >> 8), "\n");
150 }
151 exit 0;