builtin-grep: do not use setup_revisions()
Grep may want to grok multiple revisions, but it does not make much sense to walk revisions while doing so. This stops calling the code to parse parameters for the revision walker. The parameter parsing for the optional "-e" option becomes a lot simpler with it as well. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 1, 2006 at 15:58 UTC
1362671f6a716e1a34abbcab43adfb68c7e2fd3b
1 file changed
+134
-121
builtin-grep.c
+134
-121
@@ -8,8 +8,7 @@
8
#include "tree.h"
9
#include "commit.h"
10
#include "tag.h"
11
-#include "diff.h"
12
-#include "revision.h"
11
+#include "tree-walk.h"
12
#include "builtin.h"
13
#include <regex.h>
14
#include <fnmatch.h>
@@ -18,15 +17,15 @@
17
* git grep pathspecs are somewhat different from diff-tree pathspecs;
18
* pathname wildcards are allowed.
19
*/
21
-static int pathspec_matches(struct diff_options *opt, const char *name)
20
+static int pathspec_matches(const char **paths, const char *name)
21
{
22
int namelen, i;
24
- if (!opt->nr_paths)
23
+ if (!paths || !*paths)
24
return 1;
25
namelen = strlen(name);
27
- for (i = 0; i < opt->nr_paths; i++) {
28
- const char *match = opt->paths[i];
29
- int matchlen = opt->pathlens[i];
26
+ for (i = 0; paths[i]; i++) {
27
+ const char *match = paths[i];
28
+ int matchlen = strlen(match);
29
const char *slash, *cp;
30
31
if ((matchlen <= namelen) &&
@@ -241,7 +240,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
240
return i;
241
}
242
244
-static int grep_cache(struct grep_opt *opt, struct rev_info *revs, int cached)
243
+static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
244
{
245
int hit = 0;
246
int nr;
@@ -251,7 +250,7 @@ static int grep_cache(struct grep_opt *opt, struct rev_info *revs, int cached)
250
struct cache_entry *ce = active_cache[nr];
251
if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
252
continue;
254
- if (!pathspec_matches(&revs->diffopt, ce->name))
253
+ if (!pathspec_matches(paths, ce->name))
254
continue;
255
if (cached)
256
hit |= grep_sha1(opt, ce->sha1, ce->name);
@@ -261,7 +260,7 @@ static int grep_cache(struct grep_opt *opt, struct rev_info *revs, int cached)
260
return hit;
261
}
262
264
-static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
263
+static int grep_tree(struct grep_opt *opt, const char **paths,
264
struct tree_desc *tree,
265
const char *tree_name, const char *base)
266
{
@@ -297,7 +296,7 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
296
*/
297
strcpy(path_buf + len + pathlen, "/");
298
300
- if (!pathspec_matches(&revs->diffopt, down))
299
+ if (!pathspec_matches(paths, down))
300
;
301
else if (S_ISREG(mode))
302
hit |= grep_sha1(opt, sha1, path_buf);
@@ -310,7 +309,7 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
309
die("unable to read tree (%s)",
310
sha1_to_hex(sha1));
311
sub.buf = data;
313
- hit |= grep_tree(opt, revs, &sub, tree_name, down);
312
+ hit |= grep_tree(opt, paths, &sub, tree_name, down);
313
free(data);
314
}
315
update_tree_entry(tree);
@@ -318,7 +317,7 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
317
return hit;
318
}
319
321
-static int grep_object(struct grep_opt *opt, struct rev_info *revs,
320
+static int grep_object(struct grep_opt *opt, const char **paths,
321
struct object *obj, const char *name)
322
{
323
if (!strcmp(obj->type, blob_type))
@@ -333,7 +332,7 @@ static int grep_object(struct grep_opt *opt, struct rev_info *revs,
332
if (!data)
333
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
334
tree.buf = data;
336
- hit = grep_tree(opt, revs, &tree, name, "");
335
+ hit = grep_tree(opt, paths, &tree, name, "");
336
free(data);
337
return hit;
338
}
@@ -345,102 +344,119 @@ static const char builtin_grep_usage[] =
344
345
int cmd_grep(int argc, const char **argv, char **envp)
346
{
348
- struct rev_info rev;
349
- const char **dst, **src;
347
int err;
348
int hit = 0;
352
- int no_more_arg = 0;
353
- int seen_range = 0;
349
+ int no_more_flags = 0;
350
int seen_noncommit = 0;
351
int cached = 0;
352
struct grep_opt opt;
357
- struct object_list *list;
353
+ struct object_list *list, **tail, *object_list = NULL;
354
+ const char *prefix = setup_git_directory();
355
+ const char **paths = NULL;
356
357
memset(&opt, 0, sizeof(opt));
358
opt.regflags = REG_NEWLINE;
359
360
/*
363
- * Interpret and remove the grep options upfront. Sigh...
361
+ * No point using rev_info, really.
362
*/
365
- for (dst = src = &argv[1]; src < argc + argv; ) {
366
- const char *arg = *src++;
367
- if (!no_more_arg) {
368
- if (!strcmp("--", arg)) {
369
- no_more_arg = 1;
370
- *dst++ = arg;
371
- continue;
372
- }
373
- if (!strcmp("--cached", arg)) {
374
- cached = 1;
375
- continue;
376
- }
377
- if (!strcmp("-i", arg) ||
378
- !strcmp("--ignore-case", arg)) {
379
- opt.regflags |= REG_ICASE;
380
- continue;
381
- }
382
- if (!strcmp("-v", arg) ||
383
- !strcmp("--invert-match", arg)) {
384
- opt.invert = 1;
385
- continue;
386
- }
387
- if (!strcmp("-E", arg) ||
388
- !strcmp("--extended-regexp", arg)) {
389
- opt.regflags |= REG_EXTENDED;
390
- continue;
391
- }
392
- if (!strcmp("-G", arg) ||
393
- !strcmp("--basic-regexp", arg)) {
394
- opt.regflags &= ~REG_EXTENDED;
395
- continue;
396
- }
397
- if (!strcmp("-e", arg)) {
398
- if (src < argc + argv) {
399
- opt.pattern = *src++;
400
- continue;
401
- }
363
+ while (1 < argc) {
364
+ const char *arg = argv[1];
365
+ argc--; argv++;
366
+ if (!strcmp("--cached", arg)) {
367
+ cached = 1;
368
+ continue;
369
+ }
370
+ if (!strcmp("-i", arg) ||
371
+ !strcmp("--ignore-case", arg)) {
372
+ opt.regflags |= REG_ICASE;
373
+ continue;
374
+ }
375
+ if (!strcmp("-v", arg) ||
376
+ !strcmp("--invert-match", arg)) {
377
+ opt.invert = 1;
378
+ continue;
379
+ }
380
+ if (!strcmp("-E", arg) ||
381
+ !strcmp("--extended-regexp", arg)) {
382
+ opt.regflags |= REG_EXTENDED;
383
+ continue;
384
+ }
385
+ if (!strcmp("-G", arg) ||
386
+ !strcmp("--basic-regexp", arg)) {
387
+ opt.regflags &= ~REG_EXTENDED;
388
+ continue;
389
+ }
390
+ if (!strcmp("-n", arg)) {
391
+ opt.linenum = 1;
392
+ continue;
393
+ }
394
+ if (!strcmp("-H", arg)) {
395
+ /* We always show the pathname, so this
396
+ * is a noop.
397
+ */
398
+ continue;
399
+ }
400
+ if (!strcmp("-l", arg) ||
401
+ !strcmp("--files-with-matches", arg)) {
402
+ opt.name_only = 1;
403
+ continue;
404
+ }
405
+ if (!strcmp("-A", arg) ||
406
+ !strcmp("-B", arg) ||
407
+ !strcmp("-C", arg)) {
408
+ unsigned num;
409
+ if (argc <= 1 ||
410
+ sscanf(*++argv, "%u", &num) != 1)
411
usage(builtin_grep_usage);
412
+ argc--;
413
+ switch (arg[1]) {
414
+ case 'A':
415
+ opt.post_context = num;
416
+ break;
417
+ case 'C':
418
+ opt.post_context = num;
419
+ case 'B':
420
+ opt.pre_context = num;
421
+ break;
422
}
404
- if (!strcmp("-n", arg)) {
405
- opt.linenum = 1;
406
- continue;
407
- }
408
- if (!strcmp("-H", arg)) {
409
- /* We always show the pathname, so this
410
- * is a noop.
423
+ continue;
424
+ }
425
+ if (!strcmp("-e", arg)) {
426
+ if (1 < argc) {
427
+ /* We probably would want to do
428
+ * -e pat1 -e pat2 as well later...
429
*/
430
+ if (opt.pattern)
431
+ die("more than one pattern?");
432
+ opt.pattern = *++argv;
433
+ argc--;
434
continue;
435
}
414
- if (!strcmp("-l", arg) ||
415
- !strcmp("--files-with-matches", arg)) {
416
- opt.name_only = 1;
417
- continue;
418
- }
419
- if (!strcmp("-A", arg) ||
420
- !strcmp("-B", arg) ||
421
- !strcmp("-C", arg)) {
422
- unsigned num;
423
- if ((argc + argv <= src) ||
424
- sscanf(*src++, "%u", &num) != 1)
425
- usage(builtin_grep_usage);
426
- switch (arg[1]) {
427
- case 'A':
428
- opt.post_context = num;
429
- break;
430
- case 'C':
431
- opt.post_context = num;
432
- case 'B':
433
- opt.pre_context = num;
434
- break;
435
- }
436
- continue;
437
- }
436
+ usage(builtin_grep_usage);
437
+ }
438
+ if (!strcmp("--", arg)) {
439
+ no_more_flags = 1;
440
+ continue;
441
+ }
442
+ /* Either unrecognized option or a single pattern */
443
+ if (!no_more_flags && *arg == '-')
444
+ usage(builtin_grep_usage);
445
+ if (!opt.pattern) {
446
+ opt.pattern = arg;
447
+ break;
448
+ }
449
+ else {
450
+ /* We are looking at the first path or rev;
451
+ * it is found at argv[0] after leaving the
452
+ * loop.
453
+ */
454
+ argc++; argv--;
455
+ break;
456
}
439
- *dst++ = arg;
457
}
458
if (!opt.pattern)
459
die("no pattern given.");
443
-
460
err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
461
if (err) {
462
char errbuf[1024];
@@ -448,11 +464,32 @@ int cmd_grep(int argc, const char **argv, char **envp)
464
regfree(&opt.regexp);
465
die("'%s': %s", opt.pattern, errbuf);
466
}
467
+ tail = &object_list;
468
+ while (1 < argc) {
469
+ struct object *object;
470
+ struct object_list *elem;
471
+ const char *arg = argv[1];
472
+ unsigned char sha1[20];
473
+ if (get_sha1(arg, sha1) < 0)
474
+ break;
475
+ object = parse_object(sha1);
476
+ if (!object)
477
+ die("bad object %s", arg);
478
+ elem = object_list_insert(object, tail);
479
+ elem->name = arg;
480
+ tail = &elem->next;
481
+ argc--; argv++;
482
+ }
483
+ if (1 < argc)
484
+ paths = get_pathspec(prefix, argv + 1);
485
+ else if (prefix) {
486
+ paths = xcalloc(2, sizeof(const char *));
487
+ paths[0] = prefix;
488
+ paths[1] = NULL;
489
+ }
490
452
- init_revisions(&rev);
453
- *dst = NULL;
454
- argc = setup_revisions(dst - argv, argv, &rev, NULL);
455
-
491
+ if (!object_list)
492
+ return !grep_cache(&opt, paths, cached);
493
/*
494
* Do not walk "grep -e foo master next pu -- Documentation/"
495
* but do walk "grep -e foo master..next -- Documentation/".
@@ -460,43 +497,19 @@ int cmd_grep(int argc, const char **argv, char **envp)
497
* "grep -e foo v1.0.0:Documentation/ master..next"
498
* so detect that and complain.
499
*/
463
- for (list = rev.pending_objects; list; list = list->next) {
500
+ for (list = object_list; list; list = list->next) {
501
struct object *real_obj;
465
- if (list->item->flags & UNINTERESTING)
466
- seen_range = 1;
502
real_obj = deref_tag(list->item, NULL, 0);
503
if (strcmp(real_obj->type, commit_type))
504
seen_noncommit = 1;
505
}
471
- if (!rev.pending_objects)
472
- return !grep_cache(&opt, &rev, cached);
506
if (cached)
507
die("both --cached and revisions given.");
508
476
- if (seen_range && seen_noncommit)
477
- die("both A..B and non commit are given.");
478
- if (seen_range) {
479
- struct commit *commit;
480
- prepare_revision_walk(&rev);
481
- while ((commit = get_revision(&rev)) != NULL) {
482
- unsigned char *sha1 = commit->object.sha1;
483
- const char *n = find_unique_abbrev(sha1, rev.abbrev);
484
- char rev_name[41];
485
- strcpy(rev_name, n);
486
- if (grep_object(&opt, &rev, &commit->object, rev_name))
487
- hit = 1;
488
- commit->buffer = NULL;
489
- }
490
- return !hit;
491
- }
492
-
493
- /* all of them are non-commit; do not walk, and
494
- * do not lose their names.
495
- */
496
- for (list = rev.pending_objects; list; list = list->next) {
509
+ for (list = object_list; list; list = list->next) {
510
struct object *real_obj;
511
real_obj = deref_tag(list->item, NULL, 0);
499
- if (grep_object(&opt, &rev, real_obj, list->name))
512
+ if (grep_object(&opt, paths, real_obj, list->name))
513
hit = 1;
514
}
515
return !hit;