blob: 3a51662a35878ae6b5965c90abb747b5b27c5493 [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
Jeff King5f7c6432008-03-12 17:39:16 -040015#ifndef NO_EXTERNAL_GREP
16#ifdef __unix__
17#define NO_EXTERNAL_GREP 0
18#else
19#define NO_EXTERNAL_GREP 1
20#endif
21#endif
22
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070023/*
24 * git grep pathspecs are somewhat different from diff-tree pathspecs;
25 * pathname wildcards are allowed.
26 */
Junio C Hamano13626712006-05-01 15:58:29 -070027static int pathspec_matches(const char **paths, const char *name)
Junio C Hamano5010cb52006-04-30 23:28:15 -070028{
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070029 int namelen, i;
Junio C Hamano13626712006-05-01 15:58:29 -070030 if (!paths || !*paths)
Junio C Hamano5010cb52006-04-30 23:28:15 -070031 return 1;
32 namelen = strlen(name);
Junio C Hamano13626712006-05-01 15:58:29 -070033 for (i = 0; paths[i]; i++) {
34 const char *match = paths[i];
35 int matchlen = strlen(match);
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070036 const char *cp, *meta;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070037
Uwe Zeisbergerbb9e15a2006-06-21 11:04:12 +020038 if (!matchlen ||
39 ((matchlen <= namelen) &&
40 !strncmp(name, match, matchlen) &&
41 (match[matchlen-1] == '/' ||
42 name[matchlen] == '\0' || name[matchlen] == '/')))
Junio C Hamano5010cb52006-04-30 23:28:15 -070043 return 1;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070044 if (!fnmatch(match, name, 0))
45 return 1;
46 if (name[namelen-1] != '/')
47 continue;
48
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070049 /* We are being asked if the directory ("name") is worth
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070050 * descending into.
51 *
52 * Find the longest leading directory name that does
53 * not have metacharacter in the pathspec; the name
54 * we are looking at must overlap with that directory.
55 */
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070056 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070057 char ch = *cp;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070058 if (ch == '*' || ch == '[' || ch == '?') {
59 meta = cp;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070060 break;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070061 }
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070062 }
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070063 if (!meta)
64 meta = cp; /* fully literal */
65
66 if (namelen <= meta - match) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070067 /* Looking at "Documentation/" and
68 * the pattern says "Documentation/howto/", or
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070069 * "Documentation/diff*.txt". The name we
70 * have should match prefix.
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070071 */
72 if (!memcmp(match, name, namelen))
73 return 1;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070074 continue;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070075 }
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070076
77 if (meta - match < namelen) {
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070078 /* Looking at "Documentation/howto/" and
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070079 * the pattern says "Documentation/h*";
80 * match up to "Do.../h"; this avoids descending
81 * into "Documentation/technical/".
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070082 */
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070083 if (!memcmp(match, name, meta - match))
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070084 return 1;
Junio C Hamano1e3d90e2006-05-02 17:27:07 -070085 continue;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -070086 }
Junio C Hamano5010cb52006-04-30 23:28:15 -070087 }
88 return 0;
89}
90
Junio C Hamano0d042fe2006-08-11 00:44:42 -070091static 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 -070092{
93 unsigned long size;
94 char *data;
Nicolas Pitre21666f12007-02-26 14:55:59 -050095 enum object_type type;
Junio C Hamano0d042fe2006-08-11 00:44:42 -070096 char *to_free = NULL;
Junio C Hamano5010cb52006-04-30 23:28:15 -070097 int hit;
Junio C Hamano0d042fe2006-08-11 00:44:42 -070098
Nicolas Pitre21666f12007-02-26 14:55:59 -050099 data = read_sha1_file(sha1, &type, &size);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700100 if (!data) {
101 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
102 return 0;
103 }
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700104 if (opt->relative && opt->prefix_length) {
105 static char name_buf[PATH_MAX];
106 char *cp;
107 int name_len = strlen(name) - opt->prefix_length + 1;
108
109 if (!tree_name_len)
110 name += opt->prefix_length;
111 else {
112 if (ARRAY_SIZE(name_buf) <= name_len)
113 cp = to_free = xmalloc(name_len);
114 else
115 cp = name_buf;
116 memcpy(cp, name, tree_name_len);
117 strcpy(cp + tree_name_len,
118 name + tree_name_len + opt->prefix_length);
119 name = cp;
120 }
121 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700122 hit = grep_buffer(opt, name, data, size);
123 free(data);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700124 free(to_free);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700125 return hit;
126}
127
128static int grep_file(struct grep_opt *opt, const char *filename)
129{
130 struct stat st;
131 int i;
132 char *data;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500133 size_t sz;
134
Junio C Hamano5010cb52006-04-30 23:28:15 -0700135 if (lstat(filename, &st) < 0) {
136 err_ret:
137 if (errno != ENOENT)
138 error("'%s': %s", filename, strerror(errno));
139 return 0;
140 }
141 if (!st.st_size)
142 return 0; /* empty file -- no grep hit */
143 if (!S_ISREG(st.st_mode))
144 return 0;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500145 sz = xsize_t(st.st_size);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700146 i = open(filename, O_RDONLY);
147 if (i < 0)
148 goto err_ret;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500149 data = xmalloc(sz + 1);
150 if (st.st_size != read_in_full(i, data, sz)) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700151 error("'%s': short read %s", filename, strerror(errno));
152 close(i);
153 free(data);
154 return 0;
155 }
156 close(i);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700157 if (opt->relative && opt->prefix_length)
158 filename += opt->prefix_length;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500159 i = grep_buffer(opt, filename, data, sz);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700160 free(data);
161 return i;
162}
163
Jeff King5f7c6432008-03-12 17:39:16 -0400164#if !NO_EXTERNAL_GREP
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700165static int exec_grep(int argc, const char **argv)
166{
167 pid_t pid;
168 int status;
169
170 argv[argc] = NULL;
171 pid = fork();
172 if (pid < 0)
173 return pid;
174 if (!pid) {
175 execvp("grep", (char **) argv);
176 exit(255);
177 }
178 while (waitpid(pid, &status, 0) < 0) {
179 if (errno == EINTR)
180 continue;
181 return -1;
182 }
183 if (WIFEXITED(status)) {
184 if (!WEXITSTATUS(status))
185 return 1;
186 return 0;
187 }
188 return -1;
189}
190
191#define MAXARGS 1000
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700192#define ARGBUF 4096
193#define push_arg(a) do { \
194 if (nr < MAXARGS) argv[nr++] = (a); \
195 else die("maximum number of args exceeded"); \
196 } while (0)
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700197
Junio C Hamanod99ebf02007-09-14 00:31:00 -0700198/*
199 * If you send a singleton filename to grep, it does not give
200 * the name of the file. GNU grep has "-H" but we would want
201 * that behaviour in a portable way.
202 *
203 * So we keep two pathnames in argv buffer unsent to grep in
204 * the main loop if we need to do more than one grep.
205 */
206static int flush_grep(struct grep_opt *opt,
207 int argc, int arg0, const char **argv, int *kept)
208{
209 int status;
210 int count = argc - arg0;
211 const char *kept_0 = NULL;
212
213 if (count <= 2) {
214 /*
215 * Because we keep at least 2 paths in the call from
216 * the main loop (i.e. kept != NULL), and MAXARGS is
217 * far greater than 2, this usually is a call to
218 * conclude the grep. However, the user could attempt
219 * to overflow the argv buffer by giving too many
220 * options to leave very small number of real
221 * arguments even for the call in the main loop.
222 */
223 if (kept)
224 die("insanely many options to grep");
225
226 /*
227 * If we have two or more paths, we do not have to do
228 * anything special, but we need to push /dev/null to
229 * get "-H" behaviour of GNU grep portably but when we
230 * are not doing "-l" nor "-L" nor "-c".
231 */
232 if (count == 1 &&
233 !opt->name_only &&
234 !opt->unmatch_name_only &&
235 !opt->count) {
236 argv[argc++] = "/dev/null";
237 argv[argc] = NULL;
238 }
239 }
240
241 else if (kept) {
242 /*
243 * Called because we found many paths and haven't finished
244 * iterating over the cache yet. We keep two paths
245 * for the concluding call. argv[argc-2] and argv[argc-1]
246 * has the last two paths, so save the first one away,
247 * replace it with NULL while sending the list to grep,
248 * and recover them after we are done.
249 */
250 *kept = 2;
251 kept_0 = argv[argc-2];
252 argv[argc-2] = NULL;
253 argc -= 2;
254 }
255
256 status = exec_grep(argc, argv);
257
258 if (kept_0) {
259 /*
260 * Then recover them. Now the last arg is beyond the
261 * terminating NULL which is at argc, and the second
262 * from the last is what we saved away in kept_0
263 */
264 argv[arg0++] = kept_0;
265 argv[arg0] = argv[argc+1];
266 }
267 return status;
268}
269
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700270static int external_grep(struct grep_opt *opt, const char **paths, int cached)
271{
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700272 int i, nr, argc, hit, len, status;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700273 const char *argv[MAXARGS+1];
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700274 char randarg[ARGBUF];
275 char *argptr = randarg;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700276 struct grep_pat *p;
277
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700278 if (opt->extended || (opt->relative && opt->prefix_length))
Junio C Hamano79d36962006-06-30 03:04:05 -0700279 return -1;
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700280 len = nr = 0;
281 push_arg("grep");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700282 if (opt->fixed)
Linus Torvaldsf6647512006-05-15 17:54:01 -0700283 push_arg("-F");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700284 if (opt->linenum)
285 push_arg("-n");
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700286 if (!opt->pathname)
287 push_arg("-h");
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700288 if (opt->regflags & REG_EXTENDED)
289 push_arg("-E");
Robert Fitzsimons30264022006-06-06 23:15:16 +0000290 if (opt->regflags & REG_ICASE)
291 push_arg("-i");
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700292 if (opt->word_regexp)
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700293 push_arg("-w");
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700294 if (opt->name_only)
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700295 push_arg("-l");
296 if (opt->unmatch_name_only)
297 push_arg("-L");
298 if (opt->count)
299 push_arg("-c");
300 if (opt->post_context || opt->pre_context) {
301 if (opt->post_context != opt->pre_context) {
302 if (opt->pre_context) {
303 push_arg("-B");
304 len += snprintf(argptr, sizeof(randarg)-len,
Junio C Hamano4b874742007-11-17 21:18:14 -0800305 "%u", opt->pre_context) + 1;
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700306 if (sizeof(randarg) <= len)
307 die("maximum length of args exceeded");
308 push_arg(argptr);
309 argptr += len;
310 }
311 if (opt->post_context) {
312 push_arg("-A");
313 len += snprintf(argptr, sizeof(randarg)-len,
Junio C Hamano4b874742007-11-17 21:18:14 -0800314 "%u", opt->post_context) + 1;
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700315 if (sizeof(randarg) <= len)
316 die("maximum length of args exceeded");
317 push_arg(argptr);
318 argptr += len;
319 }
320 }
321 else {
322 push_arg("-C");
323 len += snprintf(argptr, sizeof(randarg)-len,
Junio C Hamano4b874742007-11-17 21:18:14 -0800324 "%u", opt->post_context) + 1;
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700325 if (sizeof(randarg) <= len)
326 die("maximum length of args exceeded");
327 push_arg(argptr);
328 argptr += len;
329 }
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700330 }
Junio C Hamanoffa0a7a2006-05-15 13:28:01 -0700331 for (p = opt->pattern_list; p; p = p->next) {
332 push_arg("-e");
333 push_arg(p->pattern);
334 }
Linus Torvaldsbbb66c62006-05-17 11:12:22 -0700335
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700336 hit = 0;
337 argc = nr;
338 for (i = 0; i < active_nr; i++) {
339 struct cache_entry *ce = active_cache[i];
Alex Riesenfbd01ab2006-05-21 22:45:46 +0200340 char *name;
Junio C Hamanod99ebf02007-09-14 00:31:00 -0700341 int kept;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800342 if (!S_ISREG(ce->ce_mode))
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700343 continue;
344 if (!pathspec_matches(paths, ce->name))
345 continue;
Linus Torvaldsbbb66c62006-05-17 11:12:22 -0700346 name = ce->name;
347 if (name[0] == '-') {
348 int len = ce_namelen(ce);
349 name = xmalloc(len + 3);
350 memcpy(name, "./", 2);
351 memcpy(name + 2, ce->name, len + 1);
352 }
353 argv[argc++] = name;
Junio C Hamano6326cee2007-12-05 16:13:08 -0800354 if (MAXARGS <= argc) {
355 status = flush_grep(opt, argc, nr, argv, &kept);
356 if (0 < status)
357 hit = 1;
358 argc = nr + kept;
359 }
Junio C Hamano36f25872006-11-26 12:47:52 -0800360 if (ce_stage(ce)) {
361 do {
362 i++;
363 } while (i < active_nr &&
364 !strcmp(ce->name, active_cache[i]->name));
365 i--; /* compensate for loop control */
366 }
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700367 }
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700368 if (argc > nr) {
Junio C Hamanod99ebf02007-09-14 00:31:00 -0700369 status = flush_grep(opt, argc, nr, argv, NULL);
Junio C Hamanofcfe34b2006-07-04 02:43:40 -0700370 if (0 < status)
371 hit = 1;
372 }
373 return hit;
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700374}
Shawn O. Pearceff1f9942007-03-06 20:44:14 -0500375#endif
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700376
Junio C Hamano13626712006-05-01 15:58:29 -0700377static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700378{
379 int hit = 0;
380 int nr;
381 read_cache();
382
Jeff King5f7c6432008-03-12 17:39:16 -0400383#if !NO_EXTERNAL_GREP
Linus Torvalds1e2398d2006-05-14 20:49:15 -0700384 /*
385 * Use the external "grep" command for the case where
386 * we grep through the checked-out files. It tends to
387 * be a lot more optimized
388 */
389 if (!cached) {
390 hit = external_grep(opt, paths, cached);
391 if (hit >= 0)
392 return hit;
393 }
394#endif
395
Junio C Hamano5010cb52006-04-30 23:28:15 -0700396 for (nr = 0; nr < active_nr; nr++) {
397 struct cache_entry *ce = active_cache[nr];
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800398 if (!S_ISREG(ce->ce_mode))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700399 continue;
Junio C Hamano13626712006-05-01 15:58:29 -0700400 if (!pathspec_matches(paths, ce->name))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700401 continue;
Junio C Hamano36f25872006-11-26 12:47:52 -0800402 if (cached) {
403 if (ce_stage(ce))
404 continue;
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700405 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
Junio C Hamano36f25872006-11-26 12:47:52 -0800406 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700407 else
408 hit |= grep_file(opt, ce->name);
Junio C Hamano36f25872006-11-26 12:47:52 -0800409 if (ce_stage(ce)) {
410 do {
411 nr++;
412 } while (nr < active_nr &&
413 !strcmp(ce->name, active_cache[nr]->name));
414 nr--; /* compensate for loop control */
415 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700416 }
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700417 free_grep_patterns(opt);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700418 return hit;
419}
420
Junio C Hamano13626712006-05-01 15:58:29 -0700421static int grep_tree(struct grep_opt *opt, const char **paths,
Junio C Hamano5010cb52006-04-30 23:28:15 -0700422 struct tree_desc *tree,
423 const char *tree_name, const char *base)
424{
Junio C Hamano5010cb52006-04-30 23:28:15 -0700425 int len;
426 int hit = 0;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700427 struct name_entry entry;
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700428 char *down;
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700429 int tn_len = strlen(tree_name);
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400430 struct strbuf pathbuf;
431
432 strbuf_init(&pathbuf, PATH_MAX + tn_len);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700433
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700434 if (tn_len) {
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400435 strbuf_add(&pathbuf, tree_name, tn_len);
436 strbuf_addch(&pathbuf, ':');
437 tn_len = pathbuf.len;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700438 }
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400439 strbuf_addstr(&pathbuf, base);
440 len = pathbuf.len;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700441
Linus Torvalds4c068a92006-05-30 09:45:45 -0700442 while (tree_entry(tree, &entry)) {
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400443 int te_len = tree_entry_len(entry.path, entry.sha1);
444 pathbuf.len = len;
445 strbuf_add(&pathbuf, entry.path, te_len);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700446
Linus Torvalds4c068a92006-05-30 09:45:45 -0700447 if (S_ISDIR(entry.mode))
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700448 /* Match "abc/" against pathspec to
449 * decide if we want to descend into "abc"
450 * directory.
451 */
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400452 strbuf_addch(&pathbuf, '/');
Junio C Hamanoe0eb8892006-05-01 12:27:56 -0700453
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400454 down = pathbuf.buf + tn_len;
Junio C Hamano13626712006-05-01 15:58:29 -0700455 if (!pathspec_matches(paths, down))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700456 ;
Linus Torvalds4c068a92006-05-30 09:45:45 -0700457 else if (S_ISREG(entry.mode))
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400458 hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
Linus Torvalds4c068a92006-05-30 09:45:45 -0700459 else if (S_ISDIR(entry.mode)) {
Nicolas Pitre21666f12007-02-26 14:55:59 -0500460 enum object_type type;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700461 struct tree_desc sub;
462 void *data;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700463 unsigned long size;
464
465 data = read_sha1_file(entry.sha1, &type, &size);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700466 if (!data)
467 die("unable to read tree (%s)",
Linus Torvalds4c068a92006-05-30 09:45:45 -0700468 sha1_to_hex(entry.sha1));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700469 init_tree_desc(&sub, data, size);
Junio C Hamano13626712006-05-01 15:58:29 -0700470 hit |= grep_tree(opt, paths, &sub, tree_name, down);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700471 free(data);
472 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700473 }
Dmitry Potapov620e2bb2008-07-16 19:33:29 +0400474 strbuf_release(&pathbuf);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700475 return hit;
476}
477
Junio C Hamano13626712006-05-01 15:58:29 -0700478static int grep_object(struct grep_opt *opt, const char **paths,
Junio C Hamano5010cb52006-04-30 23:28:15 -0700479 struct object *obj, const char *name)
480{
Linus Torvalds19746322006-07-11 20:45:31 -0700481 if (obj->type == OBJ_BLOB)
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700482 return grep_sha1(opt, obj->sha1, name, 0);
Linus Torvalds19746322006-07-11 20:45:31 -0700483 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700484 struct tree_desc tree;
485 void *data;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700486 unsigned long size;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700487 int hit;
488 data = read_object_with_reference(obj->sha1, tree_type,
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700489 &size, NULL);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700490 if (!data)
491 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700492 init_tree_desc(&tree, data, size);
Junio C Hamano13626712006-05-01 15:58:29 -0700493 hit = grep_tree(opt, paths, &tree, name, "");
Junio C Hamano5010cb52006-04-30 23:28:15 -0700494 free(data);
495 return hit;
496 }
Linus Torvalds885a86a2006-06-14 16:45:13 -0700497 die("unable to grep from object of type %s", typename(obj->type));
Junio C Hamano5010cb52006-04-30 23:28:15 -0700498}
499
500static const char builtin_grep_usage[] =
Junio C Hamanofa4946b2008-07-20 17:16:29 -0700501"git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]";
Junio C Hamano5010cb52006-04-30 23:28:15 -0700502
Junio C Hamano088b0842006-07-04 02:44:48 -0700503static const char emsg_invalid_context_len[] =
504"%s: invalid context length argument";
505static const char emsg_missing_context_len[] =
506"missing context length argument";
507static const char emsg_missing_argument[] =
508"option requires an argument -%s";
509
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700510int cmd_grep(int argc, const char **argv, const char *prefix)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700511{
Junio C Hamano5010cb52006-04-30 23:28:15 -0700512 int hit = 0;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700513 int cached = 0;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700514 int seen_dashdash = 0;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700515 struct grep_opt opt;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700516 struct object_array list = { 0, 0, NULL };
Junio C Hamano13626712006-05-01 15:58:29 -0700517 const char **paths = NULL;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700518 int i;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700519
520 memset(&opt, 0, sizeof(opt));
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700521 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
522 opt.relative = 1;
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700523 opt.pathname = 1;
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700524 opt.pattern_tail = &opt.pattern_list;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700525 opt.regflags = REG_NEWLINE;
526
527 /*
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700528 * If there is no -- then the paths must exist in the working
529 * tree. If there is no explicit pattern specified with -e or
530 * -f, we take the first unrecognized non option to be the
531 * pattern, but then what follows it must be zero or more
532 * valid refs up to the -- (if exists), and then existing
533 * paths. If there is an explicit pattern, then the first
Pavel Roskin82e5a822006-07-10 01:50:18 -0400534 * unrecognized non option is the beginning of the refs list
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700535 * that continues up to the -- (if exists), and then paths.
Junio C Hamano5010cb52006-04-30 23:28:15 -0700536 */
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700537
Junio C Hamano13626712006-05-01 15:58:29 -0700538 while (1 < argc) {
539 const char *arg = argv[1];
540 argc--; argv++;
541 if (!strcmp("--cached", arg)) {
542 cached = 1;
543 continue;
Junio C Hamano5010cb52006-04-30 23:28:15 -0700544 }
Junio C Hamanob8d0f5a2006-05-03 21:05:29 -0700545 if (!strcmp("-a", arg) ||
546 !strcmp("--text", arg)) {
547 opt.binary = GREP_BINARY_TEXT;
548 continue;
549 }
Junio C Hamano13626712006-05-01 15:58:29 -0700550 if (!strcmp("-i", arg) ||
551 !strcmp("--ignore-case", arg)) {
552 opt.regflags |= REG_ICASE;
553 continue;
554 }
Junio C Hamanob8d0f5a2006-05-03 21:05:29 -0700555 if (!strcmp("-I", arg)) {
556 opt.binary = GREP_BINARY_NOMATCH;
557 continue;
558 }
Junio C Hamano13626712006-05-01 15:58:29 -0700559 if (!strcmp("-v", arg) ||
560 !strcmp("--invert-match", arg)) {
561 opt.invert = 1;
562 continue;
563 }
564 if (!strcmp("-E", arg) ||
565 !strcmp("--extended-regexp", arg)) {
566 opt.regflags |= REG_EXTENDED;
567 continue;
568 }
Junio C Hamano07ea91d2006-05-09 18:28:41 -0700569 if (!strcmp("-F", arg) ||
570 !strcmp("--fixed-strings", arg)) {
571 opt.fixed = 1;
572 continue;
573 }
Junio C Hamano13626712006-05-01 15:58:29 -0700574 if (!strcmp("-G", arg) ||
575 !strcmp("--basic-regexp", arg)) {
576 opt.regflags &= ~REG_EXTENDED;
577 continue;
578 }
579 if (!strcmp("-n", arg)) {
580 opt.linenum = 1;
581 continue;
582 }
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700583 if (!strcmp("-h", arg)) {
584 opt.pathname = 0;
585 continue;
586 }
Junio C Hamano13626712006-05-01 15:58:29 -0700587 if (!strcmp("-H", arg)) {
Linus Torvalds7977f0e2006-09-14 10:45:12 -0700588 opt.pathname = 1;
Junio C Hamano13626712006-05-01 15:58:29 -0700589 continue;
590 }
591 if (!strcmp("-l", arg) ||
Shawn O. Pearce2cd5dfd2008-02-20 23:28:07 -0500592 !strcmp("--name-only", arg) ||
Junio C Hamano13626712006-05-01 15:58:29 -0700593 !strcmp("--files-with-matches", arg)) {
594 opt.name_only = 1;
595 continue;
596 }
Junio C Hamanoe23d2d62006-05-03 21:46:29 -0700597 if (!strcmp("-L", arg) ||
598 !strcmp("--files-without-match", arg)) {
599 opt.unmatch_name_only = 1;
600 continue;
601 }
Junio C Hamano2c866cf2006-05-02 15:40:49 -0700602 if (!strcmp("-c", arg) ||
603 !strcmp("--count", arg)) {
604 opt.count = 1;
605 continue;
606 }
Junio C Hamano7839a252006-05-02 15:40:49 -0700607 if (!strcmp("-w", arg) ||
608 !strcmp("--word-regexp", arg)) {
609 opt.word_regexp = 1;
610 continue;
611 }
Junio C Hamano599065a2007-02-20 01:54:00 -0800612 if (!prefixcmp(arg, "-A") ||
613 !prefixcmp(arg, "-B") ||
614 !prefixcmp(arg, "-C") ||
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700615 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
Junio C Hamano13626712006-05-01 15:58:29 -0700616 unsigned num;
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700617 const char *scan;
618 switch (arg[1]) {
619 case 'A': case 'B': case 'C':
620 if (!arg[2]) {
621 if (argc <= 1)
Junio C Hamano088b0842006-07-04 02:44:48 -0700622 die(emsg_missing_context_len);
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700623 scan = *++argv;
624 argc--;
625 }
626 else
627 scan = arg + 2;
628 break;
629 default:
630 scan = arg + 1;
631 break;
632 }
Jim Meyering6aead432007-04-10 01:01:44 +0200633 if (strtoul_ui(scan, 10, &num))
Junio C Hamano088b0842006-07-04 02:44:48 -0700634 die(emsg_invalid_context_len, scan);
Junio C Hamano13626712006-05-01 15:58:29 -0700635 switch (arg[1]) {
636 case 'A':
637 opt.post_context = num;
638 break;
Junio C Hamanof462ebb2006-05-02 15:17:05 -0700639 default:
Junio C Hamano13626712006-05-01 15:58:29 -0700640 case 'C':
641 opt.post_context = num;
642 case 'B':
643 opt.pre_context = num;
644 break;
645 }
646 continue;
647 }
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700648 if (!strcmp("-f", arg)) {
649 FILE *patterns;
650 int lno = 0;
651 char buf[1024];
652 if (argc <= 1)
Junio C Hamano088b0842006-07-04 02:44:48 -0700653 die(emsg_missing_argument, arg);
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700654 patterns = fopen(argv[1], "r");
655 if (!patterns)
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700656 die("'%s': %s", argv[1], strerror(errno));
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700657 while (fgets(buf, sizeof(buf), patterns)) {
658 int len = strlen(buf);
Jim Meyering872c9302008-01-04 18:37:41 +0100659 if (len && buf[len-1] == '\n')
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700660 buf[len-1] = 0;
661 /* ignore empty line like grep does */
662 if (!buf[0])
663 continue;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700664 append_grep_pattern(&opt, xstrdup(buf),
665 argv[1], ++lno,
666 GREP_PATTERN);
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700667 }
668 fclose(patterns);
669 argv++;
670 argc--;
671 continue;
672 }
Junio C Hamano79d36962006-06-30 03:04:05 -0700673 if (!strcmp("--not", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700674 append_grep_pattern(&opt, arg, "command line", 0,
675 GREP_NOT);
Junio C Hamano79d36962006-06-30 03:04:05 -0700676 continue;
677 }
678 if (!strcmp("--and", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700679 append_grep_pattern(&opt, arg, "command line", 0,
680 GREP_AND);
Junio C Hamano79d36962006-06-30 03:04:05 -0700681 continue;
682 }
683 if (!strcmp("--or", arg))
684 continue; /* no-op */
685 if (!strcmp("(", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700686 append_grep_pattern(&opt, arg, "command line", 0,
687 GREP_OPEN_PAREN);
Junio C Hamano79d36962006-06-30 03:04:05 -0700688 continue;
689 }
690 if (!strcmp(")", arg)) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700691 append_grep_pattern(&opt, arg, "command line", 0,
692 GREP_CLOSE_PAREN);
Junio C Hamano79d36962006-06-30 03:04:05 -0700693 continue;
694 }
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700695 if (!strcmp("--all-match", arg)) {
696 opt.all_match = 1;
697 continue;
698 }
Junio C Hamano13626712006-05-01 15:58:29 -0700699 if (!strcmp("-e", arg)) {
700 if (1 < argc) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700701 append_grep_pattern(&opt, argv[1],
702 "-e option", 0,
703 GREP_PATTERN);
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700704 argv++;
Junio C Hamano13626712006-05-01 15:58:29 -0700705 argc--;
706 continue;
707 }
Junio C Hamano088b0842006-07-04 02:44:48 -0700708 die(emsg_missing_argument, arg);
Junio C Hamano13626712006-05-01 15:58:29 -0700709 }
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700710 if (!strcmp("--full-name", arg)) {
711 opt.relative = 0;
712 continue;
713 }
Junio C Hamano53905902006-07-04 02:31:50 -0700714 if (!strcmp("--", arg)) {
715 /* later processing wants to have this at argv[1] */
716 argv--;
717 argc++;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700718 break;
Junio C Hamano53905902006-07-04 02:31:50 -0700719 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700720 if (*arg == '-')
Junio C Hamano13626712006-05-01 15:58:29 -0700721 usage(builtin_grep_usage);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700722
723 /* First unrecognized non-option token */
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700724 if (!opt.pattern_list) {
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700725 append_grep_pattern(&opt, arg, "command line", 0,
726 GREP_PATTERN);
Junio C Hamano13626712006-05-01 15:58:29 -0700727 break;
728 }
729 else {
730 /* We are looking at the first path or rev;
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700731 * it is found at argv[1] after leaving the
Junio C Hamano13626712006-05-01 15:58:29 -0700732 * loop.
733 */
734 argc++; argv--;
735 break;
736 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700737 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700738
Junio C Hamanof9b9faf2006-05-02 15:40:49 -0700739 if (!opt.pattern_list)
Junio C Hamano5010cb52006-04-30 23:28:15 -0700740 die("no pattern given.");
Junio C Hamano07ea91d2006-05-09 18:28:41 -0700741 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
742 die("cannot mix --fixed-strings and regexp");
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700743 compile_grep_patterns(&opt);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700744
745 /* Check revs and then paths */
746 for (i = 1; i < argc; i++) {
747 const char *arg = argv[i];
Junio C Hamano13626712006-05-01 15:58:29 -0700748 unsigned char sha1[20];
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700749 /* Is it a rev? */
750 if (!get_sha1(arg, sha1)) {
751 struct object *object = parse_object(sha1);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700752 if (!object)
753 die("bad object %s", arg);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700754 add_object_array(object, arg, &list);
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700755 continue;
756 }
757 if (!strcmp(arg, "--")) {
758 i++;
759 seen_dashdash = 1;
760 }
761 break;
Junio C Hamano13626712006-05-01 15:58:29 -0700762 }
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700763
764 /* The rest are paths */
765 if (!seen_dashdash) {
766 int j;
Junio C Hamanoc39c4f42006-05-09 18:15:21 -0700767 for (j = i; j < argc; j++)
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700768 verify_filename(prefix, argv[j]);
769 }
770
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700771 if (i < argc) {
Junio C Hamano5acd64e2006-05-08 23:55:47 -0700772 paths = get_pathspec(prefix, argv + i);
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700773 if (opt.prefix_length && opt.relative) {
774 /* Make sure we do not get outside of paths */
775 for (i = 0; paths[i]; i++)
776 if (strncmp(prefix, paths[i], opt.prefix_length))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700777 die("git grep: cannot generate relative filenames containing '..'");
Junio C Hamano0d042fe2006-08-11 00:44:42 -0700778 }
779 }
Junio C Hamano13626712006-05-01 15:58:29 -0700780 else if (prefix) {
781 paths = xcalloc(2, sizeof(const char *));
782 paths[0] = prefix;
783 paths[1] = NULL;
784 }
Junio C Hamano5010cb52006-04-30 23:28:15 -0700785
Nguyễn Thái Ngọc Duy6577f542008-08-28 20:04:30 +0700786 if (!list.nr) {
787 if (!cached)
788 setup_work_tree();
Junio C Hamano13626712006-05-01 15:58:29 -0700789 return !grep_cache(&opt, paths, cached);
Nguyễn Thái Ngọc Duy6577f542008-08-28 20:04:30 +0700790 }
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700791
Junio C Hamano5010cb52006-04-30 23:28:15 -0700792 if (cached)
Junio C Hamanoaa8c79a2006-05-08 13:28:27 -0700793 die("both --cached and trees are given.");
Junio C Hamano5010cb52006-04-30 23:28:15 -0700794
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700795 for (i = 0; i < list.nr; i++) {
Junio C Hamano5010cb52006-04-30 23:28:15 -0700796 struct object *real_obj;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700797 real_obj = deref_tag(list.objects[i].item, NULL, 0);
798 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
Junio C Hamano5010cb52006-04-30 23:28:15 -0700799 hit = 1;
800 }
Junio C Hamanob48fb5b2006-09-27 16:27:10 -0700801 free_grep_patterns(&opt);
Junio C Hamano5010cb52006-04-30 23:28:15 -0700802 return !hit;
803}