Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git grep" |
| 3 | * |
| 4 | * Copyright (c) 2006 Junio C Hamano |
| 5 | */ |
| 6 | #include "cache.h" |
| 7 | #include "blob.h" |
| 8 | #include "tree.h" |
| 9 | #include "commit.h" |
| 10 | #include "tag.h" |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 11 | #include "tree-walk.h" |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 12 | #include "builtin.h" |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 13 | #include "parse-options.h" |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 14 | #include "userdiff.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 15 | #include "grep.h" |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 16 | #include "quote.h" |
Junio C Hamano | 3081623 | 2010-01-15 12:52:40 -0800 | [diff] [blame] | 17 | #include "dir.h" |
Jeff King | 5f7c643 | 2008-03-12 17:39:16 -0400 | [diff] [blame] | 18 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 19 | static char const * const grep_usage[] = { |
| 20 | "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]", |
| 21 | NULL |
| 22 | }; |
| 23 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 24 | static int grep_config(const char *var, const char *value, void *cb) |
| 25 | { |
| 26 | struct grep_opt *opt = cb; |
| 27 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 28 | switch (userdiff_config(var, value)) { |
| 29 | case 0: break; |
| 30 | case -1: return -1; |
| 31 | default: return 0; |
| 32 | } |
| 33 | |
Markus Heidelberg | fe3420b | 2009-04-21 00:58:15 +0200 | [diff] [blame] | 34 | if (!strcmp(var, "color.grep")) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 35 | opt->color = git_config_colorbool(var, value, -1); |
| 36 | return 0; |
| 37 | } |
Markus Heidelberg | fe3420b | 2009-04-21 00:58:15 +0200 | [diff] [blame] | 38 | if (!strcmp(var, "color.grep.match")) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 39 | if (!value) |
| 40 | return config_error_nonbool(var); |
| 41 | color_parse(value, var, opt->color_match); |
| 42 | return 0; |
| 43 | } |
| 44 | return git_color_default_config(var, value, cb); |
| 45 | } |
| 46 | |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 47 | /* |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 48 | * Return non-zero if max_depth is negative or path has no more then max_depth |
| 49 | * slashes. |
| 50 | */ |
| 51 | static int accept_subdir(const char *path, int max_depth) |
| 52 | { |
| 53 | if (max_depth < 0) |
| 54 | return 1; |
| 55 | |
| 56 | while ((path = strchr(path, '/')) != NULL) { |
| 57 | max_depth--; |
| 58 | if (max_depth < 0) |
| 59 | return 0; |
| 60 | path++; |
| 61 | } |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Return non-zero if name is a subdirectory of match and is not too deep. |
| 67 | */ |
| 68 | static int is_subdir(const char *name, int namelen, |
| 69 | const char *match, int matchlen, int max_depth) |
| 70 | { |
| 71 | if (matchlen > namelen || strncmp(name, match, matchlen)) |
| 72 | return 0; |
| 73 | |
| 74 | if (name[matchlen] == '\0') /* exact match */ |
| 75 | return 1; |
| 76 | |
| 77 | if (!matchlen || match[matchlen-1] == '/' || name[matchlen] == '/') |
| 78 | return accept_subdir(name + matchlen + 1, max_depth); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 84 | * git grep pathspecs are somewhat different from diff-tree pathspecs; |
| 85 | * pathname wildcards are allowed. |
| 86 | */ |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 87 | static int pathspec_matches(const char **paths, const char *name, int max_depth) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 88 | { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 89 | int namelen, i; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 90 | if (!paths || !*paths) |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 91 | return accept_subdir(name, max_depth); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 92 | namelen = strlen(name); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 93 | for (i = 0; paths[i]; i++) { |
| 94 | const char *match = paths[i]; |
| 95 | int matchlen = strlen(match); |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 96 | const char *cp, *meta; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 97 | |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 98 | if (is_subdir(name, namelen, match, matchlen, max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 99 | return 1; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 100 | if (!fnmatch(match, name, 0)) |
| 101 | return 1; |
| 102 | if (name[namelen-1] != '/') |
| 103 | continue; |
| 104 | |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 105 | /* We are being asked if the directory ("name") is worth |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 106 | * descending into. |
| 107 | * |
| 108 | * Find the longest leading directory name that does |
| 109 | * not have metacharacter in the pathspec; the name |
| 110 | * we are looking at must overlap with that directory. |
| 111 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 112 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 113 | char ch = *cp; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 114 | if (ch == '*' || ch == '[' || ch == '?') { |
| 115 | meta = cp; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 116 | break; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 117 | } |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 118 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 119 | if (!meta) |
| 120 | meta = cp; /* fully literal */ |
| 121 | |
| 122 | if (namelen <= meta - match) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 123 | /* Looking at "Documentation/" and |
| 124 | * the pattern says "Documentation/howto/", or |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 125 | * "Documentation/diff*.txt". The name we |
| 126 | * have should match prefix. |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 127 | */ |
| 128 | if (!memcmp(match, name, namelen)) |
| 129 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 130 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 131 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 132 | |
| 133 | if (meta - match < namelen) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 134 | /* Looking at "Documentation/howto/" and |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 135 | * the pattern says "Documentation/h*"; |
| 136 | * match up to "Do.../h"; this avoids descending |
| 137 | * into "Documentation/technical/". |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 138 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 139 | if (!memcmp(match, name, meta - match)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 140 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 141 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 142 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 147 | static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 148 | { |
| 149 | unsigned long size; |
| 150 | char *data; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 151 | enum object_type type; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 152 | int hit; |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 153 | struct strbuf pathbuf = STRBUF_INIT; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 154 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 155 | data = read_sha1_file(sha1, &type, &size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 156 | if (!data) { |
| 157 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); |
| 158 | return 0; |
| 159 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 160 | if (opt->relative && opt->prefix_length) { |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 161 | quote_path_relative(name + tree_name_len, -1, &pathbuf, opt->prefix); |
| 162 | strbuf_insert(&pathbuf, 0, name, tree_name_len); |
| 163 | name = pathbuf.buf; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 164 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 165 | hit = grep_buffer(opt, name, data, size); |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 166 | strbuf_release(&pathbuf); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 167 | free(data); |
| 168 | return hit; |
| 169 | } |
| 170 | |
| 171 | static int grep_file(struct grep_opt *opt, const char *filename) |
| 172 | { |
| 173 | struct stat st; |
| 174 | int i; |
| 175 | char *data; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 176 | size_t sz; |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 177 | struct strbuf buf = STRBUF_INIT; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 178 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 179 | if (lstat(filename, &st) < 0) { |
| 180 | err_ret: |
| 181 | if (errno != ENOENT) |
| 182 | error("'%s': %s", filename, strerror(errno)); |
| 183 | return 0; |
| 184 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 185 | if (!S_ISREG(st.st_mode)) |
| 186 | return 0; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 187 | sz = xsize_t(st.st_size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 188 | i = open(filename, O_RDONLY); |
| 189 | if (i < 0) |
| 190 | goto err_ret; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 191 | data = xmalloc(sz + 1); |
| 192 | if (st.st_size != read_in_full(i, data, sz)) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 193 | error("'%s': short read %s", filename, strerror(errno)); |
| 194 | close(i); |
| 195 | free(data); |
| 196 | return 0; |
| 197 | } |
| 198 | close(i); |
Jim Meyering | 34f3999 | 2010-01-18 22:55:07 +0100 | [diff] [blame] | 199 | data[sz] = 0; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 200 | if (opt->relative && opt->prefix_length) |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 201 | filename = quote_path_relative(filename, -1, &buf, opt->prefix); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 202 | i = grep_buffer(opt, filename, data, sz); |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 203 | strbuf_release(&buf); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 204 | free(data); |
| 205 | return i; |
| 206 | } |
| 207 | |
Junio C Hamano | bbc09c2 | 2010-01-12 19:06:41 -0800 | [diff] [blame] | 208 | static int grep_cache(struct grep_opt *opt, const char **paths, int cached) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 209 | { |
| 210 | int hit = 0; |
| 211 | int nr; |
| 212 | read_cache(); |
| 213 | |
| 214 | for (nr = 0; nr < active_nr; nr++) { |
| 215 | struct cache_entry *ce = active_cache[nr]; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 216 | if (!S_ISREG(ce->ce_mode)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 217 | continue; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 218 | if (!pathspec_matches(paths, ce->name, opt->max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 219 | continue; |
Nguyễn Thái Ngọc Duy | 57d4346 | 2008-12-27 15:21:03 +0700 | [diff] [blame] | 220 | /* |
| 221 | * If CE_VALID is on, we assume worktree file and its cache entry |
| 222 | * are identical, even if worktree file has been modified, so use |
| 223 | * cache version instead |
| 224 | */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 225 | if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) { |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 226 | if (ce_stage(ce)) |
| 227 | continue; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 228 | hit |= grep_sha1(opt, ce->sha1, ce->name, 0); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 229 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 230 | else |
| 231 | hit |= grep_file(opt, ce->name); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 232 | if (ce_stage(ce)) { |
| 233 | do { |
| 234 | nr++; |
| 235 | } while (nr < active_nr && |
| 236 | !strcmp(ce->name, active_cache[nr]->name)); |
| 237 | nr--; /* compensate for loop control */ |
| 238 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 239 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 240 | free_grep_patterns(opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 241 | return hit; |
| 242 | } |
| 243 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 244 | static int grep_tree(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 245 | struct tree_desc *tree, |
| 246 | const char *tree_name, const char *base) |
| 247 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 248 | int len; |
| 249 | int hit = 0; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 250 | struct name_entry entry; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 251 | char *down; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 252 | int tn_len = strlen(tree_name); |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 253 | struct strbuf pathbuf; |
| 254 | |
| 255 | strbuf_init(&pathbuf, PATH_MAX + tn_len); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 256 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 257 | if (tn_len) { |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 258 | strbuf_add(&pathbuf, tree_name, tn_len); |
| 259 | strbuf_addch(&pathbuf, ':'); |
| 260 | tn_len = pathbuf.len; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 261 | } |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 262 | strbuf_addstr(&pathbuf, base); |
| 263 | len = pathbuf.len; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 264 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 265 | while (tree_entry(tree, &entry)) { |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 266 | int te_len = tree_entry_len(entry.path, entry.sha1); |
| 267 | pathbuf.len = len; |
| 268 | strbuf_add(&pathbuf, entry.path, te_len); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 269 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 270 | if (S_ISDIR(entry.mode)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 271 | /* Match "abc/" against pathspec to |
| 272 | * decide if we want to descend into "abc" |
| 273 | * directory. |
| 274 | */ |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 275 | strbuf_addch(&pathbuf, '/'); |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 276 | |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 277 | down = pathbuf.buf + tn_len; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 278 | if (!pathspec_matches(paths, down, opt->max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 279 | ; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 280 | else if (S_ISREG(entry.mode)) |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 281 | hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 282 | else if (S_ISDIR(entry.mode)) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 283 | enum object_type type; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 284 | struct tree_desc sub; |
| 285 | void *data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 286 | unsigned long size; |
| 287 | |
| 288 | data = read_sha1_file(entry.sha1, &type, &size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 289 | if (!data) |
| 290 | die("unable to read tree (%s)", |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 291 | sha1_to_hex(entry.sha1)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 292 | init_tree_desc(&sub, data, size); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 293 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 294 | free(data); |
| 295 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 296 | } |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 297 | strbuf_release(&pathbuf); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 298 | return hit; |
| 299 | } |
| 300 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 301 | static int grep_object(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 302 | struct object *obj, const char *name) |
| 303 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 304 | if (obj->type == OBJ_BLOB) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 305 | return grep_sha1(opt, obj->sha1, name, 0); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 306 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 307 | struct tree_desc tree; |
| 308 | void *data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 309 | unsigned long size; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 310 | int hit; |
| 311 | data = read_object_with_reference(obj->sha1, tree_type, |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 312 | &size, NULL); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 313 | if (!data) |
| 314 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 315 | init_tree_desc(&tree, data, size); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 316 | hit = grep_tree(opt, paths, &tree, name, ""); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 317 | free(data); |
| 318 | return hit; |
| 319 | } |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 320 | die("unable to grep from object of type %s", typename(obj->type)); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Junio C Hamano | 3081623 | 2010-01-15 12:52:40 -0800 | [diff] [blame] | 323 | static int grep_directory(struct grep_opt *opt, const char **paths) |
| 324 | { |
| 325 | struct dir_struct dir; |
| 326 | int i, hit = 0; |
| 327 | |
| 328 | memset(&dir, 0, sizeof(dir)); |
| 329 | setup_standard_excludes(&dir); |
| 330 | |
| 331 | fill_directory(&dir, paths); |
| 332 | for (i = 0; i < dir.nr; i++) |
| 333 | hit |= grep_file(opt, dir.entries[i]->name); |
| 334 | free_grep_patterns(opt); |
| 335 | return hit; |
| 336 | } |
| 337 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 338 | static int context_callback(const struct option *opt, const char *arg, |
| 339 | int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 340 | { |
| 341 | struct grep_opt *grep_opt = opt->value; |
| 342 | int value; |
| 343 | const char *endp; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 344 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 345 | if (unset) { |
| 346 | grep_opt->pre_context = grep_opt->post_context = 0; |
| 347 | return 0; |
| 348 | } |
| 349 | value = strtol(arg, (char **)&endp, 10); |
| 350 | if (*endp) { |
| 351 | return error("switch `%c' expects a numerical value", |
| 352 | opt->short_name); |
| 353 | } |
| 354 | grep_opt->pre_context = grep_opt->post_context = value; |
| 355 | return 0; |
| 356 | } |
| 357 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 358 | static int file_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 359 | { |
| 360 | struct grep_opt *grep_opt = opt->value; |
| 361 | FILE *patterns; |
| 362 | int lno = 0; |
Matt Kraai | cfe370c | 2009-10-16 07:13:25 -0700 | [diff] [blame] | 363 | struct strbuf sb = STRBUF_INIT; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 364 | |
| 365 | patterns = fopen(arg, "r"); |
| 366 | if (!patterns) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 367 | die_errno("cannot open '%s'", arg); |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 368 | while (strbuf_getline(&sb, patterns, '\n') == 0) { |
| 369 | /* ignore empty line like grep does */ |
| 370 | if (sb.len == 0) |
| 371 | continue; |
| 372 | append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg, |
| 373 | ++lno, GREP_PATTERN); |
| 374 | } |
| 375 | fclose(patterns); |
| 376 | strbuf_release(&sb); |
| 377 | return 0; |
| 378 | } |
| 379 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 380 | static int not_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 381 | { |
| 382 | struct grep_opt *grep_opt = opt->value; |
| 383 | append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); |
| 384 | return 0; |
| 385 | } |
| 386 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 387 | static int and_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 388 | { |
| 389 | struct grep_opt *grep_opt = opt->value; |
| 390 | append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); |
| 391 | return 0; |
| 392 | } |
| 393 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 394 | static int open_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 395 | { |
| 396 | struct grep_opt *grep_opt = opt->value; |
| 397 | append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); |
| 398 | return 0; |
| 399 | } |
| 400 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 401 | static int close_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 402 | { |
| 403 | struct grep_opt *grep_opt = opt->value; |
| 404 | append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); |
| 405 | return 0; |
| 406 | } |
| 407 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 408 | static int pattern_callback(const struct option *opt, const char *arg, |
| 409 | int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 410 | { |
| 411 | struct grep_opt *grep_opt = opt->value; |
| 412 | append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); |
| 413 | return 0; |
| 414 | } |
| 415 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 416 | static int help_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 417 | { |
| 418 | return -1; |
| 419 | } |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 420 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 421 | int cmd_grep(int argc, const char **argv, const char *prefix) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 422 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 423 | int hit = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 424 | int cached = 0; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 425 | int seen_dashdash = 0; |
Junio C Hamano | bbc09c2 | 2010-01-12 19:06:41 -0800 | [diff] [blame] | 426 | int external_grep_allowed__ignored; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 427 | struct grep_opt opt; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 428 | struct object_array list = { 0, 0, NULL }; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 429 | const char **paths = NULL; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 430 | int i; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 431 | int dummy; |
Junio C Hamano | 7e62265 | 2010-01-15 12:50:54 -0800 | [diff] [blame] | 432 | int nongit = 0, use_index = 1; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 433 | struct option options[] = { |
| 434 | OPT_BOOLEAN(0, "cached", &cached, |
| 435 | "search in index instead of in the work tree"), |
Junio C Hamano | 3081623 | 2010-01-15 12:52:40 -0800 | [diff] [blame] | 436 | OPT_BOOLEAN(0, "index", &use_index, |
| 437 | "--no-index finds in contents not managed by git"), |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 438 | OPT_GROUP(""), |
| 439 | OPT_BOOLEAN('v', "invert-match", &opt.invert, |
| 440 | "show non-matching lines"), |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 441 | OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case, |
| 442 | "case insensitive matching"), |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 443 | OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp, |
| 444 | "match patterns only at word boundaries"), |
| 445 | OPT_SET_INT('a', "text", &opt.binary, |
| 446 | "process binary files as text", GREP_BINARY_TEXT), |
| 447 | OPT_SET_INT('I', NULL, &opt.binary, |
| 448 | "don't match patterns in binary files", |
| 449 | GREP_BINARY_NOMATCH), |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 450 | { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth", |
| 451 | "descend at most <depth> levels", PARSE_OPT_NONEG, |
| 452 | NULL, 1 }, |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 453 | OPT_GROUP(""), |
| 454 | OPT_BIT('E', "extended-regexp", &opt.regflags, |
| 455 | "use extended POSIX regular expressions", REG_EXTENDED), |
| 456 | OPT_NEGBIT('G', "basic-regexp", &opt.regflags, |
| 457 | "use basic POSIX regular expressions (default)", |
| 458 | REG_EXTENDED), |
| 459 | OPT_BOOLEAN('F', "fixed-strings", &opt.fixed, |
| 460 | "interpret patterns as fixed strings"), |
| 461 | OPT_GROUP(""), |
| 462 | OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"), |
| 463 | OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1), |
| 464 | OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1), |
| 465 | OPT_NEGBIT(0, "full-name", &opt.relative, |
| 466 | "show filenames relative to top directory", 1), |
| 467 | OPT_BOOLEAN('l', "files-with-matches", &opt.name_only, |
| 468 | "show only filenames instead of matching lines"), |
| 469 | OPT_BOOLEAN(0, "name-only", &opt.name_only, |
| 470 | "synonym for --files-with-matches"), |
| 471 | OPT_BOOLEAN('L', "files-without-match", |
| 472 | &opt.unmatch_name_only, |
| 473 | "show only the names of files without match"), |
| 474 | OPT_BOOLEAN('z', "null", &opt.null_following_name, |
| 475 | "print NUL after filenames"), |
| 476 | OPT_BOOLEAN('c', "count", &opt.count, |
| 477 | "show the number of matches instead of matching lines"), |
| 478 | OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1), |
| 479 | OPT_GROUP(""), |
| 480 | OPT_CALLBACK('C', NULL, &opt, "n", |
| 481 | "show <n> context lines before and after matches", |
| 482 | context_callback), |
| 483 | OPT_INTEGER('B', NULL, &opt.pre_context, |
| 484 | "show <n> context lines before matches"), |
| 485 | OPT_INTEGER('A', NULL, &opt.post_context, |
| 486 | "show <n> context lines after matches"), |
| 487 | OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM", |
| 488 | context_callback), |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 489 | OPT_BOOLEAN('p', "show-function", &opt.funcname, |
| 490 | "show a line with the function name before matches"), |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 491 | OPT_GROUP(""), |
| 492 | OPT_CALLBACK('f', NULL, &opt, "file", |
| 493 | "read patterns from file", file_callback), |
| 494 | { OPTION_CALLBACK, 'e', NULL, &opt, "pattern", |
| 495 | "match <pattern>", PARSE_OPT_NONEG, pattern_callback }, |
| 496 | { OPTION_CALLBACK, 0, "and", &opt, NULL, |
| 497 | "combine patterns specified with -e", |
| 498 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback }, |
| 499 | OPT_BOOLEAN(0, "or", &dummy, ""), |
| 500 | { OPTION_CALLBACK, 0, "not", &opt, NULL, "", |
| 501 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback }, |
| 502 | { OPTION_CALLBACK, '(', NULL, &opt, NULL, "", |
| 503 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, |
| 504 | open_callback }, |
| 505 | { OPTION_CALLBACK, ')', NULL, &opt, NULL, "", |
| 506 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, |
| 507 | close_callback }, |
| 508 | OPT_BOOLEAN(0, "all-match", &opt.all_match, |
| 509 | "show only matches from files that match all patterns"), |
| 510 | OPT_GROUP(""), |
Junio C Hamano | bbc09c2 | 2010-01-12 19:06:41 -0800 | [diff] [blame] | 511 | OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored, |
| 512 | "allow calling of grep(1) (ignored by this build)"), |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 513 | { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage", |
| 514 | PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback }, |
| 515 | OPT_END() |
| 516 | }; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 517 | |
Junio C Hamano | 7e62265 | 2010-01-15 12:50:54 -0800 | [diff] [blame] | 518 | prefix = setup_git_directory_gently(&nongit); |
| 519 | |
Jonathan Nieder | 9c855c3 | 2009-11-09 09:04:42 -0600 | [diff] [blame] | 520 | /* |
| 521 | * 'git grep -h', unlike 'git grep -h <pattern>', is a request |
| 522 | * to show usage information and exit. |
| 523 | */ |
| 524 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 525 | usage_with_options(grep_usage, options); |
| 526 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 527 | memset(&opt, 0, sizeof(opt)); |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 528 | opt.prefix = prefix; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 529 | opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; |
| 530 | opt.relative = 1; |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 531 | opt.pathname = 1; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 532 | opt.pattern_tail = &opt.pattern_list; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 533 | opt.regflags = REG_NEWLINE; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 534 | opt.max_depth = -1; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 535 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 536 | strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD); |
| 537 | opt.color = -1; |
| 538 | git_config(grep_config, &opt); |
| 539 | if (opt.color == -1) |
| 540 | opt.color = git_use_color_default; |
| 541 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 542 | /* |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 543 | * If there is no -- then the paths must exist in the working |
| 544 | * tree. If there is no explicit pattern specified with -e or |
| 545 | * -f, we take the first unrecognized non option to be the |
| 546 | * pattern, but then what follows it must be zero or more |
| 547 | * valid refs up to the -- (if exists), and then existing |
| 548 | * paths. If there is an explicit pattern, then the first |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 549 | * unrecognized non option is the beginning of the refs list |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 550 | * that continues up to the -- (if exists), and then paths. |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 551 | */ |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 552 | argc = parse_options(argc, argv, prefix, options, grep_usage, |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 553 | PARSE_OPT_KEEP_DASHDASH | |
| 554 | PARSE_OPT_STOP_AT_NON_OPTION | |
| 555 | PARSE_OPT_NO_INTERNAL_HELP); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 556 | |
Junio C Hamano | 7e62265 | 2010-01-15 12:50:54 -0800 | [diff] [blame] | 557 | if (use_index && nongit) |
| 558 | /* die the same way as if we did it at the beginning */ |
| 559 | setup_git_directory(); |
| 560 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 561 | /* First unrecognized non-option token */ |
| 562 | if (argc > 0 && !opt.pattern_list) { |
| 563 | append_grep_pattern(&opt, argv[0], "command line", 0, |
| 564 | GREP_PATTERN); |
| 565 | argv++; |
| 566 | argc--; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 567 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 568 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 569 | if (!opt.pattern_list) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 570 | die("no pattern given."); |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 571 | if (!opt.fixed && opt.ignore_case) |
| 572 | opt.regflags |= REG_ICASE; |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 573 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
| 574 | die("cannot mix --fixed-strings and regexp"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 575 | compile_grep_patterns(&opt); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 576 | |
| 577 | /* Check revs and then paths */ |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 578 | for (i = 0; i < argc; i++) { |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 579 | const char *arg = argv[i]; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 580 | unsigned char sha1[20]; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 581 | /* Is it a rev? */ |
| 582 | if (!get_sha1(arg, sha1)) { |
| 583 | struct object *object = parse_object(sha1); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 584 | if (!object) |
| 585 | die("bad object %s", arg); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 586 | add_object_array(object, arg, &list); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 587 | continue; |
| 588 | } |
| 589 | if (!strcmp(arg, "--")) { |
| 590 | i++; |
| 591 | seen_dashdash = 1; |
| 592 | } |
| 593 | break; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 594 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 595 | |
| 596 | /* The rest are paths */ |
| 597 | if (!seen_dashdash) { |
| 598 | int j; |
Junio C Hamano | c39c4f4 | 2006-05-09 18:15:21 -0700 | [diff] [blame] | 599 | for (j = i; j < argc; j++) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 600 | verify_filename(prefix, argv[j]); |
| 601 | } |
| 602 | |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 603 | if (i < argc) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 604 | paths = get_pathspec(prefix, argv + i); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 605 | else if (prefix) { |
| 606 | paths = xcalloc(2, sizeof(const char *)); |
| 607 | paths[0] = prefix; |
| 608 | paths[1] = NULL; |
| 609 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 610 | |
Junio C Hamano | 3081623 | 2010-01-15 12:52:40 -0800 | [diff] [blame] | 611 | if (!use_index) { |
| 612 | if (cached) |
| 613 | die("--cached cannot be used with --no-index."); |
| 614 | if (list.nr) |
| 615 | die("--no-index cannot be used with revs."); |
| 616 | return !grep_directory(&opt, paths); |
| 617 | } |
| 618 | |
Nguyễn Thái Ngọc Duy | 6577f54 | 2008-08-28 20:04:30 +0700 | [diff] [blame] | 619 | if (!list.nr) { |
| 620 | if (!cached) |
| 621 | setup_work_tree(); |
Junio C Hamano | bbc09c2 | 2010-01-12 19:06:41 -0800 | [diff] [blame] | 622 | return !grep_cache(&opt, paths, cached); |
Nguyễn Thái Ngọc Duy | 6577f54 | 2008-08-28 20:04:30 +0700 | [diff] [blame] | 623 | } |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 624 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 625 | if (cached) |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 626 | die("both --cached and trees are given."); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 627 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 628 | for (i = 0; i < list.nr; i++) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 629 | struct object *real_obj; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 630 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
| 631 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 632 | hit = 1; |
| 633 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 634 | free_grep_patterns(&opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 635 | return !hit; |
| 636 | } |