Skip to content

Commit

Permalink
gitweb: fix #patchNN anchors when path_info is enabled
Browse files Browse the repository at this point in the history
When $feature{'pathinfo'} is used, gitweb script sets the base URL to
itself, so that relative links to static files work correctly.  It
does it by adding something like below to HTML head:

  <base href="http://HOST/gitweb.cgi">

This breaks the "patch" anchor links seen on the commitdiff pages,
because these links, being relative (<a href="#patch1">), are resolved
(computed) relative to the base URL and not relative to current URL,
i.e. as:

  http://HOST/gitweb.cgi#patch1

Instead, they should look like this:

  http://HOST/gitweb.cgi/myproject.git/commitdiff/35a9811ef9d68eae9afd76bede121da4f89b448c#patch1

Add an "-anchor" parameter to href(), and use href(-anchor=>"patch1")
to generate "patch" anchor links, so that the full path is included in
the patch link.

While at it, convert

  print "foo";
  print "bar";

to

  print "foo" .
        "bar";

in the neighborhood of changes.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Kevin Cernekee authored and Junio C Hamano committed Mar 18, 2011
1 parent e2a57aa commit 5e96a84
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions gitweb/gitweb.perl
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,15 @@ sub run {
# -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
my $href = $params{-full} ? $my_url : $my_uri;

# implicit -replay, must be first of implicit params
$params{-replay} = 1 if (keys %params == 1 && $params{-anchor});

$params{'project'} = $project unless exists $params{'project'};

if ($params{-replay}) {
Expand Down Expand Up @@ -1314,6 +1318,10 @@ sub href {
# final transformation: trailing spaces must be escaped (URI-encoded)
$href =~ s/(\s+)$/CGI::escape($1)/e;

if ($params{-anchor}) {
$href .= "#".esc_param($params{-anchor});
}

return $href;
}

Expand Down Expand Up @@ -4335,7 +4343,8 @@ sub git_difftree_body {
# link to patch
$patchno++;
print "<td class=\"link\">" .
$cgi->a({-href => "#patch$patchno"}, "patch") .
$cgi->a({-href => href(-anchor=>"patch$patchno")},
"patch") .
" | " .
"</td>\n";
}
Expand Down Expand Up @@ -4432,8 +4441,9 @@ sub git_difftree_body {
if ($action eq 'commitdiff') {
# link to patch
$patchno++;
print $cgi->a({-href => "#patch$patchno"}, "patch");
print " | ";
print $cgi->a({-href => href(-anchor=>"patch$patchno")},
"patch") .
" | ";
}
print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
hash_base=>$hash, file_name=>$diff->{'file'})},
Expand All @@ -4452,8 +4462,9 @@ sub git_difftree_body {
if ($action eq 'commitdiff') {
# link to patch
$patchno++;
print $cgi->a({-href => "#patch$patchno"}, "patch");
print " | ";
print $cgi->a({-href => href(-anchor=>"patch$patchno")},
"patch") .
" | ";
}
print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
hash_base=>$parent, file_name=>$diff->{'file'})},
Expand Down Expand Up @@ -4494,7 +4505,8 @@ sub git_difftree_body {
if ($action eq 'commitdiff') {
# link to patch
$patchno++;
print $cgi->a({-href => "#patch$patchno"}, "patch") .
print $cgi->a({-href => href(-anchor=>"patch$patchno")},
"patch") .
" | ";
} elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
# "commit" view and modified file (not onlu mode changed)
Expand Down Expand Up @@ -4539,7 +4551,8 @@ sub git_difftree_body {
if ($action eq 'commitdiff') {
# link to patch
$patchno++;
print $cgi->a({-href => "#patch$patchno"}, "patch") .
print $cgi->a({-href => href(-anchor=>"patch$patchno")},
"patch") .
" | ";
} elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
# "commit" view and modified file (not only pure rename or copy)
Expand Down

0 comments on commit 5e96a84

Please sign in to comment.