git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/6] gitweb: '--cc' for merges in 'commitdiff' view

From
Jakub Narebski <jnareb@gmail.com>
Date
Jun 8, 2007, 11:33 UTC
Message-ID
<20070608113249.3594.65410.stgit@roke.D-201>
In-Reply-To
<20070608112337.3594.93976.stgit@roke.D-201>

Allow choosing between '-c' (combined diff) and '--cc' (compact combined) diff format in 'commitdiff' view for merge (multiparent) commits. Default is now '--cc'.

In the bottom part of navigation bar there is link allowing to change diff format: "combined" for '-c' (when using '--cc') and "compact" for '--cc' (when using '-c'), just on the right of "raw" link to 'commitdiff_plain" view.

About patchset part of diff --cc output: the difftree (whatchanged table) has "patch" links to anchors to individual patches (on the same page). The --cc option further compresses the patch output by omitting some hunks; when this optimization makes all hunks disappear, the patch is not shown (like in any other "empty diff" case). But the fact that patch has been simplified out is not reflected in the raw (difftree) part of diff output; the raw part is the same for '-c' and '--cc' options. As correcting difftree is rather out of the question, as it would require scanning patchset part before writing out difftree, we add "Simple merge" empty diffs as a place to have anchor to in place of those simplified out and removed patches.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
Linus wanted '--cc' for merges as it is more compact. Here it is.
 gitweb/gitweb.perl |  111 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 99 insertions(+), 12 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index c7acfad..a6383dc 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1122,6 +1122,31 @@ sub format_diff_from_to_header {
 	return $result;
 }
 
+# create note for patch simplified by combined diff
+sub format_diff_cc_simplified {
+	my ($diffinfo, @parents) = @_;
+	my $result = '';
+
+	$result .= "<div class=\"diff header\">" .
+	           "diff --cc ";
+	if (!is_deleted($diffinfo)) {
+		$result .= $cgi->a({-href => href(action=>"blob",
+		                                  hash_base=>$hash,
+		                                  hash=>$diffinfo->{'to_id'},
+		                                  file_name=>$diffinfo->{'to_file'}),
+		                    -class => "path"},
+		                   esc_path($diffinfo->{'to_file'}));
+	} else {
+		$result .= esc_path($diffinfo->{'to_file'});
+	}
+	$result .= "</div>\n" . # class="diff header"
+	           "<div class=\"diff nodifferences\">" .
+	           "Simple merge" .
+	           "</div>\n"; # class="diff nodifferences"
+
+	return $result;
+}
+
 # format patch (diff) line (not to be used for diff headers)
 sub format_diff_line {
 	my $line = shift;
@@ -2973,13 +2998,33 @@ sub git_patchset_body {
 			# advance raw git-diff output if needed
 			$patch_idx++ if defined $diffinfo;
 
-			# read and prepare patch information
-			if (ref($difftree->[$patch_idx]) eq "HASH") {
-				# pre-parsed (or generated by hand)
-				$diffinfo = $difftree->[$patch_idx];
-			} else {
-				$diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
+			# compact combined diff output can have some patches skipped
+			# find which patch (using pathname of result) we are at now
+			my $to_name;
+			if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
+				$to_name = $1;
 			}
+
+			do {
+				# read and prepare patch information
+				if (ref($difftree->[$patch_idx]) eq "HASH") {
+					# pre-parsed (or generated by hand)
+					$diffinfo = $difftree->[$patch_idx];
+				} else {
+					$diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
+				}
+
+				# check if current raw line has no patch (it got simplified)
+				if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
+					print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
+					      format_diff_cc_simplified($diffinfo, @hash_parents) .
+					      "</div>\n";  # class="patch"
+
+					$patch_idx++;
+					$patch_number++;
+				}
+			} until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
+			         $patch_idx > $#$difftree);
 			# modifies %from, %to hashes
 			parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
 			if ($diffinfo->{'nparents'}) {
@@ -3069,6 +3114,27 @@ sub git_patchset_body {
 		print "</div>\n"; # class="patch"
 	}
 
+	# for compact combined (--cc) format, with chunk and patch simpliciaction
+	# patchset might be empty, but there might be unprocessed raw lines
+	for ($patch_idx++ if $patch_number > 0;
+	     $patch_idx < @$difftree;
+	     $patch_idx++) {
+		# read and prepare patch information
+		if (ref($difftree->[$patch_idx]) eq "HASH") {
+			# pre-parsed (or generated by hand)
+			$diffinfo = $difftree->[$patch_idx];
+		} else {
+			$diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
+		}
+
+		# generate anchor for "patch" links in difftree / whatchanged part
+		print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
+		      format_diff_cc_simplified($diffinfo, @hash_parents) .
+		      "</div>\n";  # class="patch"
+
+		$patch_number++;
+	}
+
 	if ($patch_number == 0) {
 		if (@hash_parents > 1) {
 			print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
@@ -4582,7 +4648,11 @@ sub git_commitdiff {
 		die_error(undef, "Unknown commit object");
 	}
 
-	# we need to prepare $formats_nav before any parameter munging
+	# choose format for commitdiff for merge
+	if (! defined $hash_parent && @{$co{'parents'}} > 1) {
+		$hash_parent = '--cc';
+	}
+	# we need to prepare $formats_nav before almost any parameter munging
 	my $formats_nav;
 	if ($format eq 'html') {
 		$formats_nav =
@@ -4590,7 +4660,8 @@ sub git_commitdiff {
 			                       hash=>$hash, hash_parent=>$hash_parent)},
 			        "raw");
 
-		if (defined $hash_parent) {
+		if (defined $hash_parent &&
+		    $hash_parent ne '-c' && $hash_parent ne '--cc') {
 			# commitdiff with two commits given
 			my $hash_parent_short = $hash_parent;
 			if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
@@ -4622,6 +4693,17 @@ sub git_commitdiff {
 				')';
 		} else {
 			# merge commit
+			if ($hash_parent eq '--cc') {
+				$formats_nav .= ' | ' .
+					$cgi->a({-href => href(action=>"commitdiff",
+					                       hash=>$hash, hash_parent=>'-c')},
+					        'combined');
+			} else { # $hash_parent eq '-c'
+				$formats_nav .= ' | ' .
+					$cgi->a({-href => href(action=>"commitdiff",
+					                       hash=>$hash, hash_parent=>'--cc')},
+					        'compact');
+			}
 			$formats_nav .=
 				' (merge: ' .
 				join(' ', map {
@@ -4634,9 +4716,10 @@ sub git_commitdiff {
 	}
 
 	my $hash_parent_param = $hash_parent;
-	if (!defined $hash_parent) {
+	if (!defined $hash_parent_param) {
+		# --cc for multiple parents, --root for parentless
 		$hash_parent_param =
-			@{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
+			@{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
 	}
 
 	# read commitdiff
@@ -4713,10 +4796,14 @@ TEXT
 
 	# write patch
 	if ($format eq 'html') {
-		git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
+		my $use_parents = !defined $hash_parent ||
+			$hash_parent eq '-c' || $hash_parent eq '--cc';
+		git_difftree_body(\@difftree, $hash,
+		                  $use_parents ? @{$co{'parents'}} : $hash_parent);
 		print "<br/>\n";
 
-		git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
+		git_patchset_body($fd, \@difftree, $hash,
+		                  $use_parents ? @{$co{'parents'}} : $hash_parent);
 		close $fd;
 		print "</div>\n"; # class="page_body"
 		git_footer_html();
Previous: Jakub NarebskiNext: Junio C Hamano
Message 7 of 8 in “gitweb: 'commitdiff' view improvements”
  1. 0/6 gitweb: 'commitdiff' view improvementsJakub Narebski, Jun 8, 2007
  2. 1/6 gitweb: Provide links to commitdiff to each parent in 'commitdiff' viewJakub Narebski, Jun 8, 2007
  3. 2/6 gitweb: Improve "next" link in commitdiff viewJakub Narebski, Jun 8, 2007
  4. 3/6 gitweb: Split git_patchset_body into separate subroutinesJakub Narebski, Jun 8, 2007
  5. 4/6 gitweb: Create special from-file/to-file header for combined diffJakub Narebski, Jun 8, 2007
  6. 5/6 gitweb: Add links to blobdiffs in from-file/to-file header for mergesJakub Narebski, Jun 8, 2007
  7. 6/6 gitweb: '--cc' for merges in 'commitdiff' viewJakub Narebski, Jun 8, 2007
  8. Junio C HamanoJun 11, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.