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