]> Lady’s Gitweb - Gitweb/commitdiff
gitweb: Harden "grep" search against filenames with ':'
authorJakub Narebski <redacted>
Thu, 5 Jan 2012 20:32:56 +0000 (21:32 +0100)
committerLady <redacted>
Mon, 6 Apr 2026 04:51:31 +0000 (00:51 -0400)
Run "git grep" in "grep" search with '-z' option, to be able to parse
response also for files with filename containing ':' character.  The
':' character is otherwise (without '-z') used to separate filename
from line number and from matched line.

Note that this does not protect files with filename containing
embedded newline.  This would be hard but doable for text files, and
harder or even currently impossible with binary files: git does not
quote filename in

  "Binary file <foo> matches"

message, but new `--break` and/or `--header` options to git-grep could
help here.

Signed-off-by: Jakub Narebski <redacted>
Signed-off-by: Junio C Hamano <redacted>
gitweb.perl

index 1cfb8ae525e97f0a86c13ee2c079b967b400b2bb7f5ed441f0fb6a1363ee79fb..4ba6554fbdb699ec66050b4682ce90111ad0480c712d607c7ab7df4d2becda26 100755 (executable)
@@ -5837,7 +5837,7 @@ sub git_search_files {
        my %co = @_;
 
        local $/ = "\n";
        my %co = @_;
 
        local $/ = "\n";
-       open my $fd, "-|", git_cmd(), 'grep', '-n',
+       open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
                $search_use_regexp ? ('-E', '-i') : '-F',
                $searchtext, $co{'tree'}
                        or die_error(500, "Open git-grep failed");
                $search_use_regexp ? ('-E', '-i') : '-F',
                $searchtext, $co{'tree'}
                        or die_error(500, "Open git-grep failed");
@@ -5859,7 +5859,8 @@ sub git_search_files {
                        $file = $1;
                        $binary = 1;
                } else {
                        $file = $1;
                        $binary = 1;
                } else {
-                       (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
+                       ($file, $lno, $ltext) = split(/\0/, $line, 3);
+                       $file =~ s/^$co{'tree'}://;
                }
                if ($file ne $lastfile) {
                        $lastfile and print "</td></tr>\n";
                }
                if ($file ne $lastfile) {
                        $lastfile and print "</td></tr>\n";
This page took 0.365044 seconds and 4 git commands to generate.