785
}
786
}
787
788
+# convert file mode in octal to file type description string
789
+sub file_type_long {
790
+ my $mode = shift;
791
+
792
+ if ($mode !~ m/^[0-7]+$/) {
793
+ return $mode;
794
+ } else {
795
+ $mode = oct $mode;
796
+ }
797
+
798
+ if (S_ISDIR($mode & S_IFMT)) {
799
+ return "directory";
800
+ } elsif (S_ISLNK($mode)) {
801
+ return "symlink";
802
+ } elsif (S_ISREG($mode)) {
803
+ if ($mode & S_IXUSR) {
804
+ return "executable";
805
+ } else {
806
+ return "file";
807
+ };
808
+ } else {
809
+ return "unknown";
810
+ }
811
+}
812
+
813
+
814
## ----------------------------------------------------------------------
815
## functions returning short HTML fragments, or transforming HTML fragments
816
## which don't beling to other sections
2197
my $in_header = 0;
2198
my $patch_found = 0;
2199
my $diffinfo;
2200
+ my (%from, %to);
2201
2202
print "<div class=\"patchset\">\n";
2203
2208
if ($patch_line =~ m/^diff /) { # "git diff" header
2209
# beginning of patch (in patchset)
2210
if ($patch_found) {
2211
+ # close extended header for previous empty patch
2212
+ if ($in_header) {
2213
+ print "</div>\n" # class="diff extended_header"
2214
+ }
2215
# close previous patch
2216
print "</div>\n"; # class="patch"
2217
} else {
2220
}
2221
print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2222
2223
+ # read and prepare patch information
2224
if (ref($difftree->[$patch_idx]) eq "HASH") {
2225
+ # pre-parsed (or generated by hand)
2226
$diffinfo = $difftree->[$patch_idx];
2227
} else {
2228
$diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2229
}
2230
+ $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2231
+ $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2232
+ if ($diffinfo->{'status'} ne "A") { # not new (added) file
2233
+ $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2234
+ hash=>$diffinfo->{'from_id'},
2235
+ file_name=>$from{'file'});
2236
+ }
2237
+ if ($diffinfo->{'status'} ne "D") { # not deleted file
2238
+ $to{'href'} = href(action=>"blob", hash_base=>$hash,
2239
+ hash=>$diffinfo->{'to_id'},
2240
+ file_name=>$to{'file'});
2241
+ }
2242
$patch_idx++;
2243
2199
- if ($diffinfo->{'status'} eq "A") { # added
2200
- print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2201
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2202
- hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2203
- $diffinfo->{'to_id'}) . " (new)" .
2204
- "</div>\n"; # class="diff_info"
2205
-
2206
- } elsif ($diffinfo->{'status'} eq "D") { # deleted
2207
- print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2208
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2209
- hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2210
- $diffinfo->{'from_id'}) . " (deleted)" .
2211
- "</div>\n"; # class="diff_info"
2212
-
2213
- } elsif ($diffinfo->{'status'} eq "R" || # renamed
2214
- $diffinfo->{'status'} eq "C" || # copied
2215
- $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2216
- print "<div class=\"diff_info\">" .
2217
- file_type($diffinfo->{'from_mode'}) . ":" .
2218
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2219
- hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2220
- $diffinfo->{'from_id'}) .
2221
- " -> " .
2222
- file_type($diffinfo->{'to_mode'}) . ":" .
2223
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2224
- hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2225
- $diffinfo->{'to_id'});
2226
- print "</div>\n"; # class="diff_info"
2227
-
2228
- } else { # modified, mode changed, ...
2229
- print "<div class=\"diff_info\">" .
2230
- file_type($diffinfo->{'from_mode'}) . ":" .
2231
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2232
- hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2233
- $diffinfo->{'from_id'}) .
2234
- " -> " .
2235
- file_type($diffinfo->{'to_mode'}) . ":" .
2236
- $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2237
- hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2238
- $diffinfo->{'to_id'});
2239
- print "</div>\n"; # class="diff_info"
2244
+ # print "git diff" header
2245
+ $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2246
+ if ($from{'href'}) {
2247
+ $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2248
+ 'a/' . esc_path($from{'file'}));
2249
+ } else { # file was added
2250
+ $patch_line .= 'a/' . esc_path($from{'file'});
2251
+ }
2252
+ $patch_line .= ' ';
2253
+ if ($to{'href'}) {
2254
+ $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2255
+ 'b/' . esc_path($to{'file'}));
2256
+ } else { # file was deleted
2257
+ $patch_line .= 'b/' . esc_path($to{'file'});
2258
}
2259
2242
- #print "<div class=\"diff extended_header\">\n";
2260
+ print "<div class=\"diff header\">$patch_line</div>\n";
2261
+ print "<div class=\"diff extended_header\">\n";
2262
$in_header = 1;
2263
next LINE;
2245
- } # start of patch in patchset
2264
+ }
2265
2266
+ if ($in_header) {
2267
+ if ($patch_line !~ m/^---/) {
2268
+ # match <path>
2269
+ if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2270
+ $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2271
+ esc_path($from{'file'}));
2272
+ }
2273
+ if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2274
+ $patch_line = $cgi->a({-href=>$to{'href'}, -class=>"path"},
2275
+ esc_path($to{'file'}));
2276
+ }
2277
+ # match <mode>
2278
+ if ($patch_line =~ m/\s(\d{6})$/) {
2279
+ $patch_line .= '<span class="info"> (' .
2280
+ file_type_long($1) .
2281
+ ')</span>';
2282
+ }
2283
+ # match <hash>
2284
+ if ($patch_line =~ m/^index/) {
2285
+ my ($from_link, $to_link);
2286
+ if ($from{'href'}) {
2287
+ $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2288
+ substr($diffinfo->{'from_id'},0,7));
2289
+ } else {
2290
+ $from_link = '0' x 7;
2291
+ }
2292
+ if ($to{'href'}) {
2293
+ $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2294
+ substr($diffinfo->{'to_id'},0,7));
2295
+ } else {
2296
+ $to_link = '0' x 7;
2297
+ }
2298
+ my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2299
+ $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2300
+ }
2301
+ print $patch_line . "<br/>\n";
2302
2248
- if ($in_header && $patch_line =~ m/^---/) {
2249
- #print "</div>\n"; # class="diff extended_header"
2250
- $in_header = 0;
2303
+ } else {
2304
+ #$in_header && $patch_line =~ m/^---/;
2305
+ print "</div>\n"; # class="diff extended_header"
2306
+ $in_header = 0;
2307
+
2308
+ if ($from{'href'}) {
2309
+ $patch_line = '--- a/' .
2310
+ $cgi->a({-href=>$from{'href'}, -class=>"path"},
2311
+ esc_path($from{'file'}));
2312
+ }
2313
+ print "<div class=\"diff from_file\">$patch_line</div>\n";
2314
2252
- my $file = $diffinfo->{'from_file'};
2253
- $file ||= $diffinfo->{'file'};
2254
- $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2255
- hash=>$diffinfo->{'from_id'}, file_name=>$file),
2256
- -class => "list"}, esc_path($file));
2257
- $patch_line =~ s|a/.*$|a/$file|g;
2258
- print "<div class=\"diff from_file\">$patch_line</div>\n";
2315
+ $patch_line = <$fd>;
2316
+ chomp $patch_line;
2317
2260
- $patch_line = <$fd>;
2261
- chomp $patch_line;
2318
+ #$patch_line =~ m/^+++/;
2319
+ if ($to{'href'}) {
2320
+ $patch_line = '+++ b/' .
2321
+ $cgi->a({-href=>$to{'href'}, -class=>"path"},
2322
+ esc_path($to{'file'}));
2323
+ }
2324
+ print "<div class=\"diff to_file\">$patch_line</div>\n";
2325
2263
- #$patch_line =~ m/^+++/;
2264
- $file = $diffinfo->{'to_file'};
2265
- $file ||= $diffinfo->{'file'};
2266
- $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2267
- hash=>$diffinfo->{'to_id'}, file_name=>$file),
2268
- -class => "list"}, esc_path($file));
2269
- $patch_line =~ s|b/.*|b/$file|g;
2270
- print "<div class=\"diff to_file\">$patch_line</div>\n";
2326
+ }
2327
2328
next LINE;
2329
}
2274
- next LINE if $in_header;
2330
2331
print format_diff_line($patch_line);
2332
}