2000
return wantarray ? %res : \%res;
2001
}
2002
2003
+# wrapper: return parsed line of git-diff-tree "raw" output
2004
+# (the argument might be raw line, or parsed info)
2005
+sub parsed_difftree_line {
2006
+ my $line_or_ref = shift;
2007
+
2008
+ if (ref($line_or_ref) eq "HASH") {
2009
+ # pre-parsed (or generated by hand)
2010
+ return $line_or_ref;
2011
+ } else {
2012
+ return parse_difftree_raw_line($line_or_ref);
2013
+ }
2014
+}
2015
+
2016
# parse line of git-ls-tree output
2017
sub parse_ls_tree_line ($;%) {
2018
my $line = shift;
2056
}
2057
}
2058
} else {
2059
+ # ordinary (not combined) diff
2060
$from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2061
if ($diffinfo->{'status'} ne "A") { # not new (added) file
2062
$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2780
## ......................................................................
2781
## functions printing large fragments of HTML
2782
2783
+# get pre-image filenames for merge (combined) diff
2784
sub fill_from_file_info {
2785
my ($diff, @parents) = @_;
2786
2797
return $diff;
2798
}
2799
2785
-# parameters can be strings, or references to arrays of strings
2786
-sub from_ids_eq {
2787
- my ($a, $b) = @_;
2788
-
2789
- if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2790
- for (my $i = 0; $i < @$a; ++$i) {
2791
- return 0 unless ($a->[$i] eq $b->[$i]);
2792
- }
2793
- return 1;
2794
- } elsif (!ref($a) && !ref($b)) {
2795
- return $a eq $b;
2796
- } else {
2797
- return 0;
2798
- }
2799
-}
2800
-
2800
+# is current raw difftree line of file deletion
2801
sub is_deleted {
2802
my $diffinfo = shift;
2803
2804
return $diffinfo->{'to_id'} eq ('0' x 40);
2805
}
2806
2807
+# does patch correspond to [previous] difftree raw line
2808
+# $diffinfo - hashref of parsed raw diff format
2809
+# $patchinfo - hashref of parsed patch diff format
2810
+# (the same keys as in $diffinfo)
2811
+sub is_patch_split {
2812
+ my ($diffinfo, $patchinfo) = @_;
2813
+
2814
+ return defined $diffinfo && defined $patchinfo
2815
+ && ($diffinfo->{'to_file'} || $diffinfo->{'file'}) eq $patchinfo->{'to_file'};
2816
+}
2817
+
2818
+
2819
sub git_difftree_body {
2820
my ($difftree, $hash, @parents) = @_;
2821
my ($parent) = $parents[0];
2852
my $alternate = 1;
2853
my $patchno = 0;
2854
foreach my $line (@{$difftree}) {
2843
- my $diff;
2844
- if (ref($line) eq "HASH") {
2845
- # pre-parsed (or generated by hand)
2846
- $diff = $line;
2847
- } else {
2848
- $diff = parse_difftree_raw_line($line);
2849
- }
2855
+ my $diff = parsed_difftree_line($line);
2856
2857
if ($alternate) {
2858
print "<tr class=\"dark\">\n";
3123
my ($fd, $difftree, $hash, @hash_parents) = @_;
3124
my ($hash_parent) = $hash_parents[0];
3125
3126
+ my $is_combined = (@hash_parents > 1);
3127
my $patch_idx = 0;
3128
my $patch_number = 0;
3129
my $patch_line;
3130
my $diffinfo;
3131
+ my $to_name;
3132
my (%from, %to);
3133
3134
print "<div class=\"patchset\">\n";
3142
3143
PATCH:
3144
while ($patch_line) {
3137
- my @diff_header;
3138
- my ($from_id, $to_id);
3139
-
3140
- # git diff header
3141
- #assert($patch_line =~ m/^diff /) if DEBUG;
3142
- #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3143
- $patch_number++;
3144
- push @diff_header, $patch_line;
3145
-
3146
- # extended diff header
3147
- EXTENDED_HEADER:
3148
- while ($patch_line = <$fd>) {
3149
- chomp $patch_line;
3145
3151
- last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3152
-
3153
- if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3154
- $from_id = $1;
3155
- $to_id = $2;
3156
- } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3157
- $from_id = [ split(',', $1) ];
3158
- $to_id = $2;
3159
- }
3160
-
3161
- push @diff_header, $patch_line;
3146
+ # parse "git diff" header line
3147
+ if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3148
+ # $1 is from_name, which we do not use
3149
+ $to_name = unquote($2);
3150
+ $to_name =~ s!^b/!!;
3151
+ } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3152
+ # $1 is 'cc' or 'combined', which we do not use
3153
+ $to_name = unquote($2);
3154
+ } else {
3155
+ $to_name = undef;
3156
}
3163
- my $last_patch_line = $patch_line;
3157
3158
# check if current patch belong to current raw line
3159
# and parse raw git-diff line if needed
3167
- if (defined $diffinfo &&
3168
- defined $from_id && defined $to_id &&
3169
- from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
3170
- $diffinfo->{'to_id'} eq $to_id) {
3160
+ if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3161
# this is continuation of a split patch
3162
print "<div class=\"patch cont\">\n";
3163
} else {
3164
# advance raw git-diff output if needed
3165
$patch_idx++ if defined $diffinfo;
3166
3177
- # compact combined diff output can have some patches skipped
3178
- # find which patch (using pathname of result) we are at now
3179
- my $to_name;
3180
- if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
3181
- $to_name = $1;
3182
- }
3183
-
3184
- do {
3185
- # read and prepare patch information
3186
- if (ref($difftree->[$patch_idx]) eq "HASH") {
3187
- # pre-parsed (or generated by hand)
3188
- $diffinfo = $difftree->[$patch_idx];
3189
- } else {
3190
- $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3191
- }
3167
+ # read and prepare patch information
3168
+ $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3169
3193
- # check if current raw line has no patch (it got simplified)
3194
- if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3170
+ # compact combined diff output can have some patches skipped
3171
+ # find which patch (using pathname of result) we are at now;
3172
+ if ($is_combined) {
3173
+ while ($to_name ne $diffinfo->{'to_file'}) {
3174
print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3175
format_diff_cc_simplified($diffinfo, @hash_parents) .
3176
"</div>\n"; # class="patch"
3177
3178
$patch_idx++;
3179
$patch_number++;
3180
+
3181
+ last if $patch_idx > $#$difftree;
3182
+ $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3183
}
3202
- } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3203
- $patch_idx > $#$difftree);
3184
+ }
3185
3186
# modifies %from, %to hashes
3187
parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3191
print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3192
}
3193
3194
+ # git diff header
3195
+ #assert($patch_line =~ m/^diff /) if DEBUG;
3196
+ #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3197
+ $patch_number++;
3198
# print "git diff" header
3214
- $patch_line = shift @diff_header;
3199
print format_git_diff_header_line($patch_line, $diffinfo,
3200
\%from, \%to);
3201
3202
# print extended diff header
3219
- print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3203
+ print "<div class=\"diff extended_header\">\n";
3204
EXTENDED_HEADER:
3221
- foreach $patch_line (@diff_header) {
3205
+ while ($patch_line = <$fd>) {
3206
+ chomp $patch_line;
3207
+
3208
+ last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3209
+
3210
print format_extended_diff_header_line($patch_line, $diffinfo,
3211
\%from, \%to);
3212
}
3225
- print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3213
+ print "</div>\n"; # class="diff extended_header"
3214
3215
# from-file/to-file diff header
3228
- $patch_line = $last_patch_line;
3216
if (! $patch_line) {
3217
print "</div>\n"; # class="patch"
3218
last PATCH;
3219
}
3220
next PATCH if ($patch_line =~ m/^diff /);
3221
#assert($patch_line =~ m/^---/) if DEBUG;
3235
- #assert($patch_line eq $last_patch_line) if DEBUG;
3222
3223
+ my $last_patch_line = $patch_line;
3224
$patch_line = <$fd>;
3225
chomp $patch_line;
3226
#assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3245
3246
# for compact combined (--cc) format, with chunk and patch simpliciaction
3247
# patchset might be empty, but there might be unprocessed raw lines
3261
- for ($patch_idx++ if $patch_number > 0;
3248
+ for (++$patch_idx if $patch_number > 0;
3249
$patch_idx < @$difftree;
3263
- $patch_idx++) {
3250
+ ++$patch_idx) {
3251
# read and prepare patch information
3265
- if (ref($difftree->[$patch_idx]) eq "HASH") {
3266
- # pre-parsed (or generated by hand)
3267
- $diffinfo = $difftree->[$patch_idx];
3268
- } else {
3269
- $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3270
- }
3252
+ $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3253
3254
# generate anchor for "patch" links in difftree / whatchanged part
3255
print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .