gitweb: fix #patchNN anchors when path_info is enabled
PATCH 1/2 gitweb: fix #patchNN anchors when path_info is enabled
My configuration is as follows:
$feature{'pathinfo'}{'default'} = [1];
<Location /gitweb>
Options ExecCGI
SetHandler cgi-script
</Location>
GITWEB_{JS,CSS,LOGO,...} all start with gitweb-static/
gitweb.cgi renamed to /var/www/html/gitweb
This gives me simple, easy-to-read URLs that look like:
http://HOST/gitweb/myproject.git/commitdiff/0faa4a6ef921d8a233f30d66f9a3e1b24e8ec906
The problem is that in this configuration, PATH_INFO is used to set the base URL:
<base href="http://HOST/gitweb">
This breaks the "patch" anchor links seen on the commitdiff pages, because they are computed relative to the base URL:
My solution is to add an "anchor" parameter to href(), so that the full path is included in the patchNN links.
--- gitweb/gitweb.perl | 31 +++++++++++++++++++++++++------ 1 files changed, 25 insertions(+), 6 deletions(-)
gitweb/gitweb.perl +25 -6
@@ -1199,6 +1199,7 @@ if (defined caller) { # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base) # -replay => 1 - start from a current view (replay with modifications) # -path_info => 0|1 - don't use/use path_info URL (if possible) +# -anchor - add #ANCHOR to end of URL sub href { my %params = @_; # default is to use -absolute url() i.e. $my_uri
@@ -1314,6 +1315,10 @@ sub href { # final transformation: trailing spaces must be escaped (URI-encoded) $href =~ s/(\s+)$/CGI::escape($1)/e; + if (defined($params{'anchor'})) { + $href .= "#".esc_param($params{'anchor'}); + } + return $href; }
@@ -4334,8 +4339,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print "<td class=\"link\">" . - $cgi->a({-href => "#patch$patchno"}, "patch") . + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch") . " | " . "</td>\n"; }
@@ -4432,7 +4439,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print $cgi->a({-href => "#patch$patchno"}, "patch"); + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch"); print " | "; } print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
@@ -4452,7 +4462,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print $cgi->a({-href => "#patch$patchno"}, "patch"); + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch"); print " | "; } print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
@@ -4494,7 +4507,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print $cgi->a({-href => "#patch$patchno"}, "patch") . + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch") . " | "; } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) { # "commit" view and modified file (not onlu mode changed)
@@ -4539,7 +4555,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print $cgi->a({-href => "#patch$patchno"}, "patch") . + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch") . " | "; } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) { # "commit" view and modified file (not only pure rename or copy)
Signature · 1 line
1.7.4.1
PATCH 2/2 gitweb: introduce localtime feature
With this feature enabled, all timestamps are shown in the machine's local timezone instead of GMT.
--- gitweb/gitweb.perl | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-)
gitweb/gitweb.perl +13 -1
@@ -504,6 +504,12 @@ our %feature = ( 'sub' => sub { feature_bool('remote_heads', @_) }, 'override' => 0, 'default' => [0]}, + + # Use localtime rather than GMT for all timestamps. Disabled + # by default. Project specific override is not supported. + 'localtime' => { + 'override' => 0, + 'default' => [0]}, ); sub gitweb_get_feature {
@@ -2927,6 +2933,12 @@ sub parse_date { $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", 1900+$year, $mon+1, $mday, $hour, $min, $sec, $tz); + + if (gitweb_check_feature('localtime')) { + $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d $tz", + $days[$wday], $mday, $months[$mon], + 1900+$year, $hour ,$min, $sec; + } return %date; }
@@ -3989,7 +4001,7 @@ sub git_print_authorship_rows { "</td></tr>\n" . "<tr>" . "<td></td><td> $wd{'rfc2822'}"; - print_local_time(%wd); + print_local_time(%wd) if !gitweb_check_feature('localtime'); print "</td>" . "</tr>\n"; }
Signature · 1 line
1.7.4.1
Re: [PATCH 2/2] gitweb: introduce localtime feature
Quoted 2 lines · Kevin Cernekee
With this feature enabled, all timestamps are shown in the machine's local timezone instead of GMT.
This does not describe why would one want such way of displaying timestamps, and which views would be affected.
BTW. should it be timezone of web server (machine where gitweb is run), or local time of author / committer / tagger as described in the timezone part of git timestamp?
Quoted 10 lines
Signed-off-by Kevin Cernekee <[email protected]>--- gitweb/gitweb.perl | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-)gitweb/gitweb.perl +0
-0
gitweb/gitweb.perl our %feature = ( · line 512
Quoted 9 lines
gitweb/gitweb.perl +6
-0@@ -504,6 +504,12 @@ our %feature = ( 'sub' => sub { feature_bool('remote_heads', @_) }, 'override' => 0, 'default' => [0]}, + + # Use localtime rather than GMT for all timestamps. Disabled + # by default. Project specific override is not supported. + 'localtime' => { + 'override' => 0, + 'default' => [0]},
Why project specific override is not supported? I think it might make sense to enable this feature on project-by-project basis; some projects might be dispersed geographically, some might not.
It is not as if this feature affect only non-project views, or doesn't make sense on less that site-wide basis, like other nonoverridable features.
gitweb/gitweb.perl sub parse_date { · line 2941
Quoted 13 lines
gitweb/gitweb.perl +6
-0); sub gitweb_get_feature {@@ -2927,6 +2933,12 @@ sub parse_date { $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", 1900+$year, $mon+1, $mday, $hour, $min, $sec, $tz); + + if (gitweb_check_feature('localtime')) { + $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d $tz", + $days[$wday], $mday, $months[$mon], + 1900+$year, $hour ,$min, $sec; + }
Is it still an RFC 2822 conformant date? If it is not, then above change is invalid, and we have to implement this feature in different way.
gitweb/gitweb.perl sub git_print_authorship_rows { · line 4004
Quoted 9 lines
gitweb/gitweb.perl +1
-1return %date; }@@ -3989,7 +4001,7 @@ sub git_print_authorship_rows { "</td></tr>\n" . "<tr>" . "<td></td><td> $wd{'rfc2822'}"; - print_local_time(%wd); + print_local_time(%wd) if !gitweb_check_feature('localtime');
Hmmm... I wonder if it wouldn't be better to print both times (perhaps reversed) in this case...
Quoted 6 lines
gitweb/gitweb.perl +0
-0print "</td>" . "</tr>\n"; }Signature · 1 line
1.7.4.1
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git
Re: [PATCH 2/2] gitweb: introduce localtime feature
Quoted 11 lines · Jakub Narebski
Quoted 2 lines · Kevin Cernekee
With this feature enabled, all timestamps are shown in the machine's local timezone instead of GMT.
This does not describe why would one want such way of displaying timestamps, and which views would be affected.
BTW. should it be timezone of web server (machine where gitweb is run), or local time of author / committer / tagger as described in the timezone part of git timestamp?
Both are sensible choices; another plausible choice that normal people would want to see is to use the zone of whoever is requesting the page (note that this would affect the caching layer).
Quoted 7 lines
gitweb/gitweb.perl our %feature = ( · line 512
Quoted 5 lines
gitweb/gitweb.perl +5
-0+ # Use localtime rather than GMT for all timestamps. Disabled + # by default. Project specific override is not supported. + 'localtime' => { + 'override' => 0, + 'default' => [0]},Why project specific override is not supported?
Good question.
Re: [PATCH 2/2] gitweb: introduce localtime feature
In reply to Jakub NarebskiOn Thu, Mar 17, 2011 at 4:01 AM, Jakub Narebski <[email protected]> wrote:
Jakub,
Thanks for all of your constructive feedback. I have taken your suggestions into account and posted an updated series of patches.
Quoted 6 lines
This does not describe why would one want such way of displaying timestamps, and which views would be affected.
BTW. should it be timezone of web server (machine where gitweb is run), or local time of author / committer / tagger as described in the timezone part of git timestamp?
The case I am currently trying to improve is the one in which all developers are at a single site.
In the open source world it is common to have developers scattered all over the globe, so some of them will inevitably have to perform timezone conversions.
But Git is becoming a popular tool in the private sector and it is common to have most/all contributors based in a single office. In the latter case, it is helpful to display the local timezone instead of GMT. This also helps make the data more readable by program managers and other non-developers who have an interest in tracking the project.
Quoted 3 lines
Why project specific override is not supported? I think it might make sense to enable this feature on project-by-project basis; some projects might be dispersed geographically, some might not.
Mostly ease of testing. I did not need it for any of my projects.
It turned out to be a simple change, and it is in v2. The cases I tested were:
default 0
default 1
override 1, project unset
override 1, project 0
override 1, project 1
Quoted 3 lines
Is it still an RFC 2822 conformant date? If it is not, then above change is invalid, and we have to implement this feature in different way.
I believe it is still valid.
Original date: Thu, 17 Mar 2011 02:11:05 +0000
New date: Wed, 16 Mar 2011 19:11:05 -0700
Sample date from RFC 2822 Appendix A: Fri, 21 Nov 1997 09:55:06 -0600
Quoted 2 lines
Hmmm... I wonder if it wouldn't be better to print both times (perhaps reversed) in this case...
I have submitted a third patch which does this.
Re: [PATCH 2/2] gitweb: introduce localtime feature
Quoted 16 lines · Kevin Cernekee
Quoted 6 lines · Jakub Narebski
This does not describe why would one want such way of displaying timestamps, and which views would be affected.
BTW. should it be timezone of web server (machine where gitweb is run), or local time of author / committer / tagger as described in the timezone part of git timestamp?
The case I am currently trying to improve is the one in which all developers are at a single site.
In the open source world it is common to have developers scattered all over the globe, so some of them will inevitably have to perform timezone conversions.
Quoted 5 lines
But Git is becoming a popular tool in the private sector and it is common to have most/all contributors based in a single office. In the latter case, it is helpful to display the local timezone instead of GMT. This also helps make the data more readable by program managers and other non-developers who have an interest in tracking the project.
Such explanation should in my opinion be present in the proposed commit message. Good commit message should explain not only what the change is intent to do (to catch when implementation and intent differs), but also whys behind the change (to find whether commit is worth having).
The above nicely explains why and when such feature would be useful,
Quoted 5 lines
Quoted 3 lines
Why project specific override is not supported? I think it might make sense to enable this feature on project-by-project basis; some projects might be dispersed geographically, some might not.
Mostly ease of testing. I did not need it for any of my projects.
You mean that for your instance of gitweb all projects were single office (not dispersed geographically), so for you enabling it site-wide was enough, isn't it?
Quoted 1 line
It turned out to be a simple change, and it is in v2.
Thanks.
Quoted 7 lines
The cases I tested were:
default 0
default 1
override 1, project unset
override 1, project 0
override 1, project 1
Well, I think the non-overridden / overridden feature we have tested quite well. BTW did you add test for 'localtime' feature to t9500 test? Perhaps it is not strictly necessary, though...
Quoted 9 lines
Quoted 3 lines
Is it still an RFC 2822 conformant date? If it is not, then above change is invalid, and we have to implement this feature in different way.
I believe it is still valid.
Original date: Thu, 17 Mar 2011 02:11:05 +0000
New date: Wed, 16 Mar 2011 19:11:05 -0700
Sample date from RFC 2822 Appendix A: Fri, 21 Nov 1997 09:55:06 -0600
Thanks.
Quoted 4 lines
Quoted 2 lines
Hmmm... I wonder if it wouldn't be better to print both times (perhaps reversed) in this case...
I have submitted a third patch which does this.
Note that we print localtime (time in author / committer / tagger timezone) to be able to mark given time as "atnight", so one can easily see commits and tags which needs more careful review because they were made 4 AM or something.
If you reverse the direction you still should make sure that "atnight" styling applies to localtime.
Signature · 2 lines
Jakub Narebski Poland
Quoted 1 line · Kevin Cernekee
My configuration is as follows:
Very minor issue: Documentation/SubmittingPatches states the following:
- describe changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour.
I think this also means trying to avoid "My configuration..." and "My solution..." etc. in commit message. But this is just a side issue, not worth worrying over in my opinion; perhaps something to think about in the future.
Quoted 1 line
$feature{'pathinfo'}{'default'} = [1];
[...]
Quoted 9 lines
The problem is that in this configuration,
PATH_INFOis used to set the base URL:<base href="http://git.example.com/gitweb.cgi">
This breaks the "patch" anchor links seen on the commitdiff pages, because they are computed relative to the base URL:
I think that the above configuration is enough to trigger bug / errorneous behavior that you describe, isn't it? It is better to try to find minimal way to reproduce a bug when describing it.
I guess that
Quoted 5 lines
<Location /gitweb>
Options ExecCGI
SetHandler cgi-script
</Location>
Quoted 7 lines
GITWEB_{JS,CSS,LOGO,...} all start with gitweb-static/
gitweb.cgi renamed to
/var/www/html/gitwebThis gives me simple, easy-to-read URLs that look like:
http://HOST/gitweb/myproject.git/commitdiff/0faa4a6ef921d8a233f30d66f9a3e1b24e8ec906
is not strictly necessary to trigger a bug.
Quoted 3 lines
My solution is to add an "anchor" parameter to
href(), so that the full path is included in the patchNN links.
This is a very good idea. Thank you very much for sending this patch, and contributing to gitweb.
Its implemetation could be though improved a bit; see below.
Quoted 11 lines
Signed-off-by Kevin Cernekee <[email protected]>--- gitweb/gitweb.perl | 31 +++++++++++++++++++++++++------ 1 files changed, 25 insertions(+), 6 deletions(-)gitweb/gitweb.perl +0
-0
gitweb/gitweb.perl if (defined caller) { · line 1202
Quoted 4 lines
gitweb/gitweb.perl +1
-0@@ -1199,6 +1199,7 @@ if (defined caller) { # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base) # -replay => 1 - start from a current view (replay with modifications) # -path_info => 0|1 - don't use/use path_info URL (if possible) +# -anchor - add #ANCHOR to end of URL
Shouldn't it be:
+# -anchor => ANCHOR - add #ANCHOR to end of URL
gitweb/gitweb.perl sub href { · line 1323
Quoted 13 lines
gitweb/gitweb.perl +4
-0sub href { my %params = @_; # default is to use -absolute url() i.e. $my_uri@@ -1314,6 +1315,10 @@ sub href { # final transformation: trailing spaces must be escaped (URI-encoded) $href =~ s/(\s+)$/CGI::escape($1)/e; + if (defined($params{'anchor'})) { + $href .= "#".esc_param($params{'anchor'}); + } + return $href; }
Here you have slight mismatch between description, which uses '-anchor', and code, which uses 'anchor'.
gitweb/gitweb.perl sub git_difftree_body { · line 4345
Quoted 11 lines
gitweb/gitweb.perl +4
-2@@ -4334,8 +4339,10 @@ sub git_difftree_body { if ($action eq 'commitdiff') { # link to patch $patchno++; - print "<td class=\"link\">" . - $cgi->a({-href => "#patch$patchno"}, "patch") . + print $cgi->a({-href => + href(action=>"commitdiff", + hash=>$hash, anchor=>"patch$patchno")}, + "patch") .
It would be better (less error prone) and easier to use '-replay' option to href(), i.e. write
gitweb/gitweb.perl sub git_difftree_body { · line 4347
Quoted 2 lines
gitweb/gitweb.perl +2
-0+ print $cgi->a({-href => href(-replay=>1, -anchor=>"patch$patchno")}, + "patch") .
or even make it so 'href(-anchor=>"ANCHOR")' implies '-replay => 1'. The href() part of patch would then look something like this:
@@ -1199,6 +1199,7 @@ if (defined caller) {
# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
# -replay => 1 - start from a current view (replay with modifications)
# -path_info => 0|1 - don't use/use path_info URL (if possible)
+# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
sub href {
my %params = @_;
# default is to use -absolute url() i.e. $my_uri
@@ -1310,6 +1310,7 @@ sub href {
$params{'project'} = $project unless exists $params{'project'}; - if ($params{-replay}) {
+ if ($params{-replay} ||
+ ($params{-anchor} && keys %params == 1)) {
while (my ($name, $symbol) = each %cgi_param_mapping) {
if (!exists $params{$name}) {
$params{$name} = $input_params{$name};
@@ -1314,6 +1315,10 @@ sub href {
# final transformation: trailing spaces must be escaped (URI-encoded) $href =~ s/(\s+)$/CGI::escape($1)/e;
+ if (defined($params{'anchor'})) {
+ $href .= "#".esc_param($params{'anchor'});
+ }
+
return $href;
}Do you want to resend patch with those corrections yourself, or should I do this?
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git
Quoted 7 lines · Jakub Narebski
It would be better (less error prone) and easier to use '-replay' option to
href(), i.e. writeReview
Quoted 2 lines
(quoted diff) +2
-0+ print $cgi->a({-href => href(-replay=>1, -anchor=>"patch$patchno")}, + "patch") .or even make it so '
href(-anchor=>"ANCHOR")' implies '-replay => 1'.
I don't see why "or even" is an improvement, given the following implementation.
Quoted 10 lines
@@ -1310,6 +1310,7 @@ sub href { $params{'project'} = $project unless exists $params{'project'};- if ($params{-replay}) {
+ if ($params{-replay} ||
+ ($params{-anchor} && keys %params == 1)) {
while (my ($name, $symbol) = each %cgi_param_mapping) {
if (!exists $params{$name}) {
$params{$name} = $input_params{$name};
I don't share your intuition that "anchor" is so special that this part will not grow into a long chain of "(I want an implicit replay too) ||".
Implicitly enabling it in certain obvious cases is perfectly fine, but the logic to do so should be in a separate place. Wouldn't it better to have a separate code that sets 'replay' under this and that condition so that other people can later add to it at the very beginning of "sub href"?
Unless we do so, if there are other places that need to change the behaviour based on 'replay', they need to duplicate the "implicit" logic.
Quoted 5 lines · Junio C Hamano
Quoted 3 lines · Jakub Narebski
It would be better (less error prone) and easier to use '-replay' option to
href(), i.e. write
Quoted 7 lines
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ print $cgi->a({-href => href(-replay=>1, -anchor=>"patch$patchno")}, + "patch") .or even make it so '
href(-anchor=>"ANCHOR")' implies '-replay => 1'.I don't see why "or even" is an improvement, given the following implementation.
Well,
-href => href(-anchor=>"patch$patchno")
is closer in spirit to
-href => "#patch$patchno"
that is currently used, and does not work with path_info.
Quoted 14 lines
Quoted 10 lines
@@ -1310,6 +1310,7 @@ sub href { $params{'project'} = $project unless exists $params{'project'};- if ($params{-replay}) {
+ if ($params{-replay} ||
+ ($params{-anchor} && keys %params == 1)) {
while (my ($name, $symbol) = each %cgi_param_mapping) {
if (!exists $params{$name}) {
$params{$name} = $input_params{$name};I don't share your intuition that "anchor" is so special that this part will not grow into a long chain of "(I want an implicit replay too) ||".
Quoted 7 lines
Implicitly enabling it in certain obvious cases is perfectly fine, but the logic to do so should be in a separate place. Wouldn't it better to have a separate code that sets 'replay' under this and that condition so that other people can later add to it at the very beginning of "sub href"?
Unless we do so, if there are other places that need to change the behaviour based on 'replay', they need to duplicate the "implicit" logic.
So you would prefer to have something like this:
# implicit -replay
if (keys %params == 1 && $params{-anchor}) {
# href(-anchor=>"ANCHOR") works like "#ANCHOR",
# correctly if base href is set (for path_info URLs)
$params{-replay} = 1;
}set above 'if ($params{-replay}) {'?
Signature · 2 lines
Jakub Narebski Poland
Quoted 7 lines · Jakub Narebski
Quoted 4 lines
Quoted 1 line
or even make it so '
href(-anchor=>"ANCHOR")' implies '-replay => 1'.I don't see why "or even" is an improvement, given the following implementation.
Well,
Quoted 7 lines
-href =>
href(-anchor=>"patch$patchno")is closer in spirit to
-href => "#patch$patchno"
that is currently used, and does not work with
path_info.
I questioned "implies '-replay => 1'" part, not the use of href(...) part.