@matheusbsbs / matheus-bagliano / commits / 0cec6db5cf
Fork 0

gitweb: Fix and simplify "split patch" detection

There are some cases when one line from "raw" git-diff output (raw format) corresponds to more than one patch in the patchset git-diff output; we call this situation "split patch". Old code misdetected subsequent patches (for different files) with the same pre-image and post-image as fragments of "split patch", leading to mislabeled from-file/to-file diff header etc. Old code used pre-image and post-image SHA-1 identifier ('from_id' and 'to_id') to check if current patch corresponds to old raw diff format line, to find if one difftree raw line coresponds to more than one patch in the patch format. Now we use post-image filename for that. This assumes that post-image filename alone can be used to identify difftree raw line. In the case this changes (which is unlikely considering current diff engine) we can add 'from_id' and 'to_id' to detect "patch splitting" together with 'to_file'. Because old code got pre-image and post-image SHA-1 identifier for the patch from the "index" line in extended diff header, diff header had to be buffered. New code takes post-image filename from "git diff" header, which is first line of a patch; this allows to simplify git_patchset_body code. A side effect of resigning diff header buffering is that there is always "diff extended_header" div, even if extended diff header is empty. Alternate solution would be to check when git splits patches, and do not check if parsed info from current patch corresponds to current or next raw diff format output line. Git splits patches only for 'T' (typechange) status filepair, and there always two patches corresponding to one raw diff line. It was not used because it would tie gitweb code to minute details of git diff output. While at it, use newly introduced parsed_difftree_line wrapper subroutine in git_difftree_body. Noticed-by: Yann Dirson <ydirson@altern.org> Diagnosed-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Oct 30, 2007 at 01:35 UTC 0cec6db5cfbb109eebb3a50b6213cab2dc68f0c8
1 file changed +67 -85
gitweb/gitweb.perl
+67 -85
@@ -2000,6 +2000,19 @@ sub parse_difftree_raw_line {
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;
@@ -2043,6 +2056,7 @@ sub parse_from_to_diffinfo {
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,
@@ -2766,6 +2780,7 @@ sub git_print_tree_entry {
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
@@ -2782,28 +2797,25 @@ sub fill_from_file_info {
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];
@@ -2840,13 +2852,7 @@ sub git_difftree_body {
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";
@@ -3117,10 +3123,12 @@ sub git_patchset_body {
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";
@@ -3134,73 +3142,46 @@ sub git_patchset_body {
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);
@@ -3210,30 +3191,36 @@ sub git_patchset_body {
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;
@@ -3258,16 +3245,11 @@ sub git_patchset_body {
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" .