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