]>
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" |
3e230fa1 | 13 | #include "parse-options.h" |
678e484b JS |
14 | #include "string-list.h" |
15 | #include "run-command.h" | |
60ecac98 | 16 | #include "userdiff.h" |
83b5d2f5 | 17 | #include "grep.h" |
493b7a08 | 18 | #include "quote.h" |
59332d13 | 19 | #include "dir.h" |
64acde94 | 20 | #include "pathspec.h" |
0281e487 | 21 | #include "submodule.h" |
74ed4371 | 22 | #include "submodule-config.h" |
5b594f45 | 23 | |
3e230fa1 | 24 | static char const * const grep_usage[] = { |
9c9b4f2f | 25 | N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"), |
3e230fa1 RS |
26 | NULL |
27 | }; | |
28 | ||
0281e487 BW |
29 | static const char *super_prefix; |
30 | static int recurse_submodules; | |
31 | static struct argv_array submodule_options = ARGV_ARRAY_INIT; | |
74ed4371 | 32 | static const char *parent_basename; |
0281e487 BW |
33 | |
34 | static int grep_submodule_launch(struct grep_opt *opt, | |
35 | const struct grep_source *gs); | |
36 | ||
89f09dd3 VL |
37 | #define GREP_NUM_THREADS_DEFAULT 8 |
38 | static int num_threads; | |
5b594f45 FK |
39 | |
40 | #ifndef NO_PTHREADS | |
89f09dd3 | 41 | static pthread_t *threads; |
5b594f45 | 42 | |
5b594f45 FK |
43 | /* We use one producer thread and THREADS consumer |
44 | * threads. The producer adds struct work_items to 'todo' and the | |
45 | * consumers pick work items from the same array. | |
46 | */ | |
9cba13ca | 47 | struct work_item { |
8f24a632 | 48 | struct grep_source source; |
5b594f45 FK |
49 | char done; |
50 | struct strbuf out; | |
51 | }; | |
52 | ||
53 | /* In the range [todo_done, todo_start) in 'todo' we have work_items | |
54 | * that have been or are processed by a consumer thread. We haven't | |
55 | * written the result for these to stdout yet. | |
56 | * | |
57 | * The work_items in [todo_start, todo_end) are waiting to be picked | |
58 | * up by a consumer thread. | |
59 | * | |
60 | * The ranges are modulo TODO_SIZE. | |
61 | */ | |
62 | #define TODO_SIZE 128 | |
63 | static struct work_item todo[TODO_SIZE]; | |
64 | static int todo_start; | |
65 | static int todo_end; | |
66 | static int todo_done; | |
67 | ||
68 | /* Has all work items been added? */ | |
69 | static int all_work_added; | |
70 | ||
71 | /* This lock protects all the variables above. */ | |
72 | static pthread_mutex_t grep_mutex; | |
73 | ||
1487a12b JH |
74 | static inline void grep_lock(void) |
75 | { | |
89f09dd3 | 76 | if (num_threads) |
1487a12b JH |
77 | pthread_mutex_lock(&grep_mutex); |
78 | } | |
79 | ||
80 | static inline void grep_unlock(void) | |
81 | { | |
89f09dd3 | 82 | if (num_threads) |
1487a12b JH |
83 | pthread_mutex_unlock(&grep_mutex); |
84 | } | |
85 | ||
5b594f45 FK |
86 | /* Signalled when a new work_item is added to todo. */ |
87 | static pthread_cond_t cond_add; | |
88 | ||
89 | /* Signalled when the result from one work_item is written to | |
90 | * stdout. | |
91 | */ | |
92 | static pthread_cond_t cond_write; | |
93 | ||
94 | /* Signalled when we are finished with everything. */ | |
95 | static pthread_cond_t cond_result; | |
96 | ||
08303c36 | 97 | static int skip_first_line; |
431d6e7b | 98 | |
9dd5245c | 99 | static void add_work(struct grep_opt *opt, enum grep_source_type type, |
55c61688 | 100 | const char *name, const char *path, const void *id) |
5b594f45 FK |
101 | { |
102 | grep_lock(); | |
103 | ||
104 | while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { | |
105 | pthread_cond_wait(&cond_write, &grep_mutex); | |
106 | } | |
107 | ||
55c61688 | 108 | grep_source_init(&todo[todo_end].source, type, name, path, id); |
9dd5245c JK |
109 | if (opt->binary != GREP_BINARY_TEXT) |
110 | grep_source_load_driver(&todo[todo_end].source); | |
5b594f45 FK |
111 | todo[todo_end].done = 0; |
112 | strbuf_reset(&todo[todo_end].out); | |
113 | todo_end = (todo_end + 1) % ARRAY_SIZE(todo); | |
114 | ||
115 | pthread_cond_signal(&cond_add); | |
116 | grep_unlock(); | |
117 | } | |
118 | ||
119 | static struct work_item *get_work(void) | |
120 | { | |
121 | struct work_item *ret; | |
122 | ||
123 | grep_lock(); | |
124 | while (todo_start == todo_end && !all_work_added) { | |
125 | pthread_cond_wait(&cond_add, &grep_mutex); | |
126 | } | |
127 | ||
128 | if (todo_start == todo_end && all_work_added) { | |
129 | ret = NULL; | |
130 | } else { | |
131 | ret = &todo[todo_start]; | |
132 | todo_start = (todo_start + 1) % ARRAY_SIZE(todo); | |
133 | } | |
134 | grep_unlock(); | |
135 | return ret; | |
136 | } | |
137 | ||
5b594f45 FK |
138 | static void work_done(struct work_item *w) |
139 | { | |
140 | int old_done; | |
141 | ||
142 | grep_lock(); | |
143 | w->done = 1; | |
144 | old_done = todo_done; | |
145 | for(; todo[todo_done].done && todo_done != todo_start; | |
146 | todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { | |
147 | w = &todo[todo_done]; | |
431d6e7b | 148 | if (w->out.len) { |
08303c36 RS |
149 | const char *p = w->out.buf; |
150 | size_t len = w->out.len; | |
151 | ||
152 | /* Skip the leading hunk mark of the first file. */ | |
153 | if (skip_first_line) { | |
154 | while (len) { | |
155 | len--; | |
156 | if (*p++ == '\n') | |
157 | break; | |
158 | } | |
159 | skip_first_line = 0; | |
160 | } | |
161 | ||
162 | write_or_die(1, p, len); | |
431d6e7b | 163 | } |
8f24a632 | 164 | grep_source_clear(&w->source); |
5b594f45 FK |
165 | } |
166 | ||
167 | if (old_done != todo_done) | |
168 | pthread_cond_signal(&cond_write); | |
169 | ||
170 | if (all_work_added && todo_done == todo_end) | |
171 | pthread_cond_signal(&cond_result); | |
172 | ||
173 | grep_unlock(); | |
174 | } | |
175 | ||
176 | static void *run(void *arg) | |
177 | { | |
178 | int hit = 0; | |
179 | struct grep_opt *opt = arg; | |
180 | ||
181 | while (1) { | |
182 | struct work_item *w = get_work(); | |
183 | if (!w) | |
184 | break; | |
185 | ||
186 | opt->output_priv = w; | |
0281e487 BW |
187 | if (w->source.type == GREP_SOURCE_SUBMODULE) |
188 | hit |= grep_submodule_launch(opt, &w->source); | |
189 | else | |
190 | hit |= grep_source(opt, &w->source); | |
8f24a632 | 191 | grep_source_clear_data(&w->source); |
5b594f45 FK |
192 | work_done(w); |
193 | } | |
bfac23d9 DM |
194 | free_grep_patterns(arg); |
195 | free(arg); | |
5b594f45 FK |
196 | |
197 | return (void*) (intptr_t) hit; | |
198 | } | |
199 | ||
200 | static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size) | |
201 | { | |
202 | struct work_item *w = opt->output_priv; | |
203 | strbuf_add(&w->out, buf, size); | |
204 | } | |
205 | ||
206 | static void start_threads(struct grep_opt *opt) | |
207 | { | |
208 | int i; | |
209 | ||
210 | pthread_mutex_init(&grep_mutex, NULL); | |
b3aeb285 | 211 | pthread_mutex_init(&grep_read_mutex, NULL); |
0579f91d | 212 | pthread_mutex_init(&grep_attr_mutex, NULL); |
5b594f45 FK |
213 | pthread_cond_init(&cond_add, NULL); |
214 | pthread_cond_init(&cond_write, NULL); | |
215 | pthread_cond_init(&cond_result, NULL); | |
78db6ea9 | 216 | grep_use_locks = 1; |
5b594f45 FK |
217 | |
218 | for (i = 0; i < ARRAY_SIZE(todo); i++) { | |
219 | strbuf_init(&todo[i].out, 0); | |
220 | } | |
221 | ||
89f09dd3 VL |
222 | threads = xcalloc(num_threads, sizeof(*threads)); |
223 | for (i = 0; i < num_threads; i++) { | |
5b594f45 FK |
224 | int err; |
225 | struct grep_opt *o = grep_opt_dup(opt); | |
226 | o->output = strbuf_out; | |
208f5aa4 | 227 | o->debug = 0; |
5b594f45 FK |
228 | compile_grep_patterns(o); |
229 | err = pthread_create(&threads[i], NULL, run, o); | |
230 | ||
231 | if (err) | |
2fc5f9f1 | 232 | die(_("grep: failed to create thread: %s"), |
5b594f45 FK |
233 | strerror(err)); |
234 | } | |
235 | } | |
236 | ||
237 | static int wait_all(void) | |
238 | { | |
239 | int hit = 0; | |
240 | int i; | |
241 | ||
242 | grep_lock(); | |
243 | all_work_added = 1; | |
244 | ||
245 | /* Wait until all work is done. */ | |
246 | while (todo_done != todo_end) | |
247 | pthread_cond_wait(&cond_result, &grep_mutex); | |
248 | ||
249 | /* Wake up all the consumer threads so they can see that there | |
250 | * is no more work to do. | |
251 | */ | |
252 | pthread_cond_broadcast(&cond_add); | |
253 | grep_unlock(); | |
254 | ||
89f09dd3 | 255 | for (i = 0; i < num_threads; i++) { |
5b594f45 FK |
256 | void *h; |
257 | pthread_join(threads[i], &h); | |
258 | hit |= (int) (intptr_t) h; | |
259 | } | |
260 | ||
89f09dd3 VL |
261 | free(threads); |
262 | ||
5b594f45 | 263 | pthread_mutex_destroy(&grep_mutex); |
b3aeb285 | 264 | pthread_mutex_destroy(&grep_read_mutex); |
0579f91d | 265 | pthread_mutex_destroy(&grep_attr_mutex); |
5b594f45 FK |
266 | pthread_cond_destroy(&cond_add); |
267 | pthread_cond_destroy(&cond_write); | |
268 | pthread_cond_destroy(&cond_result); | |
78db6ea9 | 269 | grep_use_locks = 0; |
5b594f45 FK |
270 | |
271 | return hit; | |
272 | } | |
273 | #else /* !NO_PTHREADS */ | |
5b594f45 FK |
274 | |
275 | static int wait_all(void) | |
276 | { | |
277 | return 0; | |
278 | } | |
279 | #endif | |
280 | ||
15fabd1b JH |
281 | static int grep_cmd_config(const char *var, const char *value, void *cb) |
282 | { | |
283 | int st = grep_config(var, value, cb); | |
284 | if (git_color_default_config(var, value, cb) < 0) | |
285 | st = -1; | |
89f09dd3 VL |
286 | |
287 | if (!strcmp(var, "grep.threads")) { | |
288 | num_threads = git_config_int(var, value); | |
289 | if (num_threads < 0) | |
290 | die(_("invalid number of threads specified (%d) for %s"), | |
291 | num_threads, var); | |
d1edee4a ÆAB |
292 | #ifdef NO_PTHREADS |
293 | else if (num_threads && num_threads != 1) { | |
294 | /* | |
295 | * TRANSLATORS: %s is the configuration | |
296 | * variable for tweaking threads, currently | |
297 | * grep.threads | |
298 | */ | |
299 | warning(_("no threads support, ignoring %s"), var); | |
300 | num_threads = 0; | |
301 | } | |
302 | #endif | |
89f09dd3 VL |
303 | } |
304 | ||
15fabd1b JH |
305 | return st; |
306 | } | |
307 | ||
1db11086 | 308 | static void *lock_and_read_oid_file(const struct object_id *oid, enum object_type *type, unsigned long *size) |
5f02d315 JH |
309 | { |
310 | void *data; | |
311 | ||
b3aeb285 | 312 | grep_read_lock(); |
1db11086 | 313 | data = read_sha1_file(oid->hash, type, size); |
b3aeb285 | 314 | grep_read_unlock(); |
5b594f45 FK |
315 | return data; |
316 | } | |
317 | ||
1db11086 | 318 | static int grep_oid(struct grep_opt *opt, const struct object_id *oid, |
55c61688 NTND |
319 | const char *filename, int tree_name_len, |
320 | const char *path) | |
5b594f45 FK |
321 | { |
322 | struct strbuf pathbuf = STRBUF_INIT; | |
5b594f45 | 323 | |
be80a239 | 324 | if (super_prefix) { |
0281e487 BW |
325 | strbuf_add(&pathbuf, filename, tree_name_len); |
326 | strbuf_addstr(&pathbuf, super_prefix); | |
327 | strbuf_addstr(&pathbuf, filename + tree_name_len); | |
5b594f45 FK |
328 | } else { |
329 | strbuf_addstr(&pathbuf, filename); | |
330 | } | |
331 | ||
be80a239 BW |
332 | if (opt->relative && opt->prefix_length) { |
333 | char *name = strbuf_detach(&pathbuf, NULL); | |
334 | quote_path_relative(name + tree_name_len, opt->prefix, &pathbuf); | |
335 | strbuf_insert(&pathbuf, 0, name, tree_name_len); | |
336 | free(name); | |
337 | } | |
338 | ||
5b594f45 | 339 | #ifndef NO_PTHREADS |
89f09dd3 | 340 | if (num_threads) { |
1db11086 | 341 | add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid); |
8f24a632 | 342 | strbuf_release(&pathbuf); |
5b594f45 FK |
343 | return 0; |
344 | } else | |
345 | #endif | |
346 | { | |
8f24a632 | 347 | struct grep_source gs; |
5b594f45 | 348 | int hit; |
5010cb5f | 349 | |
1db11086 | 350 | grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid); |
8f24a632 JK |
351 | strbuf_release(&pathbuf); |
352 | hit = grep_source(opt, &gs); | |
dc49cd76 | 353 | |
8f24a632 JK |
354 | grep_source_clear(&gs); |
355 | return hit; | |
5010cb5f | 356 | } |
5b594f45 FK |
357 | } |
358 | ||
359 | static int grep_file(struct grep_opt *opt, const char *filename) | |
360 | { | |
361 | struct strbuf buf = STRBUF_INIT; | |
5b594f45 | 362 | |
be80a239 BW |
363 | if (super_prefix) |
364 | strbuf_addstr(&buf, super_prefix); | |
365 | strbuf_addstr(&buf, filename); | |
366 | ||
0281e487 | 367 | if (opt->relative && opt->prefix_length) { |
be80a239 BW |
368 | char *name = strbuf_detach(&buf, NULL); |
369 | quote_path_relative(name, opt->prefix, &buf); | |
370 | free(name); | |
0281e487 | 371 | } |
5b594f45 FK |
372 | |
373 | #ifndef NO_PTHREADS | |
89f09dd3 | 374 | if (num_threads) { |
55c61688 | 375 | add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename); |
8f24a632 | 376 | strbuf_release(&buf); |
5b594f45 FK |
377 | return 0; |
378 | } else | |
379 | #endif | |
380 | { | |
8f24a632 | 381 | struct grep_source gs; |
5b594f45 | 382 | int hit; |
5b594f45 | 383 | |
55c61688 | 384 | grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename); |
8f24a632 JK |
385 | strbuf_release(&buf); |
386 | hit = grep_source(opt, &gs); | |
387 | ||
388 | grep_source_clear(&gs); | |
5b594f45 FK |
389 | return hit; |
390 | } | |
5010cb5f JH |
391 | } |
392 | ||
678e484b JS |
393 | static void append_path(struct grep_opt *opt, const void *data, size_t len) |
394 | { | |
395 | struct string_list *path_list = opt->output_priv; | |
396 | ||
397 | if (len == 1 && *(const char *)data == '\0') | |
398 | return; | |
0c72cead | 399 | string_list_append(path_list, xstrndup(data, len)); |
678e484b JS |
400 | } |
401 | ||
402 | static void run_pager(struct grep_opt *opt, const char *prefix) | |
403 | { | |
404 | struct string_list *path_list = opt->output_priv; | |
850d2fec | 405 | struct child_process child = CHILD_PROCESS_INIT; |
678e484b JS |
406 | int i, status; |
407 | ||
408 | for (i = 0; i < path_list->nr; i++) | |
850d2fec JK |
409 | argv_array_push(&child.args, path_list->items[i].string); |
410 | child.dir = prefix; | |
411 | child.use_shell = 1; | |
678e484b | 412 | |
850d2fec | 413 | status = run_command(&child); |
678e484b JS |
414 | if (status) |
415 | exit(status); | |
678e484b JS |
416 | } |
417 | ||
0281e487 | 418 | static void compile_submodule_options(const struct grep_opt *opt, |
be80a239 | 419 | const char **argv, |
0281e487 BW |
420 | int cached, int untracked, |
421 | int opt_exclude, int use_index, | |
422 | int pattern_type_arg) | |
423 | { | |
424 | struct grep_pat *pattern; | |
0281e487 BW |
425 | |
426 | if (recurse_submodules) | |
427 | argv_array_push(&submodule_options, "--recurse-submodules"); | |
428 | ||
429 | if (cached) | |
430 | argv_array_push(&submodule_options, "--cached"); | |
431 | if (!use_index) | |
432 | argv_array_push(&submodule_options, "--no-index"); | |
433 | if (untracked) | |
434 | argv_array_push(&submodule_options, "--untracked"); | |
435 | if (opt_exclude > 0) | |
436 | argv_array_push(&submodule_options, "--exclude-standard"); | |
437 | ||
438 | if (opt->invert) | |
439 | argv_array_push(&submodule_options, "-v"); | |
440 | if (opt->ignore_case) | |
441 | argv_array_push(&submodule_options, "-i"); | |
442 | if (opt->word_regexp) | |
443 | argv_array_push(&submodule_options, "-w"); | |
444 | switch (opt->binary) { | |
445 | case GREP_BINARY_NOMATCH: | |
446 | argv_array_push(&submodule_options, "-I"); | |
447 | break; | |
448 | case GREP_BINARY_TEXT: | |
449 | argv_array_push(&submodule_options, "-a"); | |
450 | break; | |
451 | default: | |
452 | break; | |
453 | } | |
454 | if (opt->allow_textconv) | |
455 | argv_array_push(&submodule_options, "--textconv"); | |
456 | if (opt->max_depth != -1) | |
457 | argv_array_pushf(&submodule_options, "--max-depth=%d", | |
458 | opt->max_depth); | |
459 | if (opt->linenum) | |
460 | argv_array_push(&submodule_options, "-n"); | |
461 | if (!opt->pathname) | |
462 | argv_array_push(&submodule_options, "-h"); | |
463 | if (!opt->relative) | |
464 | argv_array_push(&submodule_options, "--full-name"); | |
465 | if (opt->name_only) | |
466 | argv_array_push(&submodule_options, "-l"); | |
467 | if (opt->unmatch_name_only) | |
468 | argv_array_push(&submodule_options, "-L"); | |
469 | if (opt->null_following_name) | |
470 | argv_array_push(&submodule_options, "-z"); | |
471 | if (opt->count) | |
472 | argv_array_push(&submodule_options, "-c"); | |
473 | if (opt->file_break) | |
474 | argv_array_push(&submodule_options, "--break"); | |
475 | if (opt->heading) | |
476 | argv_array_push(&submodule_options, "--heading"); | |
477 | if (opt->pre_context) | |
478 | argv_array_pushf(&submodule_options, "--before-context=%d", | |
479 | opt->pre_context); | |
480 | if (opt->post_context) | |
481 | argv_array_pushf(&submodule_options, "--after-context=%d", | |
482 | opt->post_context); | |
483 | if (opt->funcname) | |
484 | argv_array_push(&submodule_options, "-p"); | |
485 | if (opt->funcbody) | |
486 | argv_array_push(&submodule_options, "-W"); | |
487 | if (opt->all_match) | |
488 | argv_array_push(&submodule_options, "--all-match"); | |
489 | if (opt->debug) | |
490 | argv_array_push(&submodule_options, "--debug"); | |
491 | if (opt->status_only) | |
492 | argv_array_push(&submodule_options, "-q"); | |
493 | ||
494 | switch (pattern_type_arg) { | |
495 | case GREP_PATTERN_TYPE_BRE: | |
496 | argv_array_push(&submodule_options, "-G"); | |
497 | break; | |
498 | case GREP_PATTERN_TYPE_ERE: | |
499 | argv_array_push(&submodule_options, "-E"); | |
500 | break; | |
501 | case GREP_PATTERN_TYPE_FIXED: | |
502 | argv_array_push(&submodule_options, "-F"); | |
503 | break; | |
504 | case GREP_PATTERN_TYPE_PCRE: | |
505 | argv_array_push(&submodule_options, "-P"); | |
506 | break; | |
507 | case GREP_PATTERN_TYPE_UNSPECIFIED: | |
508 | break; | |
374166cb ÆAB |
509 | default: |
510 | die("BUG: Added a new grep pattern type without updating switch statement"); | |
0281e487 BW |
511 | } |
512 | ||
513 | for (pattern = opt->pattern_list; pattern != NULL; | |
514 | pattern = pattern->next) { | |
515 | switch (pattern->token) { | |
516 | case GREP_PATTERN: | |
517 | argv_array_pushf(&submodule_options, "-e%s", | |
518 | pattern->pattern); | |
519 | break; | |
520 | case GREP_AND: | |
521 | case GREP_OPEN_PAREN: | |
522 | case GREP_CLOSE_PAREN: | |
523 | case GREP_NOT: | |
524 | case GREP_OR: | |
525 | argv_array_push(&submodule_options, pattern->pattern); | |
526 | break; | |
527 | /* BODY and HEAD are not used by git-grep */ | |
528 | case GREP_PATTERN_BODY: | |
529 | case GREP_PATTERN_HEAD: | |
530 | break; | |
531 | } | |
532 | } | |
533 | ||
534 | /* | |
535 | * Limit number of threads for child process to use. | |
536 | * This is to prevent potential fork-bomb behavior of git-grep as each | |
537 | * submodule process has its own thread pool. | |
538 | */ | |
539 | argv_array_pushf(&submodule_options, "--threads=%d", | |
540 | (num_threads + 1) / 2); | |
541 | ||
542 | /* Add Pathspecs */ | |
543 | argv_array_push(&submodule_options, "--"); | |
be80a239 BW |
544 | for (; *argv; argv++) |
545 | argv_array_push(&submodule_options, *argv); | |
0281e487 BW |
546 | } |
547 | ||
548 | /* | |
549 | * Launch child process to grep contents of a submodule | |
550 | */ | |
551 | static int grep_submodule_launch(struct grep_opt *opt, | |
552 | const struct grep_source *gs) | |
553 | { | |
554 | struct child_process cp = CHILD_PROCESS_INIT; | |
555 | int status, i; | |
74ed4371 BW |
556 | const char *end_of_base; |
557 | const char *name; | |
2225e1ea | 558 | struct strbuf child_output = STRBUF_INIT; |
0281e487 | 559 | |
74ed4371 BW |
560 | end_of_base = strchr(gs->name, ':'); |
561 | if (gs->identifier && end_of_base) | |
562 | name = end_of_base + 1; | |
563 | else | |
564 | name = gs->name; | |
565 | ||
0281e487 | 566 | prepare_submodule_repo_env(&cp.env_array); |
e6fac7f3 | 567 | argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT); |
0281e487 | 568 | |
be80a239 BW |
569 | if (opt->relative && opt->prefix_length) |
570 | argv_array_pushf(&cp.env_array, "%s=%s", | |
571 | GIT_TOPLEVEL_PREFIX_ENVIRONMENT, | |
572 | opt->prefix); | |
573 | ||
0281e487 BW |
574 | /* Add super prefix */ |
575 | argv_array_pushf(&cp.args, "--super-prefix=%s%s/", | |
576 | super_prefix ? super_prefix : "", | |
74ed4371 | 577 | name); |
0281e487 BW |
578 | argv_array_push(&cp.args, "grep"); |
579 | ||
74ed4371 BW |
580 | /* |
581 | * Add basename of parent project | |
582 | * When performing grep on a tree object the filename is prefixed | |
583 | * with the object's name: 'tree-name:filename'. In order to | |
584 | * provide uniformity of output we want to pass the name of the | |
585 | * parent project's object name to the submodule so the submodule can | |
586 | * prefix its output with the parent's name and not its own SHA1. | |
587 | */ | |
588 | if (gs->identifier && end_of_base) | |
589 | argv_array_pushf(&cp.args, "--parent-basename=%.*s", | |
590 | (int) (end_of_base - gs->name), | |
591 | gs->name); | |
592 | ||
0281e487 | 593 | /* Add options */ |
74ed4371 BW |
594 | for (i = 0; i < submodule_options.argc; i++) { |
595 | /* | |
596 | * If there is a tree identifier for the submodule, add the | |
597 | * rev after adding the submodule options but before the | |
598 | * pathspecs. To do this we listen for the '--' and insert the | |
599 | * sha1 before pushing the '--' onto the child process argv | |
600 | * array. | |
601 | */ | |
602 | if (gs->identifier && | |
603 | !strcmp("--", submodule_options.argv[i])) { | |
604 | argv_array_push(&cp.args, sha1_to_hex(gs->identifier)); | |
605 | } | |
606 | ||
0281e487 | 607 | argv_array_push(&cp.args, submodule_options.argv[i]); |
74ed4371 | 608 | } |
0281e487 BW |
609 | |
610 | cp.git_cmd = 1; | |
611 | cp.dir = gs->path; | |
612 | ||
613 | /* | |
614 | * Capture output to output buffer and check the return code from the | |
615 | * child process. A '0' indicates a hit, a '1' indicates no hit and | |
616 | * anything else is an error. | |
617 | */ | |
2225e1ea | 618 | status = capture_command(&cp, &child_output, 0); |
0281e487 BW |
619 | if (status && (status != 1)) { |
620 | /* flush the buffer */ | |
2225e1ea | 621 | write_or_die(1, child_output.buf, child_output.len); |
0281e487 BW |
622 | die("process for submodule '%s' failed with exit code: %d", |
623 | gs->name, status); | |
624 | } | |
625 | ||
2225e1ea BW |
626 | opt->output(opt, child_output.buf, child_output.len); |
627 | strbuf_release(&child_output); | |
0281e487 BW |
628 | /* invert the return code to make a hit equal to 1 */ |
629 | return !status; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Prep grep structures for a submodule grep | |
634 | * sha1: the sha1 of the submodule or NULL if using the working tree | |
635 | * filename: name of the submodule including tree name of parent | |
636 | * path: location of the submodule | |
637 | */ | |
638 | static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1, | |
639 | const char *filename, const char *path) | |
640 | { | |
641 | if (!is_submodule_initialized(path)) | |
642 | return 0; | |
15cdc647 | 643 | if (!is_submodule_populated_gently(path, NULL)) { |
e6fac7f3 BW |
644 | /* |
645 | * If searching history, check for the presense of the | |
646 | * submodule's gitdir before skipping the submodule. | |
647 | */ | |
648 | if (sha1) { | |
649 | const struct submodule *sub = | |
650 | submodule_from_path(null_sha1, path); | |
651 | if (sub) | |
652 | path = git_path("modules/%s", sub->name); | |
653 | ||
654 | if (!(is_directory(path) && is_git_directory(path))) | |
655 | return 0; | |
656 | } else { | |
657 | return 0; | |
658 | } | |
659 | } | |
0281e487 BW |
660 | |
661 | #ifndef NO_PTHREADS | |
662 | if (num_threads) { | |
663 | add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1); | |
664 | return 0; | |
665 | } else | |
666 | #endif | |
667 | { | |
2225e1ea | 668 | struct grep_source gs; |
0281e487 BW |
669 | int hit; |
670 | ||
2225e1ea | 671 | grep_source_init(&gs, GREP_SOURCE_SUBMODULE, |
0281e487 | 672 | filename, path, sha1); |
2225e1ea | 673 | hit = grep_submodule_launch(opt, &gs); |
0281e487 | 674 | |
2225e1ea | 675 | grep_source_clear(&gs); |
0281e487 BW |
676 | return hit; |
677 | } | |
678 | } | |
679 | ||
680 | static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, | |
681 | int cached) | |
5010cb5f JH |
682 | { |
683 | int hit = 0; | |
684 | int nr; | |
0281e487 BW |
685 | struct strbuf name = STRBUF_INIT; |
686 | int name_base_len = 0; | |
687 | if (super_prefix) { | |
688 | name_base_len = strlen(super_prefix); | |
689 | strbuf_addstr(&name, super_prefix); | |
690 | } | |
691 | ||
5010cb5f JH |
692 | read_cache(); |
693 | ||
694 | for (nr = 0; nr < active_nr; nr++) { | |
9c5e6c80 | 695 | const struct cache_entry *ce = active_cache[nr]; |
0281e487 BW |
696 | strbuf_setlen(&name, name_base_len); |
697 | strbuf_addstr(&name, ce->name); | |
698 | ||
699 | if (S_ISREG(ce->ce_mode) && | |
700 | match_pathspec(pathspec, name.buf, name.len, 0, NULL, | |
701 | S_ISDIR(ce->ce_mode) || | |
702 | S_ISGITLINK(ce->ce_mode))) { | |
703 | /* | |
704 | * If CE_VALID is on, we assume worktree file and its | |
705 | * cache entry are identical, even if worktree file has | |
706 | * been modified, so use cache version instead | |
707 | */ | |
708 | if (cached || (ce->ce_flags & CE_VALID) || | |
709 | ce_skip_worktree(ce)) { | |
710 | if (ce_stage(ce) || ce_intent_to_add(ce)) | |
711 | continue; | |
1db11086 | 712 | hit |= grep_oid(opt, &ce->oid, ce->name, |
0281e487 BW |
713 | 0, ce->name); |
714 | } else { | |
715 | hit |= grep_file(opt, ce->name); | |
716 | } | |
717 | } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && | |
718 | submodule_path_match(pathspec, name.buf, NULL)) { | |
719 | hit |= grep_submodule(opt, NULL, ce->name, ce->name); | |
720 | } else { | |
5010cb5f | 721 | continue; |
36f2587f | 722 | } |
0281e487 | 723 | |
36f2587f JH |
724 | if (ce_stage(ce)) { |
725 | do { | |
726 | nr++; | |
727 | } while (nr < active_nr && | |
728 | !strcmp(ce->name, active_cache[nr]->name)); | |
729 | nr--; /* compensate for loop control */ | |
730 | } | |
c8610a2e JH |
731 | if (hit && opt->status_only) |
732 | break; | |
5010cb5f | 733 | } |
0281e487 BW |
734 | |
735 | strbuf_release(&name); | |
5010cb5f JH |
736 | return hit; |
737 | } | |
738 | ||
f34bbc15 | 739 | static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, |
55c61688 NTND |
740 | struct tree_desc *tree, struct strbuf *base, int tn_len, |
741 | int check_attr) | |
5010cb5f | 742 | { |
d688cf07 NTND |
743 | int hit = 0; |
744 | enum interesting match = entry_not_interesting; | |
4c068a98 | 745 | struct name_entry entry; |
e5e062b6 | 746 | int old_baselen = base->len; |
74ed4371 BW |
747 | struct strbuf name = STRBUF_INIT; |
748 | int name_base_len = 0; | |
749 | if (super_prefix) { | |
750 | strbuf_addstr(&name, super_prefix); | |
751 | name_base_len = name.len; | |
752 | } | |
5010cb5f | 753 | |
4c068a98 | 754 | while (tree_entry(tree, &entry)) { |
0de16337 | 755 | int te_len = tree_entry_len(&entry); |
e5e062b6 | 756 | |
d688cf07 | 757 | if (match != all_entries_interesting) { |
74ed4371 BW |
758 | strbuf_addstr(&name, base->buf + tn_len); |
759 | match = tree_entry_interesting(&entry, &name, | |
760 | 0, pathspec); | |
761 | strbuf_setlen(&name, name_base_len); | |
762 | ||
d688cf07 | 763 | if (match == all_entries_not_interesting) |
97d0b74a | 764 | break; |
d688cf07 | 765 | if (match == entry_not_interesting) |
1376e507 NTND |
766 | continue; |
767 | } | |
5010cb5f | 768 | |
1376e507 | 769 | strbuf_add(base, entry.path, te_len); |
e0eb889f | 770 | |
1376e507 | 771 | if (S_ISREG(entry.mode)) { |
1db11086 | 772 | hit |= grep_oid(opt, entry.oid, base->buf, tn_len, |
55c61688 | 773 | check_attr ? base->buf + tn_len : NULL); |
74ed4371 | 774 | } else if (S_ISDIR(entry.mode)) { |
21666f1a | 775 | enum object_type type; |
5010cb5f JH |
776 | struct tree_desc sub; |
777 | void *data; | |
6fda5e51 LT |
778 | unsigned long size; |
779 | ||
1db11086 | 780 | data = lock_and_read_oid_file(entry.oid, &type, &size); |
5010cb5f | 781 | if (!data) |
2fc5f9f1 | 782 | die(_("unable to read tree (%s)"), |
7d924c91 | 783 | oid_to_hex(entry.oid)); |
1376e507 NTND |
784 | |
785 | strbuf_addch(base, '/'); | |
6fda5e51 | 786 | init_tree_desc(&sub, data, size); |
55c61688 NTND |
787 | hit |= grep_tree(opt, pathspec, &sub, base, tn_len, |
788 | check_attr); | |
5010cb5f | 789 | free(data); |
74ed4371 BW |
790 | } else if (recurse_submodules && S_ISGITLINK(entry.mode)) { |
791 | hit |= grep_submodule(opt, entry.oid->hash, base->buf, | |
792 | base->buf + tn_len); | |
5010cb5f | 793 | } |
74ed4371 | 794 | |
e5e062b6 NTND |
795 | strbuf_setlen(base, old_baselen); |
796 | ||
c8610a2e JH |
797 | if (hit && opt->status_only) |
798 | break; | |
5010cb5f | 799 | } |
74ed4371 BW |
800 | |
801 | strbuf_release(&name); | |
5010cb5f JH |
802 | return hit; |
803 | } | |
804 | ||
f34bbc15 | 805 | static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, |
9e0c3c4f | 806 | struct object *obj, const char *name, const char *path) |
5010cb5f | 807 | { |
1974632c | 808 | if (obj->type == OBJ_BLOB) |
1db11086 | 809 | return grep_oid(opt, &obj->oid, name, 0, path); |
1974632c | 810 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
5010cb5f JH |
811 | struct tree_desc tree; |
812 | void *data; | |
6fda5e51 | 813 | unsigned long size; |
e5e062b6 NTND |
814 | struct strbuf base; |
815 | int hit, len; | |
816 | ||
b3aeb285 | 817 | grep_read_lock(); |
f2fd0760 | 818 | data = read_object_with_reference(obj->oid.hash, tree_type, |
6fda5e51 | 819 | &size, NULL); |
b3aeb285 | 820 | grep_read_unlock(); |
8cb5775b | 821 | |
5010cb5f | 822 | if (!data) |
f2fd0760 | 823 | die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid)); |
e5e062b6 | 824 | |
74ed4371 BW |
825 | /* Use parent's name as base when recursing submodules */ |
826 | if (recurse_submodules && parent_basename) | |
827 | name = parent_basename; | |
828 | ||
e5e062b6 NTND |
829 | len = name ? strlen(name) : 0; |
830 | strbuf_init(&base, PATH_MAX + len + 1); | |
831 | if (len) { | |
832 | strbuf_add(&base, name, len); | |
833 | strbuf_addch(&base, ':'); | |
834 | } | |
6fda5e51 | 835 | init_tree_desc(&tree, data, size); |
55c61688 NTND |
836 | hit = grep_tree(opt, pathspec, &tree, &base, base.len, |
837 | obj->type == OBJ_COMMIT); | |
e5e062b6 | 838 | strbuf_release(&base); |
5010cb5f JH |
839 | free(data); |
840 | return hit; | |
841 | } | |
2fc5f9f1 | 842 | die(_("unable to grep from object of type %s"), typename(obj->type)); |
5010cb5f JH |
843 | } |
844 | ||
f34bbc15 | 845 | static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec, |
30d00c39 JN |
846 | const struct object_array *list) |
847 | { | |
848 | unsigned int i; | |
849 | int hit = 0; | |
850 | const unsigned int nr = list->nr; | |
851 | ||
852 | for (i = 0; i < nr; i++) { | |
853 | struct object *real_obj; | |
854 | real_obj = deref_tag(list->objects[i].item, NULL, 0); | |
74ed4371 BW |
855 | |
856 | /* load the gitmodules file for this rev */ | |
857 | if (recurse_submodules) { | |
858 | submodule_free(); | |
859 | gitmodules_config_sha1(real_obj->oid.hash); | |
860 | } | |
9e0c3c4f | 861 | if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path)) { |
30d00c39 JN |
862 | hit = 1; |
863 | if (opt->status_only) | |
864 | break; | |
865 | } | |
866 | } | |
867 | return hit; | |
868 | } | |
869 | ||
dbfae86a | 870 | static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, |
85975c0c | 871 | int exc_std, int use_index) |
59332d13 JH |
872 | { |
873 | struct dir_struct dir; | |
874 | int i, hit = 0; | |
875 | ||
876 | memset(&dir, 0, sizeof(dir)); | |
85975c0c JK |
877 | if (!use_index) |
878 | dir.flags |= DIR_NO_GITLINKS; | |
0a93fb8a JH |
879 | if (exc_std) |
880 | setup_standard_excludes(&dir); | |
59332d13 | 881 | |
7327d3d1 | 882 | fill_directory(&dir, pathspec); |
59332d13 | 883 | for (i = 0; i < dir.nr; i++) { |
ebb32893 | 884 | if (!dir_path_match(dir.entries[i], pathspec, 0, NULL)) |
9d8b831b | 885 | continue; |
59332d13 JH |
886 | hit |= grep_file(opt, dir.entries[i]->name); |
887 | if (hit && opt->status_only) | |
888 | break; | |
889 | } | |
59332d13 JH |
890 | return hit; |
891 | } | |
892 | ||
ff3c7f9a RS |
893 | static int context_callback(const struct option *opt, const char *arg, |
894 | int unset) | |
3e230fa1 RS |
895 | { |
896 | struct grep_opt *grep_opt = opt->value; | |
897 | int value; | |
898 | const char *endp; | |
899 | ||
900 | if (unset) { | |
901 | grep_opt->pre_context = grep_opt->post_context = 0; | |
902 | return 0; | |
903 | } | |
904 | value = strtol(arg, (char **)&endp, 10); | |
905 | if (*endp) { | |
2fc5f9f1 | 906 | return error(_("switch `%c' expects a numerical value"), |
3e230fa1 RS |
907 | opt->short_name); |
908 | } | |
909 | grep_opt->pre_context = grep_opt->post_context = value; | |
910 | return 0; | |
911 | } | |
912 | ||
ff3c7f9a | 913 | static int file_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
914 | { |
915 | struct grep_opt *grep_opt = opt->value; | |
c41dd2fd | 916 | int from_stdin = !strcmp(arg, "-"); |
3e230fa1 RS |
917 | FILE *patterns; |
918 | int lno = 0; | |
cfe370c6 | 919 | struct strbuf sb = STRBUF_INIT; |
3e230fa1 | 920 | |
c41dd2fd | 921 | patterns = from_stdin ? stdin : fopen(arg, "r"); |
3e230fa1 | 922 | if (!patterns) |
2fc5f9f1 | 923 | die_errno(_("cannot open '%s'"), arg); |
a5518431 | 924 | while (strbuf_getline(&sb, patterns) == 0) { |
3e230fa1 RS |
925 | /* ignore empty line like grep does */ |
926 | if (sb.len == 0) | |
927 | continue; | |
ed40a095 | 928 | |
ec830611 RS |
929 | append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno, |
930 | GREP_PATTERN); | |
3e230fa1 | 931 | } |
c41dd2fd RS |
932 | if (!from_stdin) |
933 | fclose(patterns); | |
3e230fa1 RS |
934 | strbuf_release(&sb); |
935 | return 0; | |
936 | } | |
937 | ||
ff3c7f9a | 938 | static int not_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
939 | { |
940 | struct grep_opt *grep_opt = opt->value; | |
941 | append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); | |
942 | return 0; | |
943 | } | |
944 | ||
ff3c7f9a | 945 | static int and_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
946 | { |
947 | struct grep_opt *grep_opt = opt->value; | |
948 | append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); | |
949 | return 0; | |
950 | } | |
951 | ||
ff3c7f9a | 952 | static int open_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
953 | { |
954 | struct grep_opt *grep_opt = opt->value; | |
955 | append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); | |
956 | return 0; | |
957 | } | |
958 | ||
ff3c7f9a | 959 | static int close_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
960 | { |
961 | struct grep_opt *grep_opt = opt->value; | |
962 | append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); | |
963 | return 0; | |
964 | } | |
965 | ||
ff3c7f9a RS |
966 | static int pattern_callback(const struct option *opt, const char *arg, |
967 | int unset) | |
3e230fa1 RS |
968 | { |
969 | struct grep_opt *grep_opt = opt->value; | |
970 | append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); | |
971 | return 0; | |
972 | } | |
5010cb5f | 973 | |
a633fca0 | 974 | int cmd_grep(int argc, const char **argv, const char *prefix) |
5010cb5f | 975 | { |
5010cb5f | 976 | int hit = 0; |
0a93fb8a | 977 | int cached = 0, untracked = 0, opt_exclude = -1; |
5acd64ed | 978 | int seen_dashdash = 0; |
bbc09c22 | 979 | int external_grep_allowed__ignored; |
0af88c15 | 980 | const char *show_in_pager = NULL, *default_pager = "dummy"; |
5010cb5f | 981 | struct grep_opt opt; |
3cd47459 | 982 | struct object_array list = OBJECT_ARRAY_INIT; |
f34bbc15 | 983 | struct pathspec pathspec; |
183113a5 | 984 | struct string_list path_list = STRING_LIST_INIT_NODUP; |
5acd64ed | 985 | int i; |
3e230fa1 | 986 | int dummy; |
ff38d1a9 | 987 | int use_index = 1; |
84befcd0 | 988 | int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED; |
131f3c96 | 989 | int allow_revs; |
cca2c172 | 990 | |
3e230fa1 | 991 | struct option options[] = { |
d5d09d47 | 992 | OPT_BOOL(0, "cached", &cached, |
4b407bc5 | 993 | N_("search in index instead of in the work tree")), |
cbb08c2e | 994 | OPT_NEGBIT(0, "no-index", &use_index, |
f63cf8c9 | 995 | N_("find in contents not managed by git"), 1), |
d5d09d47 | 996 | OPT_BOOL(0, "untracked", &untracked, |
4b407bc5 | 997 | N_("search in both tracked and untracked files")), |
0a93fb8a | 998 | OPT_SET_INT(0, "exclude-standard", &opt_exclude, |
77fdb8a8 | 999 | N_("ignore files specified via '.gitignore'"), 1), |
0281e487 | 1000 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules, |
4fb1c6aa | 1001 | N_("recursively search in each submodule")), |
74ed4371 BW |
1002 | OPT_STRING(0, "parent-basename", &parent_basename, |
1003 | N_("basename"), | |
1004 | N_("prepend parent project's basename to output")), | |
3e230fa1 | 1005 | OPT_GROUP(""), |
d5d09d47 | 1006 | OPT_BOOL('v', "invert-match", &opt.invert, |
4b407bc5 | 1007 | N_("show non-matching lines")), |
d5d09d47 | 1008 | OPT_BOOL('i', "ignore-case", &opt.ignore_case, |
4b407bc5 | 1009 | N_("case insensitive matching")), |
d5d09d47 | 1010 | OPT_BOOL('w', "word-regexp", &opt.word_regexp, |
4b407bc5 | 1011 | N_("match patterns only at word boundaries")), |
3e230fa1 | 1012 | OPT_SET_INT('a', "text", &opt.binary, |
4b407bc5 | 1013 | N_("process binary files as text"), GREP_BINARY_TEXT), |
3e230fa1 | 1014 | OPT_SET_INT('I', NULL, &opt.binary, |
4b407bc5 | 1015 | N_("don't match patterns in binary files"), |
3e230fa1 | 1016 | GREP_BINARY_NOMATCH), |
335ec3bf JK |
1017 | OPT_BOOL(0, "textconv", &opt.allow_textconv, |
1018 | N_("process binary files with textconv filters")), | |
4b407bc5 NTND |
1019 | { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"), |
1020 | N_("descend at most <depth> levels"), PARSE_OPT_NONEG, | |
a91f453f | 1021 | NULL, 1 }, |
3e230fa1 | 1022 | OPT_GROUP(""), |
84befcd0 | 1023 | OPT_SET_INT('E', "extended-regexp", &pattern_type_arg, |
4b407bc5 | 1024 | N_("use extended POSIX regular expressions"), |
84befcd0 S |
1025 | GREP_PATTERN_TYPE_ERE), |
1026 | OPT_SET_INT('G', "basic-regexp", &pattern_type_arg, | |
4b407bc5 | 1027 | N_("use basic POSIX regular expressions (default)"), |
84befcd0 S |
1028 | GREP_PATTERN_TYPE_BRE), |
1029 | OPT_SET_INT('F', "fixed-strings", &pattern_type_arg, | |
4b407bc5 | 1030 | N_("interpret patterns as fixed strings"), |
84befcd0 S |
1031 | GREP_PATTERN_TYPE_FIXED), |
1032 | OPT_SET_INT('P', "perl-regexp", &pattern_type_arg, | |
4b407bc5 | 1033 | N_("use Perl-compatible regular expressions"), |
84befcd0 | 1034 | GREP_PATTERN_TYPE_PCRE), |
3e230fa1 | 1035 | OPT_GROUP(""), |
d5d09d47 | 1036 | OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")), |
4b407bc5 NTND |
1037 | OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1), |
1038 | OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1), | |
3e230fa1 | 1039 | OPT_NEGBIT(0, "full-name", &opt.relative, |
4b407bc5 | 1040 | N_("show filenames relative to top directory"), 1), |
d5d09d47 | 1041 | OPT_BOOL('l', "files-with-matches", &opt.name_only, |
4b407bc5 | 1042 | N_("show only filenames instead of matching lines")), |
d5d09d47 | 1043 | OPT_BOOL(0, "name-only", &opt.name_only, |
4b407bc5 | 1044 | N_("synonym for --files-with-matches")), |
d5d09d47 | 1045 | OPT_BOOL('L', "files-without-match", |
3e230fa1 | 1046 | &opt.unmatch_name_only, |
4b407bc5 | 1047 | N_("show only the names of files without match")), |
d5d09d47 | 1048 | OPT_BOOL('z', "null", &opt.null_following_name, |
4b407bc5 | 1049 | N_("print NUL after filenames")), |
d5d09d47 | 1050 | OPT_BOOL('c', "count", &opt.count, |
4b407bc5 NTND |
1051 | N_("show the number of matches instead of matching lines")), |
1052 | OPT__COLOR(&opt.color, N_("highlight matches")), | |
d5d09d47 | 1053 | OPT_BOOL(0, "break", &opt.file_break, |
4b407bc5 | 1054 | N_("print empty line between matches from different files")), |
d5d09d47 | 1055 | OPT_BOOL(0, "heading", &opt.heading, |
4b407bc5 | 1056 | N_("show filename only once above matches from same file")), |
3e230fa1 | 1057 | OPT_GROUP(""), |
4b407bc5 NTND |
1058 | OPT_CALLBACK('C', "context", &opt, N_("n"), |
1059 | N_("show <n> context lines before and after matches"), | |
3e230fa1 | 1060 | context_callback), |
317f63c2 | 1061 | OPT_INTEGER('B', "before-context", &opt.pre_context, |
4b407bc5 | 1062 | N_("show <n> context lines before matches")), |
317f63c2 | 1063 | OPT_INTEGER('A', "after-context", &opt.post_context, |
4b407bc5 | 1064 | N_("show <n> context lines after matches")), |
89f09dd3 VL |
1065 | OPT_INTEGER(0, "threads", &num_threads, |
1066 | N_("use <n> worker threads")), | |
4b407bc5 | 1067 | OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"), |
3e230fa1 | 1068 | context_callback), |
d5d09d47 | 1069 | OPT_BOOL('p', "show-function", &opt.funcname, |
4b407bc5 | 1070 | N_("show a line with the function name before matches")), |
d5d09d47 | 1071 | OPT_BOOL('W', "function-context", &opt.funcbody, |
4b407bc5 | 1072 | N_("show the surrounding function")), |
3e230fa1 | 1073 | OPT_GROUP(""), |
4b407bc5 NTND |
1074 | OPT_CALLBACK('f', NULL, &opt, N_("file"), |
1075 | N_("read patterns from file"), file_callback), | |
1076 | { OPTION_CALLBACK, 'e', NULL, &opt, N_("pattern"), | |
1077 | N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback }, | |
3e230fa1 | 1078 | { OPTION_CALLBACK, 0, "and", &opt, NULL, |
4b407bc5 | 1079 | N_("combine patterns specified with -e"), |
3e230fa1 | 1080 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback }, |
d5d09d47 | 1081 | OPT_BOOL(0, "or", &dummy, ""), |
3e230fa1 RS |
1082 | { OPTION_CALLBACK, 0, "not", &opt, NULL, "", |
1083 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback }, | |
1084 | { OPTION_CALLBACK, '(', NULL, &opt, NULL, "", | |
1085 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
1086 | open_callback }, | |
1087 | { OPTION_CALLBACK, ')', NULL, &opt, NULL, "", | |
1088 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
1089 | close_callback }, | |
d52ee6e6 | 1090 | OPT__QUIET(&opt.status_only, |
4b407bc5 | 1091 | N_("indicate hit with exit status without output")), |
d5d09d47 | 1092 | OPT_BOOL(0, "all-match", &opt.all_match, |
4b407bc5 | 1093 | N_("show only matches from files that match all patterns")), |
17bf35a3 | 1094 | { OPTION_SET_INT, 0, "debug", &opt.debug, NULL, |
3d7535e4 | 1095 | N_("show parse tree for grep expression"), |
17bf35a3 | 1096 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1 }, |
3e230fa1 | 1097 | OPT_GROUP(""), |
0af88c15 | 1098 | { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager, |
4b407bc5 | 1099 | N_("pager"), N_("show matching files in the pager"), |
0af88c15 | 1100 | PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager }, |
d5d09d47 SB |
1101 | OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored, |
1102 | N_("allow calling of grep(1) (ignored by this build)")), | |
3e230fa1 RS |
1103 | OPT_END() |
1104 | }; | |
5010cb5f | 1105 | |
15fabd1b JH |
1106 | init_grep_defaults(); |
1107 | git_config(grep_cmd_config, NULL); | |
1108 | grep_init(&opt, prefix); | |
0281e487 | 1109 | super_prefix = get_super_prefix(); |
7e8f59d5 | 1110 | |
5010cb5f | 1111 | /* |
5acd64ed JH |
1112 | * If there is no -- then the paths must exist in the working |
1113 | * tree. If there is no explicit pattern specified with -e or | |
1114 | * -f, we take the first unrecognized non option to be the | |
1115 | * pattern, but then what follows it must be zero or more | |
1116 | * valid refs up to the -- (if exists), and then existing | |
1117 | * paths. If there is an explicit pattern, then the first | |
82e5a82f | 1118 | * unrecognized non option is the beginning of the refs list |
5acd64ed | 1119 | * that continues up to the -- (if exists), and then paths. |
5010cb5f | 1120 | */ |
37782920 | 1121 | argc = parse_options(argc, argv, prefix, options, grep_usage, |
3e230fa1 | 1122 | PARSE_OPT_KEEP_DASHDASH | |
44415499 | 1123 | PARSE_OPT_STOP_AT_NON_OPTION); |
c5c31d33 | 1124 | grep_commit_pattern_type(pattern_type_arg, &opt); |
3e230fa1 | 1125 | |
ecd9ba61 TG |
1126 | if (use_index && !startup_info->have_repository) { |
1127 | int fallback = 0; | |
1128 | git_config_get_bool("grep.fallbacktonoindex", &fallback); | |
1129 | if (fallback) | |
1130 | use_index = 0; | |
1131 | else | |
1132 | /* die the same way as if we did it at the beginning */ | |
1133 | setup_git_directory(); | |
1134 | } | |
59332d13 | 1135 | |
1123c67c JK |
1136 | /* |
1137 | * skip a -- separator; we know it cannot be | |
1138 | * separating revisions from pathnames if | |
1139 | * we haven't even had any patterns yet | |
1140 | */ | |
1141 | if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) { | |
1142 | argv++; | |
1143 | argc--; | |
1144 | } | |
1145 | ||
3e230fa1 RS |
1146 | /* First unrecognized non-option token */ |
1147 | if (argc > 0 && !opt.pattern_list) { | |
1148 | append_grep_pattern(&opt, argv[0], "command line", 0, | |
1149 | GREP_PATTERN); | |
1150 | argv++; | |
1151 | argc--; | |
5010cb5f | 1152 | } |
5acd64ed | 1153 | |
0af88c15 JS |
1154 | if (show_in_pager == default_pager) |
1155 | show_in_pager = git_pager(1); | |
678e484b | 1156 | if (show_in_pager) { |
e7b082a4 | 1157 | opt.color = 0; |
0af88c15 JS |
1158 | opt.name_only = 1; |
1159 | opt.null_following_name = 1; | |
1160 | opt.output_priv = &path_list; | |
1161 | opt.output = append_path; | |
0c72cead | 1162 | string_list_append(&path_list, show_in_pager); |
678e484b JS |
1163 | } |
1164 | ||
f9b9faf6 | 1165 | if (!opt.pattern_list) |
2fc5f9f1 | 1166 | die(_("no pattern given.")); |
5183bf67 BC |
1167 | if (!opt.fixed && opt.ignore_case) |
1168 | opt.regflags |= REG_ICASE; | |
5b594f45 | 1169 | |
83b5d2f5 | 1170 | compile_grep_patterns(&opt); |
5acd64ed | 1171 | |
b5b81136 JK |
1172 | /* |
1173 | * We have to find "--" in a separate pass, because its presence | |
1174 | * influences how we will parse arguments that come before it. | |
1175 | */ | |
1176 | for (i = 0; i < argc; i++) { | |
1177 | if (!strcmp(argv[i], "--")) { | |
1178 | seen_dashdash = 1; | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * Resolve any rev arguments. If we have a dashdash, then everything up | |
1185 | * to it must resolve as a rev. If not, then we stop at the first | |
1186 | * non-rev and assume everything else is a path. | |
1187 | */ | |
131f3c96 | 1188 | allow_revs = use_index && !untracked; |
3e230fa1 | 1189 | for (i = 0; i < argc; i++) { |
5acd64ed | 1190 | const char *arg = argv[i]; |
1db11086 | 1191 | struct object_id oid; |
afa15f3c | 1192 | struct object_context oc; |
20d6421c JK |
1193 | struct object *object; |
1194 | ||
dca3b5f5 JT |
1195 | if (!strcmp(arg, "--")) { |
1196 | i++; | |
dca3b5f5 JT |
1197 | break; |
1198 | } | |
20d6421c | 1199 | |
131f3c96 | 1200 | if (!allow_revs) { |
d0ffc069 | 1201 | if (seen_dashdash) |
131f3c96 | 1202 | die(_("--no-index or --untracked cannot be used with revs")); |
d0ffc069 JK |
1203 | break; |
1204 | } | |
1205 | ||
e1fae930 | 1206 | if (get_sha1_with_context(arg, 0, oid.hash, &oc)) { |
b5b81136 JK |
1207 | if (seen_dashdash) |
1208 | die(_("unable to resolve revision: %s"), arg); | |
20d6421c | 1209 | break; |
b5b81136 | 1210 | } |
20d6421c | 1211 | |
e1fae930 | 1212 | object = parse_object_or_die(oid.hash, arg); |
20d6421c JK |
1213 | if (!seen_dashdash) |
1214 | verify_non_filename(prefix, arg); | |
1215 | add_object_array_with_path(object, arg, &list, oc.mode, oc.path); | |
1362671f | 1216 | } |
5acd64ed | 1217 | |
b5b81136 JK |
1218 | /* |
1219 | * Anything left over is presumed to be a path. But in the non-dashdash | |
1220 | * "do what I mean" case, we verify and complain when that isn't true. | |
1221 | */ | |
a0fe2b0d JK |
1222 | if (!seen_dashdash) { |
1223 | int j; | |
1224 | for (j = i; j < argc; j++) | |
131f3c96 | 1225 | verify_filename(prefix, argv[j], j == i && allow_revs); |
a0fe2b0d JK |
1226 | } |
1227 | ||
1228 | parse_pathspec(&pathspec, 0, | |
1229 | PATHSPEC_PREFER_CWD | | |
1230 | (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0), | |
1231 | prefix, argv + i); | |
1232 | pathspec.max_depth = opt.max_depth; | |
1233 | pathspec.recursive = 1; | |
1234 | ||
53b8d931 | 1235 | #ifndef NO_PTHREADS |
044b1f3c | 1236 | if (list.nr || cached || show_in_pager) |
89f09dd3 VL |
1237 | num_threads = 0; |
1238 | else if (num_threads == 0) | |
1239 | num_threads = GREP_NUM_THREADS_DEFAULT; | |
1240 | else if (num_threads < 0) | |
1241 | die(_("invalid number of threads specified (%d)"), num_threads); | |
53b8d931 | 1242 | #else |
d1edee4a ÆAB |
1243 | if (num_threads) |
1244 | warning(_("no threads support, ignoring --threads")); | |
89f09dd3 | 1245 | num_threads = 0; |
53b8d931 TR |
1246 | #endif |
1247 | ||
53b8d931 | 1248 | #ifndef NO_PTHREADS |
89f09dd3 | 1249 | if (num_threads) { |
50dd0f2f AY |
1250 | if (!(opt.name_only || opt.unmatch_name_only || opt.count) |
1251 | && (opt.pre_context || opt.post_context || | |
1252 | opt.file_break || opt.funcbody)) | |
53b8d931 TR |
1253 | skip_first_line = 1; |
1254 | start_threads(&opt); | |
1255 | } | |
1256 | #endif | |
1257 | ||
0281e487 BW |
1258 | if (recurse_submodules) { |
1259 | gitmodules_config(); | |
be80a239 | 1260 | compile_submodule_options(&opt, argv + i, cached, untracked, |
0281e487 BW |
1261 | opt_exclude, use_index, |
1262 | pattern_type_arg); | |
1263 | } | |
1264 | ||
678e484b | 1265 | if (show_in_pager && (cached || list.nr)) |
e4fe4ba5 | 1266 | die(_("--open-files-in-pager only works on the worktree")); |
678e484b JS |
1267 | |
1268 | if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) { | |
1269 | const char *pager = path_list.items[0].string; | |
1270 | int len = strlen(pager); | |
1271 | ||
1272 | if (len > 4 && is_dir_sep(pager[len - 5])) | |
1273 | pager += len - 4; | |
1274 | ||
f7febbea JS |
1275 | if (opt.ignore_case && !strcmp("less", pager)) |
1276 | string_list_append(&path_list, "-I"); | |
1277 | ||
678e484b JS |
1278 | if (!strcmp("less", pager) || !strcmp("vi", pager)) { |
1279 | struct strbuf buf = STRBUF_INIT; | |
1280 | strbuf_addf(&buf, "+/%s%s", | |
1281 | strcmp("less", pager) ? "" : "*", | |
1282 | opt.pattern_list->pattern); | |
0c72cead | 1283 | string_list_append(&path_list, buf.buf); |
678e484b JS |
1284 | strbuf_detach(&buf, NULL); |
1285 | } | |
1286 | } | |
1287 | ||
74ed4371 | 1288 | if (recurse_submodules && (!use_index || untracked)) |
0281e487 BW |
1289 | die(_("option not supported with --recurse-submodules.")); |
1290 | ||
c2048f0b | 1291 | if (!show_in_pager && !opt.status_only) |
678e484b JS |
1292 | setup_pager(); |
1293 | ||
0a93fb8a | 1294 | if (!use_index && (untracked || cached)) |
dbfae86a | 1295 | die(_("--cached or --untracked cannot be used with --no-index.")); |
678e484b | 1296 | |
0a93fb8a | 1297 | if (!use_index || untracked) { |
0a93fb8a | 1298 | int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude; |
85975c0c | 1299 | hit = grep_directory(&opt, &pathspec, use_exclude, use_index); |
dbfae86a | 1300 | } else if (0 <= opt_exclude) { |
9fddaf78 | 1301 | die(_("--[no-]exclude-standard cannot be used for tracked contents.")); |
685359cf | 1302 | } else if (!list.nr) { |
6577f542 NTND |
1303 | if (!cached) |
1304 | setup_work_tree(); | |
5b594f45 | 1305 | |
f34bbc15 | 1306 | hit = grep_cache(&opt, &pathspec, cached); |
685359cf JS |
1307 | } else { |
1308 | if (cached) | |
2fc5f9f1 | 1309 | die(_("both --cached and trees are given.")); |
f34bbc15 | 1310 | hit = grep_objects(&opt, &pathspec, &list); |
5010cb5f | 1311 | } |
5b594f45 | 1312 | |
89f09dd3 | 1313 | if (num_threads) |
5b594f45 | 1314 | hit |= wait_all(); |
678e484b JS |
1315 | if (hit && show_in_pager) |
1316 | run_pager(&opt, prefix); | |
7861fa07 | 1317 | clear_pathspec(&pathspec); |
b48fb5b6 | 1318 | free_grep_patterns(&opt); |
5010cb5f JH |
1319 | return !hit; |
1320 | } |