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

gitweb: Use parse_difftree_raw_line in git_difftree_body

Use newly introduced parse_difftree_raw_line function in the git_difftree_body subroutine. While at it correct error in parse_difftree_raw_line (unquote is unprototyped function), and add comment explaining this function. It also refactors git_difftree_body somewhat, and tries to fit it in 80 columns. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Aug 21, 2006 at 23:08 UTC e8e41a9383ff8e1fb22a7abb899b16486ad25fe1
1 file changed +97 -81
gitweb/gitweb.perl
+97 -81
@@ -918,6 +918,7 @@ sub parse_ref {
918 return %ref_item;
919 }
920
921 +# parse line of git-diff-tree "raw" output
922 sub parse_difftree_raw_line {
923 my $line = shift;
924 my %res;
@@ -932,7 +933,7 @@ sub parse_difftree_raw_line {
933 $res{'status'} = $5;
934 $res{'similarity'} = $6;
935 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
935 - ($res{'from_file'}, $res{'to_file'}) = map(unquote, split("\t", $7));
936 + ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
937 } else {
938 $res{'file'} = unquote($7);
939 }
@@ -1355,18 +1356,7 @@ sub git_difftree_body {
1356 print "<table class=\"diff_tree\">\n";
1357 my $alternate = 0;
1358 foreach my $line (@{$difftree}) {
1358 - # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1359 - # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1360 - if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1361 - next;
1362 - }
1363 - my $from_mode = $1;
1364 - my $to_mode = $2;
1365 - my $from_id = $3;
1366 - my $to_id = $4;
1367 - my $status = $5;
1368 - my $similarity = $6; # score
1369 - my $file = validate_input(unquote($7));
1359 + my %diff = parse_difftree_raw_line($line);
1360
1361 if ($alternate) {
1362 print "<tr class=\"dark\">\n";
@@ -1375,105 +1365,131 @@ sub git_difftree_body {
1365 }
1366 $alternate ^= 1;
1367
1378 - if ($status eq "A") { # created
1379 - my $mode_chng = "";
1380 - if (S_ISREG(oct $to_mode)) {
1381 - $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1368 + my ($to_mode_oct, $to_mode_str, $to_file_type);
1369 + my ($from_mode_oct, $from_mode_str, $from_file_type);
1370 + if ($diff{'to_mode'} ne ('0' x 6)) {
1371 + $to_mode_oct = oct $diff{'to_mode'};
1372 + if (S_ISREG($to_mode_oct)) { # only for regular file
1373 + $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1374 }
1375 + $to_file_type = file_type($diff{'to_mode'});
1376 + }
1377 + if ($diff{'from_mode'} ne ('0' x 6)) {
1378 + $from_mode_oct = oct $diff{'from_mode'};
1379 + if (S_ISREG($to_mode_oct)) { # only for regular file
1380 + $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1381 + }
1382 + $from_file_type = file_type($diff{'from_mode'});
1383 + }
1384 +
1385 + if ($diff{'status'} eq "A") { # created
1386 + my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1387 + $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1388 + $mode_chng .= "]</span>";
1389 print "<td>" .
1384 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$file),
1385 - -class => "list"}, esc_html($file)) .
1390 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1391 + hash_base=>$hash, file_name=>$diff{'file'}),
1392 + -class => "list"}, esc_html($diff{'file'})) .
1393 "</td>\n" .
1387 - "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1394 + "<td>$mode_chng</td>\n" .
1395 "<td class=\"link\">" .
1389 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$file)}, "blob") .
1396 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1397 + hash_base=>$hash, file_name=>$diff{'file'})},
1398 + "blob") .
1399 "</td>\n";
1400
1392 - } elsif ($status eq "D") { # deleted
1401 + } elsif ($diff{'status'} eq "D") { # deleted
1402 + my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1403 print "<td>" .
1394 - $cgi->a({-href => href(action=>"blob", hash=>$from_id, hash_base=>$parent, file_name=>$file),
1395 - -class => "list"}, esc_html($file)) . "</td>\n" .
1396 - "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1404 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1405 + hash_base=>$parent, file_name=>$diff{'file'}),
1406 + -class => "list"}, esc_html($diff{'file'})) .
1407 + "</td>\n" .
1408 + "<td>$mode_chng</td>\n" .
1409 "<td class=\"link\">" .
1398 - $cgi->a({-href => href(action=>"blob", hash=>$from_id, hash_base=>$parent, file_name=>$file)}, "blob") . " | " .
1399 - $cgi->a({-href => href(action=>"history", hash_base=>$parent, file_name=>$file)}, "history") .
1400 - "</td>\n"
1410 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1411 + hash_base=>$parent, file_name=>$diff{'file'})},
1412 + "blob") .
1413 + " | " .
1414 + $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1415 + file_name=>$diff{'file'})},\
1416 + "history") .
1417 + "</td>\n";
1418
1402 - } elsif ($status eq "M" || $status eq "T") { # modified, or type changed
1419 + } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1420 my $mode_chnge = "";
1404 - if ($from_mode != $to_mode) {
1405 - $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
1406 - if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1407 - $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1421 + if ($diff{'from_mode'} != $diff{'to_mode'}) {
1422 + $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1423 + if ($from_file_type != $to_file_type) {
1424 + $mode_chnge .= " from $from_file_type to $to_file_type";
1425 }
1409 - if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1410 - if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1411 - $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1412 - } elsif (S_ISREG($to_mode)) {
1413 - $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1426 + if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1427 + if ($from_mode_str && $to_mode_str) {
1428 + $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1429 + } elsif ($to_mode_str) {
1430 + $mode_chnge .= " mode: $to_mode_str";
1431 }
1432 }
1433 $mode_chnge .= "]</span>\n";
1434 }
1435 print "<td>";
1419 - if ($to_id ne $from_id) { # modified
1420 - print $cgi->a({-href => href(action=>"blobdiff", hash=>$to_id, hash_parent=>$from_id, hash_base=>$hash, file_name=>$file),
1421 - -class => "list"}, esc_html($file));
1422 - } else { # mode changed
1423 - print $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$file),
1424 - -class => "list"}, esc_html($file));
1436 + if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1437 + print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1438 + hash_base=>$hash, file_name=>$diff{'file'}),
1439 + -class => "list"}, esc_html($diff{'file'}));
1440 + } else { # only mode changed
1441 + print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1442 + hash_base=>$hash, file_name=>$diff{'file'}),
1443 + -class => "list"}, esc_html($diff{'file'}));
1444 }
1445 print "</td>\n" .
1446 "<td>$mode_chnge</td>\n" .
1447 "<td class=\"link\">" .
1429 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$file)}, "blob");
1430 - if ($to_id ne $from_id) { # modified
1431 - print " | " . $cgi->a({-href => href(action=>"blobdiff", hash=>$to_id, hash_parent=>$from_id, hash_base=>$hash, file_name=>$file)}, "diff");
1432 - }
1433 - print " | " . $cgi->a({-href => href(action=>"history", hash_base=>$hash, file_name=>$file)}, "history") . "\n";
1434 - print "</td>\n";
1435 -
1436 - } elsif ($status eq "R") { # renamed
1437 - my ($from_file, $to_file) = split "\t", $file;
1438 - my $mode_chng = "";
1439 - if ($from_mode != $to_mode) {
1440 - $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1441 - }
1442 - print "<td>" .
1443 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$to_file),
1444 - -class => "list"}, esc_html($to_file)) . "</td>\n" .
1445 - "<td><span class=\"file_status moved\">[moved from " .
1446 - $cgi->a({-href => href(action=>"blob", hash=>$from_id, hash_base=>$parent, file_name=>$from_file),
1447 - -class => "list"}, esc_html($from_file)) .
1448 - " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1449 - "<td class=\"link\">" .
1450 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$to_file)}, "blob");
1451 - if ($to_id ne $from_id) {
1448 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1449 + hash_base=>$hash, file_name=>$diff{'file'})},
1450 + "blob");
1451 + if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1452 print " | " .
1453 - $cgi->a({-href => href(action=>"blobdiff", hash=>$to_id, hash_parent=>$from_id, hash_base=>$hash, file_name=>$to_file, file_parent=>$from_file)}, "diff");
1453 + $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1454 + hash_base=>$hash, file_name=>$diff{'file'})},
1455 + "diff");
1456 }
1457 + print " | " .
1458 + $cgi->a({-href => href(action=>"history",
1459 + hash_base=>$hash, file_name=>$diff{'file'})},
1460 + "history");
1461 print "</td>\n";
1462
1457 - } elsif ($status eq "C") { # copied
1458 - my ($from_file, $to_file) = split "\t", $file;
1463 + } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1464 + my %status_name = ('R' => 'moved', 'C' => 'copied');
1465 + my $nstatus = $status_name{$diff{'status'}};
1466 my $mode_chng = "";
1460 - if ($from_mode != $to_mode) {
1461 - $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1467 + if ($diff{'from_mode'} != $diff{'to_mode'}) {
1468 + # mode also for directories, so we cannot use $to_mode_str
1469 + $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1470 }
1471 print "<td>" .
1464 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$to_file),
1465 - -class => "list"}, esc_html($to_file)) . "</td>\n" .
1466 - "<td><span class=\"file_status copied\">[copied from " .
1467 - $cgi->a({-href => href(action=>"blob", hash=>$from_id, hash_base=>$parent, file_name=>$from_file),
1468 - -class => "list"}, esc_html($from_file)) .
1469 - " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1472 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1473 + hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1474 + -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1475 + "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1476 + $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1477 + hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1478 + -class => "list"}, esc_html($diff{'from_file'})) .
1479 + " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1480 "<td class=\"link\">" .
1471 - $cgi->a({-href => href(action=>"blob", hash=>$to_id, hash_base=>$hash, file_name=>$to_file)}, "blob");
1472 - if ($to_id ne $from_id) {
1481 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1482 + hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1483 + "blob");
1484 + if ($diff{'to_id'} ne $diff{'from_id'}) {
1485 print " | " .
1474 - $cgi->a({-href => href(action=>"blobdiff", hash=>$to_id, hash_parent=>$from_id, hash_base=>$hash, file_name=>$to_file, file_parent=>$from_file)}, "diff");
1486 + $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1487 + hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1488 + file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1489 + "diff");
1490 }
1491 print "</td>\n";
1492 +
1493 } # we should not encounter Unmerged (U) or Unknown (X) status
1494 print "</tr>\n";
1495 }