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