threads / patch / 21101

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

Subject: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges

## tl;dr

10 messages between Sep 30, 2009 and Oct 10, 2009. Diffs are folded; open one to read it.

replies: 9people: 3as markdown or json

Jakub Narebski· Sep 30, 2009, 20:21 UTC · lore

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.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
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(-)
Show changes to gitweb/gitweb.perl +1 −1
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 24b2193..fb045a1 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -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");
Jeff King· Oct 1, 2009, 03:11 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges

On Wed, Sep 30, 2009 at 10:21:53PM +0200, Jakub Narebski wrote:
> 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
Jakub Narebski· Oct 1, 2009, 07:36 UTC · re: Jeff King · lore

Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges

Dnia czwartek 1. października 2009 05:11, Jeff King napisał:
Show 13 quoted lines
> On Wed, Sep 30, 2009 at 10:21:53PM +0200, Jakub Narebski wrote:
> 
> > 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?
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...

-- 
Jakub Narebski
Poland
Jeff King· Oct 1, 2009, 07:55 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges

On Thu, Oct 01, 2009 at 09:36:23AM +0200, Jakub Narebski wrote:
Show 9 quoted lines
> 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
Jakub Narebski· Oct 1, 2009, 09:18 UTC · re: Jeff King · lore

Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges

Dnia czwartek 1. października 2009 09:55, Jeff King napisał:
Show 20 quoted lines
> On Thu, Oct 01, 2009 at 09:36:23AM +0200, Jakub Narebski wrote:
> 
> > 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.

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.
-- 
Jakub Narebski
Poland
Jakub Narebski· Oct 9, 2009, 12:10 UTC · re: Jakub Narebski · lore

[PATCH v2] 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.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
On Thu, 1 Oct 2009, Jakub Narebski wrote:
> 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(-)
Show changes to gitweb/gitweb.perl +3 −3
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 24b2193..14f31dc 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -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 {
-- 
1.6.4.2
Jakub Narebski· Oct 9, 2009, 12:23 UTC · re: Jakub Narebski · lore

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

Jakub Narebski wrote:
Show 6 quoted lines
> @@ -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!
>                         $formats_nav .= " | " .
>                                 $cgi->a({-href => href(action=>"patch", -replay=>1)},
>                                         "patch");
-- 
Jakub Narebski
Poland
Jakub Narebski· Oct 9, 2009, 12:26 UTC · re: Jakub Narebski · lore

[PATCH v2 (amend)] 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.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
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(-)
Show changes to gitweb/gitweb.perl +3 −3
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 24b2193..14f31dc 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -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 {
-- 
1.6.4.2
Jeff King· Oct 9, 2009, 19:19 UTC · re: Jakub Narebski · lore

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

On Fri, Oct 09, 2009 at 02:26:44PM +0200, Jakub Narebski wrote:
Show 7 quoted lines
> 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

← back to recent threads