]>
Commit | Line | Data |
---|---|---|
5010cb5f JH |
1 | /* |
2 | * Builtin "git grep" | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
6 | #include "cache.h" | |
7 | #include "blob.h" | |
8 | #include "tree.h" | |
9 | #include "commit.h" | |
10 | #include "tag.h" | |
1362671f | 11 | #include "tree-walk.h" |
5010cb5f | 12 | #include "builtin.h" |
3e230fa1 | 13 | #include "parse-options.h" |
60ecac98 | 14 | #include "userdiff.h" |
83b5d2f5 | 15 | #include "grep.h" |
493b7a08 | 16 | #include "quote.h" |
5f7c643a | 17 | |
5b594f45 FK |
18 | #ifndef NO_PTHREADS |
19 | #include "thread-utils.h" | |
20 | #include <pthread.h> | |
21 | #endif | |
22 | ||
3e230fa1 RS |
23 | static char const * const grep_usage[] = { |
24 | "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]", | |
25 | NULL | |
26 | }; | |
27 | ||
5b594f45 FK |
28 | static int use_threads = 1; |
29 | ||
30 | #ifndef NO_PTHREADS | |
31 | #define THREADS 8 | |
32 | static pthread_t threads[THREADS]; | |
33 | ||
34 | static void *load_sha1(const unsigned char *sha1, unsigned long *size, | |
35 | const char *name); | |
36 | static void *load_file(const char *filename, size_t *sz); | |
37 | ||
38 | enum work_type {WORK_SHA1, WORK_FILE}; | |
39 | ||
40 | /* We use one producer thread and THREADS consumer | |
41 | * threads. The producer adds struct work_items to 'todo' and the | |
42 | * consumers pick work items from the same array. | |
43 | */ | |
44 | struct work_item | |
45 | { | |
46 | enum work_type type; | |
47 | char *name; | |
48 | ||
49 | /* if type == WORK_SHA1, then 'identifier' is a SHA1, | |
50 | * otherwise type == WORK_FILE, and 'identifier' is a NUL | |
51 | * terminated filename. | |
52 | */ | |
53 | void *identifier; | |
54 | char done; | |
55 | struct strbuf out; | |
56 | }; | |
57 | ||
58 | /* In the range [todo_done, todo_start) in 'todo' we have work_items | |
59 | * that have been or are processed by a consumer thread. We haven't | |
60 | * written the result for these to stdout yet. | |
61 | * | |
62 | * The work_items in [todo_start, todo_end) are waiting to be picked | |
63 | * up by a consumer thread. | |
64 | * | |
65 | * The ranges are modulo TODO_SIZE. | |
66 | */ | |
67 | #define TODO_SIZE 128 | |
68 | static struct work_item todo[TODO_SIZE]; | |
69 | static int todo_start; | |
70 | static int todo_end; | |
71 | static int todo_done; | |
72 | ||
73 | /* Has all work items been added? */ | |
74 | static int all_work_added; | |
75 | ||
76 | /* This lock protects all the variables above. */ | |
77 | static pthread_mutex_t grep_mutex; | |
78 | ||
79 | /* Used to serialize calls to read_sha1_file. */ | |
80 | static pthread_mutex_t read_sha1_mutex; | |
81 | ||
82 | #define grep_lock() pthread_mutex_lock(&grep_mutex) | |
83 | #define grep_unlock() pthread_mutex_unlock(&grep_mutex) | |
84 | #define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex) | |
85 | #define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex) | |
86 | ||
87 | /* Signalled when a new work_item is added to todo. */ | |
88 | static pthread_cond_t cond_add; | |
89 | ||
90 | /* Signalled when the result from one work_item is written to | |
91 | * stdout. | |
92 | */ | |
93 | static pthread_cond_t cond_write; | |
94 | ||
95 | /* Signalled when we are finished with everything. */ | |
96 | static pthread_cond_t cond_result; | |
97 | ||
98 | static void add_work(enum work_type type, char *name, void *id) | |
99 | { | |
100 | grep_lock(); | |
101 | ||
102 | while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { | |
103 | pthread_cond_wait(&cond_write, &grep_mutex); | |
104 | } | |
105 | ||
106 | todo[todo_end].type = type; | |
107 | todo[todo_end].name = name; | |
108 | todo[todo_end].identifier = id; | |
109 | todo[todo_end].done = 0; | |
110 | strbuf_reset(&todo[todo_end].out); | |
111 | todo_end = (todo_end + 1) % ARRAY_SIZE(todo); | |
112 | ||
113 | pthread_cond_signal(&cond_add); | |
114 | grep_unlock(); | |
115 | } | |
116 | ||
117 | static struct work_item *get_work(void) | |
118 | { | |
119 | struct work_item *ret; | |
120 | ||
121 | grep_lock(); | |
122 | while (todo_start == todo_end && !all_work_added) { | |
123 | pthread_cond_wait(&cond_add, &grep_mutex); | |
124 | } | |
125 | ||
126 | if (todo_start == todo_end && all_work_added) { | |
127 | ret = NULL; | |
128 | } else { | |
129 | ret = &todo[todo_start]; | |
130 | todo_start = (todo_start + 1) % ARRAY_SIZE(todo); | |
131 | } | |
132 | grep_unlock(); | |
133 | return ret; | |
134 | } | |
135 | ||
136 | static void grep_sha1_async(struct grep_opt *opt, char *name, | |
137 | const unsigned char *sha1) | |
138 | { | |
139 | unsigned char *s; | |
140 | s = xmalloc(20); | |
141 | memcpy(s, sha1, 20); | |
142 | add_work(WORK_SHA1, name, s); | |
143 | } | |
144 | ||
145 | static void grep_file_async(struct grep_opt *opt, char *name, | |
146 | const char *filename) | |
147 | { | |
148 | add_work(WORK_FILE, name, xstrdup(filename)); | |
149 | } | |
150 | ||
151 | static void work_done(struct work_item *w) | |
152 | { | |
153 | int old_done; | |
154 | ||
155 | grep_lock(); | |
156 | w->done = 1; | |
157 | old_done = todo_done; | |
158 | for(; todo[todo_done].done && todo_done != todo_start; | |
159 | todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { | |
160 | w = &todo[todo_done]; | |
161 | write_or_die(1, w->out.buf, w->out.len); | |
162 | free(w->name); | |
163 | free(w->identifier); | |
164 | } | |
165 | ||
166 | if (old_done != todo_done) | |
167 | pthread_cond_signal(&cond_write); | |
168 | ||
169 | if (all_work_added && todo_done == todo_end) | |
170 | pthread_cond_signal(&cond_result); | |
171 | ||
172 | grep_unlock(); | |
173 | } | |
174 | ||
175 | static void *run(void *arg) | |
176 | { | |
177 | int hit = 0; | |
178 | struct grep_opt *opt = arg; | |
179 | ||
180 | while (1) { | |
181 | struct work_item *w = get_work(); | |
182 | if (!w) | |
183 | break; | |
184 | ||
185 | opt->output_priv = w; | |
186 | if (w->type == WORK_SHA1) { | |
187 | unsigned long sz; | |
188 | void* data = load_sha1(w->identifier, &sz, w->name); | |
189 | ||
190 | if (data) { | |
191 | hit |= grep_buffer(opt, w->name, data, sz); | |
192 | free(data); | |
193 | } | |
194 | } else if (w->type == WORK_FILE) { | |
195 | size_t sz; | |
196 | void* data = load_file(w->identifier, &sz); | |
197 | if (data) { | |
198 | hit |= grep_buffer(opt, w->name, data, sz); | |
199 | free(data); | |
200 | } | |
201 | } else { | |
202 | assert(0); | |
203 | } | |
204 | ||
205 | work_done(w); | |
206 | } | |
bfac23d9 DM |
207 | free_grep_patterns(arg); |
208 | free(arg); | |
5b594f45 FK |
209 | |
210 | return (void*) (intptr_t) hit; | |
211 | } | |
212 | ||
213 | static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size) | |
214 | { | |
215 | struct work_item *w = opt->output_priv; | |
216 | strbuf_add(&w->out, buf, size); | |
217 | } | |
218 | ||
219 | static void start_threads(struct grep_opt *opt) | |
220 | { | |
221 | int i; | |
222 | ||
223 | pthread_mutex_init(&grep_mutex, NULL); | |
224 | pthread_mutex_init(&read_sha1_mutex, NULL); | |
225 | pthread_cond_init(&cond_add, NULL); | |
226 | pthread_cond_init(&cond_write, NULL); | |
227 | pthread_cond_init(&cond_result, NULL); | |
228 | ||
229 | for (i = 0; i < ARRAY_SIZE(todo); i++) { | |
230 | strbuf_init(&todo[i].out, 0); | |
231 | } | |
232 | ||
233 | for (i = 0; i < ARRAY_SIZE(threads); i++) { | |
234 | int err; | |
235 | struct grep_opt *o = grep_opt_dup(opt); | |
236 | o->output = strbuf_out; | |
237 | compile_grep_patterns(o); | |
238 | err = pthread_create(&threads[i], NULL, run, o); | |
239 | ||
240 | if (err) | |
241 | die("grep: failed to create thread: %s", | |
242 | strerror(err)); | |
243 | } | |
244 | } | |
245 | ||
246 | static int wait_all(void) | |
247 | { | |
248 | int hit = 0; | |
249 | int i; | |
250 | ||
251 | grep_lock(); | |
252 | all_work_added = 1; | |
253 | ||
254 | /* Wait until all work is done. */ | |
255 | while (todo_done != todo_end) | |
256 | pthread_cond_wait(&cond_result, &grep_mutex); | |
257 | ||
258 | /* Wake up all the consumer threads so they can see that there | |
259 | * is no more work to do. | |
260 | */ | |
261 | pthread_cond_broadcast(&cond_add); | |
262 | grep_unlock(); | |
263 | ||
264 | for (i = 0; i < ARRAY_SIZE(threads); i++) { | |
265 | void *h; | |
266 | pthread_join(threads[i], &h); | |
267 | hit |= (int) (intptr_t) h; | |
268 | } | |
269 | ||
270 | pthread_mutex_destroy(&grep_mutex); | |
271 | pthread_mutex_destroy(&read_sha1_mutex); | |
272 | pthread_cond_destroy(&cond_add); | |
273 | pthread_cond_destroy(&cond_write); | |
274 | pthread_cond_destroy(&cond_result); | |
275 | ||
276 | return hit; | |
277 | } | |
278 | #else /* !NO_PTHREADS */ | |
279 | #define read_sha1_lock() | |
280 | #define read_sha1_unlock() | |
281 | ||
282 | static int wait_all(void) | |
283 | { | |
284 | return 0; | |
285 | } | |
286 | #endif | |
287 | ||
7e8f59d5 RS |
288 | static int grep_config(const char *var, const char *value, void *cb) |
289 | { | |
290 | struct grep_opt *opt = cb; | |
291 | ||
60ecac98 RS |
292 | switch (userdiff_config(var, value)) { |
293 | case 0: break; | |
294 | case -1: return -1; | |
295 | default: return 0; | |
296 | } | |
297 | ||
fe3420b6 | 298 | if (!strcmp(var, "color.grep")) { |
7e8f59d5 RS |
299 | opt->color = git_config_colorbool(var, value, -1); |
300 | return 0; | |
301 | } | |
fe3420b6 | 302 | if (!strcmp(var, "color.grep.match")) { |
7e8f59d5 RS |
303 | if (!value) |
304 | return config_error_nonbool(var); | |
305 | color_parse(value, var, opt->color_match); | |
306 | return 0; | |
307 | } | |
308 | return git_color_default_config(var, value, cb); | |
309 | } | |
310 | ||
a91f453f MK |
311 | /* |
312 | * Return non-zero if max_depth is negative or path has no more then max_depth | |
313 | * slashes. | |
314 | */ | |
315 | static int accept_subdir(const char *path, int max_depth) | |
316 | { | |
317 | if (max_depth < 0) | |
318 | return 1; | |
319 | ||
320 | while ((path = strchr(path, '/')) != NULL) { | |
321 | max_depth--; | |
322 | if (max_depth < 0) | |
323 | return 0; | |
324 | path++; | |
325 | } | |
326 | return 1; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Return non-zero if name is a subdirectory of match and is not too deep. | |
331 | */ | |
332 | static int is_subdir(const char *name, int namelen, | |
333 | const char *match, int matchlen, int max_depth) | |
334 | { | |
335 | if (matchlen > namelen || strncmp(name, match, matchlen)) | |
336 | return 0; | |
337 | ||
338 | if (name[matchlen] == '\0') /* exact match */ | |
339 | return 1; | |
340 | ||
341 | if (!matchlen || match[matchlen-1] == '/' || name[matchlen] == '/') | |
342 | return accept_subdir(name + matchlen + 1, max_depth); | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
e0eb889f JH |
347 | /* |
348 | * git grep pathspecs are somewhat different from diff-tree pathspecs; | |
349 | * pathname wildcards are allowed. | |
350 | */ | |
a91f453f | 351 | static int pathspec_matches(const char **paths, const char *name, int max_depth) |
5010cb5f | 352 | { |
e0eb889f | 353 | int namelen, i; |
1362671f | 354 | if (!paths || !*paths) |
a91f453f | 355 | return accept_subdir(name, max_depth); |
5010cb5f | 356 | namelen = strlen(name); |
1362671f JH |
357 | for (i = 0; paths[i]; i++) { |
358 | const char *match = paths[i]; | |
359 | int matchlen = strlen(match); | |
1e3d90e0 | 360 | const char *cp, *meta; |
e0eb889f | 361 | |
a91f453f | 362 | if (is_subdir(name, namelen, match, matchlen, max_depth)) |
e0eb889f JH |
363 | return 1; |
364 | if (!fnmatch(match, name, 0)) | |
365 | return 1; | |
366 | if (name[namelen-1] != '/') | |
5010cb5f | 367 | continue; |
e0eb889f | 368 | |
1e3d90e0 | 369 | /* We are being asked if the directory ("name") is worth |
e0eb889f JH |
370 | * descending into. |
371 | * | |
372 | * Find the longest leading directory name that does | |
373 | * not have metacharacter in the pathspec; the name | |
374 | * we are looking at must overlap with that directory. | |
5010cb5f | 375 | */ |
1e3d90e0 | 376 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
e0eb889f | 377 | char ch = *cp; |
1e3d90e0 JH |
378 | if (ch == '*' || ch == '[' || ch == '?') { |
379 | meta = cp; | |
5010cb5f | 380 | break; |
1e3d90e0 | 381 | } |
e0eb889f | 382 | } |
1e3d90e0 JH |
383 | if (!meta) |
384 | meta = cp; /* fully literal */ | |
385 | ||
386 | if (namelen <= meta - match) { | |
e0eb889f JH |
387 | /* Looking at "Documentation/" and |
388 | * the pattern says "Documentation/howto/", or | |
1e3d90e0 JH |
389 | * "Documentation/diff*.txt". The name we |
390 | * have should match prefix. | |
e0eb889f JH |
391 | */ |
392 | if (!memcmp(match, name, namelen)) | |
393 | return 1; | |
1e3d90e0 | 394 | continue; |
e0eb889f | 395 | } |
1e3d90e0 JH |
396 | |
397 | if (meta - match < namelen) { | |
e0eb889f | 398 | /* Looking at "Documentation/howto/" and |
1e3d90e0 JH |
399 | * the pattern says "Documentation/h*"; |
400 | * match up to "Do.../h"; this avoids descending | |
401 | * into "Documentation/technical/". | |
e0eb889f | 402 | */ |
1e3d90e0 | 403 | if (!memcmp(match, name, meta - match)) |
e0eb889f | 404 | return 1; |
1e3d90e0 | 405 | continue; |
e0eb889f | 406 | } |
5010cb5f JH |
407 | } |
408 | return 0; | |
409 | } | |
410 | ||
5b594f45 FK |
411 | static void *load_sha1(const unsigned char *sha1, unsigned long *size, |
412 | const char *name) | |
5010cb5f | 413 | { |
21666f1a | 414 | enum object_type type; |
5b594f45 | 415 | char *data; |
0d042fec | 416 | |
5b594f45 FK |
417 | read_sha1_lock(); |
418 | data = read_sha1_file(sha1, &type, size); | |
419 | read_sha1_unlock(); | |
420 | ||
421 | if (!data) | |
5010cb5f | 422 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); |
5b594f45 FK |
423 | |
424 | return data; | |
425 | } | |
426 | ||
427 | static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, | |
428 | const char *filename, int tree_name_len) | |
429 | { | |
430 | struct strbuf pathbuf = STRBUF_INIT; | |
431 | char *name; | |
432 | ||
0d042fec | 433 | if (opt->relative && opt->prefix_length) { |
5b594f45 FK |
434 | quote_path_relative(filename + tree_name_len, -1, &pathbuf, |
435 | opt->prefix); | |
436 | strbuf_insert(&pathbuf, 0, filename, tree_name_len); | |
437 | } else { | |
438 | strbuf_addstr(&pathbuf, filename); | |
439 | } | |
440 | ||
441 | name = strbuf_detach(&pathbuf, NULL); | |
442 | ||
443 | #ifndef NO_PTHREADS | |
444 | if (use_threads) { | |
445 | grep_sha1_async(opt, name, sha1); | |
446 | return 0; | |
447 | } else | |
448 | #endif | |
449 | { | |
450 | int hit; | |
451 | unsigned long sz; | |
452 | void *data = load_sha1(sha1, &sz, name); | |
453 | if (!data) | |
454 | hit = 0; | |
455 | else | |
456 | hit = grep_buffer(opt, name, data, sz); | |
457 | ||
458 | free(data); | |
459 | free(name); | |
460 | return hit; | |
0d042fec | 461 | } |
5010cb5f JH |
462 | } |
463 | ||
5b594f45 | 464 | static void *load_file(const char *filename, size_t *sz) |
5010cb5f JH |
465 | { |
466 | struct stat st; | |
5010cb5f | 467 | char *data; |
5b594f45 | 468 | int i; |
dc49cd76 | 469 | |
5010cb5f JH |
470 | if (lstat(filename, &st) < 0) { |
471 | err_ret: | |
472 | if (errno != ENOENT) | |
473 | error("'%s': %s", filename, strerror(errno)); | |
474 | return 0; | |
475 | } | |
5010cb5f JH |
476 | if (!S_ISREG(st.st_mode)) |
477 | return 0; | |
5b594f45 | 478 | *sz = xsize_t(st.st_size); |
5010cb5f JH |
479 | i = open(filename, O_RDONLY); |
480 | if (i < 0) | |
481 | goto err_ret; | |
5b594f45 FK |
482 | data = xmalloc(*sz + 1); |
483 | if (st.st_size != read_in_full(i, data, *sz)) { | |
5010cb5f JH |
484 | error("'%s': short read %s", filename, strerror(errno)); |
485 | close(i); | |
486 | free(data); | |
487 | return 0; | |
488 | } | |
489 | close(i); | |
5b594f45 FK |
490 | data[*sz] = 0; |
491 | return data; | |
492 | } | |
493 | ||
494 | static int grep_file(struct grep_opt *opt, const char *filename) | |
495 | { | |
496 | struct strbuf buf = STRBUF_INIT; | |
497 | char *name; | |
498 | ||
0d042fec | 499 | if (opt->relative && opt->prefix_length) |
5b594f45 FK |
500 | quote_path_relative(filename, -1, &buf, opt->prefix); |
501 | else | |
502 | strbuf_addstr(&buf, filename); | |
503 | name = strbuf_detach(&buf, NULL); | |
504 | ||
505 | #ifndef NO_PTHREADS | |
506 | if (use_threads) { | |
507 | grep_file_async(opt, name, filename); | |
508 | return 0; | |
509 | } else | |
510 | #endif | |
511 | { | |
512 | int hit; | |
513 | size_t sz; | |
514 | void *data = load_file(filename, &sz); | |
515 | if (!data) | |
516 | hit = 0; | |
517 | else | |
518 | hit = grep_buffer(opt, name, data, sz); | |
519 | ||
520 | free(data); | |
521 | free(name); | |
522 | return hit; | |
523 | } | |
5010cb5f JH |
524 | } |
525 | ||
bbc09c22 | 526 | static int grep_cache(struct grep_opt *opt, const char **paths, int cached) |
5010cb5f JH |
527 | { |
528 | int hit = 0; | |
529 | int nr; | |
530 | read_cache(); | |
531 | ||
532 | for (nr = 0; nr < active_nr; nr++) { | |
533 | struct cache_entry *ce = active_cache[nr]; | |
7a51ed66 | 534 | if (!S_ISREG(ce->ce_mode)) |
5010cb5f | 535 | continue; |
a91f453f | 536 | if (!pathspec_matches(paths, ce->name, opt->max_depth)) |
5010cb5f | 537 | continue; |
57d43466 NTND |
538 | /* |
539 | * If CE_VALID is on, we assume worktree file and its cache entry | |
540 | * are identical, even if worktree file has been modified, so use | |
541 | * cache version instead | |
542 | */ | |
b4d1690d | 543 | if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) { |
36f2587f JH |
544 | if (ce_stage(ce)) |
545 | continue; | |
0d042fec | 546 | hit |= grep_sha1(opt, ce->sha1, ce->name, 0); |
36f2587f | 547 | } |
5010cb5f JH |
548 | else |
549 | hit |= grep_file(opt, ce->name); | |
36f2587f JH |
550 | if (ce_stage(ce)) { |
551 | do { | |
552 | nr++; | |
553 | } while (nr < active_nr && | |
554 | !strcmp(ce->name, active_cache[nr]->name)); | |
555 | nr--; /* compensate for loop control */ | |
556 | } | |
c8610a2e JH |
557 | if (hit && opt->status_only) |
558 | break; | |
5010cb5f | 559 | } |
b48fb5b6 | 560 | free_grep_patterns(opt); |
5010cb5f JH |
561 | return hit; |
562 | } | |
563 | ||
1362671f | 564 | static int grep_tree(struct grep_opt *opt, const char **paths, |
5010cb5f JH |
565 | struct tree_desc *tree, |
566 | const char *tree_name, const char *base) | |
567 | { | |
5010cb5f JH |
568 | int len; |
569 | int hit = 0; | |
4c068a98 | 570 | struct name_entry entry; |
e0eb889f | 571 | char *down; |
0d042fec | 572 | int tn_len = strlen(tree_name); |
620e2bb9 DP |
573 | struct strbuf pathbuf; |
574 | ||
575 | strbuf_init(&pathbuf, PATH_MAX + tn_len); | |
5010cb5f | 576 | |
0d042fec | 577 | if (tn_len) { |
620e2bb9 DP |
578 | strbuf_add(&pathbuf, tree_name, tn_len); |
579 | strbuf_addch(&pathbuf, ':'); | |
580 | tn_len = pathbuf.len; | |
5010cb5f | 581 | } |
620e2bb9 DP |
582 | strbuf_addstr(&pathbuf, base); |
583 | len = pathbuf.len; | |
5010cb5f | 584 | |
4c068a98 | 585 | while (tree_entry(tree, &entry)) { |
620e2bb9 DP |
586 | int te_len = tree_entry_len(entry.path, entry.sha1); |
587 | pathbuf.len = len; | |
588 | strbuf_add(&pathbuf, entry.path, te_len); | |
5010cb5f | 589 | |
4c068a98 | 590 | if (S_ISDIR(entry.mode)) |
e0eb889f JH |
591 | /* Match "abc/" against pathspec to |
592 | * decide if we want to descend into "abc" | |
593 | * directory. | |
594 | */ | |
620e2bb9 | 595 | strbuf_addch(&pathbuf, '/'); |
e0eb889f | 596 | |
620e2bb9 | 597 | down = pathbuf.buf + tn_len; |
a91f453f | 598 | if (!pathspec_matches(paths, down, opt->max_depth)) |
5010cb5f | 599 | ; |
4c068a98 | 600 | else if (S_ISREG(entry.mode)) |
620e2bb9 | 601 | hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len); |
4c068a98 | 602 | else if (S_ISDIR(entry.mode)) { |
21666f1a | 603 | enum object_type type; |
5010cb5f JH |
604 | struct tree_desc sub; |
605 | void *data; | |
6fda5e51 LT |
606 | unsigned long size; |
607 | ||
5b594f45 | 608 | read_sha1_lock(); |
6fda5e51 | 609 | data = read_sha1_file(entry.sha1, &type, &size); |
5b594f45 FK |
610 | read_sha1_unlock(); |
611 | ||
5010cb5f JH |
612 | if (!data) |
613 | die("unable to read tree (%s)", | |
4c068a98 | 614 | sha1_to_hex(entry.sha1)); |
6fda5e51 | 615 | init_tree_desc(&sub, data, size); |
1362671f | 616 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
5010cb5f JH |
617 | free(data); |
618 | } | |
c8610a2e JH |
619 | if (hit && opt->status_only) |
620 | break; | |
5010cb5f | 621 | } |
620e2bb9 | 622 | strbuf_release(&pathbuf); |
5010cb5f JH |
623 | return hit; |
624 | } | |
625 | ||
1362671f | 626 | static int grep_object(struct grep_opt *opt, const char **paths, |
5010cb5f JH |
627 | struct object *obj, const char *name) |
628 | { | |
1974632c | 629 | if (obj->type == OBJ_BLOB) |
0d042fec | 630 | return grep_sha1(opt, obj->sha1, name, 0); |
1974632c | 631 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
5010cb5f JH |
632 | struct tree_desc tree; |
633 | void *data; | |
6fda5e51 | 634 | unsigned long size; |
5010cb5f JH |
635 | int hit; |
636 | data = read_object_with_reference(obj->sha1, tree_type, | |
6fda5e51 | 637 | &size, NULL); |
5010cb5f JH |
638 | if (!data) |
639 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); | |
6fda5e51 | 640 | init_tree_desc(&tree, data, size); |
1362671f | 641 | hit = grep_tree(opt, paths, &tree, name, ""); |
5010cb5f JH |
642 | free(data); |
643 | return hit; | |
644 | } | |
885a86ab | 645 | die("unable to grep from object of type %s", typename(obj->type)); |
5010cb5f JH |
646 | } |
647 | ||
ff3c7f9a RS |
648 | static int context_callback(const struct option *opt, const char *arg, |
649 | int unset) | |
3e230fa1 RS |
650 | { |
651 | struct grep_opt *grep_opt = opt->value; | |
652 | int value; | |
653 | const char *endp; | |
654 | ||
655 | if (unset) { | |
656 | grep_opt->pre_context = grep_opt->post_context = 0; | |
657 | return 0; | |
658 | } | |
659 | value = strtol(arg, (char **)&endp, 10); | |
660 | if (*endp) { | |
661 | return error("switch `%c' expects a numerical value", | |
662 | opt->short_name); | |
663 | } | |
664 | grep_opt->pre_context = grep_opt->post_context = value; | |
665 | return 0; | |
666 | } | |
667 | ||
ff3c7f9a | 668 | static int file_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
669 | { |
670 | struct grep_opt *grep_opt = opt->value; | |
671 | FILE *patterns; | |
672 | int lno = 0; | |
cfe370c6 | 673 | struct strbuf sb = STRBUF_INIT; |
3e230fa1 RS |
674 | |
675 | patterns = fopen(arg, "r"); | |
676 | if (!patterns) | |
d824cbba | 677 | die_errno("cannot open '%s'", arg); |
3e230fa1 RS |
678 | while (strbuf_getline(&sb, patterns, '\n') == 0) { |
679 | /* ignore empty line like grep does */ | |
680 | if (sb.len == 0) | |
681 | continue; | |
682 | append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg, | |
683 | ++lno, GREP_PATTERN); | |
684 | } | |
685 | fclose(patterns); | |
686 | strbuf_release(&sb); | |
687 | return 0; | |
688 | } | |
689 | ||
ff3c7f9a | 690 | static int not_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
691 | { |
692 | struct grep_opt *grep_opt = opt->value; | |
693 | append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); | |
694 | return 0; | |
695 | } | |
696 | ||
ff3c7f9a | 697 | static int and_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
698 | { |
699 | struct grep_opt *grep_opt = opt->value; | |
700 | append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); | |
701 | return 0; | |
702 | } | |
703 | ||
ff3c7f9a | 704 | static int open_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
705 | { |
706 | struct grep_opt *grep_opt = opt->value; | |
707 | append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); | |
708 | return 0; | |
709 | } | |
710 | ||
ff3c7f9a | 711 | static int close_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
712 | { |
713 | struct grep_opt *grep_opt = opt->value; | |
714 | append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); | |
715 | return 0; | |
716 | } | |
717 | ||
ff3c7f9a RS |
718 | static int pattern_callback(const struct option *opt, const char *arg, |
719 | int unset) | |
3e230fa1 RS |
720 | { |
721 | struct grep_opt *grep_opt = opt->value; | |
722 | append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); | |
723 | return 0; | |
724 | } | |
5010cb5f | 725 | |
ff3c7f9a | 726 | static int help_callback(const struct option *opt, const char *arg, int unset) |
3e230fa1 RS |
727 | { |
728 | return -1; | |
729 | } | |
088b084b | 730 | |
a633fca0 | 731 | int cmd_grep(int argc, const char **argv, const char *prefix) |
5010cb5f | 732 | { |
5010cb5f | 733 | int hit = 0; |
5010cb5f | 734 | int cached = 0; |
5acd64ed | 735 | int seen_dashdash = 0; |
bbc09c22 | 736 | int external_grep_allowed__ignored; |
5010cb5f | 737 | struct grep_opt opt; |
1f1e895f | 738 | struct object_array list = { 0, 0, NULL }; |
1362671f | 739 | const char **paths = NULL; |
5acd64ed | 740 | int i; |
3e230fa1 RS |
741 | int dummy; |
742 | struct option options[] = { | |
743 | OPT_BOOLEAN(0, "cached", &cached, | |
744 | "search in index instead of in the work tree"), | |
745 | OPT_GROUP(""), | |
746 | OPT_BOOLEAN('v', "invert-match", &opt.invert, | |
747 | "show non-matching lines"), | |
5183bf67 BC |
748 | OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case, |
749 | "case insensitive matching"), | |
3e230fa1 RS |
750 | OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp, |
751 | "match patterns only at word boundaries"), | |
752 | OPT_SET_INT('a', "text", &opt.binary, | |
753 | "process binary files as text", GREP_BINARY_TEXT), | |
754 | OPT_SET_INT('I', NULL, &opt.binary, | |
755 | "don't match patterns in binary files", | |
756 | GREP_BINARY_NOMATCH), | |
a91f453f MK |
757 | { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth", |
758 | "descend at most <depth> levels", PARSE_OPT_NONEG, | |
759 | NULL, 1 }, | |
3e230fa1 RS |
760 | OPT_GROUP(""), |
761 | OPT_BIT('E', "extended-regexp", &opt.regflags, | |
762 | "use extended POSIX regular expressions", REG_EXTENDED), | |
763 | OPT_NEGBIT('G', "basic-regexp", &opt.regflags, | |
764 | "use basic POSIX regular expressions (default)", | |
765 | REG_EXTENDED), | |
766 | OPT_BOOLEAN('F', "fixed-strings", &opt.fixed, | |
767 | "interpret patterns as fixed strings"), | |
768 | OPT_GROUP(""), | |
769 | OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"), | |
770 | OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1), | |
771 | OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1), | |
772 | OPT_NEGBIT(0, "full-name", &opt.relative, | |
773 | "show filenames relative to top directory", 1), | |
774 | OPT_BOOLEAN('l', "files-with-matches", &opt.name_only, | |
775 | "show only filenames instead of matching lines"), | |
776 | OPT_BOOLEAN(0, "name-only", &opt.name_only, | |
777 | "synonym for --files-with-matches"), | |
778 | OPT_BOOLEAN('L', "files-without-match", | |
779 | &opt.unmatch_name_only, | |
780 | "show only the names of files without match"), | |
781 | OPT_BOOLEAN('z', "null", &opt.null_following_name, | |
782 | "print NUL after filenames"), | |
783 | OPT_BOOLEAN('c', "count", &opt.count, | |
784 | "show the number of matches instead of matching lines"), | |
785 | OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1), | |
786 | OPT_GROUP(""), | |
787 | OPT_CALLBACK('C', NULL, &opt, "n", | |
788 | "show <n> context lines before and after matches", | |
789 | context_callback), | |
790 | OPT_INTEGER('B', NULL, &opt.pre_context, | |
791 | "show <n> context lines before matches"), | |
792 | OPT_INTEGER('A', NULL, &opt.post_context, | |
793 | "show <n> context lines after matches"), | |
794 | OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM", | |
795 | context_callback), | |
2944e4e6 RS |
796 | OPT_BOOLEAN('p', "show-function", &opt.funcname, |
797 | "show a line with the function name before matches"), | |
3e230fa1 RS |
798 | OPT_GROUP(""), |
799 | OPT_CALLBACK('f', NULL, &opt, "file", | |
800 | "read patterns from file", file_callback), | |
801 | { OPTION_CALLBACK, 'e', NULL, &opt, "pattern", | |
802 | "match <pattern>", PARSE_OPT_NONEG, pattern_callback }, | |
803 | { OPTION_CALLBACK, 0, "and", &opt, NULL, | |
804 | "combine patterns specified with -e", | |
805 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback }, | |
806 | OPT_BOOLEAN(0, "or", &dummy, ""), | |
807 | { OPTION_CALLBACK, 0, "not", &opt, NULL, "", | |
808 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback }, | |
809 | { OPTION_CALLBACK, '(', NULL, &opt, NULL, "", | |
810 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
811 | open_callback }, | |
812 | { OPTION_CALLBACK, ')', NULL, &opt, NULL, "", | |
813 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, | |
814 | close_callback }, | |
4ff61c21 | 815 | OPT_BOOLEAN('q', "quiet", &opt.status_only, |
c8610a2e | 816 | "indicate hit with exit status without output"), |
3e230fa1 RS |
817 | OPT_BOOLEAN(0, "all-match", &opt.all_match, |
818 | "show only matches from files that match all patterns"), | |
819 | OPT_GROUP(""), | |
bbc09c22 JH |
820 | OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored, |
821 | "allow calling of grep(1) (ignored by this build)"), | |
3e230fa1 RS |
822 | { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage", |
823 | PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback }, | |
824 | OPT_END() | |
825 | }; | |
5010cb5f | 826 | |
9c855c31 JN |
827 | /* |
828 | * 'git grep -h', unlike 'git grep -h <pattern>', is a request | |
829 | * to show usage information and exit. | |
830 | */ | |
831 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
832 | usage_with_options(grep_usage, options); | |
833 | ||
5010cb5f | 834 | memset(&opt, 0, sizeof(opt)); |
493b7a08 | 835 | opt.prefix = prefix; |
0d042fec JH |
836 | opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; |
837 | opt.relative = 1; | |
7977f0ea | 838 | opt.pathname = 1; |
f9b9faf6 | 839 | opt.pattern_tail = &opt.pattern_list; |
5010cb5f | 840 | opt.regflags = REG_NEWLINE; |
a91f453f | 841 | opt.max_depth = -1; |
5010cb5f | 842 | |
7e8f59d5 RS |
843 | strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD); |
844 | opt.color = -1; | |
845 | git_config(grep_config, &opt); | |
846 | if (opt.color == -1) | |
847 | opt.color = git_use_color_default; | |
848 | ||
5010cb5f | 849 | /* |
5acd64ed JH |
850 | * If there is no -- then the paths must exist in the working |
851 | * tree. If there is no explicit pattern specified with -e or | |
852 | * -f, we take the first unrecognized non option to be the | |
853 | * pattern, but then what follows it must be zero or more | |
854 | * valid refs up to the -- (if exists), and then existing | |
855 | * paths. If there is an explicit pattern, then the first | |
82e5a82f | 856 | * unrecognized non option is the beginning of the refs list |
5acd64ed | 857 | * that continues up to the -- (if exists), and then paths. |
5010cb5f | 858 | */ |
37782920 | 859 | argc = parse_options(argc, argv, prefix, options, grep_usage, |
3e230fa1 RS |
860 | PARSE_OPT_KEEP_DASHDASH | |
861 | PARSE_OPT_STOP_AT_NON_OPTION | | |
862 | PARSE_OPT_NO_INTERNAL_HELP); | |
863 | ||
864 | /* First unrecognized non-option token */ | |
865 | if (argc > 0 && !opt.pattern_list) { | |
866 | append_grep_pattern(&opt, argv[0], "command line", 0, | |
867 | GREP_PATTERN); | |
868 | argv++; | |
869 | argc--; | |
5010cb5f | 870 | } |
5acd64ed | 871 | |
f9b9faf6 | 872 | if (!opt.pattern_list) |
5010cb5f | 873 | die("no pattern given."); |
5183bf67 BC |
874 | if (!opt.fixed && opt.ignore_case) |
875 | opt.regflags |= REG_ICASE; | |
07ea91d8 JH |
876 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
877 | die("cannot mix --fixed-strings and regexp"); | |
5b594f45 FK |
878 | |
879 | #ifndef NO_PTHREADS | |
880 | if (online_cpus() == 1 || !grep_threads_ok(&opt)) | |
881 | use_threads = 0; | |
882 | ||
883 | if (use_threads) | |
884 | start_threads(&opt); | |
885 | #else | |
886 | use_threads = 0; | |
887 | #endif | |
888 | ||
83b5d2f5 | 889 | compile_grep_patterns(&opt); |
5acd64ed JH |
890 | |
891 | /* Check revs and then paths */ | |
3e230fa1 | 892 | for (i = 0; i < argc; i++) { |
5acd64ed | 893 | const char *arg = argv[i]; |
1362671f | 894 | unsigned char sha1[20]; |
5acd64ed JH |
895 | /* Is it a rev? */ |
896 | if (!get_sha1(arg, sha1)) { | |
897 | struct object *object = parse_object(sha1); | |
5acd64ed JH |
898 | if (!object) |
899 | die("bad object %s", arg); | |
1f1e895f | 900 | add_object_array(object, arg, &list); |
5acd64ed JH |
901 | continue; |
902 | } | |
903 | if (!strcmp(arg, "--")) { | |
904 | i++; | |
905 | seen_dashdash = 1; | |
906 | } | |
907 | break; | |
1362671f | 908 | } |
5acd64ed JH |
909 | |
910 | /* The rest are paths */ | |
911 | if (!seen_dashdash) { | |
912 | int j; | |
c39c4f47 | 913 | for (j = i; j < argc; j++) |
5acd64ed JH |
914 | verify_filename(prefix, argv[j]); |
915 | } | |
916 | ||
493b7a08 | 917 | if (i < argc) |
5acd64ed | 918 | paths = get_pathspec(prefix, argv + i); |
1362671f JH |
919 | else if (prefix) { |
920 | paths = xcalloc(2, sizeof(const char *)); | |
921 | paths[0] = prefix; | |
922 | paths[1] = NULL; | |
923 | } | |
5010cb5f | 924 | |
6577f542 | 925 | if (!list.nr) { |
5b594f45 | 926 | int hit; |
6577f542 NTND |
927 | if (!cached) |
928 | setup_work_tree(); | |
5b594f45 FK |
929 | |
930 | hit = grep_cache(&opt, paths, cached); | |
931 | if (use_threads) | |
932 | hit |= wait_all(); | |
933 | return !hit; | |
6577f542 | 934 | } |
aa8c79ad | 935 | |
5010cb5f | 936 | if (cached) |
aa8c79ad | 937 | die("both --cached and trees are given."); |
5010cb5f | 938 | |
1f1e895f | 939 | for (i = 0; i < list.nr; i++) { |
5010cb5f | 940 | struct object *real_obj; |
1f1e895f | 941 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
c8610a2e | 942 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) { |
5010cb5f | 943 | hit = 1; |
c8610a2e JH |
944 | if (opt.status_only) |
945 | break; | |
946 | } | |
5010cb5f | 947 | } |
5b594f45 FK |
948 | |
949 | if (use_threads) | |
950 | hit |= wait_all(); | |
b48fb5b6 | 951 | free_grep_patterns(&opt); |
5010cb5f JH |
952 | return !hit; |
953 | } |