git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] gitweb: Harden "grep" search against filenames with ':'

From
Jakub Narebski <jnareb@gmail.com>
Date
Jan 5, 2012, 20:32 UTC
Message-ID
<201201052132.56581.jnareb@gmail.com>
In-Reply-To
<201201052126.49087.jnareb@gmail.com>

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>
---
This is what I did after fixing previous issue, after looking at current
code.  Hopefully nobody sane uses filenames with embedded newlines...
  http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
 gitweb/gitweb.perl |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index fa58156..f884dfe 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -5836,7 +5836,7 @@ sub git_search_files {
 	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");
@@ -5858,7 +5858,8 @@ sub git_search_files {
 			$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";
-- 
1.7.6
Previous: Jakub NarebskiNext: Thomas Perl
Message 5 of 7 in “[BUG] gitweb generates wrong links in grep search results (git_search_files)”
  1. Thomas PerlJan 2, 2012
  2. Junio C HamanoJan 4, 2012
  3. Jakub NarębskiJan 4, 2012
  4. 1/2 gitweb: Fix file links in "grep" searchJakub Narebski, Jan 5, 2012
  5. 2/2 gitweb: Harden "grep" search against filenames with ':'Jakub Narebski, Jan 5, 2012
  6. Thomas PerlJan 13, 2012
  7. Junio C HamanoJan 13, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.