builtin-grep: workaround for non GNU grep.
Of course, it still ignores the fact that not all grep's support some of the flags like -F/-L/-A/-C etc, but for those cases, the external grep itself will happily just say "unrecognized option -F" or similar. So with this change, "git grep" should handle all the flags the native grep handles, which is really quite fine. We don't _need_ to expose anything more, and if you do want our extensions, you can get them with "--uncached" and an up-to-date index. No configuration necessary, and we automatically take advantage of any native grep we have, if possible. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
May 17, 2006 at 11:12 UTC
bbb66c60613fe89c58d7c97a92e067add7056f4d
1 file changed
+20
-3
builtin-grep.c
+20
-3
@@ -453,7 +453,6 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
453
454
len = nr = 0;
455
push_arg("grep");
456
- push_arg("-H");
456
if (opt->fixed)
457
push_arg("-F");
458
if (opt->linenum)
@@ -503,17 +502,35 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
502
push_arg("-e");
503
push_arg(p->pattern);
504
}
506
- push_arg("--");
505
+
506
+ /*
507
+ * To make sure we get the header printed out when we want it,
508
+ * add /dev/null to the paths to grep. This is unnecessary
509
+ * (and wrong) with "-l" or "-L", which always print out the
510
+ * name anyway.
511
+ *
512
+ * GNU grep has "-H", but this is portable.
513
+ */
514
+ if (!opt->name_only && !opt->unmatch_name_only)
515
+ push_arg("/dev/null");
516
517
hit = 0;
518
argc = nr;
519
for (i = 0; i < active_nr; i++) {
520
struct cache_entry *ce = active_cache[i];
521
+ const char *name;
522
if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
523
continue;
524
if (!pathspec_matches(paths, ce->name))
525
continue;
516
- argv[argc++] = ce->name;
526
+ name = ce->name;
527
+ if (name[0] == '-') {
528
+ int len = ce_namelen(ce);
529
+ name = xmalloc(len + 3);
530
+ memcpy(name, "./", 2);
531
+ memcpy(name + 2, ce->name, len + 1);
532
+ }
533
+ argv[argc++] = name;
534
if (argc < MAXARGS)
535
continue;
536
hit += exec_grep(argc, argv);