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