]> Lady’s Gitweb - Gitweb/commitdiff
gitweb: use highlight's shebang detection
authorIan Kelling <redacted>
Sat, 24 Sep 2016 22:32:58 +0000 (15:32 -0700)
committerLady <redacted>
Mon, 6 Apr 2026 04:51:32 +0000 (00:51 -0400)
The "highlight" binary can, in some cases, determine the language type
by the means of file contents, for example the shebang in the first line
for some scripting languages.  Make use of this autodetection for files
which syntax is not known by gitweb.  In that case, pass the blob
contents to "highlight --force"; the parameter is needed to make it
always generate HTML output (which includes HTML-escaping).

Although we now run highlight on files which do not end up highlighted,
performance is virtually unaffected because when we call highlight, it
is used for escaping HTML.  In the case that highlight is used, gitweb
calls sanitize() instead of esc_html(), and the latter is significantly
slower (it does more, being roughly a superset of sanitize()).  Simple
benchmark comparing performance of 'blob' view of files without syntax
highlighting in gitweb before and after this change indicates ±1%
difference in request time for all file types.  Benchmark was performed
on local instance on Debian, using Apache/2.4.23 web server and CGI.

Document the feature and improve syntax highlight documentation, add
test to ensure gitweb doesn't crash when language detection is used.

Signed-off-by: Ian Kelling <redacted>
Acked-by: Jakub Narębski <redacted>
Signed-off-by: Junio C Hamano <redacted>
gitweb.perl

index 08115d2eeea5e1d611ca02a2ccc1cce04f5ce7c01c500af9c4d013f7c1902d59..ec4547b35b712aedecea25d5197cd3de985bdaa0a84201bb19200fd281b33621 100755 (executable)
@@ -3932,15 +3932,16 @@ sub guess_file_syntax {
 # or return original FD if no highlighting
 sub run_highlighter {
        my ($fd, $highlight, $syntax) = @_;
 # or return original FD if no highlighting
 sub run_highlighter {
        my ($fd, $highlight, $syntax) = @_;
-       return $fd unless ($highlight && defined $syntax);
+       return $fd unless ($highlight);
 
        close $fd;
 
        close $fd;
+       my $syntax_arg = (defined $syntax) ? "--syntax $syntax" : "--force";
        open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
                  quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
                    '$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
                    '--', "-fe=$fallback_encoding")." | ".
                  quote_command($highlight_bin).
        open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
                  quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
                    '$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
                    '--', "-fe=$fallback_encoding")." | ".
                  quote_command($highlight_bin).
-                 " --replace-tabs=8 --fragment --syntax $syntax |"
+                 " --replace-tabs=8 --fragment $syntax_arg |"
                or die_error(500, "Couldn't open file or run syntax highlighter");
        return $fd;
 }
                or die_error(500, "Couldn't open file or run syntax highlighter");
        return $fd;
 }
@@ -7064,8 +7065,7 @@ sub git_blob {
 
        my $highlight = gitweb_check_feature('highlight');
        my $syntax = guess_file_syntax($highlight, $file_name);
 
        my $highlight = gitweb_check_feature('highlight');
        my $syntax = guess_file_syntax($highlight, $file_name);
-       $fd = run_highlighter($fd, $highlight, $syntax)
-               if $syntax;
+       $fd = run_highlighter($fd, $highlight, $syntax);
 
        git_header_html(undef, $expires);
        my $formats_nav = '';
 
        git_header_html(undef, $expires);
        my $formats_nav = '';
@@ -7118,7 +7118,7 @@ sub git_blob {
                        $line = untabify($line);
                        printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
                               $nr, esc_attr(href(-replay => 1)), $nr, $nr,
                        $line = untabify($line);
                        printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
                               $nr, esc_attr(href(-replay => 1)), $nr, $nr,
-                              $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
+                              $highlight ? sanitize($line) : esc_html($line, -nbsp=>1);
                }
        }
        close $fd
                }
        }
        close $fd
This page took 0.481005 seconds and 4 git commands to generate.