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