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