git svn: fix reparenting when ugly http(s) URLs are used
[git/git.git] / builtin-grep.c
CommitLineData
5010cb5f
JH
1/*
2 * Builtin "git grep"
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6#include "cache.h"
7#include "blob.h"
8#include "tree.h"
9#include "commit.h"
10#include "tag.h"
1362671f 11#include "tree-walk.h"
5010cb5f 12#include "builtin.h"
3e230fa1 13#include "parse-options.h"
60ecac98 14#include "userdiff.h"
83b5d2f5 15#include "grep.h"
5010cb5f 16
5f7c643a
JK
17#ifndef NO_EXTERNAL_GREP
18#ifdef __unix__
19#define NO_EXTERNAL_GREP 0
20#else
21#define NO_EXTERNAL_GREP 1
22#endif
23#endif
24
3e230fa1
RS
25static char const * const grep_usage[] = {
26 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
27 NULL
28};
29
7e8f59d5
RS
30static int grep_config(const char *var, const char *value, void *cb)
31{
32 struct grep_opt *opt = cb;
33
60ecac98
RS
34 switch (userdiff_config(var, value)) {
35 case 0: break;
36 case -1: return -1;
37 default: return 0;
38 }
39
fe3420b6 40 if (!strcmp(var, "color.grep")) {
7e8f59d5
RS
41 opt->color = git_config_colorbool(var, value, -1);
42 return 0;
43 }
fe3420b6 44 if (!strcmp(var, "color.grep.external"))
a94982ef 45 return git_config_string(&(opt->color_external), var, value);
fe3420b6 46 if (!strcmp(var, "color.grep.match")) {
7e8f59d5
RS
47 if (!value)
48 return config_error_nonbool(var);
49 color_parse(value, var, opt->color_match);
50 return 0;
51 }
52 return git_color_default_config(var, value, cb);
53}
54
e0eb889f
JH
55/*
56 * git grep pathspecs are somewhat different from diff-tree pathspecs;
57 * pathname wildcards are allowed.
58 */
1362671f 59static int pathspec_matches(const char **paths, const char *name)
5010cb5f 60{
e0eb889f 61 int namelen, i;
1362671f 62 if (!paths || !*paths)
5010cb5f
JH
63 return 1;
64 namelen = strlen(name);
1362671f
JH
65 for (i = 0; paths[i]; i++) {
66 const char *match = paths[i];
67 int matchlen = strlen(match);
1e3d90e0 68 const char *cp, *meta;
e0eb889f 69
bb9e15a8
UKK
70 if (!matchlen ||
71 ((matchlen <= namelen) &&
72 !strncmp(name, match, matchlen) &&
73 (match[matchlen-1] == '/' ||
74 name[matchlen] == '\0' || name[matchlen] == '/')))
e0eb889f
JH
75 return 1;
76 if (!fnmatch(match, name, 0))
77 return 1;
78 if (name[namelen-1] != '/')
5010cb5f 79 continue;
e0eb889f 80
1e3d90e0 81 /* We are being asked if the directory ("name") is worth
e0eb889f
JH
82 * descending into.
83 *
84 * Find the longest leading directory name that does
85 * not have metacharacter in the pathspec; the name
86 * we are looking at must overlap with that directory.
5010cb5f 87 */
1e3d90e0 88 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
e0eb889f 89 char ch = *cp;
1e3d90e0
JH
90 if (ch == '*' || ch == '[' || ch == '?') {
91 meta = cp;
5010cb5f 92 break;
1e3d90e0 93 }
e0eb889f 94 }
1e3d90e0
JH
95 if (!meta)
96 meta = cp; /* fully literal */
97
98 if (namelen <= meta - match) {
e0eb889f
JH
99 /* Looking at "Documentation/" and
100 * the pattern says "Documentation/howto/", or
1e3d90e0
JH
101 * "Documentation/diff*.txt". The name we
102 * have should match prefix.
e0eb889f
JH
103 */
104 if (!memcmp(match, name, namelen))
105 return 1;
1e3d90e0 106 continue;
e0eb889f 107 }
1e3d90e0
JH
108
109 if (meta - match < namelen) {
e0eb889f 110 /* Looking at "Documentation/howto/" and
1e3d90e0
JH
111 * the pattern says "Documentation/h*";
112 * match up to "Do.../h"; this avoids descending
113 * into "Documentation/technical/".
e0eb889f 114 */
1e3d90e0 115 if (!memcmp(match, name, meta - match))
e0eb889f 116 return 1;
1e3d90e0 117 continue;
e0eb889f 118 }
5010cb5f
JH
119 }
120 return 0;
121}
122
0d042fec 123static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
5010cb5f
JH
124{
125 unsigned long size;
126 char *data;
21666f1a 127 enum object_type type;
0d042fec 128 char *to_free = NULL;
5010cb5f 129 int hit;
0d042fec 130
21666f1a 131 data = read_sha1_file(sha1, &type, &size);
5010cb5f
JH
132 if (!data) {
133 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
134 return 0;
135 }
0d042fec
JH
136 if (opt->relative && opt->prefix_length) {
137 static char name_buf[PATH_MAX];
138 char *cp;
139 int name_len = strlen(name) - opt->prefix_length + 1;
140
141 if (!tree_name_len)
142 name += opt->prefix_length;
143 else {
144 if (ARRAY_SIZE(name_buf) <= name_len)
145 cp = to_free = xmalloc(name_len);
146 else
147 cp = name_buf;
148 memcpy(cp, name, tree_name_len);
149 strcpy(cp + tree_name_len,
150 name + tree_name_len + opt->prefix_length);
151 name = cp;
152 }
153 }
5010cb5f
JH
154 hit = grep_buffer(opt, name, data, size);
155 free(data);
0d042fec 156 free(to_free);
5010cb5f
JH
157 return hit;
158}
159
160static int grep_file(struct grep_opt *opt, const char *filename)
161{
162 struct stat st;
163 int i;
164 char *data;
dc49cd76
SP
165 size_t sz;
166
5010cb5f
JH
167 if (lstat(filename, &st) < 0) {
168 err_ret:
169 if (errno != ENOENT)
170 error("'%s': %s", filename, strerror(errno));
171 return 0;
172 }
173 if (!st.st_size)
174 return 0; /* empty file -- no grep hit */
175 if (!S_ISREG(st.st_mode))
176 return 0;
dc49cd76 177 sz = xsize_t(st.st_size);
5010cb5f
JH
178 i = open(filename, O_RDONLY);
179 if (i < 0)
180 goto err_ret;
dc49cd76
SP
181 data = xmalloc(sz + 1);
182 if (st.st_size != read_in_full(i, data, sz)) {
5010cb5f
JH
183 error("'%s': short read %s", filename, strerror(errno));
184 close(i);
185 free(data);
186 return 0;
187 }
188 close(i);
0d042fec
JH
189 if (opt->relative && opt->prefix_length)
190 filename += opt->prefix_length;
dc49cd76 191 i = grep_buffer(opt, filename, data, sz);
5010cb5f
JH
192 free(data);
193 return i;
194}
195
5f7c643a 196#if !NO_EXTERNAL_GREP
1e2398d7
LT
197static int exec_grep(int argc, const char **argv)
198{
199 pid_t pid;
200 int status;
201
202 argv[argc] = NULL;
203 pid = fork();
204 if (pid < 0)
205 return pid;
206 if (!pid) {
207 execvp("grep", (char **) argv);
208 exit(255);
209 }
210 while (waitpid(pid, &status, 0) < 0) {
211 if (errno == EINTR)
212 continue;
213 return -1;
214 }
215 if (WIFEXITED(status)) {
216 if (!WEXITSTATUS(status))
217 return 1;
218 return 0;
219 }
220 return -1;
221}
222
223#define MAXARGS 1000
ffa0a7ab
JH
224#define ARGBUF 4096
225#define push_arg(a) do { \
226 if (nr < MAXARGS) argv[nr++] = (a); \
227 else die("maximum number of args exceeded"); \
228 } while (0)
1e2398d7 229
d99ebf08
JH
230/*
231 * If you send a singleton filename to grep, it does not give
232 * the name of the file. GNU grep has "-H" but we would want
233 * that behaviour in a portable way.
234 *
235 * So we keep two pathnames in argv buffer unsent to grep in
236 * the main loop if we need to do more than one grep.
237 */
238static int flush_grep(struct grep_opt *opt,
239 int argc, int arg0, const char **argv, int *kept)
240{
241 int status;
242 int count = argc - arg0;
243 const char *kept_0 = NULL;
244
245 if (count <= 2) {
246 /*
247 * Because we keep at least 2 paths in the call from
248 * the main loop (i.e. kept != NULL), and MAXARGS is
249 * far greater than 2, this usually is a call to
250 * conclude the grep. However, the user could attempt
251 * to overflow the argv buffer by giving too many
252 * options to leave very small number of real
253 * arguments even for the call in the main loop.
254 */
255 if (kept)
256 die("insanely many options to grep");
257
258 /*
259 * If we have two or more paths, we do not have to do
260 * anything special, but we need to push /dev/null to
261 * get "-H" behaviour of GNU grep portably but when we
262 * are not doing "-l" nor "-L" nor "-c".
263 */
264 if (count == 1 &&
265 !opt->name_only &&
266 !opt->unmatch_name_only &&
267 !opt->count) {
268 argv[argc++] = "/dev/null";
269 argv[argc] = NULL;
270 }
271 }
272
273 else if (kept) {
274 /*
275 * Called because we found many paths and haven't finished
276 * iterating over the cache yet. We keep two paths
277 * for the concluding call. argv[argc-2] and argv[argc-1]
278 * has the last two paths, so save the first one away,
279 * replace it with NULL while sending the list to grep,
280 * and recover them after we are done.
281 */
282 *kept = 2;
283 kept_0 = argv[argc-2];
284 argv[argc-2] = NULL;
285 argc -= 2;
286 }
287
ed24e401 288 if (opt->pre_context || opt->post_context) {
046802d0
RS
289 /*
290 * grep handles hunk marks between files, but we need to
291 * do that ourselves between multiple calls.
292 */
293 if (opt->show_hunk_mark)
ed24e401 294 write_or_die(1, "--\n", 3);
046802d0
RS
295 else
296 opt->show_hunk_mark = 1;
297 }
298
d99ebf08
JH
299 status = exec_grep(argc, argv);
300
301 if (kept_0) {
302 /*
303 * Then recover them. Now the last arg is beyond the
304 * terminating NULL which is at argc, and the second
305 * from the last is what we saved away in kept_0
306 */
307 argv[arg0++] = kept_0;
308 argv[arg0] = argv[argc+1];
309 }
310 return status;
311}
312
a94982ef
RS
313static void grep_add_color(struct strbuf *sb, const char *escape_seq)
314{
315 size_t orig_len = sb->len;
316
317 while (*escape_seq) {
318 if (*escape_seq == 'm')
319 strbuf_addch(sb, ';');
320 else if (*escape_seq != '\033' && *escape_seq != '[')
321 strbuf_addch(sb, *escape_seq);
322 escape_seq++;
323 }
324 if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
325 strbuf_setlen(sb, sb->len - 1);
326}
327
1e2398d7
LT
328static int external_grep(struct grep_opt *opt, const char **paths, int cached)
329{
fcfe34b5 330 int i, nr, argc, hit, len, status;
1e2398d7 331 const char *argv[MAXARGS+1];
ffa0a7ab
JH
332 char randarg[ARGBUF];
333 char *argptr = randarg;
1e2398d7
LT
334 struct grep_pat *p;
335
0d042fec 336 if (opt->extended || (opt->relative && opt->prefix_length))
79d3696c 337 return -1;
ffa0a7ab
JH
338 len = nr = 0;
339 push_arg("grep");
ffa0a7ab 340 if (opt->fixed)
f6647519 341 push_arg("-F");
ffa0a7ab
JH
342 if (opt->linenum)
343 push_arg("-n");
7977f0ea
LT
344 if (!opt->pathname)
345 push_arg("-h");
ffa0a7ab
JH
346 if (opt->regflags & REG_EXTENDED)
347 push_arg("-E");
3026402c
RF
348 if (opt->regflags & REG_ICASE)
349 push_arg("-i");
bc395643
JH
350 if (opt->binary == GREP_BINARY_NOMATCH)
351 push_arg("-I");
1e2398d7 352 if (opt->word_regexp)
ffa0a7ab 353 push_arg("-w");
1e2398d7 354 if (opt->name_only)
ffa0a7ab
JH
355 push_arg("-l");
356 if (opt->unmatch_name_only)
357 push_arg("-L");
83caecca
RZ
358 if (opt->null_following_name)
359 /* in GNU grep git's "-z" translates to "-Z" */
360 push_arg("-Z");
ffa0a7ab
JH
361 if (opt->count)
362 push_arg("-c");
363 if (opt->post_context || opt->pre_context) {
364 if (opt->post_context != opt->pre_context) {
365 if (opt->pre_context) {
366 push_arg("-B");
367 len += snprintf(argptr, sizeof(randarg)-len,
4b87474b 368 "%u", opt->pre_context) + 1;
ffa0a7ab
JH
369 if (sizeof(randarg) <= len)
370 die("maximum length of args exceeded");
371 push_arg(argptr);
372 argptr += len;
373 }
374 if (opt->post_context) {
375 push_arg("-A");
376 len += snprintf(argptr, sizeof(randarg)-len,
4b87474b 377 "%u", opt->post_context) + 1;
ffa0a7ab
JH
378 if (sizeof(randarg) <= len)
379 die("maximum length of args exceeded");
380 push_arg(argptr);
381 argptr += len;
382 }
383 }
384 else {
385 push_arg("-C");
386 len += snprintf(argptr, sizeof(randarg)-len,
4b87474b 387 "%u", opt->post_context) + 1;
ffa0a7ab
JH
388 if (sizeof(randarg) <= len)
389 die("maximum length of args exceeded");
390 push_arg(argptr);
391 argptr += len;
392 }
393 }
1e2398d7 394 for (p = opt->pattern_list; p; p = p->next) {
ffa0a7ab
JH
395 push_arg("-e");
396 push_arg(p->pattern);
1e2398d7 397 }
a94982ef
RS
398 if (opt->color) {
399 struct strbuf sb = STRBUF_INIT;
400
401 grep_add_color(&sb, opt->color_match);
402 setenv("GREP_COLOR", sb.buf, 1);
403
404 strbuf_reset(&sb);
405 strbuf_addstr(&sb, "mt=");
406 grep_add_color(&sb, opt->color_match);
407 strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
408 setenv("GREP_COLORS", sb.buf, 1);
409
410 strbuf_release(&sb);
411
412 if (opt->color_external && strlen(opt->color_external) > 0)
413 push_arg(opt->color_external);
414 }
bbb66c60 415
1e2398d7
LT
416 hit = 0;
417 argc = nr;
418 for (i = 0; i < active_nr; i++) {
419 struct cache_entry *ce = active_cache[i];
fbd01abf 420 char *name;
d99ebf08 421 int kept;
7a51ed66 422 if (!S_ISREG(ce->ce_mode))
1e2398d7
LT
423 continue;
424 if (!pathspec_matches(paths, ce->name))
425 continue;
bbb66c60
LT
426 name = ce->name;
427 if (name[0] == '-') {
428 int len = ce_namelen(ce);
429 name = xmalloc(len + 3);
430 memcpy(name, "./", 2);
431 memcpy(name + 2, ce->name, len + 1);
432 }
433 argv[argc++] = name;
6326cee5
JH
434 if (MAXARGS <= argc) {
435 status = flush_grep(opt, argc, nr, argv, &kept);
436 if (0 < status)
437 hit = 1;
438 argc = nr + kept;
439 }
36f2587f
JH
440 if (ce_stage(ce)) {
441 do {
442 i++;
443 } while (i < active_nr &&
444 !strcmp(ce->name, active_cache[i]->name));
445 i--; /* compensate for loop control */
446 }
1e2398d7 447 }
fcfe34b5 448 if (argc > nr) {
d99ebf08 449 status = flush_grep(opt, argc, nr, argv, NULL);
fcfe34b5
JH
450 if (0 < status)
451 hit = 1;
452 }
453 return hit;
1e2398d7 454}
ff1f9945 455#endif
1e2398d7 456
1b5fb44a
RS
457static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
458 int external_grep_allowed)
5010cb5f
JH
459{
460 int hit = 0;
461 int nr;
462 read_cache();
463
5f7c643a 464#if !NO_EXTERNAL_GREP
1e2398d7
LT
465 /*
466 * Use the external "grep" command for the case where
467 * we grep through the checked-out files. It tends to
468 * be a lot more optimized
469 */
1b5fb44a 470 if (!cached && external_grep_allowed) {
1e2398d7
LT
471 hit = external_grep(opt, paths, cached);
472 if (hit >= 0)
473 return hit;
474 }
475#endif
476
5010cb5f
JH
477 for (nr = 0; nr < active_nr; nr++) {
478 struct cache_entry *ce = active_cache[nr];
7a51ed66 479 if (!S_ISREG(ce->ce_mode))
5010cb5f 480 continue;
1362671f 481 if (!pathspec_matches(paths, ce->name))
5010cb5f 482 continue;
57d43466
NTND
483 /*
484 * If CE_VALID is on, we assume worktree file and its cache entry
485 * are identical, even if worktree file has been modified, so use
486 * cache version instead
487 */
488 if (cached || (ce->ce_flags & CE_VALID)) {
36f2587f
JH
489 if (ce_stage(ce))
490 continue;
0d042fec 491 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
36f2587f 492 }
5010cb5f
JH
493 else
494 hit |= grep_file(opt, ce->name);
36f2587f
JH
495 if (ce_stage(ce)) {
496 do {
497 nr++;
498 } while (nr < active_nr &&
499 !strcmp(ce->name, active_cache[nr]->name));
500 nr--; /* compensate for loop control */
501 }
5010cb5f 502 }
b48fb5b6 503 free_grep_patterns(opt);
5010cb5f
JH
504 return hit;
505}
506
1362671f 507static int grep_tree(struct grep_opt *opt, const char **paths,
5010cb5f
JH
508 struct tree_desc *tree,
509 const char *tree_name, const char *base)
510{
5010cb5f
JH
511 int len;
512 int hit = 0;
4c068a98 513 struct name_entry entry;
e0eb889f 514 char *down;
0d042fec 515 int tn_len = strlen(tree_name);
620e2bb9
DP
516 struct strbuf pathbuf;
517
518 strbuf_init(&pathbuf, PATH_MAX + tn_len);
5010cb5f 519
0d042fec 520 if (tn_len) {
620e2bb9
DP
521 strbuf_add(&pathbuf, tree_name, tn_len);
522 strbuf_addch(&pathbuf, ':');
523 tn_len = pathbuf.len;
5010cb5f 524 }
620e2bb9
DP
525 strbuf_addstr(&pathbuf, base);
526 len = pathbuf.len;
5010cb5f 527
4c068a98 528 while (tree_entry(tree, &entry)) {
620e2bb9
DP
529 int te_len = tree_entry_len(entry.path, entry.sha1);
530 pathbuf.len = len;
531 strbuf_add(&pathbuf, entry.path, te_len);
5010cb5f 532
4c068a98 533 if (S_ISDIR(entry.mode))
e0eb889f
JH
534 /* Match "abc/" against pathspec to
535 * decide if we want to descend into "abc"
536 * directory.
537 */
620e2bb9 538 strbuf_addch(&pathbuf, '/');
e0eb889f 539
620e2bb9 540 down = pathbuf.buf + tn_len;
1362671f 541 if (!pathspec_matches(paths, down))
5010cb5f 542 ;
4c068a98 543 else if (S_ISREG(entry.mode))
620e2bb9 544 hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
4c068a98 545 else if (S_ISDIR(entry.mode)) {
21666f1a 546 enum object_type type;
5010cb5f
JH
547 struct tree_desc sub;
548 void *data;
6fda5e51
LT
549 unsigned long size;
550
551 data = read_sha1_file(entry.sha1, &type, &size);
5010cb5f
JH
552 if (!data)
553 die("unable to read tree (%s)",
4c068a98 554 sha1_to_hex(entry.sha1));
6fda5e51 555 init_tree_desc(&sub, data, size);
1362671f 556 hit |= grep_tree(opt, paths, &sub, tree_name, down);
5010cb5f
JH
557 free(data);
558 }
5010cb5f 559 }
620e2bb9 560 strbuf_release(&pathbuf);
5010cb5f
JH
561 return hit;
562}
563
1362671f 564static int grep_object(struct grep_opt *opt, const char **paths,
5010cb5f
JH
565 struct object *obj, const char *name)
566{
1974632c 567 if (obj->type == OBJ_BLOB)
0d042fec 568 return grep_sha1(opt, obj->sha1, name, 0);
1974632c 569 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
5010cb5f
JH
570 struct tree_desc tree;
571 void *data;
6fda5e51 572 unsigned long size;
5010cb5f
JH
573 int hit;
574 data = read_object_with_reference(obj->sha1, tree_type,
6fda5e51 575 &size, NULL);
5010cb5f
JH
576 if (!data)
577 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
6fda5e51 578 init_tree_desc(&tree, data, size);
1362671f 579 hit = grep_tree(opt, paths, &tree, name, "");
5010cb5f
JH
580 free(data);
581 return hit;
582 }
885a86ab 583 die("unable to grep from object of type %s", typename(obj->type));
5010cb5f
JH
584}
585
ff3c7f9a
RS
586static int context_callback(const struct option *opt, const char *arg,
587 int unset)
3e230fa1
RS
588{
589 struct grep_opt *grep_opt = opt->value;
590 int value;
591 const char *endp;
592
593 if (unset) {
594 grep_opt->pre_context = grep_opt->post_context = 0;
595 return 0;
596 }
597 value = strtol(arg, (char **)&endp, 10);
598 if (*endp) {
599 return error("switch `%c' expects a numerical value",
600 opt->short_name);
601 }
602 grep_opt->pre_context = grep_opt->post_context = value;
603 return 0;
604}
605
ff3c7f9a 606static int file_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
607{
608 struct grep_opt *grep_opt = opt->value;
609 FILE *patterns;
610 int lno = 0;
611 struct strbuf sb;
612
613 patterns = fopen(arg, "r");
614 if (!patterns)
d824cbba 615 die_errno("cannot open '%s'", arg);
3e230fa1
RS
616 while (strbuf_getline(&sb, patterns, '\n') == 0) {
617 /* ignore empty line like grep does */
618 if (sb.len == 0)
619 continue;
620 append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg,
621 ++lno, GREP_PATTERN);
622 }
623 fclose(patterns);
624 strbuf_release(&sb);
625 return 0;
626}
627
ff3c7f9a 628static int not_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
629{
630 struct grep_opt *grep_opt = opt->value;
631 append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
632 return 0;
633}
634
ff3c7f9a 635static int and_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
636{
637 struct grep_opt *grep_opt = opt->value;
638 append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
639 return 0;
640}
641
ff3c7f9a 642static int open_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
643{
644 struct grep_opt *grep_opt = opt->value;
645 append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
646 return 0;
647}
648
ff3c7f9a 649static int close_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
650{
651 struct grep_opt *grep_opt = opt->value;
652 append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
653 return 0;
654}
655
ff3c7f9a
RS
656static int pattern_callback(const struct option *opt, const char *arg,
657 int unset)
3e230fa1
RS
658{
659 struct grep_opt *grep_opt = opt->value;
660 append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
661 return 0;
662}
5010cb5f 663
ff3c7f9a 664static int help_callback(const struct option *opt, const char *arg, int unset)
3e230fa1
RS
665{
666 return -1;
667}
088b084b 668
a633fca0 669int cmd_grep(int argc, const char **argv, const char *prefix)
5010cb5f 670{
5010cb5f 671 int hit = 0;
5010cb5f 672 int cached = 0;
1b5fb44a 673 int external_grep_allowed = 1;
5acd64ed 674 int seen_dashdash = 0;
5010cb5f 675 struct grep_opt opt;
1f1e895f 676 struct object_array list = { 0, 0, NULL };
1362671f 677 const char **paths = NULL;
5acd64ed 678 int i;
3e230fa1
RS
679 int dummy;
680 struct option options[] = {
681 OPT_BOOLEAN(0, "cached", &cached,
682 "search in index instead of in the work tree"),
683 OPT_GROUP(""),
684 OPT_BOOLEAN('v', "invert-match", &opt.invert,
685 "show non-matching lines"),
686 OPT_BIT('i', "ignore-case", &opt.regflags,
687 "case insensitive matching", REG_ICASE),
688 OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
689 "match patterns only at word boundaries"),
690 OPT_SET_INT('a', "text", &opt.binary,
691 "process binary files as text", GREP_BINARY_TEXT),
692 OPT_SET_INT('I', NULL, &opt.binary,
693 "don't match patterns in binary files",
694 GREP_BINARY_NOMATCH),
695 OPT_GROUP(""),
696 OPT_BIT('E', "extended-regexp", &opt.regflags,
697 "use extended POSIX regular expressions", REG_EXTENDED),
698 OPT_NEGBIT('G', "basic-regexp", &opt.regflags,
699 "use basic POSIX regular expressions (default)",
700 REG_EXTENDED),
701 OPT_BOOLEAN('F', "fixed-strings", &opt.fixed,
702 "interpret patterns as fixed strings"),
703 OPT_GROUP(""),
704 OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"),
705 OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
706 OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1),
707 OPT_NEGBIT(0, "full-name", &opt.relative,
708 "show filenames relative to top directory", 1),
709 OPT_BOOLEAN('l', "files-with-matches", &opt.name_only,
710 "show only filenames instead of matching lines"),
711 OPT_BOOLEAN(0, "name-only", &opt.name_only,
712 "synonym for --files-with-matches"),
713 OPT_BOOLEAN('L', "files-without-match",
714 &opt.unmatch_name_only,
715 "show only the names of files without match"),
716 OPT_BOOLEAN('z', "null", &opt.null_following_name,
717 "print NUL after filenames"),
718 OPT_BOOLEAN('c', "count", &opt.count,
719 "show the number of matches instead of matching lines"),
720 OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1),
721 OPT_GROUP(""),
722 OPT_CALLBACK('C', NULL, &opt, "n",
723 "show <n> context lines before and after matches",
724 context_callback),
725 OPT_INTEGER('B', NULL, &opt.pre_context,
726 "show <n> context lines before matches"),
727 OPT_INTEGER('A', NULL, &opt.post_context,
728 "show <n> context lines after matches"),
729 OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM",
730 context_callback),
2944e4e6
RS
731 OPT_BOOLEAN('p', "show-function", &opt.funcname,
732 "show a line with the function name before matches"),
3e230fa1
RS
733 OPT_GROUP(""),
734 OPT_CALLBACK('f', NULL, &opt, "file",
735 "read patterns from file", file_callback),
736 { OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
737 "match <pattern>", PARSE_OPT_NONEG, pattern_callback },
738 { OPTION_CALLBACK, 0, "and", &opt, NULL,
739 "combine patterns specified with -e",
740 PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
741 OPT_BOOLEAN(0, "or", &dummy, ""),
742 { OPTION_CALLBACK, 0, "not", &opt, NULL, "",
743 PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },
744 { OPTION_CALLBACK, '(', NULL, &opt, NULL, "",
745 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
746 open_callback },
747 { OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
748 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
749 close_callback },
750 OPT_BOOLEAN(0, "all-match", &opt.all_match,
751 "show only matches from files that match all patterns"),
752 OPT_GROUP(""),
753#if NO_EXTERNAL_GREP
754 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
755 "allow calling of grep(1) (ignored by this build)"),
756#else
757 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
758 "allow calling of grep(1) (default)"),
759#endif
760 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
761 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
762 OPT_END()
763 };
5010cb5f
JH
764
765 memset(&opt, 0, sizeof(opt));
0d042fec
JH
766 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
767 opt.relative = 1;
7977f0ea 768 opt.pathname = 1;
f9b9faf6 769 opt.pattern_tail = &opt.pattern_list;
5010cb5f
JH
770 opt.regflags = REG_NEWLINE;
771
7e8f59d5
RS
772 strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
773 opt.color = -1;
774 git_config(grep_config, &opt);
775 if (opt.color == -1)
776 opt.color = git_use_color_default;
777
5010cb5f 778 /*
5acd64ed
JH
779 * If there is no -- then the paths must exist in the working
780 * tree. If there is no explicit pattern specified with -e or
781 * -f, we take the first unrecognized non option to be the
782 * pattern, but then what follows it must be zero or more
783 * valid refs up to the -- (if exists), and then existing
784 * paths. If there is an explicit pattern, then the first
82e5a82f 785 * unrecognized non option is the beginning of the refs list
5acd64ed 786 * that continues up to the -- (if exists), and then paths.
5010cb5f 787 */
37782920 788 argc = parse_options(argc, argv, prefix, options, grep_usage,
3e230fa1
RS
789 PARSE_OPT_KEEP_DASHDASH |
790 PARSE_OPT_STOP_AT_NON_OPTION |
791 PARSE_OPT_NO_INTERNAL_HELP);
792
793 /* First unrecognized non-option token */
794 if (argc > 0 && !opt.pattern_list) {
795 append_grep_pattern(&opt, argv[0], "command line", 0,
796 GREP_PATTERN);
797 argv++;
798 argc--;
5010cb5f 799 }
5acd64ed 800
2944e4e6 801 if ((opt.color && !opt.color_external) || opt.funcname)
1b5fb44a 802 external_grep_allowed = 0;
f9b9faf6 803 if (!opt.pattern_list)
5010cb5f 804 die("no pattern given.");
07ea91d8
JH
805 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
806 die("cannot mix --fixed-strings and regexp");
83b5d2f5 807 compile_grep_patterns(&opt);
5acd64ed
JH
808
809 /* Check revs and then paths */
3e230fa1 810 for (i = 0; i < argc; i++) {
5acd64ed 811 const char *arg = argv[i];
1362671f 812 unsigned char sha1[20];
5acd64ed
JH
813 /* Is it a rev? */
814 if (!get_sha1(arg, sha1)) {
815 struct object *object = parse_object(sha1);
5acd64ed
JH
816 if (!object)
817 die("bad object %s", arg);
1f1e895f 818 add_object_array(object, arg, &list);
5acd64ed
JH
819 continue;
820 }
821 if (!strcmp(arg, "--")) {
822 i++;
823 seen_dashdash = 1;
824 }
825 break;
1362671f 826 }
5acd64ed
JH
827
828 /* The rest are paths */
829 if (!seen_dashdash) {
830 int j;
c39c4f47 831 for (j = i; j < argc; j++)
5acd64ed
JH
832 verify_filename(prefix, argv[j]);
833 }
834
0d042fec 835 if (i < argc) {
5acd64ed 836 paths = get_pathspec(prefix, argv + i);
0d042fec
JH
837 if (opt.prefix_length && opt.relative) {
838 /* Make sure we do not get outside of paths */
839 for (i = 0; paths[i]; i++)
840 if (strncmp(prefix, paths[i], opt.prefix_length))
7e44c935 841 die("git grep: cannot generate relative filenames containing '..'");
0d042fec
JH
842 }
843 }
1362671f
JH
844 else if (prefix) {
845 paths = xcalloc(2, sizeof(const char *));
846 paths[0] = prefix;
847 paths[1] = NULL;
848 }
5010cb5f 849
6577f542
NTND
850 if (!list.nr) {
851 if (!cached)
852 setup_work_tree();
1b5fb44a 853 return !grep_cache(&opt, paths, cached, external_grep_allowed);
6577f542 854 }
aa8c79ad 855
5010cb5f 856 if (cached)
aa8c79ad 857 die("both --cached and trees are given.");
5010cb5f 858
1f1e895f 859 for (i = 0; i < list.nr; i++) {
5010cb5f 860 struct object *real_obj;
1f1e895f
LT
861 real_obj = deref_tag(list.objects[i].item, NULL, 0);
862 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
5010cb5f
JH
863 hit = 1;
864 }
b48fb5b6 865 free_grep_patterns(&opt);
5010cb5f
JH
866 return !hit;
867}