blob: 2bfbdb71407dcd0ddfe970f6a614158d954cd4c0 [file] [log] [blame]
Junio C Hamano5010cb52006-04-30 23:28:15 -07001/*
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 Hamano13626712006-05-01 15:58:29 -070011#include "tree-walk.h"
Junio C Hamano5010cb52006-04-30 23:28:15 -070012#include "builtin.h"
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070013#include "grep.h"
Junio C Hamano5010cb52006-04-30 23:28:15 -070014
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070015/*
16 * git grep pathspecs are somewhat different from diff-tree pathspecs;
17 * pathname wildcards are allowed.
18 */
Junio C Hamano13626712006-05-01 15:58:29 -070019static int pathspec_matches(const char **paths, const char *name)
Junio C Hamano5010cb52006-04-30 23:28:15 -070020{
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070021 int namelen, i;
Junio C Hamano13626712006-05-01 15:58:29 -070022 if (!paths || !*paths)
Junio C Hamano5010cb52006-04-30 23:28:15 -070023 return 1;
24 namelen = strlen(name);
Junio C Hamano13626712006-05-01 15:58:29 -070025 for (i = 0; paths[i]; i++) {
26 const char *match = paths[i];
27 int matchlen = strlen(match);
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070028 const char *cp, *meta;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070029
Uwe Zeisbergerbb9e15a2006-06-21 11:04:12 +020030 if (!matchlen ||
31 ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/')))
Junio C Hamano5010cb52006-04-30 23:28:15 -070035 return 1;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070036 if (!fnmatch(match, name, 0))
37 return 1;
38 if (name[namelen-1] != '/')
39 continue;
40
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070041 /* We are being asked if the directory ("name") is worth
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070042 * 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 Hamano1e3d90e2006-05-02 17:27:07 -070048 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070049 char ch = *cp;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070050 if (ch == '*' || ch == '[' || ch == '?') {
51 meta = cp;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070052 break;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070053 }
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070054 }
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070055 if (!meta)
56 meta = cp; /* fully literal */
57
58 if (namelen <= meta - match) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070059 /* Looking at "Documentation/" and
60 * the pattern says "Documentation/howto/", or
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070061 * "Documentation/diff*.txt". The name we
62 * have should match prefix.
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070063 */
64 if (!memcmp(match, name, namelen))
65 return 1;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070066 continue;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070067 }
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070068
69 if (meta - match < namelen) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070070 /* Looking at "Documentation/howto/" and
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070071 * the pattern says "Documentation/h*";
72 * match up to "Do.../h"; this avoids descending
73 * into "Documentation/technical/".
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070074 */
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070075 if (!memcmp(match, name, meta - match))
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070076 return 1;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070077 continue;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070078 }
Junio C Hamano5010cb52006-04-30 23:28:15 -070079 }
80 return 0;
81}
82
Junio C Hamano0d042fe2006-08-11 00:44:42 -070083static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
Junio C Hamano5010cb52006-04-30 23:28:15 -070084{
85 unsigned long size;
86 char *data;
87 char type[20];
Junio C Hamano0d042fe2006-08-11 00:44:42 -070088 char *to_free = NULL;
Junio C Hamano5010cb52006-04-30 23:28:15 -070089 int hit;
Junio C Hamano0d042fe2006-08-11 00:44:42 -070090
Junio C Hamano5010cb52006-04-30 23:28:15 -070091 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 Hamano0d042fe2006-08-11 00:44:42 -070096 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 Hamano5010cb52006-04-30 23:28:15 -0700114 hit = grep_buffer(opt, name, data, size);
115 free(data);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700116 free(to_free);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700117 return hit;
118}
119
120static 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 Whitcroft93d26e42007-01-08 15:58:08 +0000139 if (st.st_size != read_in_full(i, data, st.st_size)) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700140 error("'%s': short read %s", filename, strerror(errno));
141 close(i);
142 free(data);
143 return 0;
144 }
145 close(i);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700146 if (opt->relative && opt->prefix_length)
147 filename += opt->prefix_length;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700148 i = grep_buffer(opt, filename, data, st.st_size);
149 free(data);
150 return i;
151}
152
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700153static 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 Hamanoffa0a7a2006-05-15 13:28:01 -0700180#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 Torvalds1e2398d2006-05-14 20:49:15 -0700185
186static int external_grep(struct grep_opt *opt, const char **paths, int cached)
187{
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700188 int i, nr, argc, hit, len, status;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700189 const char *argv[MAXARGS+1];
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700190 char randarg[ARGBUF];
191 char *argptr = randarg;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700192 struct grep_pat *p;
193
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700194 if (opt->extended || (opt->relative && opt->prefix_length))
Junio C Hamano79d36962006-06-30 03:04:05 -0700195 return -1;
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700196 len = nr = 0;
197 push_arg("grep");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700198 if (opt->fixed)
Linus Torvaldsf6647512006-05-15 17:54:01 -0700199 push_arg("-F");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700200 if (opt->linenum)
201 push_arg("-n");
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700202 if (!opt->pathname)
203 push_arg("-h");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700204 if (opt->regflags & REG_EXTENDED)
205 push_arg("-E");
Robert Fitzsimons30264022006-06-06 23:15:16 +0000206 if (opt->regflags & REG_ICASE)
207 push_arg("-i");
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700208 if (opt->word_regexp)
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700209 push_arg("-w");
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700210 if (opt->name_only)
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700211 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 Torvalds1e2398d2006-05-14 20:49:15 -0700246 }
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700247 for (p = opt->pattern_list; p; p = p->next) {
248 push_arg("-e");
249 push_arg(p->pattern);
250 }
Linus Torvaldsbbb66c62006-05-17 11:12:22 -0700251
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 Torvalds1e2398d2006-05-14 20:49:15 -0700262
263 hit = 0;
264 argc = nr;
265 for (i = 0; i < active_nr; i++) {
266 struct cache_entry *ce = active_cache[i];
Alex Riesenfbd01ab2006-05-21 22:45:46 +0200267 char *name;
Junio C Hamano36f25872006-11-26 12:47:52 -0800268 if (!S_ISREG(ntohl(ce->ce_mode)))
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700269 continue;
270 if (!pathspec_matches(paths, ce->name))
271 continue;
Linus Torvaldsbbb66c62006-05-17 11:12:22 -0700272 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 Hamano36f25872006-11-26 12:47:52 -0800280 if (argc < MAXARGS && !ce_stage(ce))
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700281 continue;
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700282 status = exec_grep(argc, argv);
283 if (0 < status)
284 hit = 1;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700285 argc = nr;
Junio C Hamano36f25872006-11-26 12:47:52 -0800286 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 Torvalds1e2398d2006-05-14 20:49:15 -0700293 }
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700294 if (argc > nr) {
295 status = exec_grep(argc, argv);
296 if (0 < status)
297 hit = 1;
298 }
299 return hit;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700300}
301
Junio C Hamano13626712006-05-01 15:58:29 -0700302static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700303{
304 int hit = 0;
305 int nr;
306 read_cache();
307
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700308#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 Hamano5010cb52006-04-30 23:28:15 -0700321 for (nr = 0; nr < active_nr; nr++) {
322 struct cache_entry *ce = active_cache[nr];
Junio C Hamano36f25872006-11-26 12:47:52 -0800323 if (!S_ISREG(ntohl(ce->ce_mode)))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700324 continue;
Junio C Hamano13626712006-05-01 15:58:29 -0700325 if (!pathspec_matches(paths, ce->name))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700326 continue;
Junio C Hamano36f25872006-11-26 12:47:52 -0800327 if (cached) {
328 if (ce_stage(ce))
329 continue;
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700330 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
Junio C Hamano36f25872006-11-26 12:47:52 -0800331 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700332 else
333 hit |= grep_file(opt, ce->name);
Junio C Hamano36f25872006-11-26 12:47:52 -0800334 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 Hamano5010cb52006-04-30 23:28:15 -0700341 }
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700342 free_grep_patterns(opt);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700343 return hit;
344}
345
Junio C Hamano13626712006-05-01 15:58:29 -0700346static int grep_tree(struct grep_opt *opt, const char **paths,
Junio C Hamano5010cb52006-04-30 23:28:15 -0700347 struct tree_desc *tree,
348 const char *tree_name, const char *base)
349{
Junio C Hamano5010cb52006-04-30 23:28:15 -0700350 int len;
351 int hit = 0;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700352 struct name_entry entry;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700353 char *down;
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700354 int tn_len = strlen(tree_name);
355 char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700356
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700357 if (tn_len) {
358 tn_len = sprintf(path_buf, "%s:", tree_name);
359 down = path_buf + tn_len;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700360 strcat(down, base);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700361 }
362 else {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700363 down = path_buf;
364 strcpy(down, base);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700365 }
366 len = strlen(path_buf);
367
Linus Torvalds4c068a92006-05-30 09:45:45 -0700368 while (tree_entry(tree, &entry)) {
369 strcpy(path_buf + len, entry.path);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700370
Linus Torvalds4c068a92006-05-30 09:45:45 -0700371 if (S_ISDIR(entry.mode))
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700372 /* Match "abc/" against pathspec to
373 * decide if we want to descend into "abc"
374 * directory.
375 */
Linus Torvalds4c068a92006-05-30 09:45:45 -0700376 strcpy(path_buf + len + entry.pathlen, "/");
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700377
Junio C Hamano13626712006-05-01 15:58:29 -0700378 if (!pathspec_matches(paths, down))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700379 ;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700380 else if (S_ISREG(entry.mode))
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700381 hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
Linus Torvalds4c068a92006-05-30 09:45:45 -0700382 else if (S_ISDIR(entry.mode)) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700383 char type[20];
384 struct tree_desc sub;
385 void *data;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700386 data = read_sha1_file(entry.sha1, type, &sub.size);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700387 if (!data)
388 die("unable to read tree (%s)",
Linus Torvalds4c068a92006-05-30 09:45:45 -0700389 sha1_to_hex(entry.sha1));
Junio C Hamano5010cb52006-04-30 23:28:15 -0700390 sub.buf = data;
Junio C Hamano13626712006-05-01 15:58:29 -0700391 hit |= grep_tree(opt, paths, &sub, tree_name, down);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700392 free(data);
393 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700394 }
395 return hit;
396}
397
Junio C Hamano13626712006-05-01 15:58:29 -0700398static int grep_object(struct grep_opt *opt, const char **paths,
Junio C Hamano5010cb52006-04-30 23:28:15 -0700399 struct object *obj, const char *name)
400{
Linus Torvalds19746322006-07-11 20:45:31 -0700401 if (obj->type == OBJ_BLOB)
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700402 return grep_sha1(opt, obj->sha1, name, 0);
Linus Torvalds19746322006-07-11 20:45:31 -0700403 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700404 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 Hamano13626712006-05-01 15:58:29 -0700412 hit = grep_tree(opt, paths, &tree, name, "");
Junio C Hamano5010cb52006-04-30 23:28:15 -0700413 free(data);
414 return hit;
415 }
Linus Torvalds885a86a2006-06-14 16:45:13 -0700416 die("unable to grep from object of type %s", typename(obj->type));
Junio C Hamano5010cb52006-04-30 23:28:15 -0700417}
418
419static const char builtin_grep_usage[] =
420"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
421
Junio C Hamano088b0842006-07-04 02:44:48 -0700422static const char emsg_invalid_context_len[] =
423"%s: invalid context length argument";
424static const char emsg_missing_context_len[] =
425"missing context length argument";
426static const char emsg_missing_argument[] =
427"option requires an argument -%s";
428
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700429int cmd_grep(int argc, const char **argv, const char *prefix)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700430{
Junio C Hamano5010cb52006-04-30 23:28:15 -0700431 int hit = 0;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700432 int cached = 0;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700433 int seen_dashdash = 0;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700434 struct grep_opt opt;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700435 struct object_array list = { 0, 0, NULL };
Junio C Hamano13626712006-05-01 15:58:29 -0700436 const char **paths = NULL;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700437 int i;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700438
439 memset(&opt, 0, sizeof(opt));
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700440 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
441 opt.relative = 1;
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700442 opt.pathname = 1;
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700443 opt.pattern_tail = &opt.pattern_list;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700444 opt.regflags = REG_NEWLINE;
445
446 /*
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700447 * 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 Roskin82e5a822006-07-10 01:50:18 -0400453 * unrecognized non option is the beginning of the refs list
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700454 * that continues up to the -- (if exists), and then paths.
Junio C Hamano5010cb52006-04-30 23:28:15 -0700455 */
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700456
Junio C Hamano13626712006-05-01 15:58:29 -0700457 while (1 < argc) {
458 const char *arg = argv[1];
459 argc--; argv++;
460 if (!strcmp("--cached", arg)) {
461 cached = 1;
462 continue;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700463 }
Junio C Hamanob8d0f5a2006-05-03 21:05:29 -0700464 if (!strcmp("-a", arg) ||
465 !strcmp("--text", arg)) {
466 opt.binary = GREP_BINARY_TEXT;
467 continue;
468 }
Junio C Hamano13626712006-05-01 15:58:29 -0700469 if (!strcmp("-i", arg) ||
470 !strcmp("--ignore-case", arg)) {
471 opt.regflags |= REG_ICASE;
472 continue;
473 }
Junio C Hamanob8d0f5a2006-05-03 21:05:29 -0700474 if (!strcmp("-I", arg)) {
475 opt.binary = GREP_BINARY_NOMATCH;
476 continue;
477 }
Junio C Hamano13626712006-05-01 15:58:29 -0700478 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 Hamano07ea91d2006-05-09 18:28:41 -0700488 if (!strcmp("-F", arg) ||
489 !strcmp("--fixed-strings", arg)) {
490 opt.fixed = 1;
491 continue;
492 }
Junio C Hamano13626712006-05-01 15:58:29 -0700493 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 Torvalds7977f0e2006-09-14 10:45:12 -0700502 if (!strcmp("-h", arg)) {
503 opt.pathname = 0;
504 continue;
505 }
Junio C Hamano13626712006-05-01 15:58:29 -0700506 if (!strcmp("-H", arg)) {
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700507 opt.pathname = 1;
Junio C Hamano13626712006-05-01 15:58:29 -0700508 continue;
509 }
510 if (!strcmp("-l", arg) ||
511 !strcmp("--files-with-matches", arg)) {
512 opt.name_only = 1;
513 continue;
514 }
Junio C Hamanoe23d2d62006-05-03 21:46:29 -0700515 if (!strcmp("-L", arg) ||
516 !strcmp("--files-without-match", arg)) {
517 opt.unmatch_name_only = 1;
518 continue;
519 }
Junio C Hamano2c866cf2006-05-02 15:40:49 -0700520 if (!strcmp("-c", arg) ||
521 !strcmp("--count", arg)) {
522 opt.count = 1;
523 continue;
524 }
Junio C Hamano7839a252006-05-02 15:40:49 -0700525 if (!strcmp("-w", arg) ||
526 !strcmp("--word-regexp", arg)) {
527 opt.word_regexp = 1;
528 continue;
529 }
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700530 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 Hamano13626712006-05-01 15:58:29 -0700534 unsigned num;
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700535 const char *scan;
536 switch (arg[1]) {
537 case 'A': case 'B': case 'C':
538 if (!arg[2]) {
539 if (argc <= 1)
Junio C Hamano088b0842006-07-04 02:44:48 -0700540 die(emsg_missing_context_len);
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700541 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 Hamano088b0842006-07-04 02:44:48 -0700552 die(emsg_invalid_context_len, scan);
Junio C Hamano13626712006-05-01 15:58:29 -0700553 switch (arg[1]) {
554 case 'A':
555 opt.post_context = num;
556 break;
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700557 default:
Junio C Hamano13626712006-05-01 15:58:29 -0700558 case 'C':
559 opt.post_context = num;
560 case 'B':
561 opt.pre_context = num;
562 break;
563 }
564 continue;
565 }
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700566 if (!strcmp("-f", arg)) {
567 FILE *patterns;
568 int lno = 0;
569 char buf[1024];
570 if (argc <= 1)
Junio C Hamano088b0842006-07-04 02:44:48 -0700571 die(emsg_missing_argument, arg);
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700572 patterns = fopen(argv[1], "r");
573 if (!patterns)
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700574 die("'%s': %s", argv[1], strerror(errno));
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700575 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 Hamano83b5d2f2006-09-17 16:02:52 -0700582 append_grep_pattern(&opt, xstrdup(buf),
583 argv[1], ++lno,
584 GREP_PATTERN);
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700585 }
586 fclose(patterns);
587 argv++;
588 argc--;
589 continue;
590 }
Junio C Hamano79d36962006-06-30 03:04:05 -0700591 if (!strcmp("--not", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700592 append_grep_pattern(&opt, arg, "command line", 0,
593 GREP_NOT);
Junio C Hamano79d36962006-06-30 03:04:05 -0700594 continue;
595 }
596 if (!strcmp("--and", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700597 append_grep_pattern(&opt, arg, "command line", 0,
598 GREP_AND);
Junio C Hamano79d36962006-06-30 03:04:05 -0700599 continue;
600 }
601 if (!strcmp("--or", arg))
602 continue; /* no-op */
603 if (!strcmp("(", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700604 append_grep_pattern(&opt, arg, "command line", 0,
605 GREP_OPEN_PAREN);
Junio C Hamano79d36962006-06-30 03:04:05 -0700606 continue;
607 }
608 if (!strcmp(")", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700609 append_grep_pattern(&opt, arg, "command line", 0,
610 GREP_CLOSE_PAREN);
Junio C Hamano79d36962006-06-30 03:04:05 -0700611 continue;
612 }
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700613 if (!strcmp("--all-match", arg)) {
614 opt.all_match = 1;
615 continue;
616 }
Junio C Hamano13626712006-05-01 15:58:29 -0700617 if (!strcmp("-e", arg)) {
618 if (1 < argc) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700619 append_grep_pattern(&opt, argv[1],
620 "-e option", 0,
621 GREP_PATTERN);
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700622 argv++;
Junio C Hamano13626712006-05-01 15:58:29 -0700623 argc--;
624 continue;
625 }
Junio C Hamano088b0842006-07-04 02:44:48 -0700626 die(emsg_missing_argument, arg);
Junio C Hamano13626712006-05-01 15:58:29 -0700627 }
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700628 if (!strcmp("--full-name", arg)) {
629 opt.relative = 0;
630 continue;
631 }
Junio C Hamano53905902006-07-04 02:31:50 -0700632 if (!strcmp("--", arg)) {
633 /* later processing wants to have this at argv[1] */
634 argv--;
635 argc++;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700636 break;
Junio C Hamano53905902006-07-04 02:31:50 -0700637 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700638 if (*arg == '-')
Junio C Hamano13626712006-05-01 15:58:29 -0700639 usage(builtin_grep_usage);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700640
641 /* First unrecognized non-option token */
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700642 if (!opt.pattern_list) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700643 append_grep_pattern(&opt, arg, "command line", 0,
644 GREP_PATTERN);
Junio C Hamano13626712006-05-01 15:58:29 -0700645 break;
646 }
647 else {
648 /* We are looking at the first path or rev;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700649 * it is found at argv[1] after leaving the
Junio C Hamano13626712006-05-01 15:58:29 -0700650 * loop.
651 */
652 argc++; argv--;
653 break;
654 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700655 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700656
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700657 if (!opt.pattern_list)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700658 die("no pattern given.");
Junio C Hamano07ea91d2006-05-09 18:28:41 -0700659 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
660 die("cannot mix --fixed-strings and regexp");
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700661 compile_grep_patterns(&opt);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700662
663 /* Check revs and then paths */
664 for (i = 1; i < argc; i++) {
665 const char *arg = argv[i];
Junio C Hamano13626712006-05-01 15:58:29 -0700666 unsigned char sha1[20];
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700667 /* Is it a rev? */
668 if (!get_sha1(arg, sha1)) {
669 struct object *object = parse_object(sha1);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700670 if (!object)
671 die("bad object %s", arg);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700672 add_object_array(object, arg, &list);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700673 continue;
674 }
675 if (!strcmp(arg, "--")) {
676 i++;
677 seen_dashdash = 1;
678 }
679 break;
Junio C Hamano13626712006-05-01 15:58:29 -0700680 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700681
682 /* The rest are paths */
683 if (!seen_dashdash) {
684 int j;
Junio C Hamanoc39c4f42006-05-09 18:15:21 -0700685 for (j = i; j < argc; j++)
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700686 verify_filename(prefix, argv[j]);
687 }
688
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700689 if (i < argc) {
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700690 paths = get_pathspec(prefix, argv + i);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700691 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 Hamano13626712006-05-01 15:58:29 -0700698 else if (prefix) {
699 paths = xcalloc(2, sizeof(const char *));
700 paths[0] = prefix;
701 paths[1] = NULL;
702 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700703
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700704 if (!list.nr)
Junio C Hamano13626712006-05-01 15:58:29 -0700705 return !grep_cache(&opt, paths, cached);
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700706
Junio C Hamano5010cb52006-04-30 23:28:15 -0700707 if (cached)
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700708 die("both --cached and trees are given.");
Junio C Hamano5010cb52006-04-30 23:28:15 -0700709
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700710 for (i = 0; i < list.nr; i++) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700711 struct object *real_obj;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700712 real_obj = deref_tag(list.objects[i].item, NULL, 0);
713 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700714 hit = 1;
715 }
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700716 free_grep_patterns(&opt);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700717 return !hit;
718}