]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Builtin "git grep" | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
6 | ||
7 | #define USE_THE_REPOSITORY_VARIABLE | |
8 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
9 | ||
10 | #include "builtin.h" | |
11 | #include "abspath.h" | |
12 | #include "gettext.h" | |
13 | #include "hex.h" | |
14 | #include "config.h" | |
15 | #include "tag.h" | |
16 | #include "tree-walk.h" | |
17 | #include "parse-options.h" | |
18 | #include "string-list.h" | |
19 | #include "run-command.h" | |
20 | #include "grep.h" | |
21 | #include "quote.h" | |
22 | #include "dir.h" | |
23 | #include "pathspec.h" | |
24 | #include "setup.h" | |
25 | #include "submodule.h" | |
26 | #include "submodule-config.h" | |
27 | #include "object-file.h" | |
28 | #include "object-name.h" | |
29 | #include "object-store.h" | |
30 | #include "packfile.h" | |
31 | #include "pager.h" | |
32 | #include "path.h" | |
33 | #include "read-cache-ll.h" | |
34 | #include "write-or-die.h" | |
35 | ||
36 | static const char *grep_prefix; | |
37 | ||
38 | static char const * const grep_usage[] = { | |
39 | N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"), | |
40 | NULL | |
41 | }; | |
42 | ||
43 | static int recurse_submodules; | |
44 | ||
45 | static int num_threads; | |
46 | ||
47 | static pthread_t *threads; | |
48 | ||
49 | /* We use one producer thread and THREADS consumer | |
50 | * threads. The producer adds struct work_items to 'todo' and the | |
51 | * consumers pick work items from the same array. | |
52 | */ | |
53 | struct work_item { | |
54 | struct grep_source source; | |
55 | char done; | |
56 | struct strbuf out; | |
57 | }; | |
58 | ||
59 | /* In the range [todo_done, todo_start) in 'todo' we have work_items | |
60 | * that have been or are processed by a consumer thread. We haven't | |
61 | * written the result for these to stdout yet. | |
62 | * | |
63 | * The work_items in [todo_start, todo_end) are waiting to be picked | |
64 | * up by a consumer thread. | |
65 | * | |
66 | * The ranges are modulo TODO_SIZE. | |
67 | */ | |
68 | #define TODO_SIZE 128 | |
69 | static struct work_item todo[TODO_SIZE]; | |
70 | static int todo_start; | |
71 | static int todo_end; | |
72 | static int todo_done; | |
73 | ||
74 | /* Has all work items been added? */ | |
75 | static int all_work_added; | |
76 | ||
77 | static struct repository **repos_to_free; | |
78 | static size_t repos_to_free_nr, repos_to_free_alloc; | |
79 | ||
80 | /* This lock protects all the variables above. */ | |
81 | static pthread_mutex_t grep_mutex; | |
82 | ||
83 | static inline void grep_lock(void) | |
84 | { | |
85 | pthread_mutex_lock(&grep_mutex); | |
86 | } | |
87 | ||
88 | static inline void grep_unlock(void) | |
89 | { | |
90 | pthread_mutex_unlock(&grep_mutex); | |
91 | } | |
92 | ||
93 | /* Signalled when a new work_item is added to todo. */ | |
94 | static pthread_cond_t cond_add; | |
95 | ||
96 | /* Signalled when the result from one work_item is written to | |
97 | * stdout. | |
98 | */ | |
99 | static pthread_cond_t cond_write; | |
100 | ||
101 | /* Signalled when we are finished with everything. */ | |
102 | static pthread_cond_t cond_result; | |
103 | ||
104 | static int skip_first_line; | |
105 | ||
106 | static void add_work(struct grep_opt *opt, struct grep_source *gs) | |
107 | { | |
108 | if (opt->binary != GREP_BINARY_TEXT) | |
109 | grep_source_load_driver(gs, opt->repo->index); | |
110 | ||
111 | grep_lock(); | |
112 | ||
113 | while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { | |
114 | pthread_cond_wait(&cond_write, &grep_mutex); | |
115 | } | |
116 | ||
117 | todo[todo_end].source = *gs; | |
118 | todo[todo_end].done = 0; | |
119 | strbuf_reset(&todo[todo_end].out); | |
120 | todo_end = (todo_end + 1) % ARRAY_SIZE(todo); | |
121 | ||
122 | pthread_cond_signal(&cond_add); | |
123 | grep_unlock(); | |
124 | } | |
125 | ||
126 | static struct work_item *get_work(void) | |
127 | { | |
128 | struct work_item *ret; | |
129 | ||
130 | grep_lock(); | |
131 | while (todo_start == todo_end && !all_work_added) { | |
132 | pthread_cond_wait(&cond_add, &grep_mutex); | |
133 | } | |
134 | ||
135 | if (todo_start == todo_end && all_work_added) { | |
136 | ret = NULL; | |
137 | } else { | |
138 | ret = &todo[todo_start]; | |
139 | todo_start = (todo_start + 1) % ARRAY_SIZE(todo); | |
140 | } | |
141 | grep_unlock(); | |
142 | return ret; | |
143 | } | |
144 | ||
145 | static void work_done(struct work_item *w) | |
146 | { | |
147 | int old_done; | |
148 | ||
149 | grep_lock(); | |
150 | w->done = 1; | |
151 | old_done = todo_done; | |
152 | for(; todo[todo_done].done && todo_done != todo_start; | |
153 | todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { | |
154 | w = &todo[todo_done]; | |
155 | if (w->out.len) { | |
156 | const char *p = w->out.buf; | |
157 | size_t len = w->out.len; | |
158 | ||
159 | /* Skip the leading hunk mark of the first file. */ | |
160 | if (skip_first_line) { | |
161 | while (len) { | |
162 | len--; | |
163 | if (*p++ == '\n') | |
164 | break; | |
165 | } | |
166 | skip_first_line = 0; | |
167 | } | |
168 | ||
169 | write_or_die(1, p, len); | |
170 | } | |
171 | grep_source_clear(&w->source); | |
172 | } | |
173 | ||
174 | if (old_done != todo_done) | |
175 | pthread_cond_signal(&cond_write); | |
176 | ||
177 | if (all_work_added && todo_done == todo_end) | |
178 | pthread_cond_signal(&cond_result); | |
179 | ||
180 | grep_unlock(); | |
181 | } | |
182 | ||
183 | static void free_repos(void) | |
184 | { | |
185 | int i; | |
186 | ||
187 | for (i = 0; i < repos_to_free_nr; i++) { | |
188 | repo_clear(repos_to_free[i]); | |
189 | free(repos_to_free[i]); | |
190 | } | |
191 | FREE_AND_NULL(repos_to_free); | |
192 | repos_to_free_nr = 0; | |
193 | repos_to_free_alloc = 0; | |
194 | } | |
195 | ||
196 | static void *run(void *arg) | |
197 | { | |
198 | int hit = 0; | |
199 | struct grep_opt *opt = arg; | |
200 | ||
201 | while (1) { | |
202 | struct work_item *w = get_work(); | |
203 | if (!w) | |
204 | break; | |
205 | ||
206 | opt->output_priv = w; | |
207 | hit |= grep_source(opt, &w->source); | |
208 | grep_source_clear_data(&w->source); | |
209 | work_done(w); | |
210 | } | |
211 | free_grep_patterns(opt); | |
212 | free(opt); | |
213 | ||
214 | return (void*) (intptr_t) hit; | |
215 | } | |
216 | ||
217 | static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size) | |
218 | { | |
219 | struct work_item *w = opt->output_priv; | |
220 | strbuf_add(&w->out, buf, size); | |
221 | } | |
222 | ||
223 | static void start_threads(struct grep_opt *opt) | |
224 | { | |
225 | int i; | |
226 | ||
227 | pthread_mutex_init(&grep_mutex, NULL); | |
228 | pthread_mutex_init(&grep_attr_mutex, NULL); | |
229 | pthread_cond_init(&cond_add, NULL); | |
230 | pthread_cond_init(&cond_write, NULL); | |
231 | pthread_cond_init(&cond_result, NULL); | |
232 | grep_use_locks = 1; | |
233 | enable_obj_read_lock(); | |
234 | ||
235 | for (i = 0; i < ARRAY_SIZE(todo); i++) { | |
236 | strbuf_init(&todo[i].out, 0); | |
237 | } | |
238 | ||
239 | CALLOC_ARRAY(threads, num_threads); | |
240 | for (i = 0; i < num_threads; i++) { | |
241 | int err; | |
242 | struct grep_opt *o = grep_opt_dup(opt); | |
243 | o->output = strbuf_out; | |
244 | compile_grep_patterns(o); | |
245 | err = pthread_create(&threads[i], NULL, run, o); | |
246 | ||
247 | if (err) | |
248 | die(_("grep: failed to create thread: %s"), | |
249 | strerror(err)); | |
250 | } | |
251 | } | |
252 | ||
253 | static int wait_all(void) | |
254 | { | |
255 | int hit = 0; | |
256 | int i; | |
257 | ||
258 | if (!HAVE_THREADS) | |
259 | BUG("Never call this function unless you have started threads"); | |
260 | ||
261 | grep_lock(); | |
262 | all_work_added = 1; | |
263 | ||
264 | /* Wait until all work is done. */ | |
265 | while (todo_done != todo_end) | |
266 | pthread_cond_wait(&cond_result, &grep_mutex); | |
267 | ||
268 | /* Wake up all the consumer threads so they can see that there | |
269 | * is no more work to do. | |
270 | */ | |
271 | pthread_cond_broadcast(&cond_add); | |
272 | grep_unlock(); | |
273 | ||
274 | for (i = 0; i < num_threads; i++) { | |
275 | void *h; | |
276 | pthread_join(threads[i], &h); | |
277 | hit |= (int) (intptr_t) h; | |
278 | } | |
279 | ||
280 | free(threads); | |
281 | ||
282 | pthread_mutex_destroy(&grep_mutex); | |
283 | pthread_mutex_destroy(&grep_attr_mutex); | |
284 | pthread_cond_destroy(&cond_add); | |
285 | pthread_cond_destroy(&cond_write); | |
286 | pthread_cond_destroy(&cond_result); | |
287 | grep_use_locks = 0; | |
288 | disable_obj_read_lock(); | |
289 | ||
290 | return hit; | |
291 | } | |
292 | ||
293 | static int grep_cmd_config(const char *var, const char *value, | |
294 | const struct config_context *ctx, void *cb) | |
295 | { | |
296 | int st = grep_config(var, value, ctx, cb); | |
297 | ||
298 | if (git_color_config(var, value, cb) < 0) | |
299 | st = -1; | |
300 | else if (git_default_config(var, value, ctx, cb) < 0) | |
301 | st = -1; | |
302 | ||
303 | if (!strcmp(var, "grep.threads")) { | |
304 | num_threads = git_config_int(var, value, ctx->kvi); | |
305 | if (num_threads < 0) | |
306 | die(_("invalid number of threads specified (%d) for %s"), | |
307 | num_threads, var); | |
308 | else if (!HAVE_THREADS && num_threads > 1) { | |
309 | /* | |
310 | * TRANSLATORS: %s is the configuration | |
311 | * variable for tweaking threads, currently | |
312 | * grep.threads | |
313 | */ | |
314 | warning(_("no threads support, ignoring %s"), var); | |
315 | num_threads = 1; | |
316 | } | |
317 | } | |
318 | ||
319 | if (!strcmp(var, "submodule.recurse")) | |
320 | recurse_submodules = git_config_bool(var, value); | |
321 | ||
322 | return st; | |
323 | } | |
324 | ||
325 | static void grep_source_name(struct grep_opt *opt, const char *filename, | |
326 | int tree_name_len, struct strbuf *out) | |
327 | { | |
328 | strbuf_reset(out); | |
329 | ||
330 | if (opt->null_following_name) { | |
331 | if (opt->relative && grep_prefix) { | |
332 | struct strbuf rel_buf = STRBUF_INIT; | |
333 | const char *rel_name = | |
334 | relative_path(filename + tree_name_len, | |
335 | grep_prefix, &rel_buf); | |
336 | ||
337 | if (tree_name_len) | |
338 | strbuf_add(out, filename, tree_name_len); | |
339 | ||
340 | strbuf_addstr(out, rel_name); | |
341 | strbuf_release(&rel_buf); | |
342 | } else { | |
343 | strbuf_addstr(out, filename); | |
344 | } | |
345 | return; | |
346 | } | |
347 | ||
348 | if (opt->relative && grep_prefix) | |
349 | quote_path(filename + tree_name_len, grep_prefix, out, 0); | |
350 | else | |
351 | quote_c_style(filename + tree_name_len, out, NULL, 0); | |
352 | ||
353 | if (tree_name_len) | |
354 | strbuf_insert(out, 0, filename, tree_name_len); | |
355 | } | |
356 | ||
357 | static int grep_oid(struct grep_opt *opt, const struct object_id *oid, | |
358 | const char *filename, int tree_name_len, | |
359 | const char *path) | |
360 | { | |
361 | struct strbuf pathbuf = STRBUF_INIT; | |
362 | struct grep_source gs; | |
363 | ||
364 | grep_source_name(opt, filename, tree_name_len, &pathbuf); | |
365 | grep_source_init_oid(&gs, pathbuf.buf, path, oid, opt->repo); | |
366 | strbuf_release(&pathbuf); | |
367 | ||
368 | if (num_threads > 1) { | |
369 | /* | |
370 | * add_work() copies gs and thus assumes ownership of | |
371 | * its fields, so do not call grep_source_clear() | |
372 | */ | |
373 | add_work(opt, &gs); | |
374 | return 0; | |
375 | } else { | |
376 | int hit; | |
377 | ||
378 | hit = grep_source(opt, &gs); | |
379 | ||
380 | grep_source_clear(&gs); | |
381 | return hit; | |
382 | } | |
383 | } | |
384 | ||
385 | static int grep_file(struct grep_opt *opt, const char *filename) | |
386 | { | |
387 | struct strbuf buf = STRBUF_INIT; | |
388 | struct grep_source gs; | |
389 | ||
390 | grep_source_name(opt, filename, 0, &buf); | |
391 | grep_source_init_file(&gs, buf.buf, filename); | |
392 | strbuf_release(&buf); | |
393 | ||
394 | if (num_threads > 1) { | |
395 | /* | |
396 | * add_work() copies gs and thus assumes ownership of | |
397 | * its fields, so do not call grep_source_clear() | |
398 | */ | |
399 | add_work(opt, &gs); | |
400 | return 0; | |
401 | } else { | |
402 | int hit; | |
403 | ||
404 | hit = grep_source(opt, &gs); | |
405 | ||
406 | grep_source_clear(&gs); | |
407 | return hit; | |
408 | } | |
409 | } | |
410 | ||
411 | static void append_path(struct grep_opt *opt, const void *data, size_t len) | |
412 | { | |
413 | struct string_list *path_list = opt->output_priv; | |
414 | ||
415 | if (len == 1 && *(const char *)data == '\0') | |
416 | return; | |
417 | string_list_append_nodup(path_list, xstrndup(data, len)); | |
418 | } | |
419 | ||
420 | static void run_pager(struct grep_opt *opt, const char *prefix) | |
421 | { | |
422 | struct string_list *path_list = opt->output_priv; | |
423 | struct child_process child = CHILD_PROCESS_INIT; | |
424 | int i, status; | |
425 | ||
426 | for (i = 0; i < path_list->nr; i++) | |
427 | strvec_push(&child.args, path_list->items[i].string); | |
428 | child.dir = prefix; | |
429 | child.use_shell = 1; | |
430 | ||
431 | status = run_command(&child); | |
432 | if (status) | |
433 | exit(status); | |
434 | } | |
435 | ||
436 | static int grep_cache(struct grep_opt *opt, | |
437 | const struct pathspec *pathspec, int cached); | |
438 | static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, | |
439 | struct tree_desc *tree, struct strbuf *base, int tn_len, | |
440 | int check_attr); | |
441 | ||
442 | static int grep_submodule(struct grep_opt *opt, | |
443 | const struct pathspec *pathspec, | |
444 | const struct object_id *oid, | |
445 | const char *filename, const char *path, int cached) | |
446 | { | |
447 | struct repository *subrepo; | |
448 | struct repository *superproject = opt->repo; | |
449 | struct grep_opt subopt; | |
450 | int hit = 0; | |
451 | ||
452 | if (!is_submodule_active(superproject, path)) | |
453 | return 0; | |
454 | ||
455 | subrepo = xmalloc(sizeof(*subrepo)); | |
456 | if (repo_submodule_init(subrepo, superproject, path, null_oid(opt->repo->hash_algo))) { | |
457 | free(subrepo); | |
458 | return 0; | |
459 | } | |
460 | ALLOC_GROW(repos_to_free, repos_to_free_nr + 1, repos_to_free_alloc); | |
461 | repos_to_free[repos_to_free_nr++] = subrepo; | |
462 | ||
463 | /* | |
464 | * NEEDSWORK: repo_read_gitmodules() might call | |
465 | * add_to_alternates_memory() via config_from_gitmodules(). This | |
466 | * operation causes a race condition with concurrent object readings | |
467 | * performed by the worker threads. That's why we need obj_read_lock() | |
468 | * here. It should be removed once it's no longer necessary to add the | |
469 | * subrepo's odbs to the in-memory alternates list. | |
470 | */ | |
471 | obj_read_lock(); | |
472 | ||
473 | /* | |
474 | * NEEDSWORK: when reading a submodule, the sparsity settings in the | |
475 | * superproject are incorrectly forgotten or misused. For example: | |
476 | * | |
477 | * 1. "command_requires_full_index" | |
478 | * When this setting is turned on for `grep`, only the superproject | |
479 | * knows it. All the submodules are read with their own configs | |
480 | * and get prepare_repo_settings()'d. Therefore, these submodules | |
481 | * "forget" the sparse-index feature switch. As a result, the index | |
482 | * of these submodules are expanded unexpectedly. | |
483 | * | |
484 | * 2. "core_apply_sparse_checkout" | |
485 | * When running `grep` in the superproject, this setting is | |
486 | * populated using the superproject's configs. However, once | |
487 | * initialized, this config is globally accessible and is read by | |
488 | * prepare_repo_settings() for the submodules. For instance, if a | |
489 | * submodule is using a sparse-checkout, however, the superproject | |
490 | * is not, the result is that the config from the superproject will | |
491 | * dictate the behavior for the submodule, making it "forget" its | |
492 | * sparse-checkout state. | |
493 | * | |
494 | * 3. "core_sparse_checkout_cone" | |
495 | * ditto. | |
496 | * | |
497 | * Note that this list is not exhaustive. | |
498 | */ | |
499 | repo_read_gitmodules(subrepo, 0); | |
500 | ||
501 | /* | |
502 | * All code paths tested by test code no longer need submodule ODBs to | |
503 | * be added as alternates, but add it to the list just in case. | |
504 | * Submodule ODBs added through add_submodule_odb_by_path() will be | |
505 | * lazily registered as alternates when needed (and except in an | |
506 | * unexpected code interaction, it won't be needed). | |
507 | */ | |
508 | add_submodule_odb_by_path(subrepo->objects->odb->path); | |
509 | obj_read_unlock(); | |
510 | ||
511 | memcpy(&subopt, opt, sizeof(subopt)); | |
512 | subopt.repo = subrepo; | |
513 | ||
514 | if (oid) { | |
515 | enum object_type object_type; | |
516 | struct tree_desc tree; | |
517 | void *data; | |
518 | unsigned long size; | |
519 | struct strbuf base = STRBUF_INIT; | |
520 | ||
521 | obj_read_lock(); | |
522 | object_type = oid_object_info(subrepo, oid, NULL); | |
523 | obj_read_unlock(); | |
524 | data = read_object_with_reference(subrepo, | |
525 | oid, OBJ_TREE, | |
526 | &size, NULL); | |
527 | if (!data) | |
528 | die(_("unable to read tree (%s)"), oid_to_hex(oid)); | |
529 | ||
530 | strbuf_addstr(&base, filename); | |
531 | strbuf_addch(&base, '/'); | |
532 | ||
533 | init_tree_desc(&tree, oid, data, size); | |
534 | hit = grep_tree(&subopt, pathspec, &tree, &base, base.len, | |
535 | object_type == OBJ_COMMIT); | |
536 | strbuf_release(&base); | |
537 | free(data); | |
538 | } else { | |
539 | hit = grep_cache(&subopt, pathspec, cached); | |
540 | } | |
541 | ||
542 | return hit; | |
543 | } | |
544 | ||
545 | static int grep_cache(struct grep_opt *opt, | |
546 | const struct pathspec *pathspec, int cached) | |
547 | { | |
548 | struct repository *repo = opt->repo; | |
549 | int hit = 0; | |
550 | int nr; | |
551 | struct strbuf name = STRBUF_INIT; | |
552 | int name_base_len = 0; | |
553 | if (repo->submodule_prefix) { | |
554 | name_base_len = strlen(repo->submodule_prefix); | |
555 | strbuf_addstr(&name, repo->submodule_prefix); | |
556 | } | |
557 | ||
558 | if (repo_read_index(repo) < 0) | |
559 | die(_("index file corrupt")); | |
560 | ||
561 | for (nr = 0; nr < repo->index->cache_nr; nr++) { | |
562 | const struct cache_entry *ce = repo->index->cache[nr]; | |
563 | ||
564 | if (!cached && ce_skip_worktree(ce)) | |
565 | continue; | |
566 | ||
567 | strbuf_setlen(&name, name_base_len); | |
568 | strbuf_addstr(&name, ce->name); | |
569 | if (S_ISSPARSEDIR(ce->ce_mode)) { | |
570 | enum object_type type; | |
571 | struct tree_desc tree; | |
572 | void *data; | |
573 | unsigned long size; | |
574 | ||
575 | data = repo_read_object_file(the_repository, &ce->oid, | |
576 | &type, &size); | |
577 | if (!data) | |
578 | die(_("unable to read tree %s"), oid_to_hex(&ce->oid)); | |
579 | init_tree_desc(&tree, &ce->oid, data, size); | |
580 | ||
581 | hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0); | |
582 | strbuf_setlen(&name, name_base_len); | |
583 | strbuf_addstr(&name, ce->name); | |
584 | free(data); | |
585 | } else if (S_ISREG(ce->ce_mode) && | |
586 | match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL, | |
587 | S_ISDIR(ce->ce_mode) || | |
588 | S_ISGITLINK(ce->ce_mode))) { | |
589 | /* | |
590 | * If CE_VALID is on, we assume worktree file and its | |
591 | * cache entry are identical, even if worktree file has | |
592 | * been modified, so use cache version instead | |
593 | */ | |
594 | if (cached || (ce->ce_flags & CE_VALID)) { | |
595 | if (ce_stage(ce) || ce_intent_to_add(ce)) | |
596 | continue; | |
597 | hit |= grep_oid(opt, &ce->oid, name.buf, | |
598 | 0, name.buf); | |
599 | } else { | |
600 | hit |= grep_file(opt, name.buf); | |
601 | } | |
602 | } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && | |
603 | submodule_path_match(repo->index, pathspec, name.buf, NULL)) { | |
604 | hit |= grep_submodule(opt, pathspec, NULL, ce->name, | |
605 | ce->name, cached); | |
606 | } else { | |
607 | continue; | |
608 | } | |
609 | ||
610 | if (ce_stage(ce)) { | |
611 | do { | |
612 | nr++; | |
613 | } while (nr < repo->index->cache_nr && | |
614 | !strcmp(ce->name, repo->index->cache[nr]->name)); | |
615 | nr--; /* compensate for loop control */ | |
616 | } | |
617 | if (hit && opt->status_only) | |
618 | break; | |
619 | } | |
620 | ||
621 | strbuf_release(&name); | |
622 | return hit; | |
623 | } | |
624 | ||
625 | static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, | |
626 | struct tree_desc *tree, struct strbuf *base, int tn_len, | |
627 | int check_attr) | |
628 | { | |
629 | struct repository *repo = opt->repo; | |
630 | int hit = 0; | |
631 | enum interesting match = entry_not_interesting; | |
632 | struct name_entry entry; | |
633 | int old_baselen = base->len; | |
634 | struct strbuf name = STRBUF_INIT; | |
635 | int name_base_len = 0; | |
636 | if (repo->submodule_prefix) { | |
637 | strbuf_addstr(&name, repo->submodule_prefix); | |
638 | name_base_len = name.len; | |
639 | } | |
640 | ||
641 | while (tree_entry(tree, &entry)) { | |
642 | int te_len = tree_entry_len(&entry); | |
643 | ||
644 | if (match != all_entries_interesting) { | |
645 | strbuf_addstr(&name, base->buf + tn_len); | |
646 | match = tree_entry_interesting(repo->index, | |
647 | &entry, &name, | |
648 | pathspec); | |
649 | strbuf_setlen(&name, name_base_len); | |
650 | ||
651 | if (match == all_entries_not_interesting) | |
652 | break; | |
653 | if (match == entry_not_interesting) | |
654 | continue; | |
655 | } | |
656 | ||
657 | strbuf_add(base, entry.path, te_len); | |
658 | ||
659 | if (S_ISREG(entry.mode)) { | |
660 | hit |= grep_oid(opt, &entry.oid, base->buf, tn_len, | |
661 | check_attr ? base->buf + tn_len : NULL); | |
662 | } else if (S_ISDIR(entry.mode)) { | |
663 | enum object_type type; | |
664 | struct tree_desc sub; | |
665 | void *data; | |
666 | unsigned long size; | |
667 | ||
668 | data = repo_read_object_file(the_repository, | |
669 | &entry.oid, &type, &size); | |
670 | if (!data) | |
671 | die(_("unable to read tree (%s)"), | |
672 | oid_to_hex(&entry.oid)); | |
673 | ||
674 | strbuf_addch(base, '/'); | |
675 | init_tree_desc(&sub, &entry.oid, data, size); | |
676 | hit |= grep_tree(opt, pathspec, &sub, base, tn_len, | |
677 | check_attr); | |
678 | free(data); | |
679 | } else if (recurse_submodules && S_ISGITLINK(entry.mode)) { | |
680 | hit |= grep_submodule(opt, pathspec, &entry.oid, | |
681 | base->buf, base->buf + tn_len, | |
682 | 1); /* ignored */ | |
683 | } | |
684 | ||
685 | strbuf_setlen(base, old_baselen); | |
686 | ||
687 | if (hit && opt->status_only) | |
688 | break; | |
689 | } | |
690 | ||
691 | strbuf_release(&name); | |
692 | return hit; | |
693 | } | |
694 | ||
695 | static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, | |
696 | struct object *obj, const char *name, const char *path) | |
697 | { | |
698 | if (obj->type == OBJ_BLOB) | |
699 | return grep_oid(opt, &obj->oid, name, 0, path); | |
700 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { | |
701 | struct tree_desc tree; | |
702 | void *data; | |
703 | unsigned long size; | |
704 | struct strbuf base; | |
705 | int hit, len; | |
706 | ||
707 | data = read_object_with_reference(opt->repo, | |
708 | &obj->oid, OBJ_TREE, | |
709 | &size, NULL); | |
710 | if (!data) | |
711 | die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid)); | |
712 | ||
713 | len = name ? strlen(name) : 0; | |
714 | strbuf_init(&base, PATH_MAX + len + 1); | |
715 | if (len) { | |
716 | strbuf_add(&base, name, len); | |
717 | strbuf_addch(&base, ':'); | |
718 | } | |
719 | init_tree_desc(&tree, &obj->oid, data, size); | |
720 | hit = grep_tree(opt, pathspec, &tree, &base, base.len, | |
721 | obj->type == OBJ_COMMIT); | |
722 | strbuf_release(&base); | |
723 | free(data); | |
724 | return hit; | |
725 | } | |
726 | die(_("unable to grep from object of type %s"), type_name(obj->type)); | |
727 | } | |
728 | ||
729 | static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec, | |
730 | const struct object_array *list) | |
731 | { | |
732 | unsigned int i; | |
733 | int hit = 0; | |
734 | const unsigned int nr = list->nr; | |
735 | ||
736 | for (i = 0; i < nr; i++) { | |
737 | struct object *real_obj; | |
738 | ||
739 | obj_read_lock(); | |
740 | real_obj = deref_tag(opt->repo, list->objects[i].item, | |
741 | NULL, 0); | |
742 | obj_read_unlock(); | |
743 | ||
744 | if (!real_obj) { | |
745 | char hex[GIT_MAX_HEXSZ + 1]; | |
746 | const char *name = list->objects[i].name; | |
747 | ||
748 | if (!name) { | |
749 | oid_to_hex_r(hex, &list->objects[i].item->oid); | |
750 | name = hex; | |
751 | } | |
752 | die(_("invalid object '%s' given."), name); | |
753 | } | |
754 | ||
755 | /* load the gitmodules file for this rev */ | |
756 | if (recurse_submodules) { | |
757 | submodule_free(opt->repo); | |
758 | obj_read_lock(); | |
759 | gitmodules_config_oid(&real_obj->oid); | |
760 | obj_read_unlock(); | |
761 | } | |
762 | if (grep_object(opt, pathspec, real_obj, list->objects[i].name, | |
763 | list->objects[i].path)) { | |
764 | hit = 1; | |
765 | if (opt->status_only) | |
766 | break; | |
767 | } | |
768 | } | |
769 | return hit; | |
770 | } | |
771 | ||
772 | static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, | |
773 | int exc_std, int use_index) | |
774 | { | |
775 | struct dir_struct dir = DIR_INIT; | |
776 | int i, hit = 0; | |
777 | ||
778 | if (!use_index) | |
779 | dir.flags |= DIR_NO_GITLINKS; | |
780 | if (exc_std) | |
781 | setup_standard_excludes(&dir); | |
782 | ||
783 | fill_directory(&dir, opt->repo->index, pathspec); | |
784 | for (i = 0; i < dir.nr; i++) { | |
785 | hit |= grep_file(opt, dir.entries[i]->name); | |
786 | if (hit && opt->status_only) | |
787 | break; | |
788 | } | |
789 | dir_clear(&dir); | |
790 | return hit; | |
791 | } | |
792 | ||
793 | static int context_callback(const struct option *opt, const char *arg, | |
794 | int unset) | |
795 | { | |
796 | struct grep_opt *grep_opt = opt->value; | |
797 | int value; | |
798 | const char *endp; | |
799 | ||
800 | if (unset) { | |
801 | grep_opt->pre_context = grep_opt->post_context = 0; | |
802 | return 0; | |
803 | } | |
804 | value = strtol(arg, (char **)&endp, 10); | |
805 | if (*endp) { | |
806 | return error(_("switch `%c' expects a numerical value"), | |
807 | opt->short_name); | |
808 | } | |
809 | grep_opt->pre_context = grep_opt->post_context = value; | |
810 | return 0; | |
811 | } | |
812 | ||
813 | static int file_callback(const struct option *opt, const char *arg, int unset) | |
814 | { | |
815 | struct grep_opt *grep_opt = opt->value; | |
816 | int from_stdin; | |
817 | const char *filename = arg; | |
818 | FILE *patterns; | |
819 | int lno = 0; | |
820 | struct strbuf sb = STRBUF_INIT; | |
821 | ||
822 | BUG_ON_OPT_NEG(unset); | |
823 | ||
824 | if (!*filename) | |
825 | ; /* leave it as-is */ | |
826 | else | |
827 | filename = prefix_filename_except_for_dash(grep_prefix, filename); | |
828 | ||
829 | from_stdin = !strcmp(filename, "-"); | |
830 | patterns = from_stdin ? stdin : fopen(filename, "r"); | |
831 | if (!patterns) | |
832 | die_errno(_("cannot open '%s'"), arg); | |
833 | while (strbuf_getline(&sb, patterns) == 0) { | |
834 | /* ignore empty line like grep does */ | |
835 | if (sb.len == 0) | |
836 | continue; | |
837 | ||
838 | append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno, | |
839 | GREP_PATTERN); | |
840 | } | |
841 | if (!from_stdin) | |
842 | fclose(patterns); | |
843 | strbuf_release(&sb); | |
844 | if (filename != arg) | |
845 | free((void *)filename); | |
846 | return 0; | |
847 | } | |
848 | ||
849 | static int not_callback(const struct option *opt, const char *arg, int unset) | |
850 | { | |
851 | struct grep_opt *grep_opt = opt->value; | |
852 | BUG_ON_OPT_NEG(unset); | |
853 | BUG_ON_OPT_ARG(arg); | |
854 | append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); | |
855 | return 0; | |
856 | } | |
857 | ||
858 | static int and_callback(const struct option *opt, const char *arg, int unset) | |
859 | { | |
860 | struct grep_opt *grep_opt = opt->value; | |
861 | BUG_ON_OPT_NEG(unset); | |
862 | BUG_ON_OPT_ARG(arg); | |
863 | append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static int open_callback(const struct option *opt, const char *arg, int unset) | |
868 | { | |
869 | struct grep_opt *grep_opt = opt->value; | |
870 | BUG_ON_OPT_NEG(unset); | |
871 | BUG_ON_OPT_ARG(arg); | |
872 | append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); | |
873 | return 0; | |
874 | } | |
875 | ||
876 | static int close_callback(const struct option *opt, const char *arg, int unset) | |
877 | { | |
878 | struct grep_opt *grep_opt = opt->value; | |
879 | BUG_ON_OPT_NEG(unset); | |
880 | BUG_ON_OPT_ARG(arg); | |
881 | append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); | |
882 | return 0; | |
883 | } | |
884 | ||
885 | static int pattern_callback(const struct option *opt, const char *arg, | |
886 | int unset) | |
887 | { | |
888 | struct grep_opt *grep_opt = opt->value; | |
889 | BUG_ON_OPT_NEG(unset); | |
890 | append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); | |
891 | return 0; | |
892 | } | |
893 | ||
894 | int cmd_grep(int argc, | |
895 | const char **argv, | |
896 | const char *prefix, | |
897 | struct repository *repo UNUSED) | |
898 | { | |
899 | int hit = 0; | |
900 | int cached = 0, untracked = 0, opt_exclude = -1; | |
901 | int seen_dashdash = 0; | |
902 | int external_grep_allowed__ignored; | |
903 | const char *show_in_pager = NULL, *default_pager = "dummy"; | |
904 | struct grep_opt opt; | |
905 | struct object_array list = OBJECT_ARRAY_INIT; | |
906 | struct pathspec pathspec; | |
907 | struct string_list path_list = STRING_LIST_INIT_DUP; | |
908 | int i; | |
909 | int dummy; | |
910 | int use_index = 1; | |
911 | int allow_revs; | |
912 | int ret; | |
913 | ||
914 | struct option options[] = { | |
915 | OPT_BOOL(0, "cached", &cached, | |
916 | N_("search in index instead of in the work tree")), | |
917 | OPT_NEGBIT(0, "no-index", &use_index, | |
918 | N_("find in contents not managed by git"), 1), | |
919 | OPT_BOOL(0, "untracked", &untracked, | |
920 | N_("search in both tracked and untracked files")), | |
921 | OPT_SET_INT(0, "exclude-standard", &opt_exclude, | |
922 | N_("ignore files specified via '.gitignore'"), 1), | |
923 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules, | |
924 | N_("recursively search in each submodule")), | |
925 | OPT_GROUP(""), | |
926 | OPT_BOOL('v', "invert-match", &opt.invert, | |
927 | N_("show non-matching lines")), | |
928 | OPT_BOOL('i', "ignore-case", &opt.ignore_case, | |
929 | N_("case insensitive matching")), | |
930 | OPT_BOOL('w', "word-regexp", &opt.word_regexp, | |
931 | N_("match patterns only at word boundaries")), | |
932 | OPT_SET_INT('a', "text", &opt.binary, | |
933 | N_("process binary files as text"), GREP_BINARY_TEXT), | |
934 | OPT_SET_INT('I', NULL, &opt.binary, | |
935 | N_("don't match patterns in binary files"), | |
936 | GREP_BINARY_NOMATCH), | |
937 | OPT_BOOL(0, "textconv", &opt.allow_textconv, | |
938 | N_("process binary files with textconv filters")), | |
939 | OPT_SET_INT('r', "recursive", &opt.max_depth, | |
940 | N_("search in subdirectories (default)"), -1), | |
941 | OPT_INTEGER_F(0, "max-depth", &opt.max_depth, | |
942 | N_("descend at most <n> levels"), PARSE_OPT_NONEG), | |
943 | OPT_GROUP(""), | |
944 | OPT_SET_INT('E', "extended-regexp", &opt.pattern_type_option, | |
945 | N_("use extended POSIX regular expressions"), | |
946 | GREP_PATTERN_TYPE_ERE), | |
947 | OPT_SET_INT('G', "basic-regexp", &opt.pattern_type_option, | |
948 | N_("use basic POSIX regular expressions (default)"), | |
949 | GREP_PATTERN_TYPE_BRE), | |
950 | OPT_SET_INT('F', "fixed-strings", &opt.pattern_type_option, | |
951 | N_("interpret patterns as fixed strings"), | |
952 | GREP_PATTERN_TYPE_FIXED), | |
953 | OPT_SET_INT('P', "perl-regexp", &opt.pattern_type_option, | |
954 | N_("use Perl-compatible regular expressions"), | |
955 | GREP_PATTERN_TYPE_PCRE), | |
956 | OPT_GROUP(""), | |
957 | OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")), | |
958 | OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")), | |
959 | OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1), | |
960 | OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1), | |
961 | OPT_NEGBIT(0, "full-name", &opt.relative, | |
962 | N_("show filenames relative to top directory"), 1), | |
963 | OPT_BOOL('l', "files-with-matches", &opt.name_only, | |
964 | N_("show only filenames instead of matching lines")), | |
965 | OPT_BOOL(0, "name-only", &opt.name_only, | |
966 | N_("synonym for --files-with-matches")), | |
967 | OPT_BOOL('L', "files-without-match", | |
968 | &opt.unmatch_name_only, | |
969 | N_("show only the names of files without match")), | |
970 | OPT_BOOL_F('z', "null", &opt.null_following_name, | |
971 | N_("print NUL after filenames"), | |
972 | PARSE_OPT_NOCOMPLETE), | |
973 | OPT_BOOL('o', "only-matching", &opt.only_matching, | |
974 | N_("show only matching parts of a line")), | |
975 | OPT_BOOL('c', "count", &opt.count, | |
976 | N_("show the number of matches instead of matching lines")), | |
977 | OPT__COLOR(&opt.color, N_("highlight matches")), | |
978 | OPT_BOOL(0, "break", &opt.file_break, | |
979 | N_("print empty line between matches from different files")), | |
980 | OPT_BOOL(0, "heading", &opt.heading, | |
981 | N_("show filename only once above matches from same file")), | |
982 | OPT_GROUP(""), | |
983 | OPT_CALLBACK('C', "context", &opt, N_("n"), | |
984 | N_("show <n> context lines before and after matches"), | |
985 | context_callback), | |
986 | OPT_UNSIGNED('B', "before-context", &opt.pre_context, | |
987 | N_("show <n> context lines before matches")), | |
988 | OPT_UNSIGNED('A', "after-context", &opt.post_context, | |
989 | N_("show <n> context lines after matches")), | |
990 | OPT_INTEGER(0, "threads", &num_threads, | |
991 | N_("use <n> worker threads")), | |
992 | OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"), | |
993 | context_callback), | |
994 | OPT_BOOL('p', "show-function", &opt.funcname, | |
995 | N_("show a line with the function name before matches")), | |
996 | OPT_BOOL('W', "function-context", &opt.funcbody, | |
997 | N_("show the surrounding function")), | |
998 | OPT_GROUP(""), | |
999 | OPT_CALLBACK('f', NULL, &opt, N_("file"), | |
1000 | N_("read patterns from file"), file_callback), | |
1001 | OPT_CALLBACK_F('e', NULL, &opt, N_("pattern"), | |
1002 | N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback), | |
1003 | OPT_CALLBACK_F(0, "and", &opt, NULL, | |
1004 | N_("combine patterns specified with -e"), | |
1005 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback), | |
1006 | OPT_BOOL_F(0, "or", &dummy, "", PARSE_OPT_NONEG), | |
1007 | OPT_CALLBACK_F(0, "not", &opt, NULL, "", | |
1008 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback), | |
1009 | OPT_CALLBACK_F('(', NULL, &opt, NULL, "", | |
1010 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
1011 | open_callback), | |
1012 | OPT_CALLBACK_F(')', NULL, &opt, NULL, "", | |
1013 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
1014 | close_callback), | |
1015 | OPT__QUIET(&opt.status_only, | |
1016 | N_("indicate hit with exit status without output")), | |
1017 | OPT_BOOL(0, "all-match", &opt.all_match, | |
1018 | N_("show only matches from files that match all patterns")), | |
1019 | OPT_GROUP(""), | |
1020 | { | |
1021 | .type = OPTION_STRING, | |
1022 | .short_name = 'O', | |
1023 | .long_name = "open-files-in-pager", | |
1024 | .value = &show_in_pager, | |
1025 | .argh = N_("pager"), | |
1026 | .help = N_("show matching files in the pager"), | |
1027 | .flags = PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE, | |
1028 | .defval = (intptr_t)default_pager, | |
1029 | }, | |
1030 | OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored, | |
1031 | N_("allow calling of grep(1) (ignored by this build)"), | |
1032 | PARSE_OPT_NOCOMPLETE), | |
1033 | OPT_INTEGER('m', "max-count", &opt.max_count, | |
1034 | N_("maximum number of results per file")), | |
1035 | OPT_END() | |
1036 | }; | |
1037 | grep_prefix = prefix; | |
1038 | ||
1039 | grep_init(&opt, the_repository); | |
1040 | git_config(grep_cmd_config, &opt); | |
1041 | ||
1042 | /* | |
1043 | * If there is no -- then the paths must exist in the working | |
1044 | * tree. If there is no explicit pattern specified with -e or | |
1045 | * -f, we take the first unrecognized non option to be the | |
1046 | * pattern, but then what follows it must be zero or more | |
1047 | * valid refs up to the -- (if exists), and then existing | |
1048 | * paths. If there is an explicit pattern, then the first | |
1049 | * unrecognized non option is the beginning of the refs list | |
1050 | * that continues up to the -- (if exists), and then paths. | |
1051 | */ | |
1052 | argc = parse_options(argc, argv, prefix, options, grep_usage, | |
1053 | PARSE_OPT_KEEP_DASHDASH | | |
1054 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1055 | ||
1056 | if (the_repository->gitdir) { | |
1057 | prepare_repo_settings(the_repository); | |
1058 | the_repository->settings.command_requires_full_index = 0; | |
1059 | } | |
1060 | ||
1061 | if (use_index && !startup_info->have_repository) { | |
1062 | int fallback = 0; | |
1063 | git_config_get_bool("grep.fallbacktonoindex", &fallback); | |
1064 | if (fallback) | |
1065 | use_index = 0; | |
1066 | else | |
1067 | /* die the same way as if we did it at the beginning */ | |
1068 | setup_git_directory(); | |
1069 | } | |
1070 | /* Ignore --recurse-submodules if --no-index is given or implied */ | |
1071 | if (!use_index) | |
1072 | recurse_submodules = 0; | |
1073 | ||
1074 | /* | |
1075 | * skip a -- separator; we know it cannot be | |
1076 | * separating revisions from pathnames if | |
1077 | * we haven't even had any patterns yet | |
1078 | */ | |
1079 | if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) { | |
1080 | argv++; | |
1081 | argc--; | |
1082 | } | |
1083 | ||
1084 | /* First unrecognized non-option token */ | |
1085 | if (argc > 0 && !opt.pattern_list) { | |
1086 | append_grep_pattern(&opt, argv[0], "command line", 0, | |
1087 | GREP_PATTERN); | |
1088 | argv++; | |
1089 | argc--; | |
1090 | } | |
1091 | ||
1092 | if (show_in_pager == default_pager) | |
1093 | show_in_pager = git_pager(the_repository, 1); | |
1094 | if (show_in_pager) { | |
1095 | opt.color = 0; | |
1096 | opt.name_only = 1; | |
1097 | opt.null_following_name = 1; | |
1098 | opt.output_priv = &path_list; | |
1099 | opt.output = append_path; | |
1100 | string_list_append(&path_list, show_in_pager); | |
1101 | } | |
1102 | ||
1103 | if (!opt.pattern_list) | |
1104 | die(_("no pattern given")); | |
1105 | ||
1106 | /* --only-matching has no effect with --invert. */ | |
1107 | if (opt.invert) | |
1108 | opt.only_matching = 0; | |
1109 | ||
1110 | /* | |
1111 | * We have to find "--" in a separate pass, because its presence | |
1112 | * influences how we will parse arguments that come before it. | |
1113 | */ | |
1114 | for (i = 0; i < argc; i++) { | |
1115 | if (!strcmp(argv[i], "--")) { | |
1116 | seen_dashdash = 1; | |
1117 | break; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | /* | |
1122 | * Resolve any rev arguments. If we have a dashdash, then everything up | |
1123 | * to it must resolve as a rev. If not, then we stop at the first | |
1124 | * non-rev and assume everything else is a path. | |
1125 | */ | |
1126 | allow_revs = use_index && !untracked; | |
1127 | for (i = 0; i < argc; i++) { | |
1128 | const char *arg = argv[i]; | |
1129 | struct object_id oid; | |
1130 | struct object_context oc = {0}; | |
1131 | struct object *object; | |
1132 | ||
1133 | if (!strcmp(arg, "--")) { | |
1134 | i++; | |
1135 | break; | |
1136 | } | |
1137 | ||
1138 | if (!allow_revs) { | |
1139 | if (seen_dashdash) | |
1140 | die(_("--no-index or --untracked cannot be used with revs")); | |
1141 | break; | |
1142 | } | |
1143 | ||
1144 | if (get_oid_with_context(the_repository, arg, | |
1145 | GET_OID_RECORD_PATH, | |
1146 | &oid, &oc)) { | |
1147 | if (seen_dashdash) | |
1148 | die(_("unable to resolve revision: %s"), arg); | |
1149 | object_context_release(&oc); | |
1150 | break; | |
1151 | } | |
1152 | ||
1153 | object = parse_object_or_die(the_repository, &oid, arg); | |
1154 | if (!seen_dashdash) | |
1155 | verify_non_filename(prefix, arg); | |
1156 | add_object_array_with_path(object, arg, &list, oc.mode, oc.path); | |
1157 | object_context_release(&oc); | |
1158 | } | |
1159 | ||
1160 | /* | |
1161 | * Anything left over is presumed to be a path. But in the non-dashdash | |
1162 | * "do what I mean" case, we verify and complain when that isn't true. | |
1163 | */ | |
1164 | if (!seen_dashdash) { | |
1165 | int j; | |
1166 | for (j = i; j < argc; j++) | |
1167 | verify_filename(prefix, argv[j], j == i && allow_revs); | |
1168 | } | |
1169 | ||
1170 | parse_pathspec(&pathspec, 0, | |
1171 | PATHSPEC_PREFER_CWD | | |
1172 | (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0), | |
1173 | prefix, argv + i); | |
1174 | pathspec.max_depth = opt.max_depth; | |
1175 | pathspec.recursive = 1; | |
1176 | pathspec.recurse_submodules = !!recurse_submodules; | |
1177 | ||
1178 | if (recurse_submodules && untracked) | |
1179 | die(_("--untracked not supported with --recurse-submodules")); | |
1180 | ||
1181 | /* | |
1182 | * Optimize out the case where the amount of matches is limited to zero. | |
1183 | * We do this to keep results consistent with GNU grep(1). | |
1184 | */ | |
1185 | if (opt.max_count == 0) { | |
1186 | ret = 1; | |
1187 | goto out; | |
1188 | } | |
1189 | ||
1190 | if (show_in_pager) { | |
1191 | if (num_threads > 1) | |
1192 | warning(_("invalid option combination, ignoring --threads")); | |
1193 | num_threads = 1; | |
1194 | } else if (!HAVE_THREADS && num_threads > 1) { | |
1195 | warning(_("no threads support, ignoring --threads")); | |
1196 | num_threads = 1; | |
1197 | } else if (num_threads < 0) | |
1198 | die(_("invalid number of threads specified (%d)"), num_threads); | |
1199 | else if (num_threads == 0) | |
1200 | num_threads = HAVE_THREADS ? online_cpus() : 1; | |
1201 | ||
1202 | if (num_threads > 1) { | |
1203 | if (!HAVE_THREADS) | |
1204 | BUG("Somebody got num_threads calculation wrong!"); | |
1205 | if (!(opt.name_only || opt.unmatch_name_only || opt.count) | |
1206 | && (opt.pre_context || opt.post_context || | |
1207 | opt.file_break || opt.funcbody)) | |
1208 | skip_first_line = 1; | |
1209 | ||
1210 | /* | |
1211 | * Pre-read gitmodules (if not read already) and force eager | |
1212 | * initialization of packed_git to prevent racy lazy | |
1213 | * reading/initialization once worker threads are started. | |
1214 | */ | |
1215 | if (recurse_submodules) | |
1216 | repo_read_gitmodules(the_repository, 1); | |
1217 | if (startup_info->have_repository) | |
1218 | (void)get_packed_git(the_repository); | |
1219 | ||
1220 | start_threads(&opt); | |
1221 | } else { | |
1222 | /* | |
1223 | * The compiled patterns on the main path are only | |
1224 | * used when not using threading. Otherwise | |
1225 | * start_threads() above calls compile_grep_patterns() | |
1226 | * for each thread. | |
1227 | */ | |
1228 | compile_grep_patterns(&opt); | |
1229 | } | |
1230 | ||
1231 | if (show_in_pager && (cached || list.nr)) | |
1232 | die(_("--open-files-in-pager only works on the worktree")); | |
1233 | ||
1234 | if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) { | |
1235 | const char *pager = path_list.items[0].string; | |
1236 | int len = strlen(pager); | |
1237 | ||
1238 | if (len > 4 && is_dir_sep(pager[len - 5])) | |
1239 | pager += len - 4; | |
1240 | ||
1241 | if (opt.ignore_case && !strcmp("less", pager)) | |
1242 | string_list_append(&path_list, "-I"); | |
1243 | ||
1244 | if (!strcmp("less", pager) || !strcmp("vi", pager)) { | |
1245 | struct strbuf buf = STRBUF_INIT; | |
1246 | strbuf_addf(&buf, "+/%s%s", | |
1247 | strcmp("less", pager) ? "" : "*", | |
1248 | opt.pattern_list->pattern); | |
1249 | string_list_append_nodup(&path_list, | |
1250 | strbuf_detach(&buf, NULL)); | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | if (!show_in_pager && !opt.status_only) | |
1255 | setup_pager(the_repository); | |
1256 | ||
1257 | die_for_incompatible_opt3(!use_index, "--no-index", | |
1258 | untracked, "--untracked", | |
1259 | cached, "--cached"); | |
1260 | ||
1261 | if (!use_index || untracked) { | |
1262 | int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude; | |
1263 | hit = grep_directory(&opt, &pathspec, use_exclude, use_index); | |
1264 | } else if (0 <= opt_exclude) { | |
1265 | die(_("--[no-]exclude-standard cannot be used for tracked contents")); | |
1266 | } else if (!list.nr) { | |
1267 | if (!cached) | |
1268 | setup_work_tree(); | |
1269 | ||
1270 | hit = grep_cache(&opt, &pathspec, cached); | |
1271 | } else { | |
1272 | if (cached) | |
1273 | die(_("both --cached and trees are given")); | |
1274 | ||
1275 | hit = grep_objects(&opt, &pathspec, &list); | |
1276 | } | |
1277 | ||
1278 | if (num_threads > 1) | |
1279 | hit |= wait_all(); | |
1280 | if (hit && show_in_pager) | |
1281 | run_pager(&opt, prefix); | |
1282 | ||
1283 | ret = !hit; | |
1284 | ||
1285 | out: | |
1286 | clear_pathspec(&pathspec); | |
1287 | string_list_clear(&path_list, 0); | |
1288 | free_grep_patterns(&opt); | |
1289 | object_array_clear(&list); | |
1290 | free_repos(); | |
1291 | return ret; | |
1292 | } |