4 * Copyright (c) 2006 Junio C Hamano
18 * git grep pathspecs are somewhat different from diff-tree pathspecs;
19 * pathname wildcards are allowed.
21 static int pathspec_matches(struct diff_options *opt, const char *name)
26 namelen = strlen(name);
27 for (i = 0; i < opt->nr_paths; i++) {
28 const char *match = opt->paths[i];
29 int matchlen = opt->pathlens[i];
30 const char *slash, *cp;
32 if ((matchlen <= namelen) &&
33 !strncmp(name, match, matchlen) &&
34 (match[matchlen-1] == '/' ||
35 name[matchlen] == '\0' || name[matchlen] == '/'))
37 if (!fnmatch(match, name, 0))
39 if (name[namelen-1] != '/')
42 /* We are being asked if the name directory is worth
45 * Find the longest leading directory name that does
46 * not have metacharacter in the pathspec; the name
47 * we are looking at must overlap with that directory.
49 for (cp = match, slash = NULL; cp - match < matchlen; cp++) {
53 if (ch == '*' || ch == '[')
57 slash = match; /* toplevel */
60 if (namelen <= slash - match) {
61 /* Looking at "Documentation/" and
62 * the pattern says "Documentation/howto/", or
63 * "Documentation/diff*.txt".
65 if (!memcmp(match, name, namelen))
69 /* Looking at "Documentation/howto/" and
70 * the pattern says "Documentation/h*".
72 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", 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 /* Hit at this line. If we haven't shown the
143 * pre-context lines, we would need to show them.
145 if (opt->pre_context) {
147 if (opt->pre_context < lno)
148 from = lno - opt->pre_context;
151 if (from <= last_shown)
152 from = last_shown + 1;
153 if (last_shown && from != last_shown + 1)
156 pcl = &prev[lno-from-1];
157 show_line(opt, pcl->bol, pcl->eol,
163 if (last_shown && lno != last_shown + 1)
165 show_line(opt, bol, eol, name, lno, ':');
166 last_shown = last_hit = lno;
169 lno <= last_hit + opt->post_context) {
170 /* If the last hit is within the post context,
171 * we need to show this line.
173 if (last_shown && lno != last_shown + 1)
175 show_line(opt, bol, eol, name, lno, '-');
178 if (opt->pre_context) {
179 memmove(prev+1, prev,
180 (opt->pre_context-1) * sizeof(*prev));
192 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
198 data = read_sha1_file(sha1, type, &size);
200 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
203 hit = grep_buffer(opt, name, data, size);
208 static int grep_file(struct grep_opt *opt, const char *filename)
213 if (lstat(filename, &st) < 0) {
216 error("'%s': %s", filename, strerror(errno));
220 return 0; /* empty file -- no grep hit */
221 if (!S_ISREG(st.st_mode))
223 i = open(filename, O_RDONLY);
226 data = xmalloc(st.st_size + 1);
227 if (st.st_size != xread(i, data, st.st_size)) {
228 error("'%s': short read %s", filename, strerror(errno));
234 i = grep_buffer(opt, filename, data, st.st_size);
239 static int grep_cache(struct grep_opt *opt, struct rev_info *revs, int cached)
245 for (nr = 0; nr < active_nr; nr++) {
246 struct cache_entry *ce = active_cache[nr];
247 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
249 if (!pathspec_matches(&revs->diffopt, ce->name))
252 hit |= grep_sha1(opt, ce->sha1, ce->name);
254 hit |= grep_file(opt, ce->name);
259 static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
260 struct tree_desc *tree,
261 const char *tree_name, const char *base)
267 const unsigned char *sha1;
269 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
272 int offset = sprintf(path_buf, "%s:", tree_name);
273 down = path_buf + offset;
280 len = strlen(path_buf);
284 sha1 = tree_entry_extract(tree, &path, &mode);
285 pathlen = strlen(path);
286 strcpy(path_buf + len, path);
289 /* Match "abc/" against pathspec to
290 * decide if we want to descend into "abc"
293 strcpy(path_buf + len + pathlen, "/");
295 if (!pathspec_matches(&revs->diffopt, down))
297 else if (S_ISREG(mode))
298 hit |= grep_sha1(opt, sha1, path_buf);
299 else if (S_ISDIR(mode)) {
301 struct tree_desc sub;
303 data = read_sha1_file(sha1, type, &sub.size);
305 die("unable to read tree (%s)",
308 hit |= grep_tree(opt, revs, &sub, tree_name, down);
311 update_tree_entry(tree);
316 static int grep_object(struct grep_opt *opt, struct rev_info *revs,
317 struct object *obj, const char *name)
319 if (!strcmp(obj->type, blob_type))
320 return grep_sha1(opt, obj->sha1, name);
321 if (!strcmp(obj->type, commit_type) ||
322 !strcmp(obj->type, tree_type)) {
323 struct tree_desc tree;
326 data = read_object_with_reference(obj->sha1, tree_type,
329 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
331 hit = grep_tree(opt, revs, &tree, name, "");
335 die("unable to grep from object of type %s", obj->type);
338 static const char builtin_grep_usage[] =
339 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
341 int cmd_grep(int argc, const char **argv, char **envp)
344 const char **dst, **src;
349 int seen_noncommit = 0;
352 struct object_list *list;
354 memset(&opt, 0, sizeof(opt));
355 opt.regflags = REG_NEWLINE;
358 * Interpret and remove the grep options upfront. Sigh...
360 for (dst = src = &argv[1]; src < argc + argv; ) {
361 const char *arg = *src++;
363 if (!strcmp("--", arg)) {
368 if (!strcmp("--cached", arg)) {
372 if (!strcmp("-i", arg) ||
373 !strcmp("--ignore-case", arg)) {
374 opt.regflags |= REG_ICASE;
377 if (!strcmp("-v", arg) ||
378 !strcmp("--invert-match", arg)) {
382 if (!strcmp("-E", arg) ||
383 !strcmp("--extended-regexp", arg)) {
384 opt.regflags |= REG_EXTENDED;
387 if (!strcmp("-G", arg) ||
388 !strcmp("--basic-regexp", arg)) {
389 opt.regflags &= ~REG_EXTENDED;
392 if (!strcmp("-e", arg)) {
393 if (src < argc + argv) {
394 opt.pattern = *src++;
397 usage(builtin_grep_usage);
399 if (!strcmp("-n", arg)) {
403 if (!strcmp("-H", arg)) {
404 /* We always show the pathname, so this
409 if (!strcmp("-A", arg) ||
410 !strcmp("-B", arg) ||
411 !strcmp("-C", arg)) {
413 if ((argc + argv <= src) ||
414 sscanf(*src++, "%u", &num) != 1)
415 usage(builtin_grep_usage);
418 opt.post_context = num;
421 opt.post_context = num;
423 opt.pre_context = num;
432 die("no pattern given.");
434 err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
437 regerror(err, &opt.regexp, errbuf, 1024);
438 regfree(&opt.regexp);
439 die("'%s': %s", opt.pattern, errbuf);
442 init_revisions(&rev);
444 argc = setup_revisions(dst - argv, argv, &rev, NULL);
447 * Do not walk "grep -e foo master next pu -- Documentation/"
448 * but do walk "grep -e foo master..next -- Documentation/".
449 * Ranged request mixed with a blob or tree object, like
450 * "grep -e foo v1.0.0:Documentation/ master..next"
451 * so detect that and complain.
453 for (list = rev.pending_objects; list; list = list->next) {
454 struct object *real_obj;
455 if (list->item->flags & UNINTERESTING)
457 real_obj = deref_tag(list->item, NULL, 0);
458 if (strcmp(real_obj->type, commit_type))
461 if (!rev.pending_objects)
462 return !grep_cache(&opt, &rev, cached);
464 die("both --cached and revisions given.");
466 if (seen_range && seen_noncommit)
467 die("both A..B and non commit are given.");
469 struct commit *commit;
470 prepare_revision_walk(&rev);
471 while ((commit = get_revision(&rev)) != NULL) {
472 unsigned char *sha1 = commit->object.sha1;
473 const char *n = find_unique_abbrev(sha1, rev.abbrev);
476 if (grep_object(&opt, &rev, &commit->object, rev_name))
478 commit->buffer = NULL;
483 /* all of them are non-commit; do not walk, and
484 * do not lose their names.
486 for (list = rev.pending_objects; list; list = list->next) {
487 struct object *real_obj;
488 real_obj = deref_tag(list->item, NULL, 0);
489 if (grep_object(&opt, &rev, real_obj, list->name))