From patchwork Sat Jul 11 20:39:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Girstmair X-Patchwork-Id: 11657977 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B8FD9722 for ; Sat, 11 Jul 2020 21:02:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A0705206A5 for ; Sat, 11 Jul 2020 21:02:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727930AbgGKVCP (ORCPT ); Sat, 11 Jul 2020 17:02:15 -0400 Received: from smtprelay04.ispgateway.de ([80.67.31.32]:58039 "EHLO smtprelay04.ispgateway.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726281AbgGKVCP (ORCPT ); Sat, 11 Jul 2020 17:02:15 -0400 X-Greylist: delayed 440 seconds by postgrey-1.27 at vger.kernel.org; Sat, 11 Jul 2020 17:02:14 EDT Received: from [178.112.81.104] (helo=localhost.localdomain) by smtprelay04.ispgateway.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1juMWK-0004xe-7H; Sat, 11 Jul 2020 22:54:52 +0200 From: Tobias Girstmair To: git@vger.kernel.org Cc: Tobias Girstmair Subject: [PATCH] gitweb: Replace tag with full URLs (when using PATH_INFO) Date: Sat, 11 Jul 2020 22:39:48 +0200 Message-Id: <20200711203947.23520-1-tobi@isticktoit.net> X-Mailer: git-send-email 2.21.3 MIME-Version: 1.0 X-Df-Sender: dC5naXJzdG1haXJAaXN0aWNrdG9pdC5uZXQ= Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org using a base tag has the side-effect of not just changing the few URLs of gitweb's static resources, but all other relative links (e.g. those in a README.html), too. Signed-off-by: Tobias Girstmair --- My exact use case is this: Many of my repositories have README.{rst,md} files with relative links to more md/rst files. I'm generating a README.html from that file with a post-update hook, and rewrite links, so they display the file. Some cloned repos' READMEs even have images in them, whose links I'm fixing up as well. Due to the precence of the tag, I can't just prepend "blob_plain/HEAD:" to the URLs, but have to recreate the full URL, including the last few directories of $GIT_DIR. That's annoying to keep portable (move a repo from e.g. /srv/git/foo.git to /srv/git/**public**/foo.git and the URL must be changed in the hook, and the hook re-executed). Looking forward to your feedback, tobias gitweb/gitweb.perl | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 0959a78..049bb64 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -2232,7 +2232,7 @@ sub git_get_avatar { return $pre_white . "" . $post_white; } else { @@ -2674,6 +2674,13 @@ sub get_feed_info { return %res; } +# the stylesheet, favicon etc urls won't work correctly with path_info +# unless we set the appropriate base URL. not using a tag to not +# also change relative URLs inserted by the user. +sub get_base_url { + return $ENV{'PATH_INFO'}? esc_url($base_url)."/" : ""; +} + ## ---------------------------------------------------------------------- ## git utility subroutines, invoking git commands @@ -4099,17 +4106,17 @@ sub print_header_links { # print out each stylesheet that exist, providing backwards capability # for those people who defined $stylesheet in a config file if (defined $stylesheet) { - print ''."\n"; + print ''."\n"; } else { foreach my $stylesheet (@stylesheets) { next unless $stylesheet; - print ''."\n"; + print ''."\n"; } } print_feed_meta() if ($status eq '200 OK'); if (defined $favicon) { - print qq(\n); + print qq(\n); } } @@ -4212,11 +4219,6 @@ sub git_header_html { $title EOF - # the stylesheet, favicon etc urls won't work correctly with path_info - # unless we set the appropriate base URL - if ($ENV{'PATH_INFO'}) { - print "\n"; - } print_header_links($status); if (defined $site_html_head_string) { @@ -4234,7 +4236,7 @@ sub git_header_html { if (defined $logo) { print $cgi->a({-href => esc_url($logo_url), -title => $logo_label}, - $cgi->img({-src => esc_url($logo), + $cgi->img({-src => get_base_url().esc_url($logo), -width => 72, -height => 27, -alt => "git", -class => "logo"})); @@ -4299,7 +4301,7 @@ sub git_footer_html { insert_file($site_footer); } - print qq!\n!; + print qq!\n!; if (defined $action && $action eq 'blame_incremental') { print qq!