@matheusbsbs / matheus-bagliano / commits / 5acd64edec
Fork 0

builtin-grep: tighten argument parsing.

I mistyped git grep next -e '"^@"' '*.c' and got many hits that contain "next" without complaint. Obviously what I meant to say was: git grep -e '"^@"' next -- '*.c' This tightens the argument parsing rule a bit: - All "grep" parameters should come first; - If there is no -e nor -f to specify pattern, the first non option string is the parameter; - After that, zero or more revs can follow. - An optional '--' can be present, and is skipped. - All the rest are pathspecs. If '--' was not there, they must be paths that exist in the working tree. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 8, 2006 at 23:55 UTC 5acd64edec37a7d9783af1a2be99772d466e8f02
1 file changed +50 -26
builtin-grep.c
+50 -26
@@ -494,20 +494,30 @@ static const char builtin_grep_usage[] =
494 int cmd_grep(int argc, const char **argv, char **envp)
495 {
496 int hit = 0;
497 - int no_more_flags = 0;
497 int cached = 0;
498 + int seen_dashdash = 0;
499 struct grep_opt opt;
500 struct object_list *list, **tail, *object_list = NULL;
501 const char *prefix = setup_git_directory();
502 const char **paths = NULL;
503 + int i;
504
505 memset(&opt, 0, sizeof(opt));
506 opt.pattern_tail = &opt.pattern_list;
507 opt.regflags = REG_NEWLINE;
508
509 /*
509 - * No point using rev_info, really.
510 + * If there is no -- then the paths must exist in the working
511 + * tree. If there is no explicit pattern specified with -e or
512 + * -f, we take the first unrecognized non option to be the
513 + * pattern, but then what follows it must be zero or more
514 + * valid refs up to the -- (if exists), and then existing
515 + * paths. If there is an explicit pattern, then the first
516 + * unrecocnized non option is the beginning of the refs list
517 + * that continues up to the -- (if exists), and then paths.
518 */
519 +
520 + tail = &object_list;
521 while (1 < argc) {
522 const char *arg = argv[1];
523 argc--; argv++;
@@ -618,7 +628,7 @@ int cmd_grep(int argc, const char **argv, char **envp)
628 usage(builtin_grep_usage);
629 patterns = fopen(argv[1], "r");
630 if (!patterns)
621 - die("'%s': %s", strerror(errno));
631 + die("'%s': %s", argv[1], strerror(errno));
632 while (fgets(buf, sizeof(buf), patterns)) {
633 int len = strlen(buf);
634 if (buf[len-1] == '\n')
@@ -642,47 +652,61 @@ int cmd_grep(int argc, const char **argv, char **envp)
652 }
653 usage(builtin_grep_usage);
654 }
645 - if (!strcmp("--", arg)) {
646 - no_more_flags = 1;
647 - continue;
648 - }
649 - /* Either unrecognized option or a single pattern */
650 - if (!no_more_flags && *arg == '-')
655 + if (!strcmp("--", arg))
656 + break;
657 + if (*arg == '-')
658 usage(builtin_grep_usage);
659 +
660 + /* First unrecognized non-option token */
661 if (!opt.pattern_list) {
662 add_pattern(&opt, arg, "command line", 0);
663 break;
664 }
665 else {
666 /* We are looking at the first path or rev;
658 - * it is found at argv[0] after leaving the
667 + * it is found at argv[1] after leaving the
668 * loop.
669 */
670 argc++; argv--;
671 break;
672 }
673 }
674 +
675 if (!opt.pattern_list)
676 die("no pattern given.");
677 compile_patterns(&opt);
668 - tail = &object_list;
669 - while (1 < argc) {
670 - struct object *object;
671 - struct object_list *elem;
672 - const char *arg = argv[1];
678 +
679 + /* Check revs and then paths */
680 + for (i = 1; i < argc; i++) {
681 + const char *arg = argv[i];
682 unsigned char sha1[20];
674 - if (get_sha1(arg, sha1) < 0)
675 - break;
676 - object = parse_object(sha1);
677 - if (!object)
678 - die("bad object %s", arg);
679 - elem = object_list_insert(object, tail);
680 - elem->name = arg;
681 - tail = &elem->next;
682 - argc--; argv++;
683 + /* Is it a rev? */
684 + if (!get_sha1(arg, sha1)) {
685 + struct object *object = parse_object(sha1);
686 + struct object_list *elem;
687 + if (!object)
688 + die("bad object %s", arg);
689 + elem = object_list_insert(object, tail);
690 + elem->name = arg;
691 + tail = &elem->next;
692 + continue;
693 + }
694 + if (!strcmp(arg, "--")) {
695 + i++;
696 + seen_dashdash = 1;
697 + }
698 + break;
699 }
684 - if (1 < argc)
685 - paths = get_pathspec(prefix, argv + 1);
700 +
701 + /* The rest are paths */
702 + if (!seen_dashdash) {
703 + int j;
704 + for (j = i; j < argc; i++)
705 + verify_filename(prefix, argv[j]);
706 + }
707 +
708 + if (i < argc)
709 + paths = get_pathspec(prefix, argv + i);
710 else if (prefix) {
711 paths = xcalloc(2, sizeof(const char *));
712 paths[0] = prefix;