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