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

gitweb: Add combined diff support to git_difftree_body

You have to pass all parents as final parameters of git_difftree_body subroutine; the number of parents of a diff must be equal to the number derived from parsing git-diff-tree output, raw combined diff for git_difftree_body to display combined diff correctly (but it is not checked). Currently the possibility of displaying diffree of combined diff is not used in gitweb code; git_difftree_body is always caled for ordinary diff, and with only one parent. Description of output for combined diff: ---------------------------------------- The difftree table for combined diff starts with a cell with pathname of changed blob (changed file), which if possible is hidden link (class="list") to the 'blob' view of final version (if it exists), like for difftree for ordinary diff. If file was deleted in the final commit then filename is not hyperlinked. There is no cell with single file status (new, deleted, mode change, rename), as for combined diff as there is no single status: different parents might have different status. If git_difftree_body was called from git_commitdiff (for 'commitdiff' action) there is inner link to anchor to appropriate fragment (patch) in patchset body; the "patch" link does not replace "diff" link like for ordinary diff. Each of "diff" links is in separate cell, contrary to output for ordinary diff in which all links are (at least for now) in a single cell. For each parent, if file was not present we leave cell empty. If file was deleted in the result, we provide link to 'blob' view. Otherwise we provide link to 'commitdiff' view, even if patch (diff) consist only of extended diff header, and contents is not changed (pure rename, pure mode change). The only difference is that link to "blobdiff" view with no contents change is with 'nochange' class. At last, there is provided link to current version of file as "blob" link, if the file was not deleted in the result, and lik to history of a file, if there exists one. (The link to file history might be confused, at least for now, by renames.) Note that git-diff-tree raw output dor combined diff does not provide filename before change for renames and copies; we use git_get_path_by_hash to get "src" filename for renames (this means additional call to git-ls-tree for a _whole_ tree). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed May 7, 2007 at 01:10 UTC ed224deac9436215f8c0b098baa0aecfcff1c293
2 files changed +136 -2
gitweb/gitweb.css
+17
@@ -181,6 +181,23 @@ table.diff_tree {
181 font-family: monospace;
182 }
183
184 +table.combined.diff_tree td {
185 + padding-right: 24px;
186 +}
187 +
188 +table.combined.diff_tree td.link {
189 + padding: 0px 2px;
190 +}
191 +
192 +table.combined.diff_tree td.nochange a {
193 + color: #6666ff;
194 +}
195 +
196 +table.combined.diff_tree td.nochange a:hover,
197 +table.combined.diff_tree td.nochange a:visited {
198 + color: #d06666;
199 +}
200 +
201 table.blame {
202 border-collapse: collapse;
203 }
gitweb/gitweb.perl
+119 -2
@@ -1015,6 +1015,30 @@ sub git_get_hash_by_path {
1015 return $3;
1016 }
1017
1018 +# get path of entry with given hash at given tree-ish (ref)
1019 +# used to get 'from' filename for combined diff (merge commit) for renames
1020 +sub git_get_path_by_hash {
1021 + my $base = shift || return;
1022 + my $hash = shift || return;
1023 +
1024 + local $/ = "\0";
1025 +
1026 + open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1027 + or return undef;
1028 + while (my $line = <$fd>) {
1029 + chomp $line;
1030 +
1031 + #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1032 + #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1033 + if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1034 + close $fd;
1035 + return $1;
1036 + }
1037 + }
1038 + close $fd;
1039 + return undef;
1040 +}
1041 +
1042 ## ......................................................................
1043 ## git utility functions, directly accessing git repository
1044
@@ -2210,7 +2234,8 @@ sub git_print_tree_entry {
2234 ## functions printing large fragments of HTML
2235
2236 sub git_difftree_body {
2213 - my ($difftree, $hash, $parent) = @_;
2237 + my ($difftree, $hash, @parents) = @_;
2238 + my ($parent) = $parents[0];
2239 my ($have_blame) = gitweb_check_feature('blame');
2240 print "<div class=\"list_head\">\n";
2241 if ($#{$difftree} > 10) {
@@ -2218,7 +2243,9 @@ sub git_difftree_body {
2243 }
2244 print "</div>\n";
2245
2221 - print "<table class=\"diff_tree\">\n";
2246 + print "<table class=\"" .
2247 + (@parents > 1 ? "combined " : "") .
2248 + "diff_tree\">\n";
2249 my $alternate = 1;
2250 my $patchno = 0;
2251 foreach my $line (@{$difftree}) {
@@ -2231,6 +2258,96 @@ sub git_difftree_body {
2258 }
2259 $alternate ^= 1;
2260
2261 + if (exists $diff{'nparents'}) { # combined diff
2262 +
2263 + if ($diff{'to_id'} ne ('0' x 40)) {
2264 + # file exists in the result (child) commit
2265 + print "<td>" .
2266 + $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2267 + file_name=>$diff{'to_file'},
2268 + hash_base=>$hash),
2269 + -class => "list"}, esc_path($diff{'to_file'})) .
2270 + "</td>\n";
2271 + } else {
2272 + print "<td>" .
2273 + esc_path($diff{'to_file'}) .
2274 + "</td>\n";
2275 + }
2276 +
2277 + if ($action eq 'commitdiff') {
2278 + # link to patch
2279 + $patchno++;
2280 + print "<td class=\"link\">" .
2281 + $cgi->a({-href => "#patch$patchno"}, "patch") .
2282 + " | " .
2283 + "</td>\n";
2284 + }
2285 +
2286 + my $has_history = 0;
2287 + my $not_deleted = 0;
2288 + for (my $i = 0; $i < $diff{'nparents'}; $i++) {
2289 + my $hash_parent = $parents[$i];
2290 + my $from_hash = $diff{'from_id'}[$i];
2291 + my $from_path = undef;
2292 + my $status = $diff{'status'}[$i];
2293 +
2294 + $has_history ||= ($status ne 'A');
2295 + $not_deleted ||= ($status ne 'D');
2296 +
2297 + if ($status eq 'R' || $status eq 'C') {
2298 + $from_path = git_get_path_by_hash($hash_parent, $from_hash);
2299 + }
2300 +
2301 + if ($status eq 'A') {
2302 + print "<td class=\"link\" align=\"right\"> | </td>\n";
2303 + } elsif ($status eq 'D') {
2304 + print "<td class=\"link\">" .
2305 + $cgi->a({-href => href(action=>"blob",
2306 + hash_base=>$hash,
2307 + hash=>$from_hash,
2308 + file_name=>$from_path)},
2309 + "blob" . ($i+1)) .
2310 + " | </td>\n";
2311 + } else {
2312 + if ($diff{'to_id'} eq $from_hash) {
2313 + print "<td class=\"link nochange\">";
2314 + } else {
2315 + print "<td class=\"link\">";
2316 + }
2317 + print $cgi->a({-href => href(action=>"blobdiff",
2318 + hash=>$diff{'to_id'},
2319 + hash_parent=>$from_hash,
2320 + hash_base=>$hash,
2321 + hash_parent_base=>$hash_parent,
2322 + file_name=>$diff{'to_file'},
2323 + file_parent=>$from_path)},
2324 + "diff" . ($i+1)) .
2325 + " | </td>\n";
2326 + }
2327 + }
2328 +
2329 + print "<td class=\"link\">";
2330 + if ($not_deleted) {
2331 + print $cgi->a({-href => href(action=>"blob",
2332 + hash=>$diff{'to_id'},
2333 + file_name=>$diff{'to_file'},
2334 + hash_base=>$hash)},
2335 + "blob");
2336 + print " | " if ($has_history);
2337 + }
2338 + if ($has_history) {
2339 + print $cgi->a({-href => href(action=>"history",
2340 + file_name=>$diff{'to_file'},
2341 + hash_base=>$hash)},
2342 + "history");
2343 + }
2344 + print "</td>\n";
2345 +
2346 + print "</tr>\n";
2347 + next; # instead of 'else' clause, to avoid extra indent
2348 + }
2349 + # else ordinary diff
2350 +
2351 my ($to_mode_oct, $to_mode_str, $to_file_type);
2352 my ($from_mode_oct, $from_mode_str, $from_file_type);
2353 if ($diff{'to_mode'} ne ('0' x 6)) {