Merge branch 'cb/maint-1.6.3-grep-relative-up' into maint
authorJunio C Hamano <gitster@pobox.com>
Sun, 13 Sep 2009 08:24:20 +0000 (01:24 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 13 Sep 2009 08:24:20 +0000 (01:24 -0700)
* cb/maint-1.6.3-grep-relative-up:
grep: accept relative paths outside current working directory
grep: fix exit status if external_grep() punts

Conflicts:
t/t7002-grep.sh

1  2 
builtin-grep.c
grep.h
t/t7002-grep.sh
diff --combined builtin-grep.c
index f477659100fdc63bff5938b4c96f28eaefc07460,eda2c884870abde5f9d943259916de45d9425a6c..fd450bc16e56a634b14cfa33f77e5192412643d8
  #include "tag.h"
  #include "tree-walk.h"
  #include "builtin.h"
 +#include "parse-options.h"
 +#include "userdiff.h"
  #include "grep.h"
+ #include "quote.h"
  
  #ifndef NO_EXTERNAL_GREP
  #ifdef __unix__
  #endif
  #endif
  
 -static int builtin_grep;
 +static char const * const grep_usage[] = {
 +      "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
 +      NULL
 +};
  
  static int grep_config(const char *var, const char *value, void *cb)
  {
        struct grep_opt *opt = cb;
  
 +      switch (userdiff_config(var, value)) {
 +      case 0: break;
 +      case -1: return -1;
 +      default: return 0;
 +      }
 +
        if (!strcmp(var, "color.grep")) {
                opt->color = git_config_colorbool(var, value, -1);
                return 0;
@@@ -125,8 -115,8 +126,8 @@@ static int grep_sha1(struct grep_opt *o
        unsigned long size;
        char *data;
        enum object_type type;
-       char *to_free = NULL;
        int hit;
+       struct strbuf pathbuf = STRBUF_INIT;
  
        data = read_sha1_file(sha1, &type, &size);
        if (!data) {
                return 0;
        }
        if (opt->relative && opt->prefix_length) {
-               static char name_buf[PATH_MAX];
-               char *cp;
-               int name_len = strlen(name) - opt->prefix_length + 1;
-               if (!tree_name_len)
-                       name += opt->prefix_length;
-               else {
-                       if (ARRAY_SIZE(name_buf) <= name_len)
-                               cp = to_free = xmalloc(name_len);
-                       else
-                               cp = name_buf;
-                       memcpy(cp, name, tree_name_len);
-                       strcpy(cp + tree_name_len,
-                              name + tree_name_len + opt->prefix_length);
-                       name = cp;
-               }
+               quote_path_relative(name + tree_name_len, -1, &pathbuf, opt->prefix);
+               strbuf_insert(&pathbuf, 0, name, tree_name_len);
+               name = pathbuf.buf;
        }
        hit = grep_buffer(opt, name, data, size);
+       strbuf_release(&pathbuf);
        free(data);
-       free(to_free);
        return hit;
  }
  
@@@ -163,6 -140,7 +151,7 @@@ static int grep_file(struct grep_opt *o
        int i;
        char *data;
        size_t sz;
+       struct strbuf buf = STRBUF_INIT;
  
        if (lstat(filename, &st) < 0) {
        err_ret:
        }
        close(i);
        if (opt->relative && opt->prefix_length)
-               filename += opt->prefix_length;
+               filename = quote_path_relative(filename, -1, &buf, opt->prefix);
        i = grep_buffer(opt, filename, data, sz);
+       strbuf_release(&buf);
        free(data);
        return i;
  }
@@@ -285,17 -264,6 +275,17 @@@ static int flush_grep(struct grep_opt *
                argc -= 2;
        }
  
 +      if (opt->pre_context || opt->post_context) {
 +              /*
 +               * grep handles hunk marks between files, but we need to
 +               * do that ourselves between multiple calls.
 +               */
 +              if (opt->show_hunk_mark)
 +                      write_or_die(1, "--\n", 3);
 +              else
 +                      opt->show_hunk_mark = 1;
 +      }
 +
        status = exec_grep(argc, argv);
  
        if (kept_0) {
@@@ -454,8 -422,7 +444,8 @@@ static int external_grep(struct grep_op
  }
  #endif
  
 -static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 +static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
 +                    int external_grep_allowed)
  {
        int hit = 0;
        int nr;
         * we grep through the checked-out files. It tends to
         * be a lot more optimized
         */
 -      if (!cached && !builtin_grep) {
 +      if (!cached && external_grep_allowed) {
                hit = external_grep(opt, paths, cached);
                if (hit >= 0)
                        return hit;
+               hit = 0;
        }
  #endif
  
@@@ -583,186 -551,28 +574,187 @@@ static int grep_object(struct grep_opt 
        die("unable to grep from object of type %s", typename(obj->type));
  }
  
 -static const char builtin_grep_usage[] =
 -"git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]";
 +static int context_callback(const struct option *opt, const char *arg,
 +                          int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      int value;
 +      const char *endp;
  
 -static const char emsg_invalid_context_len[] =
 -"%s: invalid context length argument";
 -static const char emsg_missing_context_len[] =
 -"missing context length argument";
 -static const char emsg_missing_argument[] =
 -"option requires an argument -%s";
 +      if (unset) {
 +              grep_opt->pre_context = grep_opt->post_context = 0;
 +              return 0;
 +      }
 +      value = strtol(arg, (char **)&endp, 10);
 +      if (*endp) {
 +              return error("switch `%c' expects a numerical value",
 +                           opt->short_name);
 +      }
 +      grep_opt->pre_context = grep_opt->post_context = value;
 +      return 0;
 +}
 +
 +static int file_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      FILE *patterns;
 +      int lno = 0;
 +      struct strbuf sb;
 +
 +      patterns = fopen(arg, "r");
 +      if (!patterns)
 +              die_errno("cannot open '%s'", arg);
 +      while (strbuf_getline(&sb, patterns, '\n') == 0) {
 +              /* ignore empty line like grep does */
 +              if (sb.len == 0)
 +                      continue;
 +              append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg,
 +                                  ++lno, GREP_PATTERN);
 +      }
 +      fclose(patterns);
 +      strbuf_release(&sb);
 +      return 0;
 +}
 +
 +static int not_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
 +      return 0;
 +}
 +
 +static int and_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
 +      return 0;
 +}
 +
 +static int open_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
 +      return 0;
 +}
 +
 +static int close_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
 +      return 0;
 +}
 +
 +static int pattern_callback(const struct option *opt, const char *arg,
 +                          int unset)
 +{
 +      struct grep_opt *grep_opt = opt->value;
 +      append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
 +      return 0;
 +}
 +
 +static int help_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      return -1;
 +}
  
  int cmd_grep(int argc, const char **argv, const char *prefix)
  {
        int hit = 0;
        int cached = 0;
 +      int external_grep_allowed = 1;
        int seen_dashdash = 0;
        struct grep_opt opt;
        struct object_array list = { 0, 0, NULL };
        const char **paths = NULL;
        int i;
 +      int dummy;
 +      struct option options[] = {
 +              OPT_BOOLEAN(0, "cached", &cached,
 +                      "search in index instead of in the work tree"),
 +              OPT_GROUP(""),
 +              OPT_BOOLEAN('v', "invert-match", &opt.invert,
 +                      "show non-matching lines"),
 +              OPT_BIT('i', "ignore-case", &opt.regflags,
 +                      "case insensitive matching", REG_ICASE),
 +              OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
 +                      "match patterns only at word boundaries"),
 +              OPT_SET_INT('a', "text", &opt.binary,
 +                      "process binary files as text", GREP_BINARY_TEXT),
 +              OPT_SET_INT('I', NULL, &opt.binary,
 +                      "don't match patterns in binary files",
 +                      GREP_BINARY_NOMATCH),
 +              OPT_GROUP(""),
 +              OPT_BIT('E', "extended-regexp", &opt.regflags,
 +                      "use extended POSIX regular expressions", REG_EXTENDED),
 +              OPT_NEGBIT('G', "basic-regexp", &opt.regflags,
 +                      "use basic POSIX regular expressions (default)",
 +                      REG_EXTENDED),
 +              OPT_BOOLEAN('F', "fixed-strings", &opt.fixed,
 +                      "interpret patterns as fixed strings"),
 +              OPT_GROUP(""),
 +              OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"),
 +              OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
 +              OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1),
 +              OPT_NEGBIT(0, "full-name", &opt.relative,
 +                      "show filenames relative to top directory", 1),
 +              OPT_BOOLEAN('l', "files-with-matches", &opt.name_only,
 +                      "show only filenames instead of matching lines"),
 +              OPT_BOOLEAN(0, "name-only", &opt.name_only,
 +                      "synonym for --files-with-matches"),
 +              OPT_BOOLEAN('L', "files-without-match",
 +                      &opt.unmatch_name_only,
 +                      "show only the names of files without match"),
 +              OPT_BOOLEAN('z', "null", &opt.null_following_name,
 +                      "print NUL after filenames"),
 +              OPT_BOOLEAN('c', "count", &opt.count,
 +                      "show the number of matches instead of matching lines"),
 +              OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1),
 +              OPT_GROUP(""),
 +              OPT_CALLBACK('C', NULL, &opt, "n",
 +                      "show <n> context lines before and after matches",
 +                      context_callback),
 +              OPT_INTEGER('B', NULL, &opt.pre_context,
 +                      "show <n> context lines before matches"),
 +              OPT_INTEGER('A', NULL, &opt.post_context,
 +                      "show <n> context lines after matches"),
 +              OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM",
 +                      context_callback),
 +              OPT_BOOLEAN('p', "show-function", &opt.funcname,
 +                      "show a line with the function name before matches"),
 +              OPT_GROUP(""),
 +              OPT_CALLBACK('f', NULL, &opt, "file",
 +                      "read patterns from file", file_callback),
 +              { OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
 +                      "match <pattern>", PARSE_OPT_NONEG, pattern_callback },
 +              { OPTION_CALLBACK, 0, "and", &opt, NULL,
 +                "combine patterns specified with -e",
 +                PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
 +              OPT_BOOLEAN(0, "or", &dummy, ""),
 +              { 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 },
 +              OPT_BOOLEAN(0, "all-match", &opt.all_match,
 +                      "show only matches from files that match all patterns"),
 +              OPT_GROUP(""),
 +#if NO_EXTERNAL_GREP
 +              OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
 +                      "allow calling of grep(1) (ignored by this build)"),
 +#else
 +              OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed,
 +                      "allow calling of grep(1) (default)"),
 +#endif
 +              { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
 +                PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
 +              OPT_END()
 +      };
  
        memset(&opt, 0, sizeof(opt));
+       opt.prefix = prefix;
        opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
        opt.relative = 1;
        opt.pathname = 1;
         * unrecognized non option is the beginning of the refs list
         * that continues up to the -- (if exists), and then paths.
         */
 -
 -      while (1 < argc) {
 -              const char *arg = argv[1];
 -              argc--; argv++;
 -              if (!strcmp("--cached", arg)) {
 -                      cached = 1;
 -                      continue;
 -              }
 -              if (!strcmp("--no-ext-grep", arg)) {
 -                      builtin_grep = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-a", arg) ||
 -                  !strcmp("--text", arg)) {
 -                      opt.binary = GREP_BINARY_TEXT;
 -                      continue;
 -              }
 -              if (!strcmp("-i", arg) ||
 -                  !strcmp("--ignore-case", arg)) {
 -                      opt.regflags |= REG_ICASE;
 -                      continue;
 -              }
 -              if (!strcmp("-I", arg)) {
 -                      opt.binary = GREP_BINARY_NOMATCH;
 -                      continue;
 -              }
 -              if (!strcmp("-v", arg) ||
 -                  !strcmp("--invert-match", arg)) {
 -                      opt.invert = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-E", arg) ||
 -                  !strcmp("--extended-regexp", arg)) {
 -                      opt.regflags |= REG_EXTENDED;
 -                      continue;
 -              }
 -              if (!strcmp("-F", arg) ||
 -                  !strcmp("--fixed-strings", arg)) {
 -                      opt.fixed = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-G", arg) ||
 -                  !strcmp("--basic-regexp", arg)) {
 -                      opt.regflags &= ~REG_EXTENDED;
 -                      continue;
 -              }
 -              if (!strcmp("-n", arg)) {
 -                      opt.linenum = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-h", arg)) {
 -                      opt.pathname = 0;
 -                      continue;
 -              }
 -              if (!strcmp("-H", arg)) {
 -                      opt.pathname = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-l", arg) ||
 -                  !strcmp("--name-only", arg) ||
 -                  !strcmp("--files-with-matches", arg)) {
 -                      opt.name_only = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-L", arg) ||
 -                  !strcmp("--files-without-match", arg)) {
 -                      opt.unmatch_name_only = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-z", arg) ||
 -                  !strcmp("--null", arg)) {
 -                      opt.null_following_name = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-c", arg) ||
 -                  !strcmp("--count", arg)) {
 -                      opt.count = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-w", arg) ||
 -                  !strcmp("--word-regexp", arg)) {
 -                      opt.word_regexp = 1;
 -                      continue;
 -              }
 -              if (!prefixcmp(arg, "-A") ||
 -                  !prefixcmp(arg, "-B") ||
 -                  !prefixcmp(arg, "-C") ||
 -                  (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
 -                      unsigned num;
 -                      const char *scan;
 -                      switch (arg[1]) {
 -                      case 'A': case 'B': case 'C':
 -                              if (!arg[2]) {
 -                                      if (argc <= 1)
 -                                              die(emsg_missing_context_len);
 -                                      scan = *++argv;
 -                                      argc--;
 -                              }
 -                              else
 -                                      scan = arg + 2;
 -                              break;
 -                      default:
 -                              scan = arg + 1;
 -                              break;
 -                      }
 -                      if (strtoul_ui(scan, 10, &num))
 -                              die(emsg_invalid_context_len, scan);
 -                      switch (arg[1]) {
 -                      case 'A':
 -                              opt.post_context = num;
 -                              break;
 -                      default:
 -                      case 'C':
 -                              opt.post_context = num;
 -                      case 'B':
 -                              opt.pre_context = num;
 -                              break;
 -                      }
 -                      continue;
 -              }
 -              if (!strcmp("-f", arg)) {
 -                      FILE *patterns;
 -                      int lno = 0;
 -                      char buf[1024];
 -                      if (argc <= 1)
 -                              die(emsg_missing_argument, arg);
 -                      patterns = fopen(argv[1], "r");
 -                      if (!patterns)
 -                              die("'%s': %s", argv[1], strerror(errno));
 -                      while (fgets(buf, sizeof(buf), patterns)) {
 -                              int len = strlen(buf);
 -                              if (len && buf[len-1] == '\n')
 -                                      buf[len-1] = 0;
 -                              /* ignore empty line like grep does */
 -                              if (!buf[0])
 -                                      continue;
 -                              append_grep_pattern(&opt, xstrdup(buf),
 -                                                  argv[1], ++lno,
 -                                                  GREP_PATTERN);
 -                      }
 -                      fclose(patterns);
 -                      argv++;
 -                      argc--;
 -                      continue;
 -              }
 -              if (!strcmp("--not", arg)) {
 -                      append_grep_pattern(&opt, arg, "command line", 0,
 -                                          GREP_NOT);
 -                      continue;
 -              }
 -              if (!strcmp("--and", arg)) {
 -                      append_grep_pattern(&opt, arg, "command line", 0,
 -                                          GREP_AND);
 -                      continue;
 -              }
 -              if (!strcmp("--or", arg))
 -                      continue; /* no-op */
 -              if (!strcmp("(", arg)) {
 -                      append_grep_pattern(&opt, arg, "command line", 0,
 -                                          GREP_OPEN_PAREN);
 -                      continue;
 -              }
 -              if (!strcmp(")", arg)) {
 -                      append_grep_pattern(&opt, arg, "command line", 0,
 -                                          GREP_CLOSE_PAREN);
 -                      continue;
 -              }
 -              if (!strcmp("--all-match", arg)) {
 -                      opt.all_match = 1;
 -                      continue;
 -              }
 -              if (!strcmp("-e", arg)) {
 -                      if (1 < argc) {
 -                              append_grep_pattern(&opt, argv[1],
 -                                                  "-e option", 0,
 -                                                  GREP_PATTERN);
 -                              argv++;
 -                              argc--;
 -                              continue;
 -                      }
 -                      die(emsg_missing_argument, arg);
 -              }
 -              if (!strcmp("--full-name", arg)) {
 -                      opt.relative = 0;
 -                      continue;
 -              }
 -              if (!strcmp("--color", arg)) {
 -                      opt.color = 1;
 -                      continue;
 -              }
 -              if (!strcmp("--no-color", arg)) {
 -                      opt.color = 0;
 -                      continue;
 -              }
 -              if (!strcmp("--", arg)) {
 -                      /* later processing wants to have this at argv[1] */
 -                      argv--;
 -                      argc++;
 -                      break;
 -              }
 -              if (*arg == '-')
 -                      usage(builtin_grep_usage);
 -
 -              /* First unrecognized non-option token */
 -              if (!opt.pattern_list) {
 -                      append_grep_pattern(&opt, arg, "command line", 0,
 -                                          GREP_PATTERN);
 -                      break;
 -              }
 -              else {
 -                      /* We are looking at the first path or rev;
 -                       * it is found at argv[1] after leaving the
 -                       * loop.
 -                       */
 -                      argc++; argv--;
 -                      break;
 -              }
 +      argc = parse_options(argc, argv, prefix, options, grep_usage,
 +                           PARSE_OPT_KEEP_DASHDASH |
 +                           PARSE_OPT_STOP_AT_NON_OPTION |
 +                           PARSE_OPT_NO_INTERNAL_HELP);
 +
 +      /* First unrecognized non-option token */
 +      if (argc > 0 && !opt.pattern_list) {
 +              append_grep_pattern(&opt, argv[0], "command line", 0,
 +                                  GREP_PATTERN);
 +              argv++;
 +              argc--;
        }
  
 -      if (opt.color && !opt.color_external)
 -              builtin_grep = 1;
 +      if ((opt.color && !opt.color_external) || opt.funcname)
 +              external_grep_allowed = 0;
        if (!opt.pattern_list)
                die("no pattern given.");
        if ((opt.regflags != REG_NEWLINE) && opt.fixed)
        compile_grep_patterns(&opt);
  
        /* Check revs and then paths */
 -      for (i = 1; i < argc; i++) {
 +      for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
                unsigned char sha1[20];
                /* Is it a rev? */
                        verify_filename(prefix, argv[j]);
        }
  
-       if (i < argc) {
+       if (i < argc)
                paths = get_pathspec(prefix, argv + i);
-               if (opt.prefix_length && opt.relative) {
-                       /* Make sure we do not get outside of paths */
-                       for (i = 0; paths[i]; i++)
-                               if (strncmp(prefix, paths[i], opt.prefix_length))
-                                       die("git grep: cannot generate relative filenames containing '..'");
-               }
-       }
        else if (prefix) {
                paths = xcalloc(2, sizeof(const char *));
                paths[0] = prefix;
        if (!list.nr) {
                if (!cached)
                        setup_work_tree();
 -              return !grep_cache(&opt, paths, cached);
 +              return !grep_cache(&opt, paths, cached, external_grep_allowed);
        }
  
        if (cached)
diff --combined grep.h
index f00db0e40273c7cca1695ddc6be00921f9da8ef4,5b767c931c0fec250d4dff74f6ef72c0abea16af..5581363c4ddb988830f9c9b776b057821107f92a
--- 1/grep.h
--- 2/grep.h
+++ b/grep.h
@@@ -59,35 -59,32 +59,36 @@@ struct grep_opt 
        struct grep_pat *pattern_list;
        struct grep_pat **pattern_tail;
        struct grep_expr *pattern_expression;
+       const char *prefix;
        int prefix_length;
        regex_t regexp;
 -      unsigned linenum:1;
 -      unsigned invert:1;
 -      unsigned status_only:1;
 -      unsigned name_only:1;
 -      unsigned unmatch_name_only:1;
 -      unsigned count:1;
 -      unsigned word_regexp:1;
 -      unsigned fixed:1;
 -      unsigned all_match:1;
 +      int linenum;
 +      int invert;
 +      int status_only;
 +      int name_only;
 +      int unmatch_name_only;
 +      int count;
 +      int word_regexp;
 +      int fixed;
 +      int all_match;
  #define GREP_BINARY_DEFAULT   0
  #define GREP_BINARY_NOMATCH   1
  #define GREP_BINARY_TEXT      2
 -      unsigned binary:2;
 -      unsigned extended:1;
 -      unsigned relative:1;
 -      unsigned pathname:1;
 -      unsigned null_following_name:1;
 +      int binary;
 +      int extended;
 +      int relative;
 +      int pathname;
 +      int null_following_name;
        int color;
 +      int funcname;
        char color_match[COLOR_MAXLEN];
        const char *color_external;
        int regflags;
        unsigned pre_context;
        unsigned post_context;
 +      unsigned last_shown;
 +      int show_hunk_mark;
 +      void *priv;
  };
  
  extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
diff --combined t/t7002-grep.sh
index b13aa7e89ad51566979ab674baa2ac36c7e33da6,fe87834fbeb22dfe21fabbbfb56f7c73558de2b2..6ca11d71465aa609232614bbbd360955869e0776
@@@ -8,15 -8,6 +8,15 @@@ test_description='git grep various
  
  . ./test-lib.sh
  
 +cat >hello.c <<EOF
 +#include <stdio.h>
 +int main(int argc, const char **argv)
 +{
 +      printf("Hello world.\n");
 +      return 0;
 +}
 +EOF
 +
  test_expect_success setup '
        {
                echo foo mmap bar
@@@ -31,7 -22,7 +31,7 @@@
        echo zzz > z &&
        mkdir t &&
        echo test >t/t &&
 -      git add file w x y z t/t &&
 +      git add file w x y z t/t hello.c &&
        test_tick &&
        git commit -m initial
  '
@@@ -164,28 -155,6 +164,28 @@@ test_expect_success 'grep -e A --and --
        test_cmp expected actual
  '
  
 +cat >expected <<EOF
 +y:y yy
 +--
 +z:zzz
 +EOF
 +
 +# Create 1024 file names that sort between "y" and "z" to make sure
 +# the two files are handled by different calls to an external grep.
 +# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 +c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
 +test_expect_success 'grep -C1, hunk mark between files' '
 +      for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 +      git add y-?? &&
 +      git grep -C1 "^[yz]" >actual &&
 +      test_cmp expected actual
 +'
 +
 +test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 +      git grep -C1 --no-ext-grep "^[yz]" >actual &&
 +      test_cmp expected actual
 +'
 +
  test_expect_success 'log grep setup' '
        echo a >>file &&
        test_tick &&
@@@ -238,45 -207,26 +238,62 @@@ test_expect_success 'log grep (6)' 
  test_expect_success 'grep with CE_VALID file' '
        git update-index --assume-unchanged t/t &&
        rm t/t &&
 -      test "$(git grep --no-ext-grep t)" = "t/t:test" &&
 +      test "$(git grep --no-ext-grep test)" = "t/t:test" &&
        git update-index --no-assume-unchanged t/t &&
        git checkout t/t
  '
  
 +cat >expected <<EOF
 +hello.c=#include <stdio.h>
 +hello.c:      return 0;
 +EOF
 +
 +test_expect_success 'grep -p with userdiff' '
 +      git config diff.custom.funcname "^#" &&
 +      echo "hello.c diff=custom" >.gitattributes &&
 +      git grep -p return >actual &&
 +      test_cmp expected actual
 +'
 +
 +cat >expected <<EOF
 +hello.c=int main(int argc, const char **argv)
 +hello.c:      return 0;
 +EOF
 +
 +test_expect_success 'grep -p' '
 +      rm -f .gitattributes &&
 +      git grep -p return >actual &&
 +      test_cmp expected actual
 +'
 +
 +cat >expected <<EOF
 +hello.c-#include <stdio.h>
 +hello.c=int main(int argc, const char **argv)
 +hello.c-{
 +hello.c-      printf("Hello world.\n");
 +hello.c:      return 0;
 +EOF
 +
 +test_expect_success 'grep -p -B5' '
 +      git grep -p -B5 return >actual &&
 +      test_cmp expected actual
 +'
 +
+ test_expect_success 'grep from a subdirectory to search wider area (1)' '
+       mkdir -p s &&
+       (
+               cd s && git grep "x x x" ..
+       )
+ '
+ test_expect_success 'grep from a subdirectory to search wider area (2)' '
+       mkdir -p s &&
+       (
+               cd s || exit 1
+               ( git grep xxyyzz .. >out ; echo $? >status )
+               ! test -s out &&
+               test 1 = $(cat status)
+       )
+ '
  test_done