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