gitweb: Harden "grep" search against filenames with ':'

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 <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Jan 5, 2012 at 21:32 UTC 8e09fd1a1e5ea8eaec960d47be51bde85df8870e
1 file changed +3 -2
gitweb/gitweb.perl
+3 -2
@@ -5699,7 +5699,7 @@ sub git_search_files {
5699 my %co = @_;
5700
5701 local $/ = "\n";
5702 - open my $fd, "-|", git_cmd(), 'grep', '-n',
5702 + open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
5703 $search_use_regexp ? ('-E', '-i') : '-F',
5704 $searchtext, $co{'tree'}
5705 or die_error(500, "Open git-grep failed");
@@ -5721,7 +5721,8 @@ sub git_search_files {
5721 $file = $1;
5722 $binary = 1;
5723 } else {
5724 - (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5724 + ($file, $lno, $ltext) = split(/\0/, $line, 3);
5725 + $file =~ s/^$co{'tree'}://;
5726 }
5727 if ($file ne $lastfile) {
5728 $lastfile and print "</td></tr>\n";