]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-grep.c
short i/o: fix calls to read to use xread or read_in_full
[thirdparty/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"
83b5d2f5 13#include "grep.h"
5010cb5f 14
e0eb889f
JH
15/*
16 * git grep pathspecs are somewhat different from diff-tree pathspecs;
17 * pathname wildcards are allowed.
18 */
1362671f 19static int pathspec_matches(const char **paths, const char *name)
5010cb5f 20{
e0eb889f 21 int namelen, i;
1362671f 22 if (!paths || !*paths)
5010cb5f
JH
23 return 1;
24 namelen = strlen(name);
1362671f
JH
25 for (i = 0; paths[i]; i++) {
26 const char *match = paths[i];
27 int matchlen = strlen(match);
1e3d90e0 28 const char *cp, *meta;
e0eb889f 29
bb9e15a8
UKK
30 if (!matchlen ||
31 ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/')))
e0eb889f
JH
35 return 1;
36 if (!fnmatch(match, name, 0))
37 return 1;
38 if (name[namelen-1] != '/')
5010cb5f 39 continue;
e0eb889f 40
1e3d90e0 41 /* We are being asked if the directory ("name") is worth
e0eb889f
JH
42 * descending into.
43 *
44 * Find the longest leading directory name that does
45 * not have metacharacter in the pathspec; the name
46 * we are looking at must overlap with that directory.
5010cb5f 47 */
1e3d90e0 48 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
e0eb889f 49 char ch = *cp;
1e3d90e0
JH
50 if (ch == '*' || ch == '[' || ch == '?') {
51 meta = cp;
5010cb5f 52 break;
1e3d90e0 53 }
e0eb889f 54 }
1e3d90e0
JH
55 if (!meta)
56 meta = cp; /* fully literal */
57
58 if (namelen <= meta - match) {
e0eb889f
JH
59 /* Looking at "Documentation/" and
60 * the pattern says "Documentation/howto/", or
1e3d90e0
JH
61 * "Documentation/diff*.txt". The name we
62 * have should match prefix.
e0eb889f
JH
63 */
64 if (!memcmp(match, name, namelen))
65 return 1;
1e3d90e0 66 continue;
e0eb889f 67 }
1e3d90e0
JH
68
69 if (meta - match < namelen) {
e0eb889f 70 /* Looking at "Documentation/howto/" and
1e3d90e0
JH
71 * the pattern says "Documentation/h*";
72 * match up to "Do.../h"; this avoids descending
73 * into "Documentation/technical/".
e0eb889f 74 */
1e3d90e0 75 if (!memcmp(match, name, meta - match))
e0eb889f 76 return 1;
1e3d90e0 77 continue;
e0eb889f 78 }
5010cb5f
JH
79 }
80 return 0;
81}
82
0d042fec 83static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
5010cb5f
JH
84{
85 unsigned long size;
86 char *data;
87 char type[20];
0d042fec 88 char *to_free = NULL;
5010cb5f 89 int hit;
0d042fec 90
5010cb5f
JH
91 data = read_sha1_file(sha1, type, &size);
92 if (!data) {
93 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
94 return 0;
95 }
0d042fec
JH
96 if (opt->relative && opt->prefix_length) {
97 static char name_buf[PATH_MAX];
98 char *cp;
99 int name_len = strlen(name) - opt->prefix_length + 1;
100
101 if (!tree_name_len)
102 name += opt->prefix_length;
103 else {
104 if (ARRAY_SIZE(name_buf) <= name_len)
105 cp = to_free = xmalloc(name_len);
106 else
107 cp = name_buf;
108 memcpy(cp, name, tree_name_len);
109 strcpy(cp + tree_name_len,
110 name + tree_name_len + opt->prefix_length);
111 name = cp;
112 }
113 }
5010cb5f
JH
114 hit = grep_buffer(opt, name, data, size);
115 free(data);
0d042fec 116 free(to_free);
5010cb5f
JH
117 return hit;
118}
119
120static int grep_file(struct grep_opt *opt, const char *filename)
121{
122 struct stat st;
123 int i;
124 char *data;
125 if (lstat(filename, &st) < 0) {
126 err_ret:
127 if (errno != ENOENT)
128 error("'%s': %s", filename, strerror(errno));
129 return 0;
130 }
131 if (!st.st_size)
132 return 0; /* empty file -- no grep hit */
133 if (!S_ISREG(st.st_mode))
134 return 0;
135 i = open(filename, O_RDONLY);
136 if (i < 0)
137 goto err_ret;
138 data = xmalloc(st.st_size + 1);
93d26e4c 139 if (st.st_size != read_in_full(i, data, st.st_size)) {
5010cb5f
JH
140 error("'%s': short read %s", filename, strerror(errno));
141 close(i);
142 free(data);
143 return 0;
144 }
145 close(i);
0d042fec
JH
146 if (opt->relative && opt->prefix_length)
147 filename += opt->prefix_length;
5010cb5f
JH
148 i = grep_buffer(opt, filename, data, st.st_size);
149 free(data);
150 return i;
151}
152
1e2398d7
LT
153static int exec_grep(int argc, const char **argv)
154{
155 pid_t pid;
156 int status;
157
158 argv[argc] = NULL;
159 pid = fork();
160 if (pid < 0)
161 return pid;
162 if (!pid) {
163 execvp("grep", (char **) argv);
164 exit(255);
165 }
166 while (waitpid(pid, &status, 0) < 0) {
167 if (errno == EINTR)
168 continue;
169 return -1;
170 }
171 if (WIFEXITED(status)) {
172 if (!WEXITSTATUS(status))
173 return 1;
174 return 0;
175 }
176 return -1;
177}
178
179#define MAXARGS 1000
ffa0a7ab
JH
180#define ARGBUF 4096
181#define push_arg(a) do { \
182 if (nr < MAXARGS) argv[nr++] = (a); \
183 else die("maximum number of args exceeded"); \
184 } while (0)
1e2398d7
LT
185
186static int external_grep(struct grep_opt *opt, const char **paths, int cached)
187{
fcfe34b5 188 int i, nr, argc, hit, len, status;
1e2398d7 189 const char *argv[MAXARGS+1];
ffa0a7ab
JH
190 char randarg[ARGBUF];
191 char *argptr = randarg;
1e2398d7
LT
192 struct grep_pat *p;
193
0d042fec 194 if (opt->extended || (opt->relative && opt->prefix_length))
79d3696c 195 return -1;
ffa0a7ab
JH
196 len = nr = 0;
197 push_arg("grep");
ffa0a7ab 198 if (opt->fixed)
f6647519 199 push_arg("-F");
ffa0a7ab
JH
200 if (opt->linenum)
201 push_arg("-n");
7977f0ea
LT
202 if (!opt->pathname)
203 push_arg("-h");
ffa0a7ab
JH
204 if (opt->regflags & REG_EXTENDED)
205 push_arg("-E");
3026402c
RF
206 if (opt->regflags & REG_ICASE)
207 push_arg("-i");
1e2398d7 208 if (opt->word_regexp)
ffa0a7ab 209 push_arg("-w");
1e2398d7 210 if (opt->name_only)
ffa0a7ab
JH
211 push_arg("-l");
212 if (opt->unmatch_name_only)
213 push_arg("-L");
214 if (opt->count)
215 push_arg("-c");
216 if (opt->post_context || opt->pre_context) {
217 if (opt->post_context != opt->pre_context) {
218 if (opt->pre_context) {
219 push_arg("-B");
220 len += snprintf(argptr, sizeof(randarg)-len,
221 "%u", opt->pre_context);
222 if (sizeof(randarg) <= len)
223 die("maximum length of args exceeded");
224 push_arg(argptr);
225 argptr += len;
226 }
227 if (opt->post_context) {
228 push_arg("-A");
229 len += snprintf(argptr, sizeof(randarg)-len,
230 "%u", opt->post_context);
231 if (sizeof(randarg) <= len)
232 die("maximum length of args exceeded");
233 push_arg(argptr);
234 argptr += len;
235 }
236 }
237 else {
238 push_arg("-C");
239 len += snprintf(argptr, sizeof(randarg)-len,
240 "%u", opt->post_context);
241 if (sizeof(randarg) <= len)
242 die("maximum length of args exceeded");
243 push_arg(argptr);
244 argptr += len;
245 }
246 }
1e2398d7 247 for (p = opt->pattern_list; p; p = p->next) {
ffa0a7ab
JH
248 push_arg("-e");
249 push_arg(p->pattern);
1e2398d7 250 }
bbb66c60
LT
251
252 /*
253 * To make sure we get the header printed out when we want it,
254 * add /dev/null to the paths to grep. This is unnecessary
255 * (and wrong) with "-l" or "-L", which always print out the
256 * name anyway.
257 *
258 * GNU grep has "-H", but this is portable.
259 */
260 if (!opt->name_only && !opt->unmatch_name_only)
261 push_arg("/dev/null");
1e2398d7
LT
262
263 hit = 0;
264 argc = nr;
265 for (i = 0; i < active_nr; i++) {
266 struct cache_entry *ce = active_cache[i];
fbd01abf 267 char *name;
36f2587f 268 if (!S_ISREG(ntohl(ce->ce_mode)))
1e2398d7
LT
269 continue;
270 if (!pathspec_matches(paths, ce->name))
271 continue;
bbb66c60
LT
272 name = ce->name;
273 if (name[0] == '-') {
274 int len = ce_namelen(ce);
275 name = xmalloc(len + 3);
276 memcpy(name, "./", 2);
277 memcpy(name + 2, ce->name, len + 1);
278 }
279 argv[argc++] = name;
36f2587f 280 if (argc < MAXARGS && !ce_stage(ce))
1e2398d7 281 continue;
fcfe34b5
JH
282 status = exec_grep(argc, argv);
283 if (0 < status)
284 hit = 1;
1e2398d7 285 argc = nr;
36f2587f
JH
286 if (ce_stage(ce)) {
287 do {
288 i++;
289 } while (i < active_nr &&
290 !strcmp(ce->name, active_cache[i]->name));
291 i--; /* compensate for loop control */
292 }
1e2398d7 293 }
fcfe34b5
JH
294 if (argc > nr) {
295 status = exec_grep(argc, argv);
296 if (0 < status)
297 hit = 1;
298 }
299 return hit;
1e2398d7
LT
300}
301
1362671f 302static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
5010cb5f
JH
303{
304 int hit = 0;
305 int nr;
306 read_cache();
307
1e2398d7
LT
308#ifdef __unix__
309 /*
310 * Use the external "grep" command for the case where
311 * we grep through the checked-out files. It tends to
312 * be a lot more optimized
313 */
314 if (!cached) {
315 hit = external_grep(opt, paths, cached);
316 if (hit >= 0)
317 return hit;
318 }
319#endif
320
5010cb5f
JH
321 for (nr = 0; nr < active_nr; nr++) {
322 struct cache_entry *ce = active_cache[nr];
36f2587f 323 if (!S_ISREG(ntohl(ce->ce_mode)))
5010cb5f 324 continue;
1362671f 325 if (!pathspec_matches(paths, ce->name))
5010cb5f 326 continue;
36f2587f
JH
327 if (cached) {
328 if (ce_stage(ce))
329 continue;
0d042fec 330 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
36f2587f 331 }
5010cb5f
JH
332 else
333 hit |= grep_file(opt, ce->name);
36f2587f
JH
334 if (ce_stage(ce)) {
335 do {
336 nr++;
337 } while (nr < active_nr &&
338 !strcmp(ce->name, active_cache[nr]->name));
339 nr--; /* compensate for loop control */
340 }
5010cb5f 341 }
b48fb5b6 342 free_grep_patterns(opt);
5010cb5f
JH
343 return hit;
344}
345
1362671f 346static int grep_tree(struct grep_opt *opt, const char **paths,
5010cb5f
JH
347 struct tree_desc *tree,
348 const char *tree_name, const char *base)
349{
5010cb5f
JH
350 int len;
351 int hit = 0;
4c068a98 352 struct name_entry entry;
e0eb889f 353 char *down;
0d042fec
JH
354 int tn_len = strlen(tree_name);
355 char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
5010cb5f 356
0d042fec
JH
357 if (tn_len) {
358 tn_len = sprintf(path_buf, "%s:", tree_name);
359 down = path_buf + tn_len;
e0eb889f 360 strcat(down, base);
5010cb5f
JH
361 }
362 else {
e0eb889f
JH
363 down = path_buf;
364 strcpy(down, base);
5010cb5f
JH
365 }
366 len = strlen(path_buf);
367
4c068a98
LT
368 while (tree_entry(tree, &entry)) {
369 strcpy(path_buf + len, entry.path);
5010cb5f 370
4c068a98 371 if (S_ISDIR(entry.mode))
e0eb889f
JH
372 /* Match "abc/" against pathspec to
373 * decide if we want to descend into "abc"
374 * directory.
375 */
4c068a98 376 strcpy(path_buf + len + entry.pathlen, "/");
e0eb889f 377
1362671f 378 if (!pathspec_matches(paths, down))
5010cb5f 379 ;
4c068a98 380 else if (S_ISREG(entry.mode))
0d042fec 381 hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
4c068a98 382 else if (S_ISDIR(entry.mode)) {
5010cb5f
JH
383 char type[20];
384 struct tree_desc sub;
385 void *data;
4c068a98 386 data = read_sha1_file(entry.sha1, type, &sub.size);
5010cb5f
JH
387 if (!data)
388 die("unable to read tree (%s)",
4c068a98 389 sha1_to_hex(entry.sha1));
5010cb5f 390 sub.buf = data;
1362671f 391 hit |= grep_tree(opt, paths, &sub, tree_name, down);
5010cb5f
JH
392 free(data);
393 }
5010cb5f
JH
394 }
395 return hit;
396}
397
1362671f 398static int grep_object(struct grep_opt *opt, const char **paths,
5010cb5f
JH
399 struct object *obj, const char *name)
400{
1974632c 401 if (obj->type == OBJ_BLOB)
0d042fec 402 return grep_sha1(opt, obj->sha1, name, 0);
1974632c 403 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
5010cb5f
JH
404 struct tree_desc tree;
405 void *data;
406 int hit;
407 data = read_object_with_reference(obj->sha1, tree_type,
408 &tree.size, NULL);
409 if (!data)
410 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
411 tree.buf = data;
1362671f 412 hit = grep_tree(opt, paths, &tree, name, "");
5010cb5f
JH
413 free(data);
414 return hit;
415 }
885a86ab 416 die("unable to grep from object of type %s", typename(obj->type));
5010cb5f
JH
417}
418
419static const char builtin_grep_usage[] =
420"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
421
088b084b
JH
422static const char emsg_invalid_context_len[] =
423"%s: invalid context length argument";
424static const char emsg_missing_context_len[] =
425"missing context length argument";
426static const char emsg_missing_argument[] =
427"option requires an argument -%s";
428
a633fca0 429int cmd_grep(int argc, const char **argv, const char *prefix)
5010cb5f 430{
5010cb5f 431 int hit = 0;
5010cb5f 432 int cached = 0;
5acd64ed 433 int seen_dashdash = 0;
5010cb5f 434 struct grep_opt opt;
1f1e895f 435 struct object_array list = { 0, 0, NULL };
1362671f 436 const char **paths = NULL;
5acd64ed 437 int i;
5010cb5f
JH
438
439 memset(&opt, 0, sizeof(opt));
0d042fec
JH
440 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
441 opt.relative = 1;
7977f0ea 442 opt.pathname = 1;
f9b9faf6 443 opt.pattern_tail = &opt.pattern_list;
5010cb5f
JH
444 opt.regflags = REG_NEWLINE;
445
446 /*
5acd64ed
JH
447 * If there is no -- then the paths must exist in the working
448 * tree. If there is no explicit pattern specified with -e or
449 * -f, we take the first unrecognized non option to be the
450 * pattern, but then what follows it must be zero or more
451 * valid refs up to the -- (if exists), and then existing
452 * paths. If there is an explicit pattern, then the first
82e5a82f 453 * unrecognized non option is the beginning of the refs list
5acd64ed 454 * that continues up to the -- (if exists), and then paths.
5010cb5f 455 */
5acd64ed 456
1362671f
JH
457 while (1 < argc) {
458 const char *arg = argv[1];
459 argc--; argv++;
460 if (!strcmp("--cached", arg)) {
461 cached = 1;
462 continue;
463 }
b8d0f5a0
JH
464 if (!strcmp("-a", arg) ||
465 !strcmp("--text", arg)) {
466 opt.binary = GREP_BINARY_TEXT;
467 continue;
468 }
1362671f
JH
469 if (!strcmp("-i", arg) ||
470 !strcmp("--ignore-case", arg)) {
471 opt.regflags |= REG_ICASE;
472 continue;
473 }
b8d0f5a0
JH
474 if (!strcmp("-I", arg)) {
475 opt.binary = GREP_BINARY_NOMATCH;
476 continue;
477 }
1362671f
JH
478 if (!strcmp("-v", arg) ||
479 !strcmp("--invert-match", arg)) {
480 opt.invert = 1;
481 continue;
482 }
483 if (!strcmp("-E", arg) ||
484 !strcmp("--extended-regexp", arg)) {
485 opt.regflags |= REG_EXTENDED;
486 continue;
487 }
07ea91d8
JH
488 if (!strcmp("-F", arg) ||
489 !strcmp("--fixed-strings", arg)) {
490 opt.fixed = 1;
491 continue;
492 }
1362671f
JH
493 if (!strcmp("-G", arg) ||
494 !strcmp("--basic-regexp", arg)) {
495 opt.regflags &= ~REG_EXTENDED;
496 continue;
497 }
498 if (!strcmp("-n", arg)) {
499 opt.linenum = 1;
500 continue;
501 }
7977f0ea
LT
502 if (!strcmp("-h", arg)) {
503 opt.pathname = 0;
504 continue;
505 }
1362671f 506 if (!strcmp("-H", arg)) {
7977f0ea 507 opt.pathname = 1;
1362671f
JH
508 continue;
509 }
510 if (!strcmp("-l", arg) ||
511 !strcmp("--files-with-matches", arg)) {
512 opt.name_only = 1;
513 continue;
514 }
e23d2d6b
JH
515 if (!strcmp("-L", arg) ||
516 !strcmp("--files-without-match", arg)) {
517 opt.unmatch_name_only = 1;
518 continue;
519 }
2c866cf1
JH
520 if (!strcmp("-c", arg) ||
521 !strcmp("--count", arg)) {
522 opt.count = 1;
523 continue;
524 }
7839a25e
JH
525 if (!strcmp("-w", arg) ||
526 !strcmp("--word-regexp", arg)) {
527 opt.word_regexp = 1;
528 continue;
529 }
f462ebb4
JH
530 if (!strncmp("-A", arg, 2) ||
531 !strncmp("-B", arg, 2) ||
532 !strncmp("-C", arg, 2) ||
533 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
1362671f 534 unsigned num;
f462ebb4
JH
535 const char *scan;
536 switch (arg[1]) {
537 case 'A': case 'B': case 'C':
538 if (!arg[2]) {
539 if (argc <= 1)
088b084b 540 die(emsg_missing_context_len);
f462ebb4
JH
541 scan = *++argv;
542 argc--;
543 }
544 else
545 scan = arg + 2;
546 break;
547 default:
548 scan = arg + 1;
549 break;
550 }
551 if (sscanf(scan, "%u", &num) != 1)
088b084b 552 die(emsg_invalid_context_len, scan);
1362671f
JH
553 switch (arg[1]) {
554 case 'A':
555 opt.post_context = num;
556 break;
f462ebb4 557 default:
1362671f
JH
558 case 'C':
559 opt.post_context = num;
560 case 'B':
561 opt.pre_context = num;
562 break;
5010cb5f 563 }
1362671f
JH
564 continue;
565 }
aa8c79ad
JH
566 if (!strcmp("-f", arg)) {
567 FILE *patterns;
568 int lno = 0;
569 char buf[1024];
570 if (argc <= 1)
088b084b 571 die(emsg_missing_argument, arg);
aa8c79ad
JH
572 patterns = fopen(argv[1], "r");
573 if (!patterns)
5acd64ed 574 die("'%s': %s", argv[1], strerror(errno));
aa8c79ad
JH
575 while (fgets(buf, sizeof(buf), patterns)) {
576 int len = strlen(buf);
577 if (buf[len-1] == '\n')
578 buf[len-1] = 0;
579 /* ignore empty line like grep does */
580 if (!buf[0])
581 continue;
83b5d2f5
JH
582 append_grep_pattern(&opt, xstrdup(buf),
583 argv[1], ++lno,
584 GREP_PATTERN);
aa8c79ad
JH
585 }
586 fclose(patterns);
587 argv++;
588 argc--;
589 continue;
590 }
79d3696c 591 if (!strcmp("--not", arg)) {
83b5d2f5
JH
592 append_grep_pattern(&opt, arg, "command line", 0,
593 GREP_NOT);
79d3696c
JH
594 continue;
595 }
596 if (!strcmp("--and", arg)) {
83b5d2f5
JH
597 append_grep_pattern(&opt, arg, "command line", 0,
598 GREP_AND);
79d3696c
JH
599 continue;
600 }
601 if (!strcmp("--or", arg))
602 continue; /* no-op */
603 if (!strcmp("(", arg)) {
83b5d2f5
JH
604 append_grep_pattern(&opt, arg, "command line", 0,
605 GREP_OPEN_PAREN);
79d3696c
JH
606 continue;
607 }
608 if (!strcmp(")", arg)) {
83b5d2f5
JH
609 append_grep_pattern(&opt, arg, "command line", 0,
610 GREP_CLOSE_PAREN);
79d3696c
JH
611 continue;
612 }
0ab7befa
JH
613 if (!strcmp("--all-match", arg)) {
614 opt.all_match = 1;
615 continue;
616 }
1362671f
JH
617 if (!strcmp("-e", arg)) {
618 if (1 < argc) {
83b5d2f5
JH
619 append_grep_pattern(&opt, argv[1],
620 "-e option", 0,
621 GREP_PATTERN);
f9b9faf6 622 argv++;
1362671f 623 argc--;
5010cb5f
JH
624 continue;
625 }
088b084b 626 die(emsg_missing_argument, arg);
1362671f 627 }
0d042fec
JH
628 if (!strcmp("--full-name", arg)) {
629 opt.relative = 0;
630 continue;
631 }
5390590f
JH
632 if (!strcmp("--", arg)) {
633 /* later processing wants to have this at argv[1] */
634 argv--;
635 argc++;
5acd64ed 636 break;
5390590f 637 }
5acd64ed 638 if (*arg == '-')
1362671f 639 usage(builtin_grep_usage);
5acd64ed
JH
640
641 /* First unrecognized non-option token */
f9b9faf6 642 if (!opt.pattern_list) {
83b5d2f5
JH
643 append_grep_pattern(&opt, arg, "command line", 0,
644 GREP_PATTERN);
1362671f
JH
645 break;
646 }
647 else {
648 /* We are looking at the first path or rev;
5acd64ed 649 * it is found at argv[1] after leaving the
1362671f
JH
650 * loop.
651 */
652 argc++; argv--;
653 break;
5010cb5f 654 }
5010cb5f 655 }
5acd64ed 656
f9b9faf6 657 if (!opt.pattern_list)
5010cb5f 658 die("no pattern given.");
07ea91d8
JH
659 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
660 die("cannot mix --fixed-strings and regexp");
83b5d2f5 661 compile_grep_patterns(&opt);
5acd64ed
JH
662
663 /* Check revs and then paths */
664 for (i = 1; i < argc; i++) {
665 const char *arg = argv[i];
1362671f 666 unsigned char sha1[20];
5acd64ed
JH
667 /* Is it a rev? */
668 if (!get_sha1(arg, sha1)) {
669 struct object *object = parse_object(sha1);
5acd64ed
JH
670 if (!object)
671 die("bad object %s", arg);
1f1e895f 672 add_object_array(object, arg, &list);
5acd64ed
JH
673 continue;
674 }
675 if (!strcmp(arg, "--")) {
676 i++;
677 seen_dashdash = 1;
678 }
679 break;
1362671f 680 }
5acd64ed
JH
681
682 /* The rest are paths */
683 if (!seen_dashdash) {
684 int j;
c39c4f47 685 for (j = i; j < argc; j++)
5acd64ed
JH
686 verify_filename(prefix, argv[j]);
687 }
688
0d042fec 689 if (i < argc) {
5acd64ed 690 paths = get_pathspec(prefix, argv + i);
0d042fec
JH
691 if (opt.prefix_length && opt.relative) {
692 /* Make sure we do not get outside of paths */
693 for (i = 0; paths[i]; i++)
694 if (strncmp(prefix, paths[i], opt.prefix_length))
695 die("git-grep: cannot generate relative filenames containing '..'");
696 }
697 }
1362671f
JH
698 else if (prefix) {
699 paths = xcalloc(2, sizeof(const char *));
700 paths[0] = prefix;
701 paths[1] = NULL;
702 }
5010cb5f 703
1f1e895f 704 if (!list.nr)
1362671f 705 return !grep_cache(&opt, paths, cached);
aa8c79ad 706
5010cb5f 707 if (cached)
aa8c79ad 708 die("both --cached and trees are given.");
5010cb5f 709
1f1e895f 710 for (i = 0; i < list.nr; i++) {
5010cb5f 711 struct object *real_obj;
1f1e895f
LT
712 real_obj = deref_tag(list.objects[i].item, NULL, 0);
713 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
5010cb5f
JH
714 hit = 1;
715 }
b48fb5b6 716 free_grep_patterns(&opt);
5010cb5f
JH
717 return !hit;
718}