From: Junio C Hamano Date: Wed, 5 Oct 2011 01:40:41 +0000 (-0700) Subject: Merge branch 'jc/maint-grep-untracked-exclude' into jc/grep-untracked-exclude X-Git-Tag: v1.7.8-rc0~71^2~1 X-Git-Url: https://git.tokkee.org/?p=git.git;a=commitdiff_plain;h=dbfae86a7b0bf9f9a4f37dbc916e8d26a5f9d51c Merge branch 'jc/maint-grep-untracked-exclude' into jc/grep-untracked-exclude * jc/maint-grep-untracked-exclude: grep: teach --untracked and --exclude-standard options grep --no-index: don't use git standard exclusions grep: do not use --index in the short usage output Conflicts: Documentation/git-grep.txt builtin/grep.c --- dbfae86a7b0bf9f9a4f37dbc916e8d26a5f9d51c diff --cc Documentation/git-grep.txt index e44a4988b,6269007b9..15d6711d4 --- a/Documentation/git-grep.txt +++ b/Documentation/git-grep.txt @@@ -19,12 -18,11 +19,12 @@@ SYNOPSI [-z | --null] [-c | --count] [--all-match] [-q | --quiet] [--max-depth ] - [--color | --no-color] + [--color[=] | --no-color] [-A ] [-B ] [-C ] [-f ] [-e] - [--and|--or|--not|(|)|-e ...] [...] - [--] [...] + [--and|--or|--not|(|)|-e ...] - [--cached | --no-index | ...] ++ [ [--exclude-standard] [--cached | --no-index | --untracked] | ...] + [--] [...] DESCRIPTION ----------- @@@ -45,11 -33,21 +45,24 @@@ grep.extendedRegexp: OPTIONS ------- --cached:: - Instead of searching in the working tree files, check - the blobs registered in the index file. + Instead of searching tracked files in the working tree, search + blobs registered in the index file. + +--no-index:: - Search files in the current directory, not just those tracked by git. ++ Search files in the current directory that is not managed by git. + + --untracked:: + In addition to searching in the tracked files in the working + tree, search also in untracked files. + + --no-exclude-standard:: + Also search in ignored files by not honoring the `.gitignore` + mechanism. Only useful with `--untracked`. + + --exclude-standard:: + Do not pay attention to ignored files specified via the `.gitignore` + mechanism. Only useful when searching files in the current directory + with `--no-index`. -a:: --text:: diff --cc builtin/grep.c index 1c359c267,000000000..5e6d3c335 mode 100644,000000..100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@@ -1,1072 -1,0 +1,1082 @@@ +/* + * Builtin "git grep" + * + * Copyright (c) 2006 Junio C Hamano + */ +#include "cache.h" +#include "blob.h" +#include "tree.h" +#include "commit.h" +#include "tag.h" +#include "tree-walk.h" +#include "builtin.h" +#include "parse-options.h" +#include "string-list.h" +#include "run-command.h" +#include "userdiff.h" +#include "grep.h" +#include "quote.h" +#include "dir.h" +#include "thread-utils.h" + +static char const * const grep_usage[] = { + "git grep [options] [-e] [...] [[--] ...]", + NULL +}; + +static int use_threads = 1; + +#ifndef NO_PTHREADS +#define THREADS 8 +static pthread_t threads[THREADS]; + +static void *load_sha1(const unsigned char *sha1, unsigned long *size, + const char *name); +static void *load_file(const char *filename, size_t *sz); + +enum work_type {WORK_SHA1, WORK_FILE}; + +/* 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. + */ +struct work_item { + enum work_type type; + char *name; + + /* if type == WORK_SHA1, then 'identifier' is a SHA1, + * otherwise type == WORK_FILE, and 'identifier' is a NUL + * terminated filename. + */ + void *identifier; + 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; + +/* Used to serialize calls to read_sha1_file. */ +static pthread_mutex_t read_sha1_mutex; + +#define grep_lock() pthread_mutex_lock(&grep_mutex) +#define grep_unlock() pthread_mutex_unlock(&grep_mutex) +#define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex) +#define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex) + +/* 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; + +static int skip_first_line; + +static void add_work(enum work_type type, char *name, void *id) +{ + grep_lock(); + + while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { + pthread_cond_wait(&cond_write, &grep_mutex); + } + + todo[todo_end].type = type; + todo[todo_end].name = name; + todo[todo_end].identifier = id; + 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 grep_sha1_async(struct grep_opt *opt, char *name, + const unsigned char *sha1) +{ + unsigned char *s; + s = xmalloc(20); + memcpy(s, sha1, 20); + add_work(WORK_SHA1, name, s); +} + +static void grep_file_async(struct grep_opt *opt, char *name, + const char *filename) +{ + add_work(WORK_FILE, name, xstrdup(filename)); +} + +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]; + if (w->out.len) { + 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); + } + free(w->name); + free(w->identifier); + } + + 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; + if (w->type == WORK_SHA1) { + unsigned long sz; + void* data = load_sha1(w->identifier, &sz, w->name); + + if (data) { + hit |= grep_buffer(opt, w->name, data, sz); + free(data); + } + } else if (w->type == WORK_FILE) { + size_t sz; + void* data = load_file(w->identifier, &sz); + if (data) { + hit |= grep_buffer(opt, w->name, data, sz); + free(data); + } + } else { + assert(0); + } + + work_done(w); + } + free_grep_patterns(arg); + free(arg); + + 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); + pthread_mutex_init(&read_sha1_mutex, NULL); + pthread_cond_init(&cond_add, NULL); + pthread_cond_init(&cond_write, NULL); + pthread_cond_init(&cond_result, NULL); + + for (i = 0; i < ARRAY_SIZE(todo); i++) { + strbuf_init(&todo[i].out, 0); + } + + for (i = 0; i < ARRAY_SIZE(threads); i++) { + int err; + struct grep_opt *o = grep_opt_dup(opt); + o->output = strbuf_out; + compile_grep_patterns(o); + err = pthread_create(&threads[i], NULL, run, o); + + if (err) + die(_("grep: failed to create thread: %s"), + strerror(err)); + } +} + +static int wait_all(void) +{ + int hit = 0; + int i; + + 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(); + + for (i = 0; i < ARRAY_SIZE(threads); i++) { + void *h; + pthread_join(threads[i], &h); + hit |= (int) (intptr_t) h; + } + + pthread_mutex_destroy(&grep_mutex); + pthread_mutex_destroy(&read_sha1_mutex); + pthread_cond_destroy(&cond_add); + pthread_cond_destroy(&cond_write); + pthread_cond_destroy(&cond_result); + + return hit; +} +#else /* !NO_PTHREADS */ +#define read_sha1_lock() +#define read_sha1_unlock() + +static int wait_all(void) +{ + return 0; +} +#endif + +static int grep_config(const char *var, const char *value, void *cb) +{ + struct grep_opt *opt = cb; + char *color = NULL; + + switch (userdiff_config(var, value)) { + case 0: break; + case -1: return -1; + default: return 0; + } + + if (!strcmp(var, "grep.extendedregexp")) { + if (git_config_bool(var, value)) + opt->regflags |= REG_EXTENDED; + else + opt->regflags &= ~REG_EXTENDED; + return 0; + } + + if (!strcmp(var, "grep.linenumber")) { + opt->linenum = git_config_bool(var, value); + return 0; + } + + if (!strcmp(var, "color.grep")) + opt->color = git_config_colorbool(var, value); + else if (!strcmp(var, "color.grep.context")) + color = opt->color_context; + else if (!strcmp(var, "color.grep.filename")) + color = opt->color_filename; + else if (!strcmp(var, "color.grep.function")) + color = opt->color_function; + else if (!strcmp(var, "color.grep.linenumber")) + color = opt->color_lineno; + else if (!strcmp(var, "color.grep.match")) + color = opt->color_match; + else if (!strcmp(var, "color.grep.selected")) + color = opt->color_selected; + else if (!strcmp(var, "color.grep.separator")) + color = opt->color_sep; + else + return git_color_default_config(var, value, cb); + if (color) { + if (!value) + return config_error_nonbool(var); + color_parse(value, var, color); + } + return 0; +} + +static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size) +{ + void *data; + + if (use_threads) { + read_sha1_lock(); + data = read_sha1_file(sha1, type, size); + read_sha1_unlock(); + } else { + data = read_sha1_file(sha1, type, size); + } + return data; +} + +static void *load_sha1(const unsigned char *sha1, unsigned long *size, + const char *name) +{ + enum object_type type; + void *data = lock_and_read_sha1_file(sha1, &type, size); + + if (!data) + error(_("'%s': unable to read %s"), name, sha1_to_hex(sha1)); + + return data; +} + +static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, + const char *filename, int tree_name_len) +{ + struct strbuf pathbuf = STRBUF_INIT; + char *name; + + if (opt->relative && opt->prefix_length) { + quote_path_relative(filename + tree_name_len, -1, &pathbuf, + opt->prefix); + strbuf_insert(&pathbuf, 0, filename, tree_name_len); + } else { + strbuf_addstr(&pathbuf, filename); + } + + name = strbuf_detach(&pathbuf, NULL); + +#ifndef NO_PTHREADS + if (use_threads) { + grep_sha1_async(opt, name, sha1); + return 0; + } else +#endif + { + int hit; + unsigned long sz; + void *data = load_sha1(sha1, &sz, name); + if (!data) + hit = 0; + else + hit = grep_buffer(opt, name, data, sz); + + free(data); + free(name); + return hit; + } +} + +static void *load_file(const char *filename, size_t *sz) +{ + struct stat st; + char *data; + int i; + + if (lstat(filename, &st) < 0) { + err_ret: + if (errno != ENOENT) + error(_("'%s': %s"), filename, strerror(errno)); + return NULL; + } + if (!S_ISREG(st.st_mode)) + return NULL; + *sz = xsize_t(st.st_size); + i = open(filename, O_RDONLY); + if (i < 0) + goto err_ret; + data = xmalloc(*sz + 1); + if (st.st_size != read_in_full(i, data, *sz)) { + error(_("'%s': short read %s"), filename, strerror(errno)); + close(i); + free(data); + return NULL; + } + close(i); + data[*sz] = 0; + return data; +} + +static int grep_file(struct grep_opt *opt, const char *filename) +{ + struct strbuf buf = STRBUF_INIT; + char *name; + + if (opt->relative && opt->prefix_length) + quote_path_relative(filename, -1, &buf, opt->prefix); + else + strbuf_addstr(&buf, filename); + name = strbuf_detach(&buf, NULL); + +#ifndef NO_PTHREADS + if (use_threads) { + grep_file_async(opt, name, filename); + return 0; + } else +#endif + { + int hit; + size_t sz; + void *data = load_file(filename, &sz); + if (!data) + hit = 0; + else + hit = grep_buffer(opt, name, data, sz); + + free(data); + free(name); + return hit; + } +} + +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; + string_list_append(path_list, xstrndup(data, len)); +} + +static void run_pager(struct grep_opt *opt, const char *prefix) +{ + struct string_list *path_list = opt->output_priv; + const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1)); + int i, status; + + for (i = 0; i < path_list->nr; i++) + argv[i] = path_list->items[i].string; + argv[path_list->nr] = NULL; + + if (prefix && chdir(prefix)) + die(_("Failed to chdir: %s"), prefix); + status = run_command_v_opt(argv, RUN_USING_SHELL); + if (status) + exit(status); + free(argv); +} + +static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached) +{ + int hit = 0; + int nr; + read_cache(); + + for (nr = 0; nr < active_nr; nr++) { + struct cache_entry *ce = active_cache[nr]; + if (!S_ISREG(ce->ce_mode)) + continue; + if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL)) + continue; + /* + * 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)) + continue; + hit |= grep_sha1(opt, ce->sha1, ce->name, 0); + } + else + hit |= grep_file(opt, ce->name); + if (ce_stage(ce)) { + do { + nr++; + } while (nr < active_nr && + !strcmp(ce->name, active_cache[nr]->name)); + nr--; /* compensate for loop control */ + } + if (hit && opt->status_only) + break; + } + return hit; +} + +static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, + struct tree_desc *tree, struct strbuf *base, int tn_len) +{ + int hit = 0, match = 0; + struct name_entry entry; + int old_baselen = base->len; + + while (tree_entry(tree, &entry)) { + int te_len = tree_entry_len(entry.path, entry.sha1); + + if (match != 2) { + match = tree_entry_interesting(&entry, base, tn_len, pathspec); + if (match < 0) + break; + if (match == 0) + continue; + } + + strbuf_add(base, entry.path, te_len); + + if (S_ISREG(entry.mode)) { + hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len); + } + else if (S_ISDIR(entry.mode)) { + enum object_type type; + struct tree_desc sub; + void *data; + unsigned long size; + + data = lock_and_read_sha1_file(entry.sha1, &type, &size); + if (!data) + die(_("unable to read tree (%s)"), + sha1_to_hex(entry.sha1)); + + strbuf_addch(base, '/'); + init_tree_desc(&sub, data, size); + hit |= grep_tree(opt, pathspec, &sub, base, tn_len); + free(data); + } + strbuf_setlen(base, old_baselen); + + if (hit && opt->status_only) + break; + } + return hit; +} + +static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, + struct object *obj, const char *name) +{ + if (obj->type == OBJ_BLOB) + return grep_sha1(opt, obj->sha1, name, 0); + if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { + struct tree_desc tree; + void *data; + unsigned long size; + struct strbuf base; + int hit, len; + + data = read_object_with_reference(obj->sha1, tree_type, + &size, NULL); + if (!data) + die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1)); + + len = name ? strlen(name) : 0; + strbuf_init(&base, PATH_MAX + len + 1); + if (len) { + strbuf_add(&base, name, len); + strbuf_addch(&base, ':'); + } + init_tree_desc(&tree, data, size); + hit = grep_tree(opt, pathspec, &tree, &base, base.len); + strbuf_release(&base); + free(data); + return hit; + } + die(_("unable to grep from object of type %s"), typename(obj->type)); +} + +static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec, + 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; + real_obj = deref_tag(list->objects[i].item, NULL, 0); + if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) { + hit = 1; + if (opt->status_only) + break; + } + } + return hit; +} + - static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec) ++static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, ++ int exc_std) +{ + struct dir_struct dir; + int i, hit = 0; + + memset(&dir, 0, sizeof(dir)); - setup_standard_excludes(&dir); ++ if (exc_std) ++ setup_standard_excludes(&dir); + + fill_directory(&dir, pathspec->raw); + for (i = 0; i < dir.nr; i++) { + const char *name = dir.entries[i]->name; + int namelen = strlen(name); + if (!match_pathspec_depth(pathspec, name, namelen, 0, NULL)) + continue; + hit |= grep_file(opt, dir.entries[i]->name); + if (hit && opt->status_only) + break; + } + return hit; +} + +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; + + 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; + int from_stdin = !strcmp(arg, "-"); + FILE *patterns; + int lno = 0; + struct strbuf sb = STRBUF_INIT; + + patterns = from_stdin ? stdin : fopen(arg, "r"); + if (!patterns) + die_errno(_("cannot open '%s'"), arg); + while (strbuf_getline(&sb, patterns, '\n') == 0) { + char *s; + size_t len; + + /* ignore empty line like grep does */ + if (sb.len == 0) + continue; + + s = strbuf_detach(&sb, &len); + append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN); + } + if (!from_stdin) + 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 cached = 0, untracked = 0, opt_exclude = -1; + int seen_dashdash = 0; + int external_grep_allowed__ignored; + const char *show_in_pager = NULL, *default_pager = "dummy"; + struct grep_opt opt; + struct object_array list = OBJECT_ARRAY_INIT; + const char **paths = NULL; + struct pathspec pathspec; + struct string_list path_list = STRING_LIST_INIT_NODUP; + int i; + int dummy; + int use_index = 1; + enum { + pattern_type_unspecified = 0, + pattern_type_bre, + pattern_type_ere, + pattern_type_fixed, + pattern_type_pcre, + }; + int pattern_type = pattern_type_unspecified; + + 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"), ++ { OPTION_BOOLEAN, 0, "index", &use_index, NULL, ++ "finds in contents not managed by git", ++ PARSE_OPT_NOARG | PARSE_OPT_NEGHELP }, ++ OPT_BOOLEAN(0, "untracked", &untracked, ++ "search in both tracked and untracked files"), ++ OPT_SET_INT(0, "exclude-standard", &opt_exclude, ++ "search also in ignored files", 1), + OPT_GROUP(""), + OPT_BOOLEAN('v', "invert-match", &opt.invert, + "show non-matching lines"), + 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, + "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), + { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth", + "descend at most levels", PARSE_OPT_NONEG, + NULL, 1 }, + OPT_GROUP(""), + OPT_SET_INT('E', "extended-regexp", &pattern_type, + "use extended POSIX regular expressions", + pattern_type_ere), + OPT_SET_INT('G', "basic-regexp", &pattern_type, + "use basic POSIX regular expressions (default)", + pattern_type_bre), + OPT_SET_INT('F', "fixed-strings", &pattern_type, + "interpret patterns as fixed strings", + pattern_type_fixed), + OPT_SET_INT('P', "perl-regexp", &pattern_type, + "use Perl-compatible regular expressions", + pattern_type_pcre), + OPT_GROUP(""), + OPT_BOOLEAN('n', "line-number", &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__COLOR(&opt.color, "highlight matches"), + OPT_BOOLEAN(0, "break", &opt.file_break, + "print empty line between matches from different files"), + OPT_BOOLEAN(0, "heading", &opt.heading, + "show filename only once above matches from same file"), + OPT_GROUP(""), + OPT_CALLBACK('C', "context", &opt, "n", + "show context lines before and after matches", + context_callback), + OPT_INTEGER('B', "before-context", &opt.pre_context, + "show context lines before matches"), + OPT_INTEGER('A', "after-context", &opt.post_context, + "show 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_BOOLEAN('W', "function-context", &opt.funcbody, + "show the surrounding function"), + OPT_GROUP(""), + OPT_CALLBACK('f', NULL, &opt, "file", + "read patterns from file", file_callback), + { OPTION_CALLBACK, 'e', NULL, &opt, "pattern", + "match ", 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__QUIET(&opt.status_only, + "indicate hit with exit status without output"), + OPT_BOOLEAN(0, "all-match", &opt.all_match, + "show only matches from files that match all patterns"), + OPT_GROUP(""), + { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager, + "pager", "show matching files in the pager", + PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager }, + 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() + }; + + /* + * 'git grep -h', unlike 'git grep -h ', 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; + opt.pattern_tail = &opt.pattern_list; + opt.header_tail = &opt.header_list; + opt.regflags = REG_NEWLINE; + opt.max_depth = -1; + + strcpy(opt.color_context, ""); + strcpy(opt.color_filename, ""); + strcpy(opt.color_function, ""); + strcpy(opt.color_lineno, ""); + strcpy(opt.color_match, GIT_COLOR_BOLD_RED); + strcpy(opt.color_selected, ""); + strcpy(opt.color_sep, GIT_COLOR_CYAN); + opt.color = -1; + git_config(grep_config, &opt); + + /* + * 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 + * unrecognized non option is the beginning of the refs list + * that continues up to the -- (if exists), and then paths. + */ + argc = parse_options(argc, argv, prefix, options, grep_usage, + PARSE_OPT_KEEP_DASHDASH | + PARSE_OPT_STOP_AT_NON_OPTION | + PARSE_OPT_NO_INTERNAL_HELP); + switch (pattern_type) { + case pattern_type_fixed: + opt.fixed = 1; + opt.pcre = 0; + break; + case pattern_type_bre: + opt.fixed = 0; + opt.pcre = 0; + opt.regflags &= ~REG_EXTENDED; + break; + case pattern_type_ere: + opt.fixed = 0; + opt.pcre = 0; + opt.regflags |= REG_EXTENDED; + break; + case pattern_type_pcre: + opt.fixed = 0; + opt.pcre = 1; + break; + default: + break; /* nothing */ + } + + if (use_index && !startup_info->have_repository) + /* die the same way as if we did it at the beginning */ + setup_git_directory(); + + /* + * 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--; + } + + /* 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 (show_in_pager == default_pager) + show_in_pager = git_pager(1); + if (show_in_pager) { + opt.color = 0; + opt.name_only = 1; + opt.null_following_name = 1; + opt.output_priv = &path_list; + opt.output = append_path; + string_list_append(&path_list, show_in_pager); + use_threads = 0; + } + + if (!opt.pattern_list) + die(_("no pattern given.")); + if (!opt.fixed && opt.ignore_case) + opt.regflags |= REG_ICASE; + +#ifndef NO_PTHREADS + if (online_cpus() == 1 || !grep_threads_ok(&opt)) + use_threads = 0; + + if (use_threads) { + if (opt.pre_context || opt.post_context || opt.file_break || + opt.funcbody) + skip_first_line = 1; + start_threads(&opt); + } +#else + use_threads = 0; +#endif + + compile_grep_patterns(&opt); + + /* Check revs and then paths */ + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + unsigned char sha1[20]; + /* Is it a rev? */ + if (!get_sha1(arg, sha1)) { + struct object *object = parse_object(sha1); + if (!object) + die(_("bad object %s"), arg); + add_object_array(object, arg, &list); + continue; + } + if (!strcmp(arg, "--")) { + i++; + seen_dashdash = 1; + } + break; + } + + /* The rest are paths */ + if (!seen_dashdash) { + int j; + for (j = i; j < argc; j++) + verify_filename(prefix, argv[j]); + } + + paths = get_pathspec(prefix, argv + i); + init_pathspec(&pathspec, paths); + pathspec.max_depth = opt.max_depth; + pathspec.recursive = 1; + + if (show_in_pager && (cached || list.nr)) + die(_("--open-files-in-pager only works on the worktree")); + + 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; + + if (!strcmp("less", pager) || !strcmp("vi", pager)) { + struct strbuf buf = STRBUF_INIT; + strbuf_addf(&buf, "+/%s%s", + strcmp("less", pager) ? "" : "*", + opt.pattern_list->pattern); + string_list_append(&path_list, buf.buf); + strbuf_detach(&buf, NULL); + } + } + + if (!show_in_pager) + setup_pager(); + ++ if (!use_index && (untracked || cached)) ++ die(_("--cached or --untracked cannot be used with --no-index.")); + - if (!use_index) { - if (cached) - die(_("--cached cannot be used with --no-index.")); ++ if (!use_index || untracked) { ++ int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude; + if (list.nr) - die(_("--no-index cannot be used with revs.")); - hit = grep_directory(&opt, &pathspec); ++ die(_("--no-index or --untracked cannot be used with revs.")); ++ hit = grep_directory(&opt, &pathspec, use_exclude); ++ } else if (0 <= opt_exclude) { ++ die(_("--exclude or --no-exclude cannot be used for tracked contents.")); + } else if (!list.nr) { + if (!cached) + setup_work_tree(); + + hit = grep_cache(&opt, &pathspec, cached); + } else { + if (cached) + die(_("both --cached and trees are given.")); + hit = grep_objects(&opt, &pathspec, &list); + } + + if (use_threads) + hit |= wait_all(); + if (hit && show_in_pager) + run_pager(&opt, prefix); + free_grep_patterns(&opt); + return !hit; +} diff --cc t/t7810-grep.sh index 0d600163c,000000000..81263b785 mode 100755,000000..100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@@ -1,828 -1,0 +1,860 @@@ +#!/bin/sh +# +# Copyright (c) 2006 Junio C Hamano +# + +test_description='git grep various. +' + +. ./test-lib.sh + +cat >hello.c < +int main(int argc, const char **argv) +{ + printf("Hello world.\n"); + return 0; + /* char ?? */ +} +EOF + +test_expect_success setup ' + { + echo foo mmap bar + echo foo_mmap bar + echo foo_mmap bar mmap + echo foo mmap bar_mmap + echo foo_mmap bar mmap baz + } >file && + { + echo Hello world + echo HeLLo world + echo Hello_world + echo HeLLo_world + } >hello_world && + { + echo "a+b*c" + echo "a+bc" + echo "abc" + } >ab && + echo vvv >v && + echo ww w >w && + echo x x xx x >x && + echo y yy >y && + echo zzz > z && + mkdir t && + echo test >t/t && + echo vvv >t/v && + mkdir t/a && + echo vvv >t/a/v && + git add . && + test_tick && + git commit -m initial +' + +test_expect_success 'grep should not segfault with a bad input' ' + test_must_fail git grep "(" +' + +for H in HEAD '' +do + case "$H" in + HEAD) HC='HEAD:' L='HEAD' ;; + '') HC= L='in working tree' ;; + esac + + test_expect_success "grep -w $L" ' + { + echo ${HC}file:1:foo mmap bar + echo ${HC}file:3:foo_mmap bar mmap + echo ${HC}file:4:foo mmap bar_mmap + echo ${HC}file:5:foo_mmap bar mmap baz + } >expected && + git -c grep.linenumber=false grep -n -w -e mmap $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L" ' + { + echo ${HC}file:1:foo mmap bar + echo ${HC}file:3:foo_mmap bar mmap + echo ${HC}file:4:foo mmap bar_mmap + echo ${HC}file:5:foo_mmap bar mmap baz + } >expected && + git -c grep.linenumber=true grep -w -e mmap $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L" ' + { + echo ${HC}file:foo mmap bar + echo ${HC}file:foo_mmap bar mmap + echo ${HC}file:foo mmap bar_mmap + echo ${HC}file:foo_mmap bar mmap baz + } >expected && + git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L (w)" ' + : >expected && + test_must_fail git grep -n -w -e "^w" >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L (x)" ' + { + echo ${HC}x:1:x x xx x + } >expected && + git grep -n -w -e "x xx* x" $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L (y-1)" ' + { + echo ${HC}y:1:y yy + } >expected && + git grep -n -w -e "^y" $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep -w $L (y-2)" ' + : >expected && + if git grep -n -w -e "^y y" $H >actual + then + echo should not have matched + cat actual + false + else + test_cmp expected actual + fi + ' + + test_expect_success "grep -w $L (z)" ' + : >expected && + if git grep -n -w -e "^z" $H >actual + then + echo should not have matched + cat actual + false + else + test_cmp expected actual + fi + ' + + test_expect_success "grep $L (t-1)" ' + echo "${HC}t/t:1:test" >expected && + git grep -n -e test $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L (t-2)" ' + echo "${HC}t:1:test" >expected && + ( + cd t && + git grep -n -e test $H + ) >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L (t-3)" ' + echo "${HC}t/t:1:test" >expected && + ( + cd t && + git grep --full-name -n -e test $H + ) >actual && + test_cmp expected actual + ' + + test_expect_success "grep -c $L (no /dev/null)" ' + ! git grep -c test $H | grep /dev/null + ' + + test_expect_success "grep --max-depth -1 $L" ' + { + echo ${HC}t/a/v:1:vvv + echo ${HC}t/v:1:vvv + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth -1 -n -e vvv $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 0 $L" ' + { + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth 0 -n -e vvv $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 0 -- '*' $L" ' + { + echo ${HC}t/a/v:1:vvv + echo ${HC}t/v:1:vvv + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth 0 -n -e vvv $H -- "*" >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 1 $L" ' + { + echo ${HC}t/v:1:vvv + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth 1 -n -e vvv $H >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 0 -- t $L" ' + { + echo ${HC}t/v:1:vvv + } >expected && + git grep --max-depth 0 -n -e vvv $H -- t >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 0 -- . t $L" ' + { + echo ${HC}t/v:1:vvv + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth 0 -n -e vvv $H -- . t >actual && + test_cmp expected actual + ' + + test_expect_success "grep --max-depth 0 -- t . $L" ' + { + echo ${HC}t/v:1:vvv + echo ${HC}v:1:vvv + } >expected && + git grep --max-depth 0 -n -e vvv $H -- t . >actual && + test_cmp expected actual + ' + test_expect_success "grep $L with grep.extendedRegexp=false" ' + echo "ab:a+bc" >expected && + git -c grep.extendedRegexp=false grep "a+b*c" ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp=true" ' + echo "ab:abc" >expected && + git -c grep.extendedRegexp=true grep "a+b*c" ab >actual && + test_cmp expected actual + ' +done + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +test_expect_success 'grep should ignore GREP_OPTIONS' ' + GREP_OPTIONS=-v git grep " mmap bar\$" >actual && + test_cmp expected actual +' + +test_expect_success 'grep -f, non-existent file' ' + test_must_fail git grep -f patterns +' + +cat >expected <pattern <actual && + test_cmp expected actual +' + +cat >expected <patterns <actual && + test_cmp expected actual +' + +cat >expected <patterns <actual && + test_cmp expected actual +' + +test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' ' + git grep -f - actual && + test_cmp expected actual +' + +cat >expected <empty && + git grep -q mmap >actual && + test_cmp empty actual && + test_must_fail git grep -q qfwfq >actual && + test_cmp empty actual +' + +# 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 hunk mark between files' ' + git grep -C1 "^[yz]" >actual && + test_cmp expected actual +' + +test_expect_success 'log grep setup' ' + echo a >>file && + test_tick && + GIT_AUTHOR_NAME="With * Asterisk" \ + GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \ + git commit -a -m "second" && + + echo a >>file && + test_tick && + git commit -a -m "third" && + + echo a >>file && + test_tick && + GIT_AUTHOR_NAME="Night Fall" \ + GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \ + git commit -a -m "fourth" +' + +test_expect_success 'log grep (1)' ' + git log --author=author --pretty=tformat:%s >actual && + ( echo third ; echo initial ) >expect && + test_cmp expect actual +' + +test_expect_success 'log grep (2)' ' + git log --author=" * " -F --pretty=tformat:%s >actual && + ( echo second ) >expect && + test_cmp expect actual +' + +test_expect_success 'log grep (3)' ' + git log --author="^A U" --pretty=tformat:%s >actual && + ( echo third ; echo initial ) >expect && + test_cmp expect actual +' + +test_expect_success 'log grep (4)' ' + git log --author="frotz\.com>$" --pretty=tformat:%s >actual && + ( echo second ) >expect && + test_cmp expect actual +' + +test_expect_success 'log grep (5)' ' + git log --author=Thor -F --pretty=tformat:%s >actual && + ( echo third ; echo initial ) >expect && + test_cmp expect actual +' + +test_expect_success 'log grep (6)' ' + git log --author=-0700 --pretty=tformat:%s >actual && + >expect && + test_cmp expect actual +' + +test_expect_success 'log --grep --author implicitly uses all-match' ' + # grep matches initial and second but not third + # author matches only initial and third + git log --author="A U Thor" --grep=s --grep=l --format=%s >actual && + echo initial >expect && + test_cmp expect actual +' + +test_expect_success 'log with multiple --author uses union' ' + git log --author="Thor" --author="Aster" --format=%s >actual && + { + echo third && echo second && echo initial + } >expect && + test_cmp expect actual +' + +test_expect_success 'log with --grep and multiple --author uses all-match' ' + git log --author="Thor" --author="Night" --grep=i --format=%s >actual && + { + echo third && echo initial + } >expect && + test_cmp expect actual +' + +test_expect_success 'log with --grep and multiple --author uses all-match' ' + git log --author="Thor" --author="Night" --grep=q --format=%s >actual && + >expect && + test_cmp expect actual +' + +test_expect_success 'grep with CE_VALID file' ' + git update-index --assume-unchanged t/t && + rm t/t && + test "$(git grep test)" = "t/t:test" && + git update-index --no-assume-unchanged t/t && + git checkout t/t +' + +cat >expected < +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 <actual && + test_cmp expected actual +' + +cat >expected < +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 +' + +cat >expected <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) + ) +' + +cat >expected <actual && + test_cmp expected actual +' + +test_expect_success 'outside of git repository' ' + rm -fr non && + mkdir -p non/git/sub && + echo hello >non/git/file1 && + echo world >non/git/sub/file2 && - echo ".*o*" >non/git/.gitignore && + { + echo file1:hello && + echo sub/file2:world + } >non/expect.full && + echo file2:world >non/expect.sub && + ( + GIT_CEILING_DIRECTORIES="$(pwd)/non/git" && + export GIT_CEILING_DIRECTORIES && + cd non/git && + test_must_fail git grep o && + git grep --no-index o >../actual.full && + test_cmp ../expect.full ../actual.full + cd sub && + test_must_fail git grep o && + git grep --no-index o >../../actual.sub && + test_cmp ../../expect.sub ../../actual.sub ++ ) && ++ ++ echo ".*o*" >non/git/.gitignore && ++ ( ++ GIT_CEILING_DIRECTORIES="$(pwd)/non/git" && ++ export GIT_CEILING_DIRECTORIES && ++ cd non/git && ++ test_must_fail git grep o && ++ git grep --no-index --exclude-standard o >../actual.full && ++ test_cmp ../expect.full ../actual.full && ++ ++ { ++ echo ".gitignore:.*o*" ++ cat ../expect.full ++ } >../expect.with.ignored && ++ git grep --no-index --no-exclude o >../actual.full && ++ test_cmp ../expect.with.ignored ../actual.full + ) +' + +test_expect_success 'inside git repository but with --no-index' ' + rm -fr is && + mkdir -p is/git/sub && + echo hello >is/git/file1 && + echo world >is/git/sub/file2 && + echo ".*o*" >is/git/.gitignore && + { + echo file1:hello && + echo sub/file2:world ++ } >is/expect.unignored && ++ { ++ echo ".gitignore:.*o*" && ++ cat is/expect.unignored + } >is/expect.full && + : >is/expect.empty && + echo file2:world >is/expect.sub && + ( + cd is/git && + git init && + test_must_fail git grep o >../actual.full && + test_cmp ../expect.empty ../actual.full && ++ ++ git grep --untracked o >../actual.unignored && ++ test_cmp ../expect.unignored ../actual.unignored && ++ + git grep --no-index o >../actual.full && + test_cmp ../expect.full ../actual.full && ++ ++ git grep --no-index --exclude-standard o >../actual.unignored && ++ test_cmp ../expect.unignored ../actual.unignored && ++ + cd sub && + test_must_fail git grep o >../../actual.sub && + test_cmp ../../expect.empty ../../actual.sub && ++ + git grep --no-index o >../../actual.sub && ++ test_cmp ../../expect.sub ../../actual.sub && ++ ++ git grep --untracked o >../../actual.sub && + test_cmp ../../expect.sub ../../actual.sub + ) +' + +test_expect_success 'setup double-dash tests' ' +cat >double-dash < +other +EOF +git add double-dash +' + +cat >expected < +EOF +test_expect_success 'grep -- pattern' ' + git grep -- "->" >actual && + test_cmp expected actual +' +test_expect_success 'grep -- pattern -- pathspec' ' + git grep -- "->" -- double-dash >actual && + test_cmp expected actual +' +test_expect_success 'grep -e pattern -- path' ' + git grep -e "->" -- double-dash >actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P pattern' ' + git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp expected actual +' + +test_expect_success 'grep pattern with grep.extendedRegexp=true' ' + >empty && + test_must_fail git -c grep.extendedregexp=true \ + grep "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp empty actual +' + +test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' ' + git -c grep.extendedregexp=true \ + grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -v pattern' ' + { + echo "ab:a+b*c" + echo "ab:a+bc" + } >expected && + git grep -P -v "abc" ab >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -i pattern' ' + cat >expected <<-EOF && + hello.c: printf("Hello world.\n"); + EOF + git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -w pattern' ' + { + echo "hello_world:Hello world" + echo "hello_world:HeLLo world" + } >expected && + git grep -P -w "He((?i)ll)o" hello_world >actual && + test_cmp expected actual +' + +test_expect_success 'grep -G invalidpattern properly dies ' ' + test_must_fail git grep -G "a[" +' + +test_expect_success 'grep -E invalidpattern properly dies ' ' + test_must_fail git grep -E "a[" +' + +test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' ' + test_must_fail git grep -P "a[" +' + +test_expect_success 'grep -G -E -F pattern' ' + echo "ab:a+b*c" >expected && + git grep -G -E -F "a+b*c" ab >actual && + test_cmp expected actual +' + +test_expect_success 'grep -E -F -G pattern' ' + echo "ab:a+bc" >expected && + git grep -E -F -G "a+b*c" ab >actual && + test_cmp expected actual +' + +test_expect_success 'grep -F -G -E pattern' ' + echo "ab:abc" >expected && + git grep -F -G -E "a+b*c" ab >actual && + test_cmp expected actual +' + +test_expect_success 'grep -G -F -P -E pattern' ' + >empty && + test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual && + test_cmp empty actual +' + +test_expect_success LIBPCRE 'grep -G -F -E -P pattern' ' + echo "ab:a+b*c" >expected && + git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual && + test_cmp expected actual +' + +test_config() { + git config "$1" "$2" && + test_when_finished "git config --unset $1" +} + +cat >expected <:int main(int argc, const char **argv) +hello.c-{ +-- +hello.c: /* char ?? */ +hello.c-} +-- +hello_world:Hello_world +hello_world-HeLLo_world +EOF + +test_expect_success 'grep --color, separator' ' + test_config color.grep.context normal && + test_config color.grep.filename normal && + test_config color.grep.function normal && + test_config color.grep.linenumber normal && + test_config color.grep.match normal && + test_config color.grep.selected normal && + test_config color.grep.separator red && + + git grep --color=always -A1 -e char -e lo_w hello.c hello_world | + test_decode_color >actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <hello.c +2:int main(int argc, const char **argv) +6: /* char ?? */ + +hello_world +3:Hello_world +EOF + +test_expect_success 'mimic ack-grep --group' ' + test_config color.grep.context normal && + test_config color.grep.filename "bold green" && + test_config color.grep.function normal && + test_config color.grep.linenumber normal && + test_config color.grep.match "black yellow" && + test_config color.grep.selected normal && + test_config color.grep.separator normal && + + git grep --break --heading -n --color \ + -e char -e lo_w hello.c hello_world | + test_decode_color >actual && + test_cmp expected actual +' + +test_done