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