Skip to contentSkip to conversation

gitweb: Do not show 'patch' link in 'commit' view for merges

PATCH In review Jakub Narebski 3 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitweb: Do not show 'patch' link in 'commit' view for merges

Show 'patch' link in the 'commit' view only for commits which have exactly one parent, otherwise call to git-format-patch would fail for the hyperlinked 'patch' action.

---
Just noticed this issue, thanks indirectly to "Re: How can I download a git commit as a diff patch?" thread.

This is conservative change: perhaps we could extend 'patch' view to work also for root commit...

 gitweb/gitweb.perl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
gitweb/gitweb.perl +1 -1
index 24b2193..fb045a1 100755
@@ -5328,7 +5328,7 @@ sub git_commit {
 			} @$parents ) .
 			')';
 	}
-	if (gitweb_check_feature('patches')) {
+	if (gitweb_check_feature('patches') && @$parents == 1) {
 		$formats_nav .= " | " .
 			$cgi->a({-href => href(action=>"patch", -replay=>1)},
 				"patch");
#1 lore raw
Quoted 3 lines · Jakub Narebski

Show 'patch' link in the 'commit' view only for commits which have exactly one parent, otherwise call to git-format-patch would fail for the hyperlinked 'patch' action.

Fail in what way? From my cursory reading of the code, it looks like the 'patch' action calls into git_commitdiff, which handles the multi-parent case.

I assume I'm reading wrong, since you obviously know gitweb much better than I do. :) But can you expand a little on the nature of the problem in the commit message?

-Peff

#2 lore raw
Quoted 6 lines · Jeff King
Quoted 3 lines · Jakub Narebski

Show 'patch' link in the 'commit' view only for commits which have exactly one parent, otherwise call to git-format-patch would fail for the hyperlinked 'patch' action.

Quoted 7 lines

Fail in what way? From my cursory reading of the code, it looks like the 'patch' action calls into git_commitdiff, which handles the multi-parent case.

I assume I'm reading wrong, since you obviously know gitweb much better than I do. :) But can you expand a little on the nature of the problem in the commit message?

Well, from the point of view of behavior, 'patch' link in 'commit' view for a merge commit, e.g.
gitweb.cgi/git.git/commit/833423ae071ffedb7fbca39789f14f9a45a3d1c4
leads to the 'patch' view
git.git/patch/833423ae071ffedb7fbca39789f14f9a45a3d1c4
which leads to 'text/plain' output with the following contents:

Reading git-format-patch failed

From the point of view of code, 'patch' view is handled by git_patch() subroutine, which in turn calls git_commitdiff(-format => 'patch', -single=> 1); git_commitdiff checks if 'patch' view is enabled feature, and then composes and calls the following command (I have skipped --git-dir=...):

  git format-patch --encoding=utf8 --stdout -1 --root <commit-id>

And git-format-patch produces no output for merge commit. Then git_commitdiff dumps output of git-format-patch

	local $/ = undef;
	print <$fd>;

and somehow fails on closing filehandle

	close $fd
		or print "Reading git-format-patch failed\n";

Even if 'patch' view didn't fail, it is not a good idea to have link to an empty page (or page with only error message). Though probably git_commitdiff could check if it is used for a merge commit...

Signature · 2 lines
Jakub Narebski
Poland
#3 lore raw
Quoted 9 lines · Jakub Narebski

From the point of view of code, 'patch' view is handled by git_patch() subroutine, which in turn calls git_commitdiff(-format => 'patch', -single=> 1); git_commitdiff checks if 'patch' view is enabled feature, and then composes and calls the following command (I have skipped --git-dir=...):

  git format-patch --encoding=utf8 --stdout -1 --root <commit-id>

And git-format-patch produces no output for merge commit. Then git_commitdiff dumps output of git-format-patch

Ah, OK, I see the code path you are talking about now. My first thought was "shouldn't it be handling the merge case?" but that really doesn't make any sense. The "patch" view is just about something that can be given to "git am", and it doesn't understand merges anyway. And the "commitdiff" and "html" formats already handle it appropriately.

So I think your patch is the right thing to do. Thanks for the explanation.

-Peff

#4 lore raw
Quoted 12 lines · Jeff King
Quoted 9 lines · Jakub Narebski

From the point of view of code, 'patch' view is handled by git_patch() subroutine, which in turn calls git_commitdiff(-format => 'patch', -single=> 1); git_commitdiff checks if 'patch' view is enabled feature, and then composes and calls the following command (I have skipped --git-dir=...):

  git format-patch --encoding=utf8 --stdout -1 --root <commit-id>

And git-format-patch produces no output for merge commit. Then git_commitdiff dumps output of git-format-patch

Quoted 8 lines

Ah, OK, I see the code path you are talking about now. My first thought was "shouldn't it be handling the merge case?" but that really doesn't make any sense. The "patch" view is just about something that can be given to "git am", and it doesn't understand merges anyway. And the "commitdiff" and "html" formats already handle it appropriately.

So I think your patch is the right thing to do. Thanks for the explanation.

I'll enhance commit message to talk about issues touched here (that 'patch' is for git-am and doesn't make sense for merges, regardless of possible bug in 'patch' view when used for merge commits), and check if this correction isn't needed also elsewhere.

I'll send v2 of this patch in a bit.

Signature · 2 lines
Jakub Narebski
Poland
#5 lore raw

PATCH v2 1/1 gitweb: Do not show 'patch' link for merge commits

The 'patch' view is about generating text/plain patch that can be given to "git am", and "git am" doesn't understand merges anyway. Therefore link to 'patch' view should not be shown for merge commits.

Also call to git-format-patch inside the 'patch' action would fail when 'patch' action is called for a merge commit, with "Reading git-format-patch failed" text as 'patch' view body.

---

Quoted 1 line · Jakub Narebski

I'll send v2 of this patch in a bit.

Here it is.

Changes since v1:
* Do not show 'patch' link for merge commits not only in 'commit' view, but also in 'commitdiff' view (more complete)
* 'patch' link is shown also for root (parentless) commits; it works correctly thanks to passing '--root' option to git-format-patch (remove unnecessary restriction)
* better commit message thanks to discussion with Jeff King

 gitweb/gitweb.perl |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
gitweb/gitweb.perl +3 -3
index 24b2193..14f31dc 100755
@@ -5328,7 +5328,7 @@ sub git_commit {
 			} @$parents ) .
 			')';
 	}
-	if (gitweb_check_feature('patches')) {
+	if (gitweb_check_feature('patches') && @$parents <= 1) {
 		$formats_nav .= " | " .
 			$cgi->a({-href => href(action=>"patch", -replay=>1)},
 				"patch");
@@ -5616,7 +5616,7 @@ sub git_commitdiff {
 		$formats_nav =
 			$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
 			        "raw");
-		if ($patch_max) {
+		if ($patch_max &&  && @{$co{'parents'}} <= 1) {
 			$formats_nav .= " | " .
 				$cgi->a({-href => href(action=>"patch", -replay=>1)},
 					"patch");
@@ -5824,7 +5824,7 @@ sub git_commitdiff_plain {
 
 # format-patch-style patches
 sub git_patch {
-	git_commitdiff(-format => 'patch', -single=> 1);
+	git_commitdiff(-format => 'patch', -single => 1);
 }
 
 sub git_patches {
Signature · 1 line
1.6.4.2
#6 lore raw

Re: [PATCH v2] gitweb: Do not show 'patch' link for merge commits

Quoted 6 lines · Jakub Narebski
(quoted diff) +0 -0
@@ -5616,7 +5616,7 @@ sub git_commitdiff {

                $formats_nav =
                        $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)}, "raw");
-               if ($patch_max) {
+               if ($patch_max &&  && @{$co{'parents'}} <= 1) {

Gaaaah. It should be "&&", not "&& &&". I'm extremely sorry!

Quoted 3 lines

                        $formats_nav .= " | " .
                                $cgi->a({-href => href(action=>"patch", -replay=>1)}, "patch");

Signature · 2 lines
Jakub Narebski
Poland
#7 lore raw

PATCH v2 1/1 gitweb: Do not show 'patch' link for merge commits

The 'patch' view is about generating text/plain patch that can be given to "git am", and "git am" doesn't understand merges anyway. Therefore link to 'patch' view should not be shown for merge commits.

Also call to git-format-patch inside the 'patch' action would fail when 'patch' action is called for a merge commit, with "Reading git-format-patch failed" text as 'patch' view body.

---
Changes since v1:
* Do not show 'patch' link for merge commits not only in 'commit' view, but also in 'commitdiff' view (more complete)
* 'patch' link is shown also for root (parentless) commits; it works correctly thanks to passing '--root' option to git-format-patch (remove unnecessary restriction)
* better commit message thanks to discussion with Jeff King

 gitweb/gitweb.perl |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
gitweb/gitweb.perl +3 -3
index 24b2193..14f31dc 100755
@@ -5328,7 +5328,7 @@ sub git_commit {
 			} @$parents ) .
 			')';
 	}
-	if (gitweb_check_feature('patches')) {
+	if (gitweb_check_feature('patches') && @$parents <= 1) {
 		$formats_nav .= " | " .
 			$cgi->a({-href => href(action=>"patch", -replay=>1)},
 				"patch");
@@ -5616,7 +5616,7 @@ sub git_commitdiff {
 		$formats_nav =
 			$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
 			        "raw");
-		if ($patch_max) {
+		if ($patch_max && @{$co{'parents'}} <= 1) {
 			$formats_nav .= " | " .
 				$cgi->a({-href => href(action=>"patch", -replay=>1)},
 					"patch");
@@ -5824,7 +5824,7 @@ sub git_commitdiff_plain {
 
 # format-patch-style patches
 sub git_patch {
-	git_commitdiff(-format => 'patch', -single=> 1);
+	git_commitdiff(-format => 'patch', -single => 1);
 }
 
 sub git_patches {
Signature · 1 line
1.6.4.2
#8 lore raw

Re: [PATCH v2 (amend)] gitweb: Do not show 'patch' link for merge commits

Quoted 7 lines · Jakub Narebski

Changes since v1:
* Do not show 'patch' link for merge commits not only in 'commit' view, but also in 'commitdiff' view (more complete)
* 'patch' link is shown also for root (parentless) commits; it works correctly thanks to passing '--root' option to git-format-patch (remove unnecessary restriction)
* better commit message thanks to discussion with Jeff King

Thanks. I am not a gitweb expert, but those changes look good to me.

-Peff