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

10 messages from 2009-09-30 to 2009-10-10. Participants: Jakub Narebski, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/21101

## Jakub Narebski, 2009-09-30 20:21

Subject: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges
Message-ID: <20090930201953.22301.73887.stgit@localhost.localdomain>
URL: https://gitlist.dev/e/20090930201953.22301.73887.stgit%40localhost.localdomain

```
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(-)

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, 2009-10-01 03:11

Subject: Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges
Message-ID: <20091001031140.GA30094@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20091001031140.GA30094%40coredump.intra.peff.net
In-Reply-To: <20090930201953.22301.73887.stgit@localhost.localdomain>

```
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, 2009-10-01 07:36

Subject: Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges
Message-ID: <200910010936.24789.jnareb@gmail.com>
URL: https://gitlist.dev/e/200910010936.24789.jnareb%40gmail.com
In-Reply-To: <20091001031140.GA30094@coredump.intra.peff.net>

```
Dnia czwartek 1. października 2009 05:11, Jeff King napisał:
> 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, 2009-10-01 07:55

Subject: Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges
Message-ID: <20091001075540.GA13436@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20091001075540.GA13436%40coredump.intra.peff.net
In-Reply-To: <200910010936.24789.jnareb@gmail.com>

```
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.

-Peff

```

## Jakub Narebski, 2009-10-01 09:18

Subject: Re: [PATCH] gitweb: Do not show 'patch' link in 'commit' view for merges
Message-ID: <200910011118.02875.jnareb@gmail.com>
URL: https://gitlist.dev/e/200910011118.02875.jnareb%40gmail.com
In-Reply-To: <20091001075540.GA13436@coredump.intra.peff.net>

```
Dnia czwartek 1. października 2009 09:55, Jeff King napisał:
> 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, 2009-10-09 12:10

Subject: [PATCH v2] gitweb: Do not show 'patch' link for merge commits
Message-ID: <200910091410.15904.jnareb@gmail.com>
URL: https://gitlist.dev/e/200910091410.15904.jnareb%40gmail.com
In-Reply-To: <200910011118.02875.jnareb@gmail.com>

```
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(-)

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, 2009-10-09 12:23

Subject: Re: [PATCH v2] gitweb: Do not show 'patch' link for merge commits
Message-ID: <200910091423.51286.jnareb@gmail.com>
URL: https://gitlist.dev/e/200910091423.51286.jnareb%40gmail.com
In-Reply-To: <200910091410.15904.jnareb@gmail.com>

```
Jakub Narebski wrote:

> @@ -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, 2009-10-09 12:26

Subject: [PATCH v2 (amend)] gitweb: Do not show 'patch' link for merge commits
Message-ID: <200910091426.44976.jnareb@gmail.com>
URL: https://gitlist.dev/e/200910091426.44976.jnareb%40gmail.com
In-Reply-To: <200910091423.51286.jnareb@gmail.com>

```
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(-)

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, 2009-10-09 19:19

Subject: Re: [PATCH v2 (amend)] gitweb: Do not show 'patch' link for merge commits
Message-ID: <20091009191930.GA14534@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20091009191930.GA14534%40coredump.intra.peff.net
In-Reply-To: <200910091426.44976.jnareb@gmail.com>

```
On Fri, Oct 09, 2009 at 02:26:44PM +0200, Jakub Narebski wrote:

> 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

```

## Junio C Hamano, 2009-10-10 00:56

Subject: Re: [PATCH v2 (amend)] gitweb: Do not show 'patch' link for merge commits
Message-ID: <7vhbu8f4me.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vhbu8f4me.fsf%40alter.siamese.dyndns.org
In-Reply-To: <200910091426.44976.jnareb@gmail.com>

```
Thanks.

```
