File: gitdiff

package info (click to toggle)
lcov 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,464 kB
  • sloc: perl: 27,911; sh: 7,320; xml: 6,982; python: 1,152; makefile: 597; cpp: 520; ansic: 176
file content (151 lines) | stat: -rwxr-xr-x 4,163 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

use Getopt::Long;
use strict;
use Cwd /realpath/;

my $verbose = 0;

sub include_me
{
    my ($path, $includes, $excludes) = @_;

    foreach my $pat (@$excludes) {
        if ($path =~ /$pat/) {
            if ($verbose) {
                print(STDERR "exclude '$path', pattern '$pat'\n");
            }
            return 0;
        }
    }
    return 1
        if 0 == scalar(@$includes);

    foreach my $pat (@$includes) {
        if ($path =~ /$pat/) {
            if ($verbose) {
                print(STDERR "include '$path', pattern '$pat'\n");
            }
            return 1;
        }
    }
    if ($verbose > 1) {
        print(STDERR "exclude '$path': no match\n");
    }
    return 0;
}

my @exclude_patterns;
my @include_patterns;
my $suppress_unchanged;
my $prefix = '';
my $ignore_whitespace;
my $repo = '.';    # cwd

if (!GetOptions("exclude=s"    => \@exclude_patterns,
                "include=s"    => \@include_patterns,
                'no-unchanged' => \$suppress_unchanged,
                'prefix=s'     => \$prefix,
                'b|blank'      => \$ignore_whitespace,
                'repo=s'       => \$repo,
                'verbose+'     => \$verbose) ||
    (2 != scalar(@ARGV) &&
        3 != scalar(@ARGV))
) {
    print(STDERR
            "usage: [(--exclude|include) regexp[,regexp]] [--repo repo] [-b] [dir] base_changelist current_changelist\n'exclude' wins if both exclude and include would match.\n"
    );
    exit(1);
}

@exclude_patterns  = split(',', join(',', @exclude_patterns));
@include_patterns  = split(',', join(',', @include_patterns));
$ignore_whitespace = '-b' if $ignore_whitespace;

if ($verbose) {
    print(STDERR join(' ', @ARGV));
    if (scalar(@exclude_patterns)) {
        print(STDERR " --exclude " . join(" --exclude ", @exclude_patterns));
    }
    if (scalar(@include_patterns)) {
        print(STDERR " --include " . join(" --include ", @include_patterns));
    }
    print(STDERR "\n");
}

$prefix .= '/' if $prefix;
if (3 == scalar(@ARGV)) {
    my $dir = shift @ARGV;
    push(@include_patterns, "$dir");
}
my $base_sha    = shift @ARGV;
my $current_sha = shift @ARGV;

$repo = Cwd::realpath($repo);

my $cmd = "cd $repo ; git diff $ignore_whitespace $base_sha $current_sha";

open(HANDLE, "-|", $cmd) or
    die("failed to exec git diff: $!");

my $includeCurrentFile;
my %allFiles;

while (<HANDLE>) {
    chomp($_);
    s/\r//g;
    my $line = $_;

    if ($line =~ /(^diff|\+\+\+|---) /) {
        # remove the a/b leader from the
        $line =~ s# [ab]/# $prefix#g;
    }

    if ($line =~ /^diff --git (\S+) (\S+)/) {
        # git diff header
        my $fileA = $1;
        my $fileB = $2;
        $includeCurrentFile =
            (include_me($fileA, \@include_patterns, \@exclude_patterns) ||
             include_me($fileB, \@include_patterns, \@exclude_patterns));
        if ($includeCurrentFile) {
            $allFiles{$fileB} = $fileA;
            $line =~ s/($fileA|$fileB)/$1/g;
        }
    }

    printf("%s\n", $line)
        if $includeCurrentFile;
}
close(HANDLE) or die("failed to close git diff pipe: $!");
if (0 != $?) {
    $? & 0x7F & die("git diff died from signal ", ($? & 0x7F), "\n");
    die("git diff exited with error ", ($? >> 8), "\n");
}

exit 0 if defined($suppress_unchanged);

# now find the list of files in the current SHA - we can process that
#  to find the list of all current files which were unchanged since the
#  baseline SHA
die("failed to exec git ls-tree")
    unless (
      open(HANDLE, "-|", "cd $repo ; git ls-tree -r --name-only $current_sha"));

while (<HANDLE>) {
    chomp($_);
    s/\r//g;
    my $filename = $repo . '/' . $_;
    my $path     = $prefix . $filename;
    if (!exists($allFiles{$path}) &&
        include_me($path, \@include_patterns, \@exclude_patterns)) {
        printf("diff --git $prefix$filename $prefix$filename\n");
        printf("=== $path\n");
    }
}
close(HANDLE) or die("failed to close git ls-tree pipe: $!");
if (0 != $?) {
    $? & 0x7F & die("git ls-tree died from signal ", ($? & 0x7F), "\n");
    die("git ls-tree exited with error ", ($? >> 8), "\n");
}
exit 0;