@matheusbsbs / matheus-bagliano / commits / bbc09c22b9
Fork 0

grep: rip out support for external grep

We still allow people to pass --[no-]ext-grep on the command line, but the option is ignored. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 12, 2010 at 19:06 UTC bbc09c22b9f7784b1aab71d4876227956e6e8f4f
5 files changed +8 -324
Documentation/config.txt
-8
@@ -645,14 +645,6 @@ color.grep::
645 `never`), never. When set to `true` or `auto`, use color only
646 when the output is written to the terminal. Defaults to `false`.
647
648 -color.grep.external::
649 - The string value of this variable is passed to an external 'grep'
650 - command as a command line option if match highlighting is turned
651 - on. If set to an empty string, no option is passed at all,
652 - turning off coloring for external 'grep' calls; this is the default.
653 - For GNU grep, set it to `--color=always` to highlight matches even
654 - when a pager is used.
655 -
648 color.grep.match::
649 Use customized color for matches. The value of this variable
650 may be specified as in color.branch.<slot>. It is passed using
Makefile
-10
@@ -185,10 +185,6 @@ all::
185 # is a simplified version of the merge sort used in glibc. This is
186 # recommended if Git triggers O(n^2) behavior in your platform's qsort().
187 #
188 -# Define NO_EXTERNAL_GREP if you don't want "git grep" to ever call
189 -# your external grep (e.g., if your system lacks grep, if its grep is
190 -# broken, or spawning external process is slower than built-in grep git has).
191 -#
188 # Define UNRELIABLE_FSTAT if your system's fstat does not return the same
189 # information on a not yet closed file that lstat would return for the same
190 # file after it was closed.
@@ -777,7 +773,6 @@ ifeq ($(uname_S),SunOS)
773 NO_MKDTEMP = YesPlease
774 NO_MKSTEMPS = YesPlease
775 NO_REGEX = YesPlease
780 - NO_EXTERNAL_GREP = YesPlease
776 THREADED_DELTA_SEARCH = YesPlease
777 ifeq ($(uname_R),5.7)
778 NEEDS_RESOLV = YesPlease
@@ -895,7 +890,6 @@ ifeq ($(uname_S),IRIX)
890 # NO_MMAP. If you suspect that your compiler is not affected by this
891 # issue, comment out the NO_MMAP statement.
892 NO_MMAP = YesPlease
898 - NO_EXTERNAL_GREP = UnfortunatelyYes
893 SNPRINTF_RETURNS_BOGUS = YesPlease
894 SHELL_PATH = /usr/gnu/bin/bash
895 NEEDS_LIBGEN = YesPlease
@@ -915,7 +909,6 @@ ifeq ($(uname_S),IRIX64)
909 # NO_MMAP. If you suspect that your compiler is not affected by this
910 # issue, comment out the NO_MMAP statement.
911 NO_MMAP = YesPlease
918 - NO_EXTERNAL_GREP = UnfortunatelyYes
912 SNPRINTF_RETURNS_BOGUS = YesPlease
913 SHELL_PATH=/usr/gnu/bin/bash
914 NEEDS_LIBGEN = YesPlease
@@ -1322,9 +1315,6 @@ endif
1315 ifdef DIR_HAS_BSD_GROUP_SEMANTICS
1316 COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS
1317 endif
1325 -ifdef NO_EXTERNAL_GREP
1326 - BASIC_CFLAGS += -DNO_EXTERNAL_GREP
1327 -endif
1318 ifdef UNRELIABLE_FSTAT
1319 BASIC_CFLAGS += -DUNRELIABLE_FSTAT
1320 endif
builtin-grep.c
+5 -302
@@ -15,14 +15,6 @@
15 #include "grep.h"
16 #include "quote.h"
17
18 -#ifndef NO_EXTERNAL_GREP
19 -#ifdef __unix__
20 -#define NO_EXTERNAL_GREP 0
21 -#else
22 -#define NO_EXTERNAL_GREP 1
23 -#endif
24 -#endif
25 -
18 static char const * const grep_usage[] = {
19 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
20 NULL
@@ -42,8 +34,6 @@ static int grep_config(const char *var, const char *value, void *cb)
34 opt->color = git_config_colorbool(var, value, -1);
35 return 0;
36 }
45 - if (!strcmp(var, "color.grep.external"))
46 - return git_config_string(&(opt->color_external), var, value);
37 if (!strcmp(var, "color.grep.match")) {
38 if (!value)
39 return config_error_nonbool(var);
@@ -215,292 +205,12 @@ static int grep_file(struct grep_opt *opt, const char *filename)
205 return i;
206 }
207
218 -#if !NO_EXTERNAL_GREP
219 -static int exec_grep(int argc, const char **argv)
220 -{
221 - pid_t pid;
222 - int status;
223 -
224 - argv[argc] = NULL;
225 - pid = fork();
226 - if (pid < 0)
227 - return pid;
228 - if (!pid) {
229 - execvp("grep", (char **) argv);
230 - exit(255);
231 - }
232 - while (waitpid(pid, &status, 0) < 0) {
233 - if (errno == EINTR)
234 - continue;
235 - return -1;
236 - }
237 - if (WIFEXITED(status)) {
238 - if (!WEXITSTATUS(status))
239 - return 1;
240 - return 0;
241 - }
242 - return -1;
243 -}
244 -
245 -#define MAXARGS 1000
246 -#define ARGBUF 4096
247 -#define push_arg(a) do { \
248 - if (nr < MAXARGS) argv[nr++] = (a); \
249 - else die("maximum number of args exceeded"); \
250 - } while (0)
251 -
252 -/*
253 - * If you send a singleton filename to grep, it does not give
254 - * the name of the file. GNU grep has "-H" but we would want
255 - * that behaviour in a portable way.
256 - *
257 - * So we keep two pathnames in argv buffer unsent to grep in
258 - * the main loop if we need to do more than one grep.
259 - */
260 -static int flush_grep(struct grep_opt *opt,
261 - int argc, int arg0, const char **argv, int *kept)
262 -{
263 - int status;
264 - int count = argc - arg0;
265 - const char *kept_0 = NULL;
266 -
267 - if (count <= 2) {
268 - /*
269 - * Because we keep at least 2 paths in the call from
270 - * the main loop (i.e. kept != NULL), and MAXARGS is
271 - * far greater than 2, this usually is a call to
272 - * conclude the grep. However, the user could attempt
273 - * to overflow the argv buffer by giving too many
274 - * options to leave very small number of real
275 - * arguments even for the call in the main loop.
276 - */
277 - if (kept)
278 - die("insanely many options to grep");
279 -
280 - /*
281 - * If we have two or more paths, we do not have to do
282 - * anything special, but we need to push /dev/null to
283 - * get "-H" behaviour of GNU grep portably but when we
284 - * are not doing "-l" nor "-L" nor "-c".
285 - */
286 - if (count == 1 &&
287 - !opt->name_only &&
288 - !opt->unmatch_name_only &&
289 - !opt->count) {
290 - argv[argc++] = "/dev/null";
291 - argv[argc] = NULL;
292 - }
293 - }
294 -
295 - else if (kept) {
296 - /*
297 - * Called because we found many paths and haven't finished
298 - * iterating over the cache yet. We keep two paths
299 - * for the concluding call. argv[argc-2] and argv[argc-1]
300 - * has the last two paths, so save the first one away,
301 - * replace it with NULL while sending the list to grep,
302 - * and recover them after we are done.
303 - */
304 - *kept = 2;
305 - kept_0 = argv[argc-2];
306 - argv[argc-2] = NULL;
307 - argc -= 2;
308 - }
309 -
310 - if (opt->pre_context || opt->post_context) {
311 - /*
312 - * grep handles hunk marks between files, but we need to
313 - * do that ourselves between multiple calls.
314 - */
315 - if (opt->show_hunk_mark)
316 - write_or_die(1, "--\n", 3);
317 - else
318 - opt->show_hunk_mark = 1;
319 - }
320 -
321 - status = exec_grep(argc, argv);
322 -
323 - if (kept_0) {
324 - /*
325 - * Then recover them. Now the last arg is beyond the
326 - * terminating NULL which is at argc, and the second
327 - * from the last is what we saved away in kept_0
328 - */
329 - argv[arg0++] = kept_0;
330 - argv[arg0] = argv[argc+1];
331 - }
332 - return status;
333 -}
334 -
335 -static void grep_add_color(struct strbuf *sb, const char *escape_seq)
336 -{
337 - size_t orig_len = sb->len;
338 -
339 - while (*escape_seq) {
340 - if (*escape_seq == 'm')
341 - strbuf_addch(sb, ';');
342 - else if (*escape_seq != '\033' && *escape_seq != '[')
343 - strbuf_addch(sb, *escape_seq);
344 - escape_seq++;
345 - }
346 - if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
347 - strbuf_setlen(sb, sb->len - 1);
348 -}
349 -
350 -static int external_grep(struct grep_opt *opt, const char **paths, int cached)
351 -{
352 - int i, nr, argc, hit, len, status;
353 - const char *argv[MAXARGS+1];
354 - char randarg[ARGBUF];
355 - char *argptr = randarg;
356 - struct grep_pat *p;
357 -
358 - if (opt->extended || (opt->relative && opt->prefix_length))
359 - return -1;
360 - len = nr = 0;
361 - push_arg("grep");
362 - if (opt->fixed)
363 - push_arg("-F");
364 - if (opt->linenum)
365 - push_arg("-n");
366 - if (!opt->pathname)
367 - push_arg("-h");
368 - if (opt->regflags & REG_EXTENDED)
369 - push_arg("-E");
370 - if (opt->ignore_case)
371 - push_arg("-i");
372 - if (opt->binary == GREP_BINARY_NOMATCH)
373 - push_arg("-I");
374 - if (opt->word_regexp)
375 - push_arg("-w");
376 - if (opt->name_only)
377 - push_arg("-l");
378 - if (opt->unmatch_name_only)
379 - push_arg("-L");
380 - if (opt->null_following_name)
381 - /* in GNU grep git's "-z" translates to "-Z" */
382 - push_arg("-Z");
383 - if (opt->count)
384 - push_arg("-c");
385 - if (opt->post_context || opt->pre_context) {
386 - if (opt->post_context != opt->pre_context) {
387 - if (opt->pre_context) {
388 - push_arg("-B");
389 - len += snprintf(argptr, sizeof(randarg)-len,
390 - "%u", opt->pre_context) + 1;
391 - if (sizeof(randarg) <= len)
392 - die("maximum length of args exceeded");
393 - push_arg(argptr);
394 - argptr += len;
395 - }
396 - if (opt->post_context) {
397 - push_arg("-A");
398 - len += snprintf(argptr, sizeof(randarg)-len,
399 - "%u", opt->post_context) + 1;
400 - if (sizeof(randarg) <= len)
401 - die("maximum length of args exceeded");
402 - push_arg(argptr);
403 - argptr += len;
404 - }
405 - }
406 - else {
407 - push_arg("-C");
408 - len += snprintf(argptr, sizeof(randarg)-len,
409 - "%u", opt->post_context) + 1;
410 - if (sizeof(randarg) <= len)
411 - die("maximum length of args exceeded");
412 - push_arg(argptr);
413 - argptr += len;
414 - }
415 - }
416 - for (p = opt->pattern_list; p; p = p->next) {
417 - push_arg("-e");
418 - push_arg(p->pattern);
419 - }
420 - if (opt->color) {
421 - struct strbuf sb = STRBUF_INIT;
422 -
423 - grep_add_color(&sb, opt->color_match);
424 - setenv("GREP_COLOR", sb.buf, 1);
425 -
426 - strbuf_reset(&sb);
427 - strbuf_addstr(&sb, "mt=");
428 - grep_add_color(&sb, opt->color_match);
429 - strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
430 - setenv("GREP_COLORS", sb.buf, 1);
431 -
432 - strbuf_release(&sb);
433 -
434 - if (opt->color_external && strlen(opt->color_external) > 0)
435 - push_arg(opt->color_external);
436 - } else {
437 - unsetenv("GREP_COLOR");
438 - unsetenv("GREP_COLORS");
439 - }
440 - unsetenv("GREP_OPTIONS");
441 -
442 - hit = 0;
443 - argc = nr;
444 - for (i = 0; i < active_nr; i++) {
445 - struct cache_entry *ce = active_cache[i];
446 - char *name;
447 - int kept;
448 - if (!S_ISREG(ce->ce_mode))
449 - continue;
450 - if (!pathspec_matches(paths, ce->name, opt->max_depth))
451 - continue;
452 - name = ce->name;
453 - if (name[0] == '-') {
454 - int len = ce_namelen(ce);
455 - name = xmalloc(len + 3);
456 - memcpy(name, "./", 2);
457 - memcpy(name + 2, ce->name, len + 1);
458 - }
459 - argv[argc++] = name;
460 - if (MAXARGS <= argc) {
461 - status = flush_grep(opt, argc, nr, argv, &kept);
462 - if (0 < status)
463 - hit = 1;
464 - argc = nr + kept;
465 - }
466 - if (ce_stage(ce)) {
467 - do {
468 - i++;
469 - } while (i < active_nr &&
470 - !strcmp(ce->name, active_cache[i]->name));
471 - i--; /* compensate for loop control */
472 - }
473 - }
474 - if (argc > nr) {
475 - status = flush_grep(opt, argc, nr, argv, NULL);
476 - if (0 < status)
477 - hit = 1;
478 - }
479 - return hit;
480 -}
481 -#endif
482 -
483 -static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
484 - int external_grep_allowed)
208 +static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
209 {
210 int hit = 0;
211 int nr;
212 read_cache();
213
490 -#if !NO_EXTERNAL_GREP
491 - /*
492 - * Use the external "grep" command for the case where
493 - * we grep through the checked-out files. It tends to
494 - * be a lot more optimized
495 - */
496 - if (!cached && external_grep_allowed) {
497 - hit = external_grep(opt, paths, cached);
498 - if (hit >= 0)
499 - return hit;
500 - hit = 0;
501 - }
502 -#endif
503 -
214 for (nr = 0; nr < active_nr; nr++) {
215 struct cache_entry *ce = active_cache[nr];
216 if (!S_ISREG(ce->ce_mode))
@@ -697,8 +407,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
407 {
408 int hit = 0;
409 int cached = 0;
700 - int external_grep_allowed = 1;
410 int seen_dashdash = 0;
411 + int external_grep_allowed__ignored;
412 struct grep_opt opt;
413 struct object_array list = { 0, 0, NULL };
414 const char **paths = NULL;
@@ -780,13 +490,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
490 OPT_BOOLEAN(0, "all-match", &opt.all_match,
491 "show only matches from files that match all patterns"),
492 OPT_GROUP(""),
783 -#if NO_EXTERNAL_GREP
784 - OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
785 - "allow calling of grep(1) (ignored by this build)"),
786 -#else
787 - OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
788 - "allow calling of grep(1) (default)"),
789 -#endif
493 + OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
494 + "allow calling of grep(1) (ignored by this build)"),
495 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
496 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
497 OPT_END()
@@ -837,8 +542,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
542 argc--;
543 }
544
840 - if ((opt.color && !opt.color_external) || opt.funcname)
841 - external_grep_allowed = 0;
545 if (!opt.pattern_list)
546 die("no pattern given.");
547 if (!opt.fixed && opt.ignore_case)
@@ -884,7 +587,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
587 if (!list.nr) {
588 if (!cached)
589 setup_work_tree();
887 - return !grep_cache(&opt, paths, cached, external_grep_allowed);
590 + return !grep_cache(&opt, paths, cached);
591 }
592
593 if (cached)
grep.h
-1
@@ -85,7 +85,6 @@ struct grep_opt {
85 int max_depth;
86 int funcname;
87 char color_match[COLOR_MAXLEN];
88 - const char *color_external;
88 int regflags;
89 unsigned pre_context;
90 unsigned post_context;
t/t7002-grep.sh
+3 -3
@@ -302,8 +302,8 @@ test_expect_success 'grep -C1, hunk mark between files' '
302 test_cmp expected actual
303 '
304
305 -test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
306 - git grep -C1 --no-ext-grep "^[yz]" >actual &&
305 +test_expect_success 'grep -C1 hunk mark between files' '
306 + git grep -C1 "^[yz]" >actual &&
307 test_cmp expected actual
308 '
309
@@ -359,7 +359,7 @@ test_expect_success 'log grep (6)' '
359 test_expect_success 'grep with CE_VALID file' '
360 git update-index --assume-unchanged t/t &&
361 rm t/t &&
362 - test "$(git grep --no-ext-grep test)" = "t/t:test" &&
362 + test "$(git grep test)" = "t/t:test" &&
363 git update-index --no-assume-unchanged t/t &&
364 git checkout t/t
365 '