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" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 13 | #include "grep.h" |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 15 | /* |
| 16 | * git grep pathspecs are somewhat different from diff-tree pathspecs; |
| 17 | * pathname wildcards are allowed. |
| 18 | */ |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 19 | static int pathspec_matches(const char **paths, const char *name) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 20 | { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 21 | int namelen, i; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 22 | if (!paths || !*paths) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 23 | return 1; |
| 24 | namelen = strlen(name); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 25 | for (i = 0; paths[i]; i++) { |
| 26 | const char *match = paths[i]; |
| 27 | int matchlen = strlen(match); |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 28 | const char *cp, *meta; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 29 | |
Uwe Zeisberger | bb9e15a | 2006-06-21 11:04:12 +0200 | [diff] [blame] | 30 | if (!matchlen || |
| 31 | ((matchlen <= namelen) && |
| 32 | !strncmp(name, match, matchlen) && |
| 33 | (match[matchlen-1] == '/' || |
| 34 | name[matchlen] == '\0' || name[matchlen] == '/'))) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 35 | return 1; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 36 | if (!fnmatch(match, name, 0)) |
| 37 | return 1; |
| 38 | if (name[namelen-1] != '/') |
| 39 | continue; |
| 40 | |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 41 | /* We are being asked if the directory ("name") is worth |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 42 | * descending into. |
| 43 | * |
| 44 | * Find the longest leading directory name that does |
| 45 | * not have metacharacter in the pathspec; the name |
| 46 | * we are looking at must overlap with that directory. |
| 47 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 48 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 49 | char ch = *cp; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 50 | if (ch == '*' || ch == '[' || ch == '?') { |
| 51 | meta = cp; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 52 | break; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 53 | } |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 54 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 55 | if (!meta) |
| 56 | meta = cp; /* fully literal */ |
| 57 | |
| 58 | if (namelen <= meta - match) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 59 | /* Looking at "Documentation/" and |
| 60 | * the pattern says "Documentation/howto/", or |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 61 | * "Documentation/diff*.txt". The name we |
| 62 | * have should match prefix. |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 63 | */ |
| 64 | if (!memcmp(match, name, namelen)) |
| 65 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 66 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 67 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 68 | |
| 69 | if (meta - match < namelen) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 70 | /* Looking at "Documentation/howto/" and |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 71 | * the pattern says "Documentation/h*"; |
| 72 | * match up to "Do.../h"; this avoids descending |
| 73 | * into "Documentation/technical/". |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 74 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 75 | if (!memcmp(match, name, meta - match)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 76 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 77 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 78 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | unsigned long size; |
| 86 | char *data; |
| 87 | char type[20]; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 88 | char *to_free = NULL; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 89 | int hit; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 90 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 91 | data = read_sha1_file(sha1, type, &size); |
| 92 | if (!data) { |
| 93 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); |
| 94 | return 0; |
| 95 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 96 | if (opt->relative && opt->prefix_length) { |
| 97 | static char name_buf[PATH_MAX]; |
| 98 | char *cp; |
| 99 | int name_len = strlen(name) - opt->prefix_length + 1; |
| 100 | |
| 101 | if (!tree_name_len) |
| 102 | name += opt->prefix_length; |
| 103 | else { |
| 104 | if (ARRAY_SIZE(name_buf) <= name_len) |
| 105 | cp = to_free = xmalloc(name_len); |
| 106 | else |
| 107 | cp = name_buf; |
| 108 | memcpy(cp, name, tree_name_len); |
| 109 | strcpy(cp + tree_name_len, |
| 110 | name + tree_name_len + opt->prefix_length); |
| 111 | name = cp; |
| 112 | } |
| 113 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 114 | hit = grep_buffer(opt, name, data, size); |
| 115 | free(data); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 116 | free(to_free); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 117 | return hit; |
| 118 | } |
| 119 | |
| 120 | static int grep_file(struct grep_opt *opt, const char *filename) |
| 121 | { |
| 122 | struct stat st; |
| 123 | int i; |
| 124 | char *data; |
| 125 | if (lstat(filename, &st) < 0) { |
| 126 | err_ret: |
| 127 | if (errno != ENOENT) |
| 128 | error("'%s': %s", filename, strerror(errno)); |
| 129 | return 0; |
| 130 | } |
| 131 | if (!st.st_size) |
| 132 | return 0; /* empty file -- no grep hit */ |
| 133 | if (!S_ISREG(st.st_mode)) |
| 134 | return 0; |
| 135 | i = open(filename, O_RDONLY); |
| 136 | if (i < 0) |
| 137 | goto err_ret; |
| 138 | data = xmalloc(st.st_size + 1); |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 139 | if (st.st_size != read_in_full(i, data, st.st_size)) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 140 | error("'%s': short read %s", filename, strerror(errno)); |
| 141 | close(i); |
| 142 | free(data); |
| 143 | return 0; |
| 144 | } |
| 145 | close(i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 146 | if (opt->relative && opt->prefix_length) |
| 147 | filename += opt->prefix_length; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 148 | i = grep_buffer(opt, filename, data, st.st_size); |
| 149 | free(data); |
| 150 | return i; |
| 151 | } |
| 152 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 153 | static int exec_grep(int argc, const char **argv) |
| 154 | { |
| 155 | pid_t pid; |
| 156 | int status; |
| 157 | |
| 158 | argv[argc] = NULL; |
| 159 | pid = fork(); |
| 160 | if (pid < 0) |
| 161 | return pid; |
| 162 | if (!pid) { |
| 163 | execvp("grep", (char **) argv); |
| 164 | exit(255); |
| 165 | } |
| 166 | while (waitpid(pid, &status, 0) < 0) { |
| 167 | if (errno == EINTR) |
| 168 | continue; |
| 169 | return -1; |
| 170 | } |
| 171 | if (WIFEXITED(status)) { |
| 172 | if (!WEXITSTATUS(status)) |
| 173 | return 1; |
| 174 | return 0; |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | #define MAXARGS 1000 |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 180 | #define ARGBUF 4096 |
| 181 | #define push_arg(a) do { \ |
| 182 | if (nr < MAXARGS) argv[nr++] = (a); \ |
| 183 | else die("maximum number of args exceeded"); \ |
| 184 | } while (0) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 185 | |
| 186 | static int external_grep(struct grep_opt *opt, const char **paths, int cached) |
| 187 | { |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 188 | int i, nr, argc, hit, len, status; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 189 | const char *argv[MAXARGS+1]; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 190 | char randarg[ARGBUF]; |
| 191 | char *argptr = randarg; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 192 | struct grep_pat *p; |
| 193 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 194 | if (opt->extended || (opt->relative && opt->prefix_length)) |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 195 | return -1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 196 | len = nr = 0; |
| 197 | push_arg("grep"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 198 | if (opt->fixed) |
Linus Torvalds | f664751 | 2006-05-15 17:54:01 -0700 | [diff] [blame] | 199 | push_arg("-F"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 200 | if (opt->linenum) |
| 201 | push_arg("-n"); |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 202 | if (!opt->pathname) |
| 203 | push_arg("-h"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 204 | if (opt->regflags & REG_EXTENDED) |
| 205 | push_arg("-E"); |
Robert Fitzsimons | 3026402 | 2006-06-06 23:15:16 +0000 | [diff] [blame] | 206 | if (opt->regflags & REG_ICASE) |
| 207 | push_arg("-i"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 208 | if (opt->word_regexp) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 209 | push_arg("-w"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 210 | if (opt->name_only) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 211 | push_arg("-l"); |
| 212 | if (opt->unmatch_name_only) |
| 213 | push_arg("-L"); |
| 214 | if (opt->count) |
| 215 | push_arg("-c"); |
| 216 | if (opt->post_context || opt->pre_context) { |
| 217 | if (opt->post_context != opt->pre_context) { |
| 218 | if (opt->pre_context) { |
| 219 | push_arg("-B"); |
| 220 | len += snprintf(argptr, sizeof(randarg)-len, |
| 221 | "%u", opt->pre_context); |
| 222 | if (sizeof(randarg) <= len) |
| 223 | die("maximum length of args exceeded"); |
| 224 | push_arg(argptr); |
| 225 | argptr += len; |
| 226 | } |
| 227 | if (opt->post_context) { |
| 228 | push_arg("-A"); |
| 229 | len += snprintf(argptr, sizeof(randarg)-len, |
| 230 | "%u", opt->post_context); |
| 231 | if (sizeof(randarg) <= len) |
| 232 | die("maximum length of args exceeded"); |
| 233 | push_arg(argptr); |
| 234 | argptr += len; |
| 235 | } |
| 236 | } |
| 237 | else { |
| 238 | push_arg("-C"); |
| 239 | len += snprintf(argptr, sizeof(randarg)-len, |
| 240 | "%u", opt->post_context); |
| 241 | if (sizeof(randarg) <= len) |
| 242 | die("maximum length of args exceeded"); |
| 243 | push_arg(argptr); |
| 244 | argptr += len; |
| 245 | } |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 246 | } |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 247 | for (p = opt->pattern_list; p; p = p->next) { |
| 248 | push_arg("-e"); |
| 249 | push_arg(p->pattern); |
| 250 | } |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * To make sure we get the header printed out when we want it, |
| 254 | * add /dev/null to the paths to grep. This is unnecessary |
| 255 | * (and wrong) with "-l" or "-L", which always print out the |
| 256 | * name anyway. |
| 257 | * |
| 258 | * GNU grep has "-H", but this is portable. |
| 259 | */ |
| 260 | if (!opt->name_only && !opt->unmatch_name_only) |
| 261 | push_arg("/dev/null"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 262 | |
| 263 | hit = 0; |
| 264 | argc = nr; |
| 265 | for (i = 0; i < active_nr; i++) { |
| 266 | struct cache_entry *ce = active_cache[i]; |
Alex Riesen | fbd01ab | 2006-05-21 22:45:46 +0200 | [diff] [blame] | 267 | char *name; |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 268 | if (!S_ISREG(ntohl(ce->ce_mode))) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 269 | continue; |
| 270 | if (!pathspec_matches(paths, ce->name)) |
| 271 | continue; |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 272 | name = ce->name; |
| 273 | if (name[0] == '-') { |
| 274 | int len = ce_namelen(ce); |
| 275 | name = xmalloc(len + 3); |
| 276 | memcpy(name, "./", 2); |
| 277 | memcpy(name + 2, ce->name, len + 1); |
| 278 | } |
| 279 | argv[argc++] = name; |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 280 | if (argc < MAXARGS && !ce_stage(ce)) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 281 | continue; |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 282 | status = exec_grep(argc, argv); |
| 283 | if (0 < status) |
| 284 | hit = 1; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 285 | argc = nr; |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 286 | if (ce_stage(ce)) { |
| 287 | do { |
| 288 | i++; |
| 289 | } while (i < active_nr && |
| 290 | !strcmp(ce->name, active_cache[i]->name)); |
| 291 | i--; /* compensate for loop control */ |
| 292 | } |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 293 | } |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 294 | if (argc > nr) { |
| 295 | status = exec_grep(argc, argv); |
| 296 | if (0 < status) |
| 297 | hit = 1; |
| 298 | } |
| 299 | return hit; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 302 | 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] | 303 | { |
| 304 | int hit = 0; |
| 305 | int nr; |
| 306 | read_cache(); |
| 307 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 308 | #ifdef __unix__ |
| 309 | /* |
| 310 | * Use the external "grep" command for the case where |
| 311 | * we grep through the checked-out files. It tends to |
| 312 | * be a lot more optimized |
| 313 | */ |
| 314 | if (!cached) { |
| 315 | hit = external_grep(opt, paths, cached); |
| 316 | if (hit >= 0) |
| 317 | return hit; |
| 318 | } |
| 319 | #endif |
| 320 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 321 | for (nr = 0; nr < active_nr; nr++) { |
| 322 | struct cache_entry *ce = active_cache[nr]; |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 323 | if (!S_ISREG(ntohl(ce->ce_mode))) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 324 | continue; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 325 | if (!pathspec_matches(paths, ce->name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 326 | continue; |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 327 | if (cached) { |
| 328 | if (ce_stage(ce)) |
| 329 | continue; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 330 | hit |= grep_sha1(opt, ce->sha1, ce->name, 0); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 331 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 332 | else |
| 333 | hit |= grep_file(opt, ce->name); |
Junio C Hamano | 36f2587 | 2006-11-26 12:47:52 -0800 | [diff] [blame] | 334 | if (ce_stage(ce)) { |
| 335 | do { |
| 336 | nr++; |
| 337 | } while (nr < active_nr && |
| 338 | !strcmp(ce->name, active_cache[nr]->name)); |
| 339 | nr--; /* compensate for loop control */ |
| 340 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 341 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 342 | free_grep_patterns(opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 343 | return hit; |
| 344 | } |
| 345 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 346 | static int grep_tree(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 347 | struct tree_desc *tree, |
| 348 | const char *tree_name, const char *base) |
| 349 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 350 | int len; |
| 351 | int hit = 0; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 352 | struct name_entry entry; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 353 | char *down; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 354 | int tn_len = strlen(tree_name); |
| 355 | char *path_buf = xmalloc(PATH_MAX + tn_len + 100); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 356 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 357 | if (tn_len) { |
| 358 | tn_len = sprintf(path_buf, "%s:", tree_name); |
| 359 | down = path_buf + tn_len; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 360 | strcat(down, base); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 361 | } |
| 362 | else { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 363 | down = path_buf; |
| 364 | strcpy(down, base); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 365 | } |
| 366 | len = strlen(path_buf); |
| 367 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 368 | while (tree_entry(tree, &entry)) { |
| 369 | strcpy(path_buf + len, entry.path); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 370 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 371 | if (S_ISDIR(entry.mode)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 372 | /* Match "abc/" against pathspec to |
| 373 | * decide if we want to descend into "abc" |
| 374 | * directory. |
| 375 | */ |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 376 | strcpy(path_buf + len + entry.pathlen, "/"); |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 377 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 378 | if (!pathspec_matches(paths, down)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 379 | ; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 380 | else if (S_ISREG(entry.mode)) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 381 | hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 382 | else if (S_ISDIR(entry.mode)) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 383 | char type[20]; |
| 384 | struct tree_desc sub; |
| 385 | void *data; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 386 | data = read_sha1_file(entry.sha1, type, &sub.size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 387 | if (!data) |
| 388 | die("unable to read tree (%s)", |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 389 | sha1_to_hex(entry.sha1)); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 390 | sub.buf = data; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 391 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 392 | free(data); |
| 393 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 394 | } |
| 395 | return hit; |
| 396 | } |
| 397 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 398 | static int grep_object(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 399 | struct object *obj, const char *name) |
| 400 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 401 | if (obj->type == OBJ_BLOB) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 402 | return grep_sha1(opt, obj->sha1, name, 0); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 403 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 404 | struct tree_desc tree; |
| 405 | void *data; |
| 406 | int hit; |
| 407 | data = read_object_with_reference(obj->sha1, tree_type, |
| 408 | &tree.size, NULL); |
| 409 | if (!data) |
| 410 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); |
| 411 | tree.buf = data; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 412 | hit = grep_tree(opt, paths, &tree, name, ""); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 413 | free(data); |
| 414 | return hit; |
| 415 | } |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 416 | 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] | 417 | } |
| 418 | |
| 419 | static const char builtin_grep_usage[] = |
| 420 | "git-grep <option>* <rev>* [-e] <pattern> [<path>...]"; |
| 421 | |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 422 | static const char emsg_invalid_context_len[] = |
| 423 | "%s: invalid context length argument"; |
| 424 | static const char emsg_missing_context_len[] = |
| 425 | "missing context length argument"; |
| 426 | static const char emsg_missing_argument[] = |
| 427 | "option requires an argument -%s"; |
| 428 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 429 | int cmd_grep(int argc, const char **argv, const char *prefix) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 430 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 431 | int hit = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 432 | int cached = 0; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 433 | int seen_dashdash = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 434 | struct grep_opt opt; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 435 | struct object_array list = { 0, 0, NULL }; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 436 | const char **paths = NULL; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 437 | int i; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 438 | |
| 439 | memset(&opt, 0, sizeof(opt)); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 440 | opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; |
| 441 | opt.relative = 1; |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 442 | opt.pathname = 1; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 443 | opt.pattern_tail = &opt.pattern_list; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 444 | opt.regflags = REG_NEWLINE; |
| 445 | |
| 446 | /* |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 447 | * If there is no -- then the paths must exist in the working |
| 448 | * tree. If there is no explicit pattern specified with -e or |
| 449 | * -f, we take the first unrecognized non option to be the |
| 450 | * pattern, but then what follows it must be zero or more |
| 451 | * valid refs up to the -- (if exists), and then existing |
| 452 | * paths. If there is an explicit pattern, then the first |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 453 | * unrecognized non option is the beginning of the refs list |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 454 | * that continues up to the -- (if exists), and then paths. |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 455 | */ |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 456 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 457 | while (1 < argc) { |
| 458 | const char *arg = argv[1]; |
| 459 | argc--; argv++; |
| 460 | if (!strcmp("--cached", arg)) { |
| 461 | cached = 1; |
| 462 | continue; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 463 | } |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 464 | if (!strcmp("-a", arg) || |
| 465 | !strcmp("--text", arg)) { |
| 466 | opt.binary = GREP_BINARY_TEXT; |
| 467 | continue; |
| 468 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 469 | if (!strcmp("-i", arg) || |
| 470 | !strcmp("--ignore-case", arg)) { |
| 471 | opt.regflags |= REG_ICASE; |
| 472 | continue; |
| 473 | } |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 474 | if (!strcmp("-I", arg)) { |
| 475 | opt.binary = GREP_BINARY_NOMATCH; |
| 476 | continue; |
| 477 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 478 | if (!strcmp("-v", arg) || |
| 479 | !strcmp("--invert-match", arg)) { |
| 480 | opt.invert = 1; |
| 481 | continue; |
| 482 | } |
| 483 | if (!strcmp("-E", arg) || |
| 484 | !strcmp("--extended-regexp", arg)) { |
| 485 | opt.regflags |= REG_EXTENDED; |
| 486 | continue; |
| 487 | } |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 488 | if (!strcmp("-F", arg) || |
| 489 | !strcmp("--fixed-strings", arg)) { |
| 490 | opt.fixed = 1; |
| 491 | continue; |
| 492 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 493 | if (!strcmp("-G", arg) || |
| 494 | !strcmp("--basic-regexp", arg)) { |
| 495 | opt.regflags &= ~REG_EXTENDED; |
| 496 | continue; |
| 497 | } |
| 498 | if (!strcmp("-n", arg)) { |
| 499 | opt.linenum = 1; |
| 500 | continue; |
| 501 | } |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 502 | if (!strcmp("-h", arg)) { |
| 503 | opt.pathname = 0; |
| 504 | continue; |
| 505 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 506 | if (!strcmp("-H", arg)) { |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame] | 507 | opt.pathname = 1; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 508 | continue; |
| 509 | } |
| 510 | if (!strcmp("-l", arg) || |
| 511 | !strcmp("--files-with-matches", arg)) { |
| 512 | opt.name_only = 1; |
| 513 | continue; |
| 514 | } |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 515 | if (!strcmp("-L", arg) || |
| 516 | !strcmp("--files-without-match", arg)) { |
| 517 | opt.unmatch_name_only = 1; |
| 518 | continue; |
| 519 | } |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 520 | if (!strcmp("-c", arg) || |
| 521 | !strcmp("--count", arg)) { |
| 522 | opt.count = 1; |
| 523 | continue; |
| 524 | } |
Junio C Hamano | 7839a25 | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 525 | if (!strcmp("-w", arg) || |
| 526 | !strcmp("--word-regexp", arg)) { |
| 527 | opt.word_regexp = 1; |
| 528 | continue; |
| 529 | } |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 530 | if (!strncmp("-A", arg, 2) || |
| 531 | !strncmp("-B", arg, 2) || |
| 532 | !strncmp("-C", arg, 2) || |
| 533 | (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) { |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 534 | unsigned num; |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 535 | const char *scan; |
| 536 | switch (arg[1]) { |
| 537 | case 'A': case 'B': case 'C': |
| 538 | if (!arg[2]) { |
| 539 | if (argc <= 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 540 | die(emsg_missing_context_len); |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 541 | scan = *++argv; |
| 542 | argc--; |
| 543 | } |
| 544 | else |
| 545 | scan = arg + 2; |
| 546 | break; |
| 547 | default: |
| 548 | scan = arg + 1; |
| 549 | break; |
| 550 | } |
| 551 | if (sscanf(scan, "%u", &num) != 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 552 | die(emsg_invalid_context_len, scan); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 553 | switch (arg[1]) { |
| 554 | case 'A': |
| 555 | opt.post_context = num; |
| 556 | break; |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 557 | default: |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 558 | case 'C': |
| 559 | opt.post_context = num; |
| 560 | case 'B': |
| 561 | opt.pre_context = num; |
| 562 | break; |
| 563 | } |
| 564 | continue; |
| 565 | } |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 566 | if (!strcmp("-f", arg)) { |
| 567 | FILE *patterns; |
| 568 | int lno = 0; |
| 569 | char buf[1024]; |
| 570 | if (argc <= 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 571 | die(emsg_missing_argument, arg); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 572 | patterns = fopen(argv[1], "r"); |
| 573 | if (!patterns) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 574 | die("'%s': %s", argv[1], strerror(errno)); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 575 | while (fgets(buf, sizeof(buf), patterns)) { |
| 576 | int len = strlen(buf); |
| 577 | if (buf[len-1] == '\n') |
| 578 | buf[len-1] = 0; |
| 579 | /* ignore empty line like grep does */ |
| 580 | if (!buf[0]) |
| 581 | continue; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 582 | append_grep_pattern(&opt, xstrdup(buf), |
| 583 | argv[1], ++lno, |
| 584 | GREP_PATTERN); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 585 | } |
| 586 | fclose(patterns); |
| 587 | argv++; |
| 588 | argc--; |
| 589 | continue; |
| 590 | } |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 591 | if (!strcmp("--not", arg)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 592 | append_grep_pattern(&opt, arg, "command line", 0, |
| 593 | GREP_NOT); |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 594 | continue; |
| 595 | } |
| 596 | if (!strcmp("--and", arg)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 597 | append_grep_pattern(&opt, arg, "command line", 0, |
| 598 | GREP_AND); |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 599 | continue; |
| 600 | } |
| 601 | if (!strcmp("--or", arg)) |
| 602 | continue; /* no-op */ |
| 603 | if (!strcmp("(", arg)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 604 | append_grep_pattern(&opt, arg, "command line", 0, |
| 605 | GREP_OPEN_PAREN); |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 606 | continue; |
| 607 | } |
| 608 | if (!strcmp(")", arg)) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 609 | append_grep_pattern(&opt, arg, "command line", 0, |
| 610 | GREP_CLOSE_PAREN); |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 611 | continue; |
| 612 | } |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 613 | if (!strcmp("--all-match", arg)) { |
| 614 | opt.all_match = 1; |
| 615 | continue; |
| 616 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 617 | if (!strcmp("-e", arg)) { |
| 618 | if (1 < argc) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 619 | append_grep_pattern(&opt, argv[1], |
| 620 | "-e option", 0, |
| 621 | GREP_PATTERN); |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 622 | argv++; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 623 | argc--; |
| 624 | continue; |
| 625 | } |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 626 | die(emsg_missing_argument, arg); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 627 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 628 | if (!strcmp("--full-name", arg)) { |
| 629 | opt.relative = 0; |
| 630 | continue; |
| 631 | } |
Junio C Hamano | 5390590 | 2006-07-04 02:31:50 -0700 | [diff] [blame] | 632 | if (!strcmp("--", arg)) { |
| 633 | /* later processing wants to have this at argv[1] */ |
| 634 | argv--; |
| 635 | argc++; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 636 | break; |
Junio C Hamano | 5390590 | 2006-07-04 02:31:50 -0700 | [diff] [blame] | 637 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 638 | if (*arg == '-') |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 639 | usage(builtin_grep_usage); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 640 | |
| 641 | /* First unrecognized non-option token */ |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 642 | if (!opt.pattern_list) { |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 643 | append_grep_pattern(&opt, arg, "command line", 0, |
| 644 | GREP_PATTERN); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 645 | break; |
| 646 | } |
| 647 | else { |
| 648 | /* We are looking at the first path or rev; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 649 | * it is found at argv[1] after leaving the |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 650 | * loop. |
| 651 | */ |
| 652 | argc++; argv--; |
| 653 | break; |
| 654 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 655 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 656 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 657 | if (!opt.pattern_list) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 658 | die("no pattern given."); |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 659 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
| 660 | die("cannot mix --fixed-strings and regexp"); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 661 | compile_grep_patterns(&opt); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 662 | |
| 663 | /* Check revs and then paths */ |
| 664 | for (i = 1; i < argc; i++) { |
| 665 | const char *arg = argv[i]; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 666 | unsigned char sha1[20]; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 667 | /* Is it a rev? */ |
| 668 | if (!get_sha1(arg, sha1)) { |
| 669 | struct object *object = parse_object(sha1); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 670 | if (!object) |
| 671 | die("bad object %s", arg); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 672 | add_object_array(object, arg, &list); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 673 | continue; |
| 674 | } |
| 675 | if (!strcmp(arg, "--")) { |
| 676 | i++; |
| 677 | seen_dashdash = 1; |
| 678 | } |
| 679 | break; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 680 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 681 | |
| 682 | /* The rest are paths */ |
| 683 | if (!seen_dashdash) { |
| 684 | int j; |
Junio C Hamano | c39c4f4 | 2006-05-09 18:15:21 -0700 | [diff] [blame] | 685 | for (j = i; j < argc; j++) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 686 | verify_filename(prefix, argv[j]); |
| 687 | } |
| 688 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 689 | if (i < argc) { |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 690 | paths = get_pathspec(prefix, argv + i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 691 | if (opt.prefix_length && opt.relative) { |
| 692 | /* Make sure we do not get outside of paths */ |
| 693 | for (i = 0; paths[i]; i++) |
| 694 | if (strncmp(prefix, paths[i], opt.prefix_length)) |
| 695 | die("git-grep: cannot generate relative filenames containing '..'"); |
| 696 | } |
| 697 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 698 | else if (prefix) { |
| 699 | paths = xcalloc(2, sizeof(const char *)); |
| 700 | paths[0] = prefix; |
| 701 | paths[1] = NULL; |
| 702 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 703 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 704 | if (!list.nr) |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 705 | return !grep_cache(&opt, paths, cached); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 706 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 707 | if (cached) |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 708 | die("both --cached and trees are given."); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 709 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 710 | for (i = 0; i < list.nr; i++) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 711 | struct object *real_obj; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 712 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
| 713 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 714 | hit = 1; |
| 715 | } |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 716 | free_grep_patterns(&opt); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 717 | return !hit; |
| 718 | } |