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" |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 16 | |
Jeff King | 5f7c643 | 2008-03-12 17:39:16 -0400 | [diff] [blame] | 17 | #ifndef NO_EXTERNAL_GREP |
| 18 | #ifdef __unix__ |
| 19 | #define NO_EXTERNAL_GREP 0 |
| 20 | #else |
| 21 | #define NO_EXTERNAL_GREP 1 |
| 22 | #endif |
| 23 | #endif |
| 24 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 25 | static char const * const grep_usage[] = { |
| 26 | "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]", |
| 27 | NULL |
| 28 | }; |
| 29 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 30 | static int grep_config(const char *var, const char *value, void *cb) |
| 31 | { |
| 32 | struct grep_opt *opt = cb; |
| 33 | |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 34 | switch (userdiff_config(var, value)) { |
| 35 | case 0: break; |
| 36 | case -1: return -1; |
| 37 | default: return 0; |
| 38 | } |
| 39 | |
Markus Heidelberg | fe3420b | 2009-04-21 00:58:15 +0200 | [diff] [blame] | 40 | if (!strcmp(var, "color.grep")) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 41 | opt->color = git_config_colorbool(var, value, -1); |
| 42 | return 0; |
| 43 | } |
Markus Heidelberg | fe3420b | 2009-04-21 00:58:15 +0200 | [diff] [blame] | 44 | if (!strcmp(var, "color.grep.external")) |
René Scharfe | a94982e | 2009-03-07 13:34:46 +0100 | [diff] [blame] | 45 | return git_config_string(&(opt->color_external), var, value); |
Markus Heidelberg | fe3420b | 2009-04-21 00:58:15 +0200 | [diff] [blame] | 46 | if (!strcmp(var, "color.grep.match")) { |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 47 | if (!value) |
| 48 | return config_error_nonbool(var); |
| 49 | color_parse(value, var, opt->color_match); |
| 50 | return 0; |
| 51 | } |
| 52 | return git_color_default_config(var, value, cb); |
| 53 | } |
| 54 | |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 55 | /* |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 56 | * Return non-zero if max_depth is negative or path has no more then max_depth |
| 57 | * slashes. |
| 58 | */ |
| 59 | static int accept_subdir(const char *path, int max_depth) |
| 60 | { |
| 61 | if (max_depth < 0) |
| 62 | return 1; |
| 63 | |
| 64 | while ((path = strchr(path, '/')) != NULL) { |
| 65 | max_depth--; |
| 66 | if (max_depth < 0) |
| 67 | return 0; |
| 68 | path++; |
| 69 | } |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Return non-zero if name is a subdirectory of match and is not too deep. |
| 75 | */ |
| 76 | static int is_subdir(const char *name, int namelen, |
| 77 | const char *match, int matchlen, int max_depth) |
| 78 | { |
| 79 | if (matchlen > namelen || strncmp(name, match, matchlen)) |
| 80 | return 0; |
| 81 | |
| 82 | if (name[matchlen] == '\0') /* exact match */ |
| 83 | return 1; |
| 84 | |
| 85 | if (!matchlen || match[matchlen-1] == '/' || name[matchlen] == '/') |
| 86 | return accept_subdir(name + matchlen + 1, max_depth); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 92 | * git grep pathspecs are somewhat different from diff-tree pathspecs; |
| 93 | * pathname wildcards are allowed. |
| 94 | */ |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 95 | 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] | 96 | { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 97 | int namelen, i; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 98 | if (!paths || !*paths) |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 99 | return accept_subdir(name, max_depth); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 100 | namelen = strlen(name); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 101 | for (i = 0; paths[i]; i++) { |
| 102 | const char *match = paths[i]; |
| 103 | int matchlen = strlen(match); |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 104 | const char *cp, *meta; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 105 | |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 106 | if (is_subdir(name, namelen, match, matchlen, max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 107 | return 1; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 108 | if (!fnmatch(match, name, 0)) |
| 109 | return 1; |
| 110 | if (name[namelen-1] != '/') |
| 111 | continue; |
| 112 | |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 113 | /* We are being asked if the directory ("name") is worth |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 114 | * descending into. |
| 115 | * |
| 116 | * Find the longest leading directory name that does |
| 117 | * not have metacharacter in the pathspec; the name |
| 118 | * we are looking at must overlap with that directory. |
| 119 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 120 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 121 | char ch = *cp; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 122 | if (ch == '*' || ch == '[' || ch == '?') { |
| 123 | meta = cp; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 124 | break; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 125 | } |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 126 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 127 | if (!meta) |
| 128 | meta = cp; /* fully literal */ |
| 129 | |
| 130 | if (namelen <= meta - match) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 131 | /* Looking at "Documentation/" and |
| 132 | * the pattern says "Documentation/howto/", or |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 133 | * "Documentation/diff*.txt". The name we |
| 134 | * have should match prefix. |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 135 | */ |
| 136 | if (!memcmp(match, name, namelen)) |
| 137 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 138 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 139 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 140 | |
| 141 | if (meta - match < namelen) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 142 | /* Looking at "Documentation/howto/" and |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 143 | * the pattern says "Documentation/h*"; |
| 144 | * match up to "Do.../h"; this avoids descending |
| 145 | * into "Documentation/technical/". |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 146 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 147 | if (!memcmp(match, name, meta - match)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 148 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 149 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 150 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 155 | 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] | 156 | { |
| 157 | unsigned long size; |
| 158 | char *data; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 159 | enum object_type type; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 160 | char *to_free = NULL; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 161 | int hit; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 162 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 163 | data = read_sha1_file(sha1, &type, &size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 164 | if (!data) { |
| 165 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); |
| 166 | return 0; |
| 167 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 168 | if (opt->relative && opt->prefix_length) { |
| 169 | static char name_buf[PATH_MAX]; |
| 170 | char *cp; |
| 171 | int name_len = strlen(name) - opt->prefix_length + 1; |
| 172 | |
| 173 | if (!tree_name_len) |
| 174 | name += opt->prefix_length; |
| 175 | else { |
| 176 | if (ARRAY_SIZE(name_buf) <= name_len) |
| 177 | cp = to_free = xmalloc(name_len); |
| 178 | else |
| 179 | cp = name_buf; |
| 180 | memcpy(cp, name, tree_name_len); |
| 181 | strcpy(cp + tree_name_len, |
| 182 | name + tree_name_len + opt->prefix_length); |
| 183 | name = cp; |
| 184 | } |
| 185 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 186 | hit = grep_buffer(opt, name, data, size); |
| 187 | free(data); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 188 | free(to_free); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 189 | return hit; |
| 190 | } |
| 191 | |
| 192 | static int grep_file(struct grep_opt *opt, const char *filename) |
| 193 | { |
| 194 | struct stat st; |
| 195 | int i; |
| 196 | char *data; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 197 | size_t sz; |
| 198 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 199 | if (lstat(filename, &st) < 0) { |
| 200 | err_ret: |
| 201 | if (errno != ENOENT) |
| 202 | error("'%s': %s", filename, strerror(errno)); |
| 203 | return 0; |
| 204 | } |
| 205 | if (!st.st_size) |
| 206 | return 0; /* empty file -- no grep hit */ |
| 207 | if (!S_ISREG(st.st_mode)) |
| 208 | return 0; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 209 | sz = xsize_t(st.st_size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 210 | i = open(filename, O_RDONLY); |
| 211 | if (i < 0) |
| 212 | goto err_ret; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 213 | data = xmalloc(sz + 1); |
| 214 | if (st.st_size != read_in_full(i, data, sz)) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 215 | error("'%s': short read %s", filename, strerror(errno)); |
| 216 | close(i); |
| 217 | free(data); |
| 218 | return 0; |
| 219 | } |
| 220 | close(i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 221 | if (opt->relative && opt->prefix_length) |
| 222 | filename += opt->prefix_length; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 223 | i = grep_buffer(opt, filename, data, sz); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 224 | free(data); |
| 225 | return i; |
| 226 | } |
| 227 | |
Jeff King | 5f7c643 | 2008-03-12 17:39:16 -0400 | [diff] [blame] | 228 | #if !NO_EXTERNAL_GREP |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 229 | static int exec_grep(int argc, const char **argv) |
| 230 | { |
| 231 | pid_t pid; |
| 232 | int status; |
| 233 | |
| 234 | argv[argc] = NULL; |
| 235 | pid = fork(); |
| 236 | if (pid < 0) |
| 237 | return pid; |
| 238 | if (!pid) { |
| 239 | execvp("grep", (char **) argv); |
| 240 | exit(255); |
| 241 | } |
| 242 | while (waitpid(pid, &status, 0) < 0) { |
| 243 | if (errno == EINTR) |
| 244 | continue; |
| 245 | return -1; |
| 246 | } |
| 247 | if (WIFEXITED(status)) { |
| 248 | if (!WEXITSTATUS(status)) |
| 249 | return 1; |
| 250 | return 0; |
| 251 | } |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | #define MAXARGS 1000 |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 256 | #define ARGBUF 4096 |
| 257 | #define push_arg(a) do { \ |
| 258 | if (nr < MAXARGS) argv[nr++] = (a); \ |
| 259 | else die("maximum number of args exceeded"); \ |
| 260 | } while (0) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 261 | |
Junio C Hamano | d99ebf0 | 2007-09-14 00:31:00 -0700 | [diff] [blame] | 262 | /* |
| 263 | * If you send a singleton filename to grep, it does not give |
| 264 | * the name of the file. GNU grep has "-H" but we would want |
| 265 | * that behaviour in a portable way. |
| 266 | * |
| 267 | * So we keep two pathnames in argv buffer unsent to grep in |
| 268 | * the main loop if we need to do more than one grep. |
| 269 | */ |
| 270 | static int flush_grep(struct grep_opt *opt, |
| 271 | int argc, int arg0, const char **argv, int *kept) |
| 272 | { |
| 273 | int status; |
| 274 | int count = argc - arg0; |
| 275 | const char *kept_0 = NULL; |
| 276 | |
| 277 | if (count <= 2) { |
| 278 | /* |
| 279 | * Because we keep at least 2 paths in the call from |
| 280 | * the main loop (i.e. kept != NULL), and MAXARGS is |
| 281 | * far greater than 2, this usually is a call to |
| 282 | * conclude the grep. However, the user could attempt |
| 283 | * to overflow the argv buffer by giving too many |
| 284 | * options to leave very small number of real |
| 285 | * arguments even for the call in the main loop. |
| 286 | */ |
| 287 | if (kept) |
| 288 | die("insanely many options to grep"); |
| 289 | |
| 290 | /* |
| 291 | * If we have two or more paths, we do not have to do |
| 292 | * anything special, but we need to push /dev/null to |
| 293 | * get "-H" behaviour of GNU grep portably but when we |
| 294 | * are not doing "-l" nor "-L" nor "-c". |
| 295 | */ |
| 296 | if (count == 1 && |
| 297 | !opt->name_only && |
| 298 | !opt->unmatch_name_only && |
| 299 | !opt->count) { |
| 300 | argv[argc++] = "/dev/null"; |
| 301 | argv[argc] = NULL; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | else if (kept) { |
| 306 | /* |
| 307 | * Called because we found many paths and haven't finished |
| 308 | * iterating over the cache yet. We keep two paths |
| 309 | * for the concluding call. argv[argc-2] and argv[argc-1] |
| 310 | * has the last two paths, so save the first one away, |
| 311 | * replace it with NULL while sending the list to grep, |
| 312 | * and recover them after we are done. |
| 313 | */ |
| 314 | *kept = 2; |
| 315 | kept_0 = argv[argc-2]; |
| 316 | argv[argc-2] = NULL; |
| 317 | argc -= 2; |
| 318 | } |
| 319 | |
René Scharfe | ed24e40 | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 320 | if (opt->pre_context || opt->post_context) { |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 321 | /* |
| 322 | * grep handles hunk marks between files, but we need to |
| 323 | * do that ourselves between multiple calls. |
| 324 | */ |
| 325 | if (opt->show_hunk_mark) |
René Scharfe | ed24e40 | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 326 | write_or_die(1, "--\n", 3); |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 327 | else |
| 328 | opt->show_hunk_mark = 1; |
| 329 | } |
| 330 | |
Junio C Hamano | d99ebf0 | 2007-09-14 00:31:00 -0700 | [diff] [blame] | 331 | status = exec_grep(argc, argv); |
| 332 | |
| 333 | if (kept_0) { |
| 334 | /* |
| 335 | * Then recover them. Now the last arg is beyond the |
| 336 | * terminating NULL which is at argc, and the second |
| 337 | * from the last is what we saved away in kept_0 |
| 338 | */ |
| 339 | argv[arg0++] = kept_0; |
| 340 | argv[arg0] = argv[argc+1]; |
| 341 | } |
| 342 | return status; |
| 343 | } |
| 344 | |
René Scharfe | a94982e | 2009-03-07 13:34:46 +0100 | [diff] [blame] | 345 | static void grep_add_color(struct strbuf *sb, const char *escape_seq) |
| 346 | { |
| 347 | size_t orig_len = sb->len; |
| 348 | |
| 349 | while (*escape_seq) { |
| 350 | if (*escape_seq == 'm') |
| 351 | strbuf_addch(sb, ';'); |
| 352 | else if (*escape_seq != '\033' && *escape_seq != '[') |
| 353 | strbuf_addch(sb, *escape_seq); |
| 354 | escape_seq++; |
| 355 | } |
| 356 | if (sb->len > orig_len && sb->buf[sb->len - 1] == ';') |
| 357 | strbuf_setlen(sb, sb->len - 1); |
| 358 | } |
| 359 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 360 | static int external_grep(struct grep_opt *opt, const char **paths, int cached) |
| 361 | { |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 362 | int i, nr, argc, hit, len, status; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 363 | const char *argv[MAXARGS+1]; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 364 | char randarg[ARGBUF]; |
| 365 | char *argptr = randarg; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 366 | struct grep_pat *p; |
| 367 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 368 | if (opt->extended || (opt->relative && opt->prefix_length)) |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 369 | return -1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 370 | len = nr = 0; |
| 371 | push_arg("grep"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 372 | if (opt->fixed) |
Linus Torvalds | f664751 | 2006-05-15 17:54:01 -0700 | [diff] [blame] | 373 | push_arg("-F"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 374 | if (opt->linenum) |
| 375 | push_arg("-n"); |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 376 | if (!opt->pathname) |
| 377 | push_arg("-h"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 378 | if (opt->regflags & REG_EXTENDED) |
| 379 | push_arg("-E"); |
Robert Fitzsimons | 3026402 | 2006-06-06 23:15:16 +0000 | [diff] [blame] | 380 | if (opt->regflags & REG_ICASE) |
| 381 | push_arg("-i"); |
Junio C Hamano | bc39564 | 2009-02-02 10:58:20 -0800 | [diff] [blame] | 382 | if (opt->binary == GREP_BINARY_NOMATCH) |
| 383 | push_arg("-I"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 384 | if (opt->word_regexp) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 385 | push_arg("-w"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 386 | if (opt->name_only) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 387 | push_arg("-l"); |
| 388 | if (opt->unmatch_name_only) |
| 389 | push_arg("-L"); |
Raphael Zimmerer | 83caecc | 2008-10-01 18:11:15 +0200 | [diff] [blame] | 390 | if (opt->null_following_name) |
| 391 | /* in GNU grep git's "-z" translates to "-Z" */ |
| 392 | push_arg("-Z"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 393 | if (opt->count) |
| 394 | push_arg("-c"); |
| 395 | if (opt->post_context || opt->pre_context) { |
| 396 | if (opt->post_context != opt->pre_context) { |
| 397 | if (opt->pre_context) { |
| 398 | push_arg("-B"); |
| 399 | len += snprintf(argptr, sizeof(randarg)-len, |
Junio C Hamano | 4b87474 | 2007-11-17 21:18:14 -0800 | [diff] [blame] | 400 | "%u", opt->pre_context) + 1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 401 | if (sizeof(randarg) <= len) |
| 402 | die("maximum length of args exceeded"); |
| 403 | push_arg(argptr); |
| 404 | argptr += len; |
| 405 | } |
| 406 | if (opt->post_context) { |
| 407 | push_arg("-A"); |
| 408 | len += snprintf(argptr, sizeof(randarg)-len, |
Junio C Hamano | 4b87474 | 2007-11-17 21:18:14 -0800 | [diff] [blame] | 409 | "%u", opt->post_context) + 1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 410 | if (sizeof(randarg) <= len) |
| 411 | die("maximum length of args exceeded"); |
| 412 | push_arg(argptr); |
| 413 | argptr += len; |
| 414 | } |
| 415 | } |
| 416 | else { |
| 417 | push_arg("-C"); |
| 418 | len += snprintf(argptr, sizeof(randarg)-len, |
Junio C Hamano | 4b87474 | 2007-11-17 21:18:14 -0800 | [diff] [blame] | 419 | "%u", opt->post_context) + 1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 420 | if (sizeof(randarg) <= len) |
| 421 | die("maximum length of args exceeded"); |
| 422 | push_arg(argptr); |
| 423 | argptr += len; |
| 424 | } |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 425 | } |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 426 | for (p = opt->pattern_list; p; p = p->next) { |
| 427 | push_arg("-e"); |
| 428 | push_arg(p->pattern); |
| 429 | } |
René Scharfe | a94982e | 2009-03-07 13:34:46 +0100 | [diff] [blame] | 430 | if (opt->color) { |
| 431 | struct strbuf sb = STRBUF_INIT; |
| 432 | |
| 433 | grep_add_color(&sb, opt->color_match); |
| 434 | setenv("GREP_COLOR", sb.buf, 1); |
| 435 | |
| 436 | strbuf_reset(&sb); |
| 437 | strbuf_addstr(&sb, "mt="); |
| 438 | grep_add_color(&sb, opt->color_match); |
| 439 | strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se="); |
| 440 | setenv("GREP_COLORS", sb.buf, 1); |
| 441 | |
| 442 | strbuf_release(&sb); |
| 443 | |
| 444 | if (opt->color_external && strlen(opt->color_external) > 0) |
| 445 | push_arg(opt->color_external); |
| 446 | } |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 447 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 448 | hit = 0; |
| 449 | argc = nr; |
| 450 | for (i = 0; i < active_nr; i++) { |
| 451 | struct cache_entry *ce = active_cache[i]; |
Alex Riesen | fbd01ab | 2006-05-21 22:45:46 +0200 | [diff] [blame] | 452 | char *name; |
Junio C Hamano | d99ebf0 | 2007-09-14 00:31:00 -0700 | [diff] [blame] | 453 | int kept; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 454 | if (!S_ISREG(ce->ce_mode)) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 455 | continue; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 456 | if (!pathspec_matches(paths, ce->name, opt->max_depth)) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 457 | continue; |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 458 | name = ce->name; |
| 459 | if (name[0] == '-') { |
| 460 | int len = ce_namelen(ce); |
| 461 | name = xmalloc(len + 3); |
| 462 | memcpy(name, "./", 2); |
| 463 | memcpy(name + 2, ce->name, len + 1); |
| 464 | } |
| 465 | argv[argc++] = name; |
Junio C Hamano | 6326cee | 2007-12-05 16:13:08 -0800 | [diff] [blame] | 466 | if (MAXARGS <= argc) { |
| 467 | status = flush_grep(opt, argc, nr, argv, &kept); |
| 468 | if (0 < status) |
| 469 | hit = 1; |
| 470 | argc = nr + kept; |
| 471 | } |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 472 | if (ce_stage(ce)) { |
| 473 | do { |
| 474 | i++; |
| 475 | } while (i < active_nr && |
| 476 | !strcmp(ce->name, active_cache[i]->name)); |
| 477 | i--; /* compensate for loop control */ |
| 478 | } |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 479 | } |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 480 | if (argc > nr) { |
Junio C Hamano | d99ebf0 | 2007-09-14 00:31:00 -0700 | [diff] [blame] | 481 | status = flush_grep(opt, argc, nr, argv, NULL); |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 482 | if (0 < status) |
| 483 | hit = 1; |
| 484 | } |
| 485 | return hit; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 486 | } |
Shawn O. Pearce | ff1f994 | 2007-03-06 20:44:14 -0500 | [diff] [blame] | 487 | #endif |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 488 | |
René Scharfe | 1b5fb44 | 2009-05-07 21:46:17 +0200 | [diff] [blame] | 489 | static int grep_cache(struct grep_opt *opt, const char **paths, int cached, |
| 490 | int external_grep_allowed) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 491 | { |
| 492 | int hit = 0; |
| 493 | int nr; |
| 494 | read_cache(); |
| 495 | |
Jeff King | 5f7c643 | 2008-03-12 17:39:16 -0400 | [diff] [blame] | 496 | #if !NO_EXTERNAL_GREP |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 497 | /* |
| 498 | * Use the external "grep" command for the case where |
| 499 | * we grep through the checked-out files. It tends to |
| 500 | * be a lot more optimized |
| 501 | */ |
René Scharfe | 1b5fb44 | 2009-05-07 21:46:17 +0200 | [diff] [blame] | 502 | if (!cached && external_grep_allowed) { |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 503 | hit = external_grep(opt, paths, cached); |
| 504 | if (hit >= 0) |
| 505 | return hit; |
| 506 | } |
| 507 | #endif |
| 508 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 509 | for (nr = 0; nr < active_nr; nr++) { |
| 510 | struct cache_entry *ce = active_cache[nr]; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 511 | if (!S_ISREG(ce->ce_mode)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 512 | continue; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 513 | if (!pathspec_matches(paths, ce->name, opt->max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 514 | continue; |
Nguyễn Thái Ngọc Duy | 57d4346 | 2008-12-27 15:21:03 +0700 | [diff] [blame] | 515 | /* |
| 516 | * If CE_VALID is on, we assume worktree file and its cache entry |
| 517 | * are identical, even if worktree file has been modified, so use |
| 518 | * cache version instead |
| 519 | */ |
| 520 | if (cached || (ce->ce_flags & CE_VALID)) { |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 521 | if (ce_stage(ce)) |
| 522 | continue; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 523 | hit |= grep_sha1(opt, ce->sha1, ce->name, 0); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 524 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 525 | else |
| 526 | hit |= grep_file(opt, ce->name); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 527 | if (ce_stage(ce)) { |
| 528 | do { |
| 529 | nr++; |
| 530 | } while (nr < active_nr && |
| 531 | !strcmp(ce->name, active_cache[nr]->name)); |
| 532 | nr--; /* compensate for loop control */ |
| 533 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 534 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 535 | free_grep_patterns(opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 536 | return hit; |
| 537 | } |
| 538 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 539 | static int grep_tree(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 540 | struct tree_desc *tree, |
| 541 | const char *tree_name, const char *base) |
| 542 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 543 | int len; |
| 544 | int hit = 0; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 545 | struct name_entry entry; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 546 | char *down; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 547 | int tn_len = strlen(tree_name); |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 548 | struct strbuf pathbuf; |
| 549 | |
| 550 | strbuf_init(&pathbuf, PATH_MAX + tn_len); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 551 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 552 | if (tn_len) { |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 553 | strbuf_add(&pathbuf, tree_name, tn_len); |
| 554 | strbuf_addch(&pathbuf, ':'); |
| 555 | tn_len = pathbuf.len; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 556 | } |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 557 | strbuf_addstr(&pathbuf, base); |
| 558 | len = pathbuf.len; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 559 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 560 | while (tree_entry(tree, &entry)) { |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 561 | int te_len = tree_entry_len(entry.path, entry.sha1); |
| 562 | pathbuf.len = len; |
| 563 | strbuf_add(&pathbuf, entry.path, te_len); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 564 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 565 | if (S_ISDIR(entry.mode)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 566 | /* Match "abc/" against pathspec to |
| 567 | * decide if we want to descend into "abc" |
| 568 | * directory. |
| 569 | */ |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 570 | strbuf_addch(&pathbuf, '/'); |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 571 | |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 572 | down = pathbuf.buf + tn_len; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 573 | if (!pathspec_matches(paths, down, opt->max_depth)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 574 | ; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 575 | else if (S_ISREG(entry.mode)) |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 576 | hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 577 | else if (S_ISDIR(entry.mode)) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 578 | enum object_type type; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 579 | struct tree_desc sub; |
| 580 | void *data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 581 | unsigned long size; |
| 582 | |
| 583 | data = read_sha1_file(entry.sha1, &type, &size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 584 | if (!data) |
| 585 | die("unable to read tree (%s)", |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 586 | sha1_to_hex(entry.sha1)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 587 | init_tree_desc(&sub, data, size); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 588 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 589 | free(data); |
| 590 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 591 | } |
Dmitry Potapov | 620e2bb | 2008-07-16 19:33:29 +0400 | [diff] [blame] | 592 | strbuf_release(&pathbuf); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 593 | return hit; |
| 594 | } |
| 595 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 596 | static int grep_object(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 597 | struct object *obj, const char *name) |
| 598 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 599 | if (obj->type == OBJ_BLOB) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 600 | return grep_sha1(opt, obj->sha1, name, 0); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 601 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 602 | struct tree_desc tree; |
| 603 | void *data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 604 | unsigned long size; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 605 | int hit; |
| 606 | data = read_object_with_reference(obj->sha1, tree_type, |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 607 | &size, NULL); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 608 | if (!data) |
| 609 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 610 | init_tree_desc(&tree, data, size); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 611 | hit = grep_tree(opt, paths, &tree, name, ""); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 612 | free(data); |
| 613 | return hit; |
| 614 | } |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 615 | 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] | 616 | } |
| 617 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 618 | static int context_callback(const struct option *opt, const char *arg, |
| 619 | int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 620 | { |
| 621 | struct grep_opt *grep_opt = opt->value; |
| 622 | int value; |
| 623 | const char *endp; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 624 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 625 | if (unset) { |
| 626 | grep_opt->pre_context = grep_opt->post_context = 0; |
| 627 | return 0; |
| 628 | } |
| 629 | value = strtol(arg, (char **)&endp, 10); |
| 630 | if (*endp) { |
| 631 | return error("switch `%c' expects a numerical value", |
| 632 | opt->short_name); |
| 633 | } |
| 634 | grep_opt->pre_context = grep_opt->post_context = value; |
| 635 | return 0; |
| 636 | } |
| 637 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 638 | 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] | 639 | { |
| 640 | struct grep_opt *grep_opt = opt->value; |
| 641 | FILE *patterns; |
| 642 | int lno = 0; |
| 643 | struct strbuf sb; |
| 644 | |
| 645 | patterns = fopen(arg, "r"); |
| 646 | if (!patterns) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 647 | die_errno("cannot open '%s'", arg); |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 648 | while (strbuf_getline(&sb, patterns, '\n') == 0) { |
| 649 | /* ignore empty line like grep does */ |
| 650 | if (sb.len == 0) |
| 651 | continue; |
| 652 | append_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg, |
| 653 | ++lno, GREP_PATTERN); |
| 654 | } |
| 655 | fclose(patterns); |
| 656 | strbuf_release(&sb); |
| 657 | return 0; |
| 658 | } |
| 659 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 660 | 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] | 661 | { |
| 662 | struct grep_opt *grep_opt = opt->value; |
| 663 | append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); |
| 664 | return 0; |
| 665 | } |
| 666 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 667 | 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] | 668 | { |
| 669 | struct grep_opt *grep_opt = opt->value; |
| 670 | append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); |
| 671 | return 0; |
| 672 | } |
| 673 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 674 | 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] | 675 | { |
| 676 | struct grep_opt *grep_opt = opt->value; |
| 677 | append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); |
| 678 | return 0; |
| 679 | } |
| 680 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 681 | 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] | 682 | { |
| 683 | struct grep_opt *grep_opt = opt->value; |
| 684 | append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); |
| 685 | return 0; |
| 686 | } |
| 687 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 688 | static int pattern_callback(const struct option *opt, const char *arg, |
| 689 | int unset) |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 690 | { |
| 691 | struct grep_opt *grep_opt = opt->value; |
| 692 | append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); |
| 693 | return 0; |
| 694 | } |
| 695 | |
René Scharfe | ff3c7f9 | 2009-05-21 00:05:22 +0200 | [diff] [blame] | 696 | 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] | 697 | { |
| 698 | return -1; |
| 699 | } |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 700 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 701 | int cmd_grep(int argc, const char **argv, const char *prefix) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 702 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 703 | int hit = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 704 | int cached = 0; |
René Scharfe | 1b5fb44 | 2009-05-07 21:46:17 +0200 | [diff] [blame] | 705 | int external_grep_allowed = 1; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 706 | int seen_dashdash = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 707 | struct grep_opt opt; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 708 | struct object_array list = { 0, 0, NULL }; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 709 | const char **paths = NULL; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 710 | int i; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 711 | int dummy; |
| 712 | struct option options[] = { |
| 713 | OPT_BOOLEAN(0, "cached", &cached, |
| 714 | "search in index instead of in the work tree"), |
| 715 | OPT_GROUP(""), |
| 716 | OPT_BOOLEAN('v', "invert-match", &opt.invert, |
| 717 | "show non-matching lines"), |
| 718 | OPT_BIT('i', "ignore-case", &opt.regflags, |
| 719 | "case insensitive matching", REG_ICASE), |
| 720 | OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp, |
| 721 | "match patterns only at word boundaries"), |
| 722 | OPT_SET_INT('a', "text", &opt.binary, |
| 723 | "process binary files as text", GREP_BINARY_TEXT), |
| 724 | OPT_SET_INT('I', NULL, &opt.binary, |
| 725 | "don't match patterns in binary files", |
| 726 | GREP_BINARY_NOMATCH), |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 727 | { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth", |
| 728 | "descend at most <depth> levels", PARSE_OPT_NONEG, |
| 729 | NULL, 1 }, |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 730 | OPT_GROUP(""), |
| 731 | OPT_BIT('E', "extended-regexp", &opt.regflags, |
| 732 | "use extended POSIX regular expressions", REG_EXTENDED), |
| 733 | OPT_NEGBIT('G', "basic-regexp", &opt.regflags, |
| 734 | "use basic POSIX regular expressions (default)", |
| 735 | REG_EXTENDED), |
| 736 | OPT_BOOLEAN('F', "fixed-strings", &opt.fixed, |
| 737 | "interpret patterns as fixed strings"), |
| 738 | OPT_GROUP(""), |
| 739 | OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"), |
| 740 | OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1), |
| 741 | OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1), |
| 742 | OPT_NEGBIT(0, "full-name", &opt.relative, |
| 743 | "show filenames relative to top directory", 1), |
| 744 | OPT_BOOLEAN('l', "files-with-matches", &opt.name_only, |
| 745 | "show only filenames instead of matching lines"), |
| 746 | OPT_BOOLEAN(0, "name-only", &opt.name_only, |
| 747 | "synonym for --files-with-matches"), |
| 748 | OPT_BOOLEAN('L', "files-without-match", |
| 749 | &opt.unmatch_name_only, |
| 750 | "show only the names of files without match"), |
| 751 | OPT_BOOLEAN('z', "null", &opt.null_following_name, |
| 752 | "print NUL after filenames"), |
| 753 | OPT_BOOLEAN('c', "count", &opt.count, |
| 754 | "show the number of matches instead of matching lines"), |
| 755 | OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1), |
| 756 | OPT_GROUP(""), |
| 757 | OPT_CALLBACK('C', NULL, &opt, "n", |
| 758 | "show <n> context lines before and after matches", |
| 759 | context_callback), |
| 760 | OPT_INTEGER('B', NULL, &opt.pre_context, |
| 761 | "show <n> context lines before matches"), |
| 762 | OPT_INTEGER('A', NULL, &opt.post_context, |
| 763 | "show <n> context lines after matches"), |
| 764 | OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM", |
| 765 | context_callback), |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 766 | OPT_BOOLEAN('p', "show-function", &opt.funcname, |
| 767 | "show a line with the function name before matches"), |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 768 | OPT_GROUP(""), |
| 769 | OPT_CALLBACK('f', NULL, &opt, "file", |
| 770 | "read patterns from file", file_callback), |
| 771 | { OPTION_CALLBACK, 'e', NULL, &opt, "pattern", |
| 772 | "match <pattern>", PARSE_OPT_NONEG, pattern_callback }, |
| 773 | { OPTION_CALLBACK, 0, "and", &opt, NULL, |
| 774 | "combine patterns specified with -e", |
| 775 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback }, |
| 776 | OPT_BOOLEAN(0, "or", &dummy, ""), |
| 777 | { OPTION_CALLBACK, 0, "not", &opt, NULL, "", |
| 778 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback }, |
| 779 | { OPTION_CALLBACK, '(', NULL, &opt, NULL, "", |
| 780 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, |
| 781 | open_callback }, |
| 782 | { OPTION_CALLBACK, ')', NULL, &opt, NULL, "", |
| 783 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, |
| 784 | close_callback }, |
| 785 | OPT_BOOLEAN(0, "all-match", &opt.all_match, |
| 786 | "show only matches from files that match all patterns"), |
| 787 | OPT_GROUP(""), |
| 788 | #if NO_EXTERNAL_GREP |
| 789 | OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed, |
| 790 | "allow calling of grep(1) (ignored by this build)"), |
| 791 | #else |
| 792 | OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed, |
| 793 | "allow calling of grep(1) (default)"), |
| 794 | #endif |
| 795 | { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage", |
| 796 | PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback }, |
| 797 | OPT_END() |
| 798 | }; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 799 | |
| 800 | memset(&opt, 0, sizeof(opt)); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 801 | opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; |
| 802 | opt.relative = 1; |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 803 | opt.pathname = 1; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 804 | opt.pattern_tail = &opt.pattern_list; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 805 | opt.regflags = REG_NEWLINE; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame^] | 806 | opt.max_depth = -1; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 807 | |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 808 | strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD); |
| 809 | opt.color = -1; |
| 810 | git_config(grep_config, &opt); |
| 811 | if (opt.color == -1) |
| 812 | opt.color = git_use_color_default; |
| 813 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 814 | /* |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 815 | * If there is no -- then the paths must exist in the working |
| 816 | * tree. If there is no explicit pattern specified with -e or |
| 817 | * -f, we take the first unrecognized non option to be the |
| 818 | * pattern, but then what follows it must be zero or more |
| 819 | * valid refs up to the -- (if exists), and then existing |
| 820 | * paths. If there is an explicit pattern, then the first |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 821 | * unrecognized non option is the beginning of the refs list |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 822 | * that continues up to the -- (if exists), and then paths. |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 823 | */ |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 824 | argc = parse_options(argc, argv, prefix, options, grep_usage, |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 825 | PARSE_OPT_KEEP_DASHDASH | |
| 826 | PARSE_OPT_STOP_AT_NON_OPTION | |
| 827 | PARSE_OPT_NO_INTERNAL_HELP); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 828 | |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 829 | /* First unrecognized non-option token */ |
| 830 | if (argc > 0 && !opt.pattern_list) { |
| 831 | append_grep_pattern(&opt, argv[0], "command line", 0, |
| 832 | GREP_PATTERN); |
| 833 | argv++; |
| 834 | argc--; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 835 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 836 | |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 837 | if ((opt.color && !opt.color_external) || opt.funcname) |
René Scharfe | 1b5fb44 | 2009-05-07 21:46:17 +0200 | [diff] [blame] | 838 | external_grep_allowed = 0; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 839 | if (!opt.pattern_list) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 840 | die("no pattern given."); |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 841 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
| 842 | die("cannot mix --fixed-strings and regexp"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 843 | compile_grep_patterns(&opt); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 844 | |
| 845 | /* Check revs and then paths */ |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 846 | for (i = 0; i < argc; i++) { |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 847 | const char *arg = argv[i]; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 848 | unsigned char sha1[20]; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 849 | /* Is it a rev? */ |
| 850 | if (!get_sha1(arg, sha1)) { |
| 851 | struct object *object = parse_object(sha1); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 852 | if (!object) |
| 853 | die("bad object %s", arg); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 854 | add_object_array(object, arg, &list); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 855 | continue; |
| 856 | } |
| 857 | if (!strcmp(arg, "--")) { |
| 858 | i++; |
| 859 | seen_dashdash = 1; |
| 860 | } |
| 861 | break; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 862 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 863 | |
| 864 | /* The rest are paths */ |
| 865 | if (!seen_dashdash) { |
| 866 | int j; |
Junio C Hamano | c39c4f4 | 2006-05-09 18:15:21 -0700 | [diff] [blame] | 867 | for (j = i; j < argc; j++) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 868 | verify_filename(prefix, argv[j]); |
| 869 | } |
| 870 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 871 | if (i < argc) { |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 872 | paths = get_pathspec(prefix, argv + i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 873 | if (opt.prefix_length && opt.relative) { |
| 874 | /* Make sure we do not get outside of paths */ |
| 875 | for (i = 0; paths[i]; i++) |
| 876 | if (strncmp(prefix, paths[i], opt.prefix_length)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 877 | die("git grep: cannot generate relative filenames containing '..'"); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 880 | else if (prefix) { |
| 881 | paths = xcalloc(2, sizeof(const char *)); |
| 882 | paths[0] = prefix; |
| 883 | paths[1] = NULL; |
| 884 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 885 | |
Nguyễn Thái Ngọc Duy | 6577f54 | 2008-08-28 20:04:30 +0700 | [diff] [blame] | 886 | if (!list.nr) { |
| 887 | if (!cached) |
| 888 | setup_work_tree(); |
René Scharfe | 1b5fb44 | 2009-05-07 21:46:17 +0200 | [diff] [blame] | 889 | return !grep_cache(&opt, paths, cached, external_grep_allowed); |
Nguyễn Thái Ngọc Duy | 6577f54 | 2008-08-28 20:04:30 +0700 | [diff] [blame] | 890 | } |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 891 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 892 | if (cached) |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 893 | die("both --cached and trees are given."); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 894 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 895 | for (i = 0; i < list.nr; i++) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 896 | struct object *real_obj; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 897 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
| 898 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 899 | hit = 1; |
| 900 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 901 | free_grep_patterns(&opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 902 | return !hit; |
| 903 | } |