grep.c 30.4 KB
Newer Older
J
Junio C Hamano 已提交
1 2 3 4 5 6
/*
 * Builtin "git grep"
 *
 * Copyright (c) 2006 Junio C Hamano
 */
#include "cache.h"
7
#include "repository.h"
8
#include "config.h"
J
Junio C Hamano 已提交
9 10 11 12
#include "blob.h"
#include "tree.h"
#include "commit.h"
#include "tag.h"
13
#include "tree-walk.h"
J
Junio C Hamano 已提交
14
#include "builtin.h"
R
René Scharfe 已提交
15
#include "parse-options.h"
16 17
#include "string-list.h"
#include "run-command.h"
18
#include "userdiff.h"
19
#include "grep.h"
20
#include "quote.h"
J
Junio C Hamano 已提交
21
#include "dir.h"
22
#include "pathspec.h"
23
#include "submodule.h"
24
#include "submodule-config.h"
25
#include "object-store.h"
F
Fredrik Kuivinen 已提交
26

R
René Scharfe 已提交
27
static char const * const grep_usage[] = {
28
	N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
R
René Scharfe 已提交
29 30 31
	NULL
};

32 33
static int recurse_submodules;

34 35
#define GREP_NUM_THREADS_DEFAULT 8
static int num_threads;
F
Fredrik Kuivinen 已提交
36

37
static pthread_t *threads;
F
Fredrik Kuivinen 已提交
38 39 40 41 42

/* We use one producer thread and THREADS consumer
 * threads. The producer adds struct work_items to 'todo' and the
 * consumers pick work items from the same array.
 */
43
struct work_item {
44
	struct grep_source source;
F
Fredrik Kuivinen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	char done;
	struct strbuf out;
};

/* In the range [todo_done, todo_start) in 'todo' we have work_items
 * that have been or are processed by a consumer thread. We haven't
 * written the result for these to stdout yet.
 *
 * The work_items in [todo_start, todo_end) are waiting to be picked
 * up by a consumer thread.
 *
 * The ranges are modulo TODO_SIZE.
 */
#define TODO_SIZE 128
static struct work_item todo[TODO_SIZE];
static int todo_start;
static int todo_end;
static int todo_done;

/* Has all work items been added? */
static int all_work_added;

/* This lock protects all the variables above. */
static pthread_mutex_t grep_mutex;

70 71
static inline void grep_lock(void)
{
72
	pthread_mutex_lock(&grep_mutex);
73 74 75 76
}

static inline void grep_unlock(void)
{
77
	pthread_mutex_unlock(&grep_mutex);
78 79
}

F
Fredrik Kuivinen 已提交
80 81 82 83 84 85 86 87 88 89 90
/* Signalled when a new work_item is added to todo. */
static pthread_cond_t cond_add;

/* Signalled when the result from one work_item is written to
 * stdout.
 */
static pthread_cond_t cond_write;

/* Signalled when we are finished with everything. */
static pthread_cond_t cond_result;

91
static int skip_first_line;
92

93
static void add_work(struct grep_opt *opt, const struct grep_source *gs)
F
Fredrik Kuivinen 已提交
94 95 96 97 98 99 100
{
	grep_lock();

	while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
		pthread_cond_wait(&cond_write, &grep_mutex);
	}

101
	todo[todo_end].source = *gs;
102
	if (opt->binary != GREP_BINARY_TEXT)
103 104
		grep_source_load_driver(&todo[todo_end].source,
					opt->repo->index);
F
Fredrik Kuivinen 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	todo[todo_end].done = 0;
	strbuf_reset(&todo[todo_end].out);
	todo_end = (todo_end + 1) % ARRAY_SIZE(todo);

	pthread_cond_signal(&cond_add);
	grep_unlock();
}

static struct work_item *get_work(void)
{
	struct work_item *ret;

	grep_lock();
	while (todo_start == todo_end && !all_work_added) {
		pthread_cond_wait(&cond_add, &grep_mutex);
	}

	if (todo_start == todo_end && all_work_added) {
		ret = NULL;
	} else {
		ret = &todo[todo_start];
		todo_start = (todo_start + 1) % ARRAY_SIZE(todo);
	}
	grep_unlock();
	return ret;
}

static void work_done(struct work_item *w)
{
	int old_done;

	grep_lock();
	w->done = 1;
	old_done = todo_done;
	for(; todo[todo_done].done && todo_done != todo_start;
	    todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
		w = &todo[todo_done];
142
		if (w->out.len) {
143 144 145 146 147 148 149 150 151 152 153 154 155 156
			const char *p = w->out.buf;
			size_t len = w->out.len;

			/* Skip the leading hunk mark of the first file. */
			if (skip_first_line) {
				while (len) {
					len--;
					if (*p++ == '\n')
						break;
				}
				skip_first_line = 0;
			}

			write_or_die(1, p, len);
157
		}
158
		grep_source_clear(&w->source);
F
Fredrik Kuivinen 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	}

	if (old_done != todo_done)
		pthread_cond_signal(&cond_write);

	if (all_work_added && todo_done == todo_end)
		pthread_cond_signal(&cond_result);

	grep_unlock();
}

static void *run(void *arg)
{
	int hit = 0;
	struct grep_opt *opt = arg;

	while (1) {
		struct work_item *w = get_work();
		if (!w)
			break;

		opt->output_priv = w;
181
		hit |= grep_source(opt, &w->source);
182
		grep_source_clear_data(&w->source);
F
Fredrik Kuivinen 已提交
183 184
		work_done(w);
	}
D
Dan McGee 已提交
185 186
	free_grep_patterns(arg);
	free(arg);
F
Fredrik Kuivinen 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

	return (void*) (intptr_t) hit;
}

static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size)
{
	struct work_item *w = opt->output_priv;
	strbuf_add(&w->out, buf, size);
}

static void start_threads(struct grep_opt *opt)
{
	int i;

	pthread_mutex_init(&grep_mutex, NULL);
202
	pthread_mutex_init(&grep_read_mutex, NULL);
203
	pthread_mutex_init(&grep_attr_mutex, NULL);
F
Fredrik Kuivinen 已提交
204 205 206
	pthread_cond_init(&cond_add, NULL);
	pthread_cond_init(&cond_write, NULL);
	pthread_cond_init(&cond_result, NULL);
J
Jeff King 已提交
207
	grep_use_locks = 1;
F
Fredrik Kuivinen 已提交
208 209 210 211 212

	for (i = 0; i < ARRAY_SIZE(todo); i++) {
		strbuf_init(&todo[i].out, 0);
	}

213 214
	threads = xcalloc(num_threads, sizeof(*threads));
	for (i = 0; i < num_threads; i++) {
F
Fredrik Kuivinen 已提交
215 216 217
		int err;
		struct grep_opt *o = grep_opt_dup(opt);
		o->output = strbuf_out;
218 219
		if (i)
			o->debug = 0;
F
Fredrik Kuivinen 已提交
220 221 222 223
		compile_grep_patterns(o);
		err = pthread_create(&threads[i], NULL, run, o);

		if (err)
224
			die(_("grep: failed to create thread: %s"),
F
Fredrik Kuivinen 已提交
225 226 227 228 229 230 231 232 233
			    strerror(err));
	}
}

static int wait_all(void)
{
	int hit = 0;
	int i;

234
	if (!HAVE_THREADS)
235
		BUG("Never call this function unless you have started threads");
236

F
Fredrik Kuivinen 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
	grep_lock();
	all_work_added = 1;

	/* Wait until all work is done. */
	while (todo_done != todo_end)
		pthread_cond_wait(&cond_result, &grep_mutex);

	/* Wake up all the consumer threads so they can see that there
	 * is no more work to do.
	 */
	pthread_cond_broadcast(&cond_add);
	grep_unlock();

250
	for (i = 0; i < num_threads; i++) {
F
Fredrik Kuivinen 已提交
251 252 253 254 255
		void *h;
		pthread_join(threads[i], &h);
		hit |= (int) (intptr_t) h;
	}

256 257
	free(threads);

F
Fredrik Kuivinen 已提交
258
	pthread_mutex_destroy(&grep_mutex);
259
	pthread_mutex_destroy(&grep_read_mutex);
260
	pthread_mutex_destroy(&grep_attr_mutex);
F
Fredrik Kuivinen 已提交
261 262 263
	pthread_cond_destroy(&cond_add);
	pthread_cond_destroy(&cond_write);
	pthread_cond_destroy(&cond_result);
J
Jeff King 已提交
264
	grep_use_locks = 0;
F
Fredrik Kuivinen 已提交
265 266 267 268

	return hit;
}

269 270 271
static int grep_cmd_config(const char *var, const char *value, void *cb)
{
	int st = grep_config(var, value, cb);
272
	if (git_color_default_config(var, value, cb) < 0)
273
		st = -1;
274 275 276 277 278 279

	if (!strcmp(var, "grep.threads")) {
		num_threads = git_config_int(var, value);
		if (num_threads < 0)
			die(_("invalid number of threads specified (%d) for %s"),
			    num_threads, var);
280
		else if (!HAVE_THREADS && num_threads > 1) {
281 282 283 284 285 286
			/*
			 * TRANSLATORS: %s is the configuration
			 * variable for tweaking threads, currently
			 * grep.threads
			 */
			warning(_("no threads support, ignoring %s"), var);
287
			num_threads = 1;
288
		}
289 290
	}

291 292 293
	if (!strcmp(var, "submodule.recurse"))
		recurse_submodules = git_config_bool(var, value);

294 295 296
	return st;
}

297
static void *lock_and_read_oid_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
298 299 300
{
	void *data;

301
	grep_read_lock();
302
	data = read_object_file(oid, type, size);
303
	grep_read_unlock();
F
Fredrik Kuivinen 已提交
304 305 306
	return data;
}

307
static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
308 309
		     const char *filename, int tree_name_len,
		     const char *path)
F
Fredrik Kuivinen 已提交
310 311
{
	struct strbuf pathbuf = STRBUF_INIT;
312
	struct grep_source gs;
F
Fredrik Kuivinen 已提交
313

314 315 316
	if (opt->relative && opt->prefix_length) {
		quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
		strbuf_insert(&pathbuf, 0, filename, tree_name_len);
F
Fredrik Kuivinen 已提交
317 318 319 320
	} else {
		strbuf_addstr(&pathbuf, filename);
	}

321
	grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
322
	strbuf_release(&pathbuf);
323

324
	if (num_threads > 1) {
325 326 327 328 329
		/*
		 * add_work() copies gs and thus assumes ownership of
		 * its fields, so do not call grep_source_clear()
		 */
		add_work(opt, &gs);
F
Fredrik Kuivinen 已提交
330
		return 0;
331
	} else {
F
Fredrik Kuivinen 已提交
332
		int hit;
J
Junio C Hamano 已提交
333

334
		hit = grep_source(opt, &gs);
335

336 337
		grep_source_clear(&gs);
		return hit;
J
Junio C Hamano 已提交
338
	}
F
Fredrik Kuivinen 已提交
339 340 341 342 343
}

static int grep_file(struct grep_opt *opt, const char *filename)
{
	struct strbuf buf = STRBUF_INIT;
344
	struct grep_source gs;
F
Fredrik Kuivinen 已提交
345

346 347 348 349
	if (opt->relative && opt->prefix_length)
		quote_path_relative(filename, opt->prefix, &buf);
	else
		strbuf_addstr(&buf, filename);
F
Fredrik Kuivinen 已提交
350

351
	grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
352
	strbuf_release(&buf);
353

354
	if (num_threads > 1) {
355 356 357 358 359
		/*
		 * add_work() copies gs and thus assumes ownership of
		 * its fields, so do not call grep_source_clear()
		 */
		add_work(opt, &gs);
F
Fredrik Kuivinen 已提交
360
		return 0;
361
	} else {
F
Fredrik Kuivinen 已提交
362 363
		int hit;

364 365 366
		hit = grep_source(opt, &gs);

		grep_source_clear(&gs);
F
Fredrik Kuivinen 已提交
367 368
		return hit;
	}
J
Junio C Hamano 已提交
369 370
}

371 372 373 374 375 376
static void append_path(struct grep_opt *opt, const void *data, size_t len)
{
	struct string_list *path_list = opt->output_priv;

	if (len == 1 && *(const char *)data == '\0')
		return;
377
	string_list_append(path_list, xstrndup(data, len));
378 379 380 381 382
}

static void run_pager(struct grep_opt *opt, const char *prefix)
{
	struct string_list *path_list = opt->output_priv;
383
	struct child_process child = CHILD_PROCESS_INIT;
384 385 386
	int i, status;

	for (i = 0; i < path_list->nr; i++)
387 388 389
		argv_array_push(&child.args, path_list->items[i].string);
	child.dir = prefix;
	child.use_shell = 1;
390

391
	status = run_command(&child);
392 393 394 395
	if (status)
		exit(status);
}

396 397 398 399 400
static int grep_cache(struct grep_opt *opt, struct repository *repo,
		      const struct pathspec *pathspec, int cached);
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
		     struct tree_desc *tree, struct strbuf *base, int tn_len,
		     int check_attr, struct repository *repo);
401

402 403 404 405
static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
			  const struct pathspec *pathspec,
			  const struct object_id *oid,
			  const char *filename, const char *path)
406
{
407 408
	struct repository submodule;
	int hit;
409

410 411 412 413 414 415 416 417 418 419
	/*
	 * NEEDSWORK: submodules functions need to be protected because they
	 * access the object store via config_from_gitmodules(): the latter
	 * uses get_oid() which, for now, relies on the global the_repository
	 * object.
	 */
	grep_read_lock();

	if (!is_submodule_active(superproject, path)) {
		grep_read_unlock();
420
		return 0;
421
	}
422

423 424
	if (repo_submodule_init(&submodule, superproject, path)) {
		grep_read_unlock();
425
		return 0;
426
	}
427

428
	repo_read_gitmodules(&submodule);
429

430
	/*
431 432 433 434 435 436 437 438
	 * NEEDSWORK: This adds the submodule's object directory to the list of
	 * alternates for the single in-memory object store.  This has some bad
	 * consequences for memory (processed objects will never be freed) and
	 * performance (this increases the number of pack files git has to pay
	 * attention to, to the sum of the number of pack files in all the
	 * repositories processed so far).  This can be removed once the object
	 * store is no longer global and instead is a member of the repository
	 * object.
439
	 */
440
	add_to_alternates_memory(submodule.objects->objectdir);
441
	grep_read_unlock();
442

443 444 445 446 447 448
	if (oid) {
		struct object *object;
		struct tree_desc tree;
		void *data;
		unsigned long size;
		struct strbuf base = STRBUF_INIT;
449

450
		object = parse_object_or_die(oid, oid_to_hex(oid));
451

452
		grep_read_lock();
453
		data = read_object_with_reference(&object->oid, tree_type,
454 455
						  &size, NULL);
		grep_read_unlock();
456

457 458
		if (!data)
			die(_("unable to read tree (%s)"), oid_to_hex(&object->oid));
459

460 461
		strbuf_addstr(&base, filename);
		strbuf_addch(&base, '/');
462

463 464 465 466 467 468 469
		init_tree_desc(&tree, data, size);
		hit = grep_tree(opt, pathspec, &tree, &base, base.len,
				object->type == OBJ_COMMIT, &submodule);
		strbuf_release(&base);
		free(data);
	} else {
		hit = grep_cache(opt, &submodule, pathspec, 1);
470
	}
471

472 473
	repo_clear(&submodule);
	return hit;
474 475
}

476 477
static int grep_cache(struct grep_opt *opt, struct repository *repo,
		      const struct pathspec *pathspec, int cached)
J
Junio C Hamano 已提交
478 479 480
{
	int hit = 0;
	int nr;
481 482
	struct strbuf name = STRBUF_INIT;
	int name_base_len = 0;
483 484 485
	if (repo->submodule_prefix) {
		name_base_len = strlen(repo->submodule_prefix);
		strbuf_addstr(&name, repo->submodule_prefix);
486 487
	}

488
	if (repo_read_index(repo) < 0)
489
		die(_("index file corrupt"));
J
Junio C Hamano 已提交
490

491 492
	for (nr = 0; nr < repo->index->cache_nr; nr++) {
		const struct cache_entry *ce = repo->index->cache[nr];
493 494 495 496
		strbuf_setlen(&name, name_base_len);
		strbuf_addstr(&name, ce->name);

		if (S_ISREG(ce->ce_mode) &&
497
		    match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,
498 499 500 501 502 503 504 505 506 507 508
				   S_ISDIR(ce->ce_mode) ||
				   S_ISGITLINK(ce->ce_mode))) {
			/*
			 * If CE_VALID is on, we assume worktree file and its
			 * cache entry are identical, even if worktree file has
			 * been modified, so use cache version instead
			 */
			if (cached || (ce->ce_flags & CE_VALID) ||
			    ce_skip_worktree(ce)) {
				if (ce_stage(ce) || ce_intent_to_add(ce))
					continue;
509 510
				hit |= grep_oid(opt, &ce->oid, name.buf,
						 0, name.buf);
511
			} else {
512
				hit |= grep_file(opt, name.buf);
513 514
			}
		} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
515
			   submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
516
			hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
517
		} else {
J
Junio C Hamano 已提交
518
			continue;
519
		}
520

521 522 523
		if (ce_stage(ce)) {
			do {
				nr++;
524 525
			} while (nr < repo->index->cache_nr &&
				 !strcmp(ce->name, repo->index->cache[nr]->name));
526 527
			nr--; /* compensate for loop control */
		}
528 529
		if (hit && opt->status_only)
			break;
J
Junio C Hamano 已提交
530
	}
531 532

	strbuf_release(&name);
J
Junio C Hamano 已提交
533 534 535
	return hit;
}

536
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
537
		     struct tree_desc *tree, struct strbuf *base, int tn_len,
538
		     int check_attr, struct repository *repo)
J
Junio C Hamano 已提交
539
{
540 541
	int hit = 0;
	enum interesting match = entry_not_interesting;
542
	struct name_entry entry;
543
	int old_baselen = base->len;
544 545
	struct strbuf name = STRBUF_INIT;
	int name_base_len = 0;
546 547
	if (repo->submodule_prefix) {
		strbuf_addstr(&name, repo->submodule_prefix);
548 549
		name_base_len = name.len;
	}
J
Junio C Hamano 已提交
550

551
	while (tree_entry(tree, &entry)) {
552
		int te_len = tree_entry_len(&entry);
553

554
		if (match != all_entries_interesting) {
555 556 557 558 559
			strbuf_addstr(&name, base->buf + tn_len);
			match = tree_entry_interesting(&entry, &name,
						       0, pathspec);
			strbuf_setlen(&name, name_base_len);

560
			if (match == all_entries_not_interesting)
561
				break;
562
			if (match == entry_not_interesting)
563 564
				continue;
		}
J
Junio C Hamano 已提交
565

566
		strbuf_add(base, entry.path, te_len);
567

568
		if (S_ISREG(entry.mode)) {
569
			hit |= grep_oid(opt, entry.oid, base->buf, tn_len,
570
					 check_attr ? base->buf + tn_len : NULL);
571
		} else if (S_ISDIR(entry.mode)) {
572
			enum object_type type;
J
Junio C Hamano 已提交
573 574
			struct tree_desc sub;
			void *data;
575 576
			unsigned long size;

577
			data = lock_and_read_oid_file(entry.oid, &type, &size);
J
Junio C Hamano 已提交
578
			if (!data)
579
				die(_("unable to read tree (%s)"),
580
				    oid_to_hex(entry.oid));
581 582

			strbuf_addch(base, '/');
583
			init_tree_desc(&sub, data, size);
584
			hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
585
					 check_attr, repo);
J
Junio C Hamano 已提交
586
			free(data);
587
		} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
588 589
			hit |= grep_submodule(opt, repo, pathspec, entry.oid,
					      base->buf, base->buf + tn_len);
J
Junio C Hamano 已提交
590
		}
591

592 593
		strbuf_setlen(base, old_baselen);

594 595
		if (hit && opt->status_only)
			break;
J
Junio C Hamano 已提交
596
	}
597 598

	strbuf_release(&name);
J
Junio C Hamano 已提交
599 600 601
	return hit;
}

602
static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
603
		       struct object *obj, const char *name, const char *path)
J
Junio C Hamano 已提交
604
{
605
	if (obj->type == OBJ_BLOB)
606
		return grep_oid(opt, &obj->oid, name, 0, path);
607
	if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
J
Junio C Hamano 已提交
608 609
		struct tree_desc tree;
		void *data;
610
		unsigned long size;
611 612 613
		struct strbuf base;
		int hit, len;

614
		grep_read_lock();
615
		data = read_object_with_reference(&obj->oid, tree_type,
616
						  &size, NULL);
617
		grep_read_unlock();
618

J
Junio C Hamano 已提交
619
		if (!data)
620
			die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
621 622 623 624 625 626 627

		len = name ? strlen(name) : 0;
		strbuf_init(&base, PATH_MAX + len + 1);
		if (len) {
			strbuf_add(&base, name, len);
			strbuf_addch(&base, ':');
		}
628
		init_tree_desc(&tree, data, size);
629
		hit = grep_tree(opt, pathspec, &tree, &base, base.len,
630
				obj->type == OBJ_COMMIT, the_repository);
631
		strbuf_release(&base);
J
Junio C Hamano 已提交
632 633 634
		free(data);
		return hit;
	}
635
	die(_("unable to grep from object of type %s"), type_name(obj->type));
J
Junio C Hamano 已提交
636 637
}

638
static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
639 640 641 642 643 644 645 646
			const struct object_array *list)
{
	unsigned int i;
	int hit = 0;
	const unsigned int nr = list->nr;

	for (i = 0; i < nr; i++) {
		struct object *real_obj;
647 648
		real_obj = deref_tag(the_repository, list->objects[i].item,
				     NULL, 0);
649 650 651

		/* load the gitmodules file for this rev */
		if (recurse_submodules) {
652
			submodule_free(the_repository);
653
			gitmodules_config_oid(&real_obj->oid);
654
		}
655 656
		if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
				list->objects[i].path)) {
657 658 659 660 661 662 663 664
			hit = 1;
			if (opt->status_only)
				break;
		}
	}
	return hit;
}

665
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
666
			  int exc_std, int use_index)
J
Junio C Hamano 已提交
667 668 669 670 671
{
	struct dir_struct dir;
	int i, hit = 0;

	memset(&dir, 0, sizeof(dir));
672 673
	if (!use_index)
		dir.flags |= DIR_NO_GITLINKS;
674 675
	if (exc_std)
		setup_standard_excludes(&dir);
J
Junio C Hamano 已提交
676

677
	fill_directory(&dir, &the_index, pathspec);
J
Junio C Hamano 已提交
678
	for (i = 0; i < dir.nr; i++) {
679
		if (!dir_path_match(&the_index, dir.entries[i], pathspec, 0, NULL))
680
			continue;
J
Junio C Hamano 已提交
681 682 683 684 685 686 687
		hit |= grep_file(opt, dir.entries[i]->name);
		if (hit && opt->status_only)
			break;
	}
	return hit;
}

688 689
static int context_callback(const struct option *opt, const char *arg,
			    int unset)
R
René Scharfe 已提交
690 691 692 693 694 695 696 697 698 699 700
{
	struct grep_opt *grep_opt = opt->value;
	int value;
	const char *endp;

	if (unset) {
		grep_opt->pre_context = grep_opt->post_context = 0;
		return 0;
	}
	value = strtol(arg, (char **)&endp, 10);
	if (*endp) {
701
		return error(_("switch `%c' expects a numerical value"),
R
René Scharfe 已提交
702 703 704 705 706 707
			     opt->short_name);
	}
	grep_opt->pre_context = grep_opt->post_context = value;
	return 0;
}

708
static int file_callback(const struct option *opt, const char *arg, int unset)
R
René Scharfe 已提交
709 710
{
	struct grep_opt *grep_opt = opt->value;
711
	int from_stdin = !strcmp(arg, "-");
R
René Scharfe 已提交
712 713
	FILE *patterns;
	int lno = 0;
M
Matt Kraai 已提交
714
	struct strbuf sb = STRBUF_INIT;
R
René Scharfe 已提交
715

716
	patterns = from_stdin ? stdin : fopen(arg, "r");
R
René Scharfe 已提交
717
	if (!patterns)
718
		die_errno(_("cannot open '%s'"), arg);
719
	while (strbuf_getline(&sb, patterns) == 0) {
R
René Scharfe 已提交
720 721 722
		/* ignore empty line like grep does */
		if (sb.len == 0)
			continue;
723

724 725
		append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
				GREP_PATTERN);
R
René Scharfe 已提交
726
	}
727 728
	if (!from_stdin)
		fclose(patterns);
R
René Scharfe 已提交
729 730 731 732
	strbuf_release(&sb);
	return 0;
}

733
static int not_callback(const struct option *opt, const char *arg, int unset)
R
René Scharfe 已提交
734 735 736 737 738 739
{
	struct grep_opt *grep_opt = opt->value;
	append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
	return 0;
}

740
static int and_callback(const struct option *opt, const char *arg, int unset)
R
René Scharfe 已提交
741 742 743 744 745 746
{
	struct grep_opt *grep_opt = opt->value;
	append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
	return 0;
}

747
static int open_callback(const struct option *opt, const char *arg, int unset)
R
René Scharfe 已提交
748 749 750 751 752 753
{
	struct grep_opt *grep_opt = opt->value;
	append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
	return 0;
}

754
static int close_callback(const struct option *opt, const char *arg, int unset)
R
René Scharfe 已提交
755 756 757 758 759 760
{
	struct grep_opt *grep_opt = opt->value;
	append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
	return 0;
}

761 762
static int pattern_callback(const struct option *opt, const char *arg,
			    int unset)
R
René Scharfe 已提交
763 764 765 766 767
{
	struct grep_opt *grep_opt = opt->value;
	append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
	return 0;
}
J
Junio C Hamano 已提交
768

769
int cmd_grep(int argc, const char **argv, const char *prefix)
J
Junio C Hamano 已提交
770 771
{
	int hit = 0;
772
	int cached = 0, untracked = 0, opt_exclude = -1;
773
	int seen_dashdash = 0;
774
	int external_grep_allowed__ignored;
775
	const char *show_in_pager = NULL, *default_pager = "dummy";
J
Junio C Hamano 已提交
776
	struct grep_opt opt;
777
	struct object_array list = OBJECT_ARRAY_INIT;
778
	struct pathspec pathspec;
779
	struct string_list path_list = STRING_LIST_INIT_NODUP;
780
	int i;
R
René Scharfe 已提交
781
	int dummy;
782
	int use_index = 1;
783
	int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
784
	int allow_revs;
785

R
René Scharfe 已提交
786
	struct option options[] = {
787
		OPT_BOOL(0, "cached", &cached,
788
			N_("search in index instead of in the work tree")),
789
		OPT_NEGBIT(0, "no-index", &use_index,
790
			 N_("find in contents not managed by git"), 1),
791
		OPT_BOOL(0, "untracked", &untracked,
792
			N_("search in both tracked and untracked files")),
793
		OPT_SET_INT(0, "exclude-standard", &opt_exclude,
794
			    N_("ignore files specified via '.gitignore'"), 1),
795
		OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
B
Brandon Williams 已提交
796
			 N_("recursively search in each submodule")),
R
René Scharfe 已提交
797
		OPT_GROUP(""),
798
		OPT_BOOL('v', "invert-match", &opt.invert,
799
			N_("show non-matching lines")),
800
		OPT_BOOL('i', "ignore-case", &opt.ignore_case,
801
			N_("case insensitive matching")),
802
		OPT_BOOL('w', "word-regexp", &opt.word_regexp,
803
			N_("match patterns only at word boundaries")),
R
René Scharfe 已提交
804
		OPT_SET_INT('a', "text", &opt.binary,
805
			N_("process binary files as text"), GREP_BINARY_TEXT),
R
René Scharfe 已提交
806
		OPT_SET_INT('I', NULL, &opt.binary,
807
			N_("don't match patterns in binary files"),
R
René Scharfe 已提交
808
			GREP_BINARY_NOMATCH),
J
Jeff King 已提交
809 810
		OPT_BOOL(0, "textconv", &opt.allow_textconv,
			 N_("process binary files with textconv filters")),
R
René Scharfe 已提交
811 812
		OPT_SET_INT('r', "recursive", &opt.max_depth,
			    N_("search in subdirectories (default)"), -1),
813 814
		{ OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
			N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
815
			NULL, 1 },
R
René Scharfe 已提交
816
		OPT_GROUP(""),
817
		OPT_SET_INT('E', "extended-regexp", &pattern_type_arg,
818
			    N_("use extended POSIX regular expressions"),
819 820
			    GREP_PATTERN_TYPE_ERE),
		OPT_SET_INT('G', "basic-regexp", &pattern_type_arg,
821
			    N_("use basic POSIX regular expressions (default)"),
822 823
			    GREP_PATTERN_TYPE_BRE),
		OPT_SET_INT('F', "fixed-strings", &pattern_type_arg,
824
			    N_("interpret patterns as fixed strings"),
825 826
			    GREP_PATTERN_TYPE_FIXED),
		OPT_SET_INT('P', "perl-regexp", &pattern_type_arg,
827
			    N_("use Perl-compatible regular expressions"),
828
			    GREP_PATTERN_TYPE_PCRE),
R
René Scharfe 已提交
829
		OPT_GROUP(""),
830
		OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")),
831
		OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")),
832 833
		OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
		OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
R
René Scharfe 已提交
834
		OPT_NEGBIT(0, "full-name", &opt.relative,
835
			N_("show filenames relative to top directory"), 1),
836
		OPT_BOOL('l', "files-with-matches", &opt.name_only,
837
			N_("show only filenames instead of matching lines")),
838
		OPT_BOOL(0, "name-only", &opt.name_only,
839
			N_("synonym for --files-with-matches")),
840
		OPT_BOOL('L', "files-without-match",
R
René Scharfe 已提交
841
			&opt.unmatch_name_only,
842
			N_("show only the names of files without match")),
843 844 845
		OPT_BOOL_F('z', "null", &opt.null_following_name,
			   N_("print NUL after filenames"),
			   PARSE_OPT_NOCOMPLETE),
846 847
		OPT_BOOL('o', "only-matching", &opt.only_matching,
			N_("show only matching parts of a line")),
848
		OPT_BOOL('c', "count", &opt.count,
849 850
			N_("show the number of matches instead of matching lines")),
		OPT__COLOR(&opt.color, N_("highlight matches")),
851
		OPT_BOOL(0, "break", &opt.file_break,
852
			N_("print empty line between matches from different files")),
853
		OPT_BOOL(0, "heading", &opt.heading,
854
			N_("show filename only once above matches from same file")),
R
René Scharfe 已提交
855
		OPT_GROUP(""),
856 857
		OPT_CALLBACK('C', "context", &opt, N_("n"),
			N_("show <n> context lines before and after matches"),
R
René Scharfe 已提交
858
			context_callback),
R
René Scharfe 已提交
859
		OPT_INTEGER('B', "before-context", &opt.pre_context,
860
			N_("show <n> context lines before matches")),
R
René Scharfe 已提交
861
		OPT_INTEGER('A', "after-context", &opt.post_context,
862
			N_("show <n> context lines after matches")),
863 864
		OPT_INTEGER(0, "threads", &num_threads,
			N_("use <n> worker threads")),
865
		OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"),
R
René Scharfe 已提交
866
			context_callback),
867
		OPT_BOOL('p', "show-function", &opt.funcname,
868
			N_("show a line with the function name before matches")),
869
		OPT_BOOL('W', "function-context", &opt.funcbody,
870
			N_("show the surrounding function")),
R
René Scharfe 已提交
871
		OPT_GROUP(""),
872 873 874 875
		OPT_CALLBACK('f', NULL, &opt, N_("file"),
			N_("read patterns from file"), file_callback),
		{ OPTION_CALLBACK, 'e', NULL, &opt, N_("pattern"),
			N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback },
R
René Scharfe 已提交
876
		{ OPTION_CALLBACK, 0, "and", &opt, NULL,
877
		  N_("combine patterns specified with -e"),
R
René Scharfe 已提交
878
		  PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
879
		OPT_BOOL(0, "or", &dummy, ""),
R
René Scharfe 已提交
880 881 882 883 884 885 886 887
		{ OPTION_CALLBACK, 0, "not", &opt, NULL, "",
		  PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },
		{ OPTION_CALLBACK, '(', NULL, &opt, NULL, "",
		  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
		  open_callback },
		{ OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
		  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
		  close_callback },
888
		OPT__QUIET(&opt.status_only,
889
			   N_("indicate hit with exit status without output")),
890
		OPT_BOOL(0, "all-match", &opt.all_match,
891
			N_("show only matches from files that match all patterns")),
892 893 894
		OPT_SET_INT_F(0, "debug", &opt.debug,
			      N_("show parse tree for grep expression"),
			      1, PARSE_OPT_HIDDEN),
R
René Scharfe 已提交
895
		OPT_GROUP(""),
896
		{ OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
897
			N_("pager"), N_("show matching files in the pager"),
898 899 900 901 902
			PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE,
			NULL, (intptr_t)default_pager },
		OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored,
			   N_("allow calling of grep(1) (ignored by this build)"),
			   PARSE_OPT_NOCOMPLETE),
R
René Scharfe 已提交
903 904
		OPT_END()
	};
J
Junio C Hamano 已提交
905

906
	init_grep_defaults(the_repository);
907
	git_config(grep_cmd_config, NULL);
908
	grep_init(&opt, the_repository, prefix);
R
René Scharfe 已提交
909

J
Junio C Hamano 已提交
910
	/*
911 912 913 914 915 916
	 * If there is no -- then the paths must exist in the working
	 * tree.  If there is no explicit pattern specified with -e or
	 * -f, we take the first unrecognized non option to be the
	 * pattern, but then what follows it must be zero or more
	 * valid refs up to the -- (if exists), and then existing
	 * paths.  If there is an explicit pattern, then the first
917
	 * unrecognized non option is the beginning of the refs list
918
	 * that continues up to the -- (if exists), and then paths.
J
Junio C Hamano 已提交
919
	 */
920
	argc = parse_options(argc, argv, prefix, options, grep_usage,
R
René Scharfe 已提交
921
			     PARSE_OPT_KEEP_DASHDASH |
922
			     PARSE_OPT_STOP_AT_NON_OPTION);
923
	grep_commit_pattern_type(pattern_type_arg, &opt);
R
René Scharfe 已提交
924

925 926 927 928 929 930 931 932 933
	if (use_index && !startup_info->have_repository) {
		int fallback = 0;
		git_config_get_bool("grep.fallbacktonoindex", &fallback);
		if (fallback)
			use_index = 0;
		else
			/* die the same way as if we did it at the beginning */
			setup_git_directory();
	}
J
Junio C Hamano 已提交
934

J
Jeff King 已提交
935 936 937 938 939 940 941 942 943 944
	/*
	 * skip a -- separator; we know it cannot be
	 * separating revisions from pathnames if
	 * we haven't even had any patterns yet
	 */
	if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
		argv++;
		argc--;
	}

R
René Scharfe 已提交
945 946 947 948 949 950
	/* First unrecognized non-option token */
	if (argc > 0 && !opt.pattern_list) {
		append_grep_pattern(&opt, argv[0], "command line", 0,
				    GREP_PATTERN);
		argv++;
		argc--;
J
Junio C Hamano 已提交
951
	}
952

953 954
	if (show_in_pager == default_pager)
		show_in_pager = git_pager(1);
955
	if (show_in_pager) {
956
		opt.color = 0;
957 958 959 960
		opt.name_only = 1;
		opt.null_following_name = 1;
		opt.output_priv = &path_list;
		opt.output = append_path;
961
		string_list_append(&path_list, show_in_pager);
962 963
	}

964
	if (!opt.pattern_list)
965
		die(_("no pattern given"));
F
Fredrik Kuivinen 已提交
966

967 968 969 970
	/* --only-matching has no effect with --invert. */
	if (opt.invert)
		opt.only_matching = 0;

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	/*
	 * We have to find "--" in a separate pass, because its presence
	 * influences how we will parse arguments that come before it.
	 */
	for (i = 0; i < argc; i++) {
		if (!strcmp(argv[i], "--")) {
			seen_dashdash = 1;
			break;
		}
	}

	/*
	 * Resolve any rev arguments. If we have a dashdash, then everything up
	 * to it must resolve as a rev. If not, then we stop at the first
	 * non-rev and assume everything else is a path.
	 */
987
	allow_revs = use_index && !untracked;
R
René Scharfe 已提交
988
	for (i = 0; i < argc; i++) {
989
		const char *arg = argv[i];
990
		struct object_id oid;
991
		struct object_context oc;
J
Jeff King 已提交
992 993
		struct object *object;

994 995 996 997
		if (!strcmp(arg, "--")) {
			i++;
			break;
		}
J
Jeff King 已提交
998

999
		if (!allow_revs) {
1000
			if (seen_dashdash)
1001
				die(_("--no-index or --untracked cannot be used with revs"));
1002 1003 1004
			break;
		}

1005
		if (get_oid_with_context(arg, GET_OID_RECORD_PATH,
1006
					 &oid, &oc)) {
1007 1008
			if (seen_dashdash)
				die(_("unable to resolve revision: %s"), arg);
J
Jeff King 已提交
1009
			break;
1010
		}
J
Jeff King 已提交
1011

1012
		object = parse_object_or_die(&oid, arg);
J
Jeff King 已提交
1013 1014 1015
		if (!seen_dashdash)
			verify_non_filename(prefix, arg);
		add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
1016
		free(oc.path);
1017
	}
1018

1019 1020 1021 1022
	/*
	 * Anything left over is presumed to be a path. But in the non-dashdash
	 * "do what I mean" case, we verify and complain when that isn't true.
	 */
1023 1024 1025
	if (!seen_dashdash) {
		int j;
		for (j = i; j < argc; j++)
1026
			verify_filename(prefix, argv[j], j == i && allow_revs);
1027 1028 1029 1030 1031 1032 1033 1034
	}

	parse_pathspec(&pathspec, 0,
		       PATHSPEC_PREFER_CWD |
		       (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0),
		       prefix, argv + i);
	pathspec.max_depth = opt.max_depth;
	pathspec.recursive = 1;
1035
	pathspec.recurse_submodules = !!recurse_submodules;
1036

1037 1038 1039 1040 1041
	if (list.nr || cached || show_in_pager) {
		if (num_threads > 1)
			warning(_("invalid option combination, ignoring --threads"));
		num_threads = 1;
	} else if (!HAVE_THREADS && num_threads > 1) {
1042
		warning(_("no threads support, ignoring --threads"));
1043 1044 1045 1046 1047
		num_threads = 1;
	} else if (num_threads < 0)
		die(_("invalid number of threads specified (%d)"), num_threads);
	else if (num_threads == 0)
		num_threads = HAVE_THREADS ? GREP_NUM_THREADS_DEFAULT : 1;
1048

1049 1050 1051 1052 1053 1054 1055 1056
	if (num_threads > 1) {
		if (!HAVE_THREADS)
			BUG("Somebody got num_threads calculation wrong!");
		if (!(opt.name_only || opt.unmatch_name_only || opt.count)
		    && (opt.pre_context || opt.post_context ||
			opt.file_break || opt.funcbody))
			skip_first_line = 1;
		start_threads(&opt);
1057
	} else {
1058 1059 1060
		/*
		 * The compiled patterns on the main path are only
		 * used when not using threading. Otherwise
1061
		 * start_threads() above calls compile_grep_patterns()
1062 1063 1064
		 * for each thread.
		 */
		compile_grep_patterns(&opt);
1065 1066
	}

1067
	if (show_in_pager && (cached || list.nr))
1068
		die(_("--open-files-in-pager only works on the worktree"));
1069 1070 1071 1072 1073 1074 1075 1076

	if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
		const char *pager = path_list.items[0].string;
		int len = strlen(pager);

		if (len > 4 && is_dir_sep(pager[len - 5]))
			pager += len - 4;

1077 1078 1079
		if (opt.ignore_case && !strcmp("less", pager))
			string_list_append(&path_list, "-I");

1080 1081 1082 1083 1084
		if (!strcmp("less", pager) || !strcmp("vi", pager)) {
			struct strbuf buf = STRBUF_INIT;
			strbuf_addf(&buf, "+/%s%s",
					strcmp("less", pager) ? "" : "*",
					opt.pattern_list->pattern);
1085
			string_list_append(&path_list, buf.buf);
1086 1087 1088 1089
			strbuf_detach(&buf, NULL);
		}
	}

1090
	if (recurse_submodules && (!use_index || untracked))
1091
		die(_("option not supported with --recurse-submodules"));
1092

1093
	if (!show_in_pager && !opt.status_only)
1094 1095
		setup_pager();

1096
	if (!use_index && (untracked || cached))
1097
		die(_("--cached or --untracked cannot be used with --no-index"));
1098

1099 1100
	if (!use_index || untracked) {
		int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
1101
		hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
1102
	} else if (0 <= opt_exclude) {
1103
		die(_("--[no-]exclude-standard cannot be used for tracked contents"));
1104
	} else if (!list.nr) {
1105 1106
		if (!cached)
			setup_work_tree();
F
Fredrik Kuivinen 已提交
1107

1108
		hit = grep_cache(&opt, the_repository, &pathspec, cached);
1109 1110
	} else {
		if (cached)
1111
			die(_("both --cached and trees are given"));
1112

1113
		hit = grep_objects(&opt, &pathspec, &list);
J
Junio C Hamano 已提交
1114
	}
F
Fredrik Kuivinen 已提交
1115

1116
	if (num_threads > 1)
F
Fredrik Kuivinen 已提交
1117
		hit |= wait_all();
1118 1119
	if (hit && show_in_pager)
		run_pager(&opt, prefix);
1120
	clear_pathspec(&pathspec);
1121
	free_grep_patterns(&opt);
J
Junio C Hamano 已提交
1122 1123
	return !hit;
}