4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
17 * git grep pathspecs are somewhat different from diff-tree pathspecs;
18 * pathname wildcards are allowed.
20 static int pathspec_matches(const char **paths, const char *name)
23 if (!paths || !*paths)
25 namelen = strlen(name);
26 for (i = 0; paths[i]; i++) {
27 const char *match = paths[i];
28 int matchlen = strlen(match);
29 const char *slash, *cp;
31 if ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/'))
36 if (!fnmatch(match, name, 0))
38 if (name[namelen-1] != '/')
41 /* We are being asked if the name directory is worth
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.
48 for (cp = match, slash = NULL; cp - match < matchlen; cp++) {
52 if (ch == '*' || ch == '[')
56 slash = match; /* toplevel */
59 if (namelen <= slash - match) {
60 /* Looking at "Documentation/" and
61 * the pattern says "Documentation/howto/", or
62 * "Documentation/diff*.txt".
64 if (!memcmp(match, name, namelen))
68 /* Looking at "Documentation/howto/" and
69 * the pattern says "Documentation/h*".
71 if (!memcmp(match, name, slash - match))
86 unsigned post_context;
89 static char *end_of_line(char *cp, unsigned long *left)
91 unsigned long l = *left;
92 while (l && *cp != '\n') {
100 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
101 const char *name, unsigned lno, char sign)
103 printf("%s%c", name, sign);
105 printf("%d%c", lno, sign);
106 printf("%.*s\n", (int)(eol-bol), bol);
109 static int grep_buffer(struct grep_opt *opt, const char *name,
110 char *buf, unsigned long size)
113 unsigned long left = size;
115 struct pre_context_line {
118 } *prev = NULL, *pcl;
119 unsigned last_hit = 0;
120 unsigned last_shown = 0;
121 const char *hunk_mark = "";
123 if (opt->pre_context)
124 prev = xcalloc(opt->pre_context, sizeof(*prev));
125 if (opt->pre_context || opt->post_context)
129 regmatch_t pmatch[10];
133 eol = end_of_line(bol, &left);
137 hit = !regexec(&opt->regexp, bol, ARRAY_SIZE(pmatch),
142 if (opt->name_only) {
143 printf("%s\n", name);
146 /* Hit at this line. If we haven't shown the
147 * pre-context lines, we would need to show them.
149 if (opt->pre_context) {
151 if (opt->pre_context < lno)
152 from = lno - opt->pre_context;
155 if (from <= last_shown)
156 from = last_shown + 1;
157 if (last_shown && from != last_shown + 1)
160 pcl = &prev[lno-from-1];
161 show_line(opt, pcl->bol, pcl->eol,
167 if (last_shown && lno != last_shown + 1)
169 show_line(opt, bol, eol, name, lno, ':');
170 last_shown = last_hit = lno;
173 lno <= last_hit + opt->post_context) {
174 /* If the last hit is within the post context,
175 * we need to show this line.
177 if (last_shown && lno != last_shown + 1)
179 show_line(opt, bol, eol, name, lno, '-');
182 if (opt->pre_context) {
183 memmove(prev+1, prev,
184 (opt->pre_context-1) * sizeof(*prev));
196 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
202 data = read_sha1_file(sha1, type, &size);
204 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
207 hit = grep_buffer(opt, name, data, size);
212 static int grep_file(struct grep_opt *opt, const char *filename)
217 if (lstat(filename, &st) < 0) {
220 error("'%s': %s", filename, strerror(errno));
224 return 0; /* empty file -- no grep hit */
225 if (!S_ISREG(st.st_mode))
227 i = open(filename, O_RDONLY);
230 data = xmalloc(st.st_size + 1);
231 if (st.st_size != xread(i, data, st.st_size)) {
232 error("'%s': short read %s", filename, strerror(errno));
238 i = grep_buffer(opt, filename, data, st.st_size);
243 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
249 for (nr = 0; nr < active_nr; nr++) {
250 struct cache_entry *ce = active_cache[nr];
251 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
253 if (!pathspec_matches(paths, ce->name))
256 hit |= grep_sha1(opt, ce->sha1, ce->name);
258 hit |= grep_file(opt, ce->name);
263 static int grep_tree(struct grep_opt *opt, const char **paths,
264 struct tree_desc *tree,
265 const char *tree_name, const char *base)
271 const unsigned char *sha1;
273 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
276 int offset = sprintf(path_buf, "%s:", tree_name);
277 down = path_buf + offset;
284 len = strlen(path_buf);
288 sha1 = tree_entry_extract(tree, &path, &mode);
289 pathlen = strlen(path);
290 strcpy(path_buf + len, path);
293 /* Match "abc/" against pathspec to
294 * decide if we want to descend into "abc"
297 strcpy(path_buf + len + pathlen, "/");
299 if (!pathspec_matches(paths, down))
301 else if (S_ISREG(mode))
302 hit |= grep_sha1(opt, sha1, path_buf);
303 else if (S_ISDIR(mode)) {
305 struct tree_desc sub;
307 data = read_sha1_file(sha1, type, &sub.size);
309 die("unable to read tree (%s)",
312 hit |= grep_tree(opt, paths, &sub, tree_name, down);
315 update_tree_entry(tree);
320 static int grep_object(struct grep_opt *opt, const char **paths,
321 struct object *obj, const char *name)
323 if (!strcmp(obj->type, blob_type))
324 return grep_sha1(opt, obj->sha1, name);
325 if (!strcmp(obj->type, commit_type) ||
326 !strcmp(obj->type, tree_type)) {
327 struct tree_desc tree;
330 data = read_object_with_reference(obj->sha1, tree_type,
333 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
335 hit = grep_tree(opt, paths, &tree, name, "");
339 die("unable to grep from object of type %s", obj->type);
342 static const char builtin_grep_usage[] =
343 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
345 int cmd_grep(int argc, const char **argv, char **envp)
349 int no_more_flags = 0;
350 int seen_noncommit = 0;
353 struct object_list *list, **tail, *object_list = NULL;
354 const char *prefix = setup_git_directory();
355 const char **paths = NULL;
357 memset(&opt, 0, sizeof(opt));
358 opt.regflags = REG_NEWLINE;
361 * No point using rev_info, really.
364 const char *arg = argv[1];
366 if (!strcmp("--cached", arg)) {
370 if (!strcmp("-i", arg) ||
371 !strcmp("--ignore-case", arg)) {
372 opt.regflags |= REG_ICASE;
375 if (!strcmp("-v", arg) ||
376 !strcmp("--invert-match", arg)) {
380 if (!strcmp("-E", arg) ||
381 !strcmp("--extended-regexp", arg)) {
382 opt.regflags |= REG_EXTENDED;
385 if (!strcmp("-G", arg) ||
386 !strcmp("--basic-regexp", arg)) {
387 opt.regflags &= ~REG_EXTENDED;
390 if (!strcmp("-n", arg)) {
394 if (!strcmp("-H", arg)) {
395 /* We always show the pathname, so this
400 if (!strcmp("-l", arg) ||
401 !strcmp("--files-with-matches", arg)) {
405 if (!strcmp("-A", arg) ||
406 !strcmp("-B", arg) ||
407 !strcmp("-C", arg)) {
410 sscanf(*++argv, "%u", &num) != 1)
411 usage(builtin_grep_usage);
415 opt.post_context = num;
418 opt.post_context = num;
420 opt.pre_context = num;
425 if (!strcmp("-e", arg)) {
427 /* We probably would want to do
428 * -e pat1 -e pat2 as well later...
431 die("more than one pattern?");
432 opt.pattern = *++argv;
436 usage(builtin_grep_usage);
438 if (!strcmp("--", arg)) {
442 /* Either unrecognized option or a single pattern */
443 if (!no_more_flags && *arg == '-')
444 usage(builtin_grep_usage);
450 /* We are looking at the first path or rev;
451 * it is found at argv[0] after leaving the
459 die("no pattern given.");
460 err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
463 regerror(err, &opt.regexp, errbuf, 1024);
464 regfree(&opt.regexp);
465 die("'%s': %s", opt.pattern, errbuf);
469 struct object *object;
470 struct object_list *elem;
471 const char *arg = argv[1];
472 unsigned char sha1[20];
473 if (get_sha1(arg, sha1) < 0)
475 object = parse_object(sha1);
477 die("bad object %s", arg);
478 elem = object_list_insert(object, tail);
484 paths = get_pathspec(prefix, argv + 1);
486 paths = xcalloc(2, sizeof(const char *));
492 return !grep_cache(&opt, paths, cached);
494 * Do not walk "grep -e foo master next pu -- Documentation/"
495 * but do walk "grep -e foo master..next -- Documentation/".
496 * Ranged request mixed with a blob or tree object, like
497 * "grep -e foo v1.0.0:Documentation/ master..next"
498 * so detect that and complain.
500 for (list = object_list; list; list = list->next) {
501 struct object *real_obj;
502 real_obj = deref_tag(list->item, NULL, 0);
503 if (strcmp(real_obj->type, commit_type))
507 die("both --cached and revisions given.");
509 for (list = object_list; list; list = list->next) {
510 struct object *real_obj;
511 real_obj = deref_tag(list->item, NULL, 0);
512 if (grep_object(&opt, paths, real_obj, list->name))