Code

test-lib: unset GIT_NOTES_REF to stop it from influencing tests
[git.git] / builtin-grep.c
index 04ac60a29a55189d2c757a0a205b4cc0e7ece9da..da854fa94f5cc159ea93156ba4c87973ee15cae9 100644 (file)
 #include "parse-options.h"
 #include "userdiff.h"
 #include "grep.h"
-
-#ifndef NO_EXTERNAL_GREP
-#ifdef __unix__
-#define NO_EXTERNAL_GREP 0
-#else
-#define NO_EXTERNAL_GREP 1
-#endif
-#endif
+#include "quote.h"
+#include "dir.h"
 
 static char const * const grep_usage[] = {
        "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
@@ -41,8 +35,6 @@ static int grep_config(const char *var, const char *value, void *cb)
                opt->color = git_config_colorbool(var, value, -1);
                return 0;
        }
-       if (!strcmp(var, "color.grep.external"))
-               return git_config_string(&(opt->color_external), var, value);
        if (!strcmp(var, "color.grep.match")) {
                if (!value)
                        return config_error_nonbool(var);
@@ -157,8 +149,8 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
        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) {
@@ -166,26 +158,13 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
                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;
 }
 
@@ -195,6 +174,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
        int i;
        char *data;
        size_t sz;
+       struct strbuf buf = STRBUF_INIT;
 
        if (lstat(filename, &st) < 0) {
        err_ret:
@@ -202,8 +182,6 @@ static int grep_file(struct grep_opt *opt, const char *filename)
                        error("'%s': %s", filename, strerror(errno));
                return 0;
        }
-       if (!st.st_size)
-               return 0; /* empty file -- no grep hit */
        if (!S_ISREG(st.st_mode))
                return 0;
        sz = xsize_t(st.st_size);
@@ -218,311 +196,21 @@ static int grep_file(struct grep_opt *opt, const char *filename)
                return 0;
        }
        close(i);
+       data[sz] = 0;
        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;
 }
 
-#if !NO_EXTERNAL_GREP
-static int exec_grep(int argc, const char **argv)
-{
-       pid_t pid;
-       int status;
-
-       argv[argc] = NULL;
-       trace_argv_printf(argv, "trace: grep:");
-       pid = fork();
-       if (pid < 0)
-               return pid;
-       if (!pid) {
-               execvp("grep", (char **) argv);
-               exit(255);
-       }
-       while (waitpid(pid, &status, 0) < 0) {
-               if (errno == EINTR)
-                       continue;
-               return -1;
-       }
-       if (WIFEXITED(status)) {
-               if (!WEXITSTATUS(status))
-                       return 1;
-               return 0;
-       }
-       return -1;
-}
-
-#define MAXARGS 1000
-#define ARGBUF 4096
-#define push_arg(a) do { \
-       if (nr < MAXARGS) argv[nr++] = (a); \
-       else die("maximum number of args exceeded"); \
-       } while (0)
-
-/*
- * If you send a singleton filename to grep, it does not give
- * the name of the file.  GNU grep has "-H" but we would want
- * that behaviour in a portable way.
- *
- * So we keep two pathnames in argv buffer unsent to grep in
- * the main loop if we need to do more than one grep.
- */
-static int flush_grep(struct grep_opt *opt,
-                     int argc, int arg0, const char **argv, int *kept)
-{
-       int status;
-       int count = argc - arg0;
-       const char *kept_0 = NULL;
-
-       if (count <= 2) {
-               /*
-                * Because we keep at least 2 paths in the call from
-                * the main loop (i.e. kept != NULL), and MAXARGS is
-                * far greater than 2, this usually is a call to
-                * conclude the grep.  However, the user could attempt
-                * to overflow the argv buffer by giving too many
-                * options to leave very small number of real
-                * arguments even for the call in the main loop.
-                */
-               if (kept)
-                       die("insanely many options to grep");
-
-               /*
-                * If we have two or more paths, we do not have to do
-                * anything special, but we need to push /dev/null to
-                * get "-H" behaviour of GNU grep portably but when we
-                * are not doing "-l" nor "-L" nor "-c".
-                */
-               if (count == 1 &&
-                   !opt->name_only &&
-                   !opt->unmatch_name_only &&
-                   !opt->count) {
-                       argv[argc++] = "/dev/null";
-                       argv[argc] = NULL;
-               }
-       }
-
-       else if (kept) {
-               /*
-                * Called because we found many paths and haven't finished
-                * iterating over the cache yet.  We keep two paths
-                * for the concluding call.  argv[argc-2] and argv[argc-1]
-                * has the last two paths, so save the first one away,
-                * replace it with NULL while sending the list to grep,
-                * and recover them after we are done.
-                */
-               *kept = 2;
-               kept_0 = argv[argc-2];
-               argv[argc-2] = NULL;
-               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) {
-               /*
-                * Then recover them.  Now the last arg is beyond the
-                * terminating NULL which is at argc, and the second
-                * from the last is what we saved away in kept_0
-                */
-               argv[arg0++] = kept_0;
-               argv[arg0] = argv[argc+1];
-       }
-       return status;
-}
-
-static void grep_add_color(struct strbuf *sb, const char *escape_seq)
-{
-       size_t orig_len = sb->len;
-
-       while (*escape_seq) {
-               if (*escape_seq == 'm')
-                       strbuf_addch(sb, ';');
-               else if (*escape_seq != '\033' && *escape_seq  != '[')
-                       strbuf_addch(sb, *escape_seq);
-               escape_seq++;
-       }
-       if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
-               strbuf_setlen(sb, sb->len - 1);
-}
-
-static int has_skip_worktree_entry(struct grep_opt *opt, const char **paths)
-{
-       int nr;
-       for (nr = 0; nr < active_nr; nr++) {
-               struct cache_entry *ce = active_cache[nr];
-               if (!S_ISREG(ce->ce_mode))
-                       continue;
-               if (!pathspec_matches(paths, ce->name, opt->max_depth))
-                       continue;
-               if (ce_skip_worktree(ce))
-                       return 1;
-       }
-       return 0;
-}
-
-static int external_grep(struct grep_opt *opt, const char **paths, int cached)
-{
-       int i, nr, argc, hit, len, status;
-       const char *argv[MAXARGS+1];
-       char randarg[ARGBUF];
-       char *argptr = randarg;
-       struct grep_pat *p;
-
-       if (opt->extended || (opt->relative && opt->prefix_length)
-           || has_skip_worktree_entry(opt, paths))
-               return -1;
-       len = nr = 0;
-       push_arg("grep");
-       if (opt->fixed)
-               push_arg("-F");
-       if (opt->linenum)
-               push_arg("-n");
-       if (!opt->pathname)
-               push_arg("-h");
-       if (opt->regflags & REG_EXTENDED)
-               push_arg("-E");
-       if (opt->regflags & REG_ICASE)
-               push_arg("-i");
-       if (opt->binary == GREP_BINARY_NOMATCH)
-               push_arg("-I");
-       if (opt->word_regexp)
-               push_arg("-w");
-       if (opt->name_only)
-               push_arg("-l");
-       if (opt->unmatch_name_only)
-               push_arg("-L");
-       if (opt->null_following_name)
-               /* in GNU grep git's "-z" translates to "-Z" */
-               push_arg("-Z");
-       if (opt->count)
-               push_arg("-c");
-       if (opt->post_context || opt->pre_context) {
-               if (opt->post_context != opt->pre_context) {
-                       if (opt->pre_context) {
-                               push_arg("-B");
-                               len += snprintf(argptr, sizeof(randarg)-len,
-                                               "%u", opt->pre_context) + 1;
-                               if (sizeof(randarg) <= len)
-                                       die("maximum length of args exceeded");
-                               push_arg(argptr);
-                               argptr += len;
-                       }
-                       if (opt->post_context) {
-                               push_arg("-A");
-                               len += snprintf(argptr, sizeof(randarg)-len,
-                                               "%u", opt->post_context) + 1;
-                               if (sizeof(randarg) <= len)
-                                       die("maximum length of args exceeded");
-                               push_arg(argptr);
-                               argptr += len;
-                       }
-               }
-               else {
-                       push_arg("-C");
-                       len += snprintf(argptr, sizeof(randarg)-len,
-                                       "%u", opt->post_context) + 1;
-                       if (sizeof(randarg) <= len)
-                               die("maximum length of args exceeded");
-                       push_arg(argptr);
-                       argptr += len;
-               }
-       }
-       for (p = opt->pattern_list; p; p = p->next) {
-               push_arg("-e");
-               push_arg(p->pattern);
-       }
-       if (opt->color) {
-               struct strbuf sb = STRBUF_INIT;
-
-               grep_add_color(&sb, opt->color_match);
-               setenv("GREP_COLOR", sb.buf, 1);
-
-               strbuf_reset(&sb);
-               strbuf_addstr(&sb, "mt=");
-               grep_add_color(&sb, opt->color_match);
-               strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
-               setenv("GREP_COLORS", sb.buf, 1);
-
-               strbuf_release(&sb);
-
-               if (opt->color_external && strlen(opt->color_external) > 0)
-                       push_arg(opt->color_external);
-       }
-
-       hit = 0;
-       argc = nr;
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
-               char *name;
-               int kept;
-               if (!S_ISREG(ce->ce_mode))
-                       continue;
-               if (!pathspec_matches(paths, ce->name, opt->max_depth))
-                       continue;
-               name = ce->name;
-               if (name[0] == '-') {
-                       int len = ce_namelen(ce);
-                       name = xmalloc(len + 3);
-                       memcpy(name, "./", 2);
-                       memcpy(name + 2, ce->name, len + 1);
-               }
-               argv[argc++] = name;
-               if (MAXARGS <= argc) {
-                       status = flush_grep(opt, argc, nr, argv, &kept);
-                       if (0 < status)
-                               hit = 1;
-                       argc = nr + kept;
-               }
-               if (ce_stage(ce)) {
-                       do {
-                               i++;
-                       } while (i < active_nr &&
-                                !strcmp(ce->name, active_cache[i]->name));
-                       i--; /* compensate for loop control */
-               }
-       }
-       if (argc > nr) {
-               status = flush_grep(opt, argc, nr, argv, NULL);
-               if (0 < status)
-                       hit = 1;
-       }
-       return hit;
-}
-#endif
-
-static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
-                     int external_grep_allowed)
+static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 {
        int hit = 0;
        int nr;
        read_cache();
 
-#if !NO_EXTERNAL_GREP
-       /*
-        * Use the external "grep" command for the case where
-        * we grep through the checked-out files. It tends to
-        * be a lot more optimized
-        */
-       if (!cached && external_grep_allowed) {
-               hit = external_grep(opt, paths, cached);
-               if (hit >= 0)
-                       return hit;
-       }
-#endif
-
        for (nr = 0; nr < active_nr; nr++) {
                struct cache_entry *ce = active_cache[nr];
                if (!S_ISREG(ce->ce_mode))
@@ -632,6 +320,21 @@ static int grep_object(struct grep_opt *opt, const char **paths,
        die("unable to grep from object of type %s", typename(obj->type));
 }
 
+static int grep_directory(struct grep_opt *opt, const char **paths)
+{
+       struct dir_struct dir;
+       int i, hit = 0;
+
+       memset(&dir, 0, sizeof(dir));
+       setup_standard_excludes(&dir);
+
+       fill_directory(&dir, paths);
+       for (i = 0; i < dir.nr; i++)
+               hit |= grep_file(opt, dir.entries[i]->name);
+       free_grep_patterns(opt);
+       return hit;
+}
+
 static int context_callback(const struct option *opt, const char *arg,
                            int unset)
 {
@@ -657,7 +360,7 @@ 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;
+       struct strbuf sb = STRBUF_INIT;
 
        patterns = fopen(arg, "r");
        if (!patterns)
@@ -719,21 +422,24 @@ 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;
+       int external_grep_allowed__ignored;
        struct grep_opt opt;
        struct object_array list = { 0, 0, NULL };
        const char **paths = NULL;
        int i;
        int dummy;
+       int nongit = 0, use_index = 1;
        struct option options[] = {
                OPT_BOOLEAN(0, "cached", &cached,
                        "search in index instead of in the work tree"),
+               OPT_BOOLEAN(0, "index", &use_index,
+                       "--no-index finds in contents not managed by git"),
                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('i', "ignore-case", &opt.ignore_case,
+                       "case insensitive matching"),
                OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
                        "match patterns only at word boundaries"),
                OPT_SET_INT('a', "text", &opt.binary,
@@ -802,19 +508,24 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                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
+               OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
+                           "allow calling of grep(1) (ignored by this build)"),
                { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
                  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
                OPT_END()
        };
 
+       prefix = setup_git_directory_gently(&nongit);
+
+       /*
+        * 'git grep -h', unlike 'git grep -h <pattern>', is a request
+        * to show usage information and exit.
+        */
+       if (argc == 2 && !strcmp(argv[1], "-h"))
+               usage_with_options(grep_usage, options);
+
        memset(&opt, 0, sizeof(opt));
+       opt.prefix = prefix;
        opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
        opt.relative = 1;
        opt.pathname = 1;
@@ -843,6 +554,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                             PARSE_OPT_STOP_AT_NON_OPTION |
                             PARSE_OPT_NO_INTERNAL_HELP);
 
+       if (use_index && nongit)
+               /* die the same way as if we did it at the beginning */
+               setup_git_directory();
+
        /* First unrecognized non-option token */
        if (argc > 0 && !opt.pattern_list) {
                append_grep_pattern(&opt, argv[0], "command line", 0,
@@ -851,10 +566,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                argc--;
        }
 
-       if ((opt.color && !opt.color_external) || opt.funcname)
-               external_grep_allowed = 0;
        if (!opt.pattern_list)
                die("no pattern given.");
+       if (!opt.fixed && opt.ignore_case)
+               opt.regflags |= REG_ICASE;
        if ((opt.regflags != REG_NEWLINE) && opt.fixed)
                die("cannot mix --fixed-strings and regexp");
        compile_grep_patterns(&opt);
@@ -885,25 +600,26 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        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;
                paths[1] = NULL;
        }
 
+       if (!use_index) {
+               if (cached)
+                       die("--cached cannot be used with --no-index.");
+               if (list.nr)
+                       die("--no-index cannot be used with revs.");
+               return !grep_directory(&opt, paths);
+       }
+
        if (!list.nr) {
                if (!cached)
                        setup_work_tree();
-               return !grep_cache(&opt, paths, cached, external_grep_allowed);
+               return !grep_cache(&opt, paths, cached);
        }
 
        if (cached)