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))
87 unsigned post_context;
90 static char *end_of_line(char *cp, unsigned long *left)
92 unsigned long l = *left;
93 while (l && *cp != '\n') {
101 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
102 const char *name, unsigned lno, char sign)
104 printf("%s%c", name, sign);
106 printf("%d%c", lno, sign);
107 printf("%.*s\n", eol-bol, bol);
110 static int grep_buffer(struct grep_opt *opt, const char *name,
111 char *buf, unsigned long size)
114 unsigned long left = size;
116 struct pre_context_line {
119 } *prev = NULL, *pcl;
120 unsigned last_hit = 0;
121 unsigned last_shown = 0;
122 const char *hunk_mark = "";
124 if (opt->pre_context)
125 prev = xcalloc(opt->pre_context, sizeof(*prev));
126 if (opt->pre_context || opt->post_context)
130 regmatch_t pmatch[10];
134 eol = end_of_line(bol, &left);
138 hit = !regexec(&opt->regexp, bol, ARRAY_SIZE(pmatch),
143 if (opt->name_only) {
144 printf("%s\n", name);
147 /* Hit at this line. If we haven't shown the
148 * pre-context lines, we would need to show them.
150 if (opt->pre_context) {
152 if (opt->pre_context < lno)
153 from = lno - opt->pre_context;
156 if (from <= last_shown)
157 from = last_shown + 1;
158 if (last_shown && from != last_shown + 1)
161 pcl = &prev[lno-from-1];
162 show_line(opt, pcl->bol, pcl->eol,
168 if (last_shown && lno != last_shown + 1)
170 show_line(opt, bol, eol, name, lno, ':');
171 last_shown = last_hit = lno;
174 lno <= last_hit + opt->post_context) {
175 /* If the last hit is within the post context,
176 * we need to show this line.
178 if (last_shown && lno != last_shown + 1)
180 show_line(opt, bol, eol, name, lno, '-');
183 if (opt->pre_context) {
184 memmove(prev+1, prev,
185 (opt->pre_context-1) * sizeof(*prev));
197 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
203 data = read_sha1_file(sha1, type, &size);
205 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
208 hit = grep_buffer(opt, name, data, size);
213 static int grep_file(struct grep_opt *opt, const char *filename)
218 if (lstat(filename, &st) < 0) {
221 error("'%s': %s", filename, strerror(errno));
225 return 0; /* empty file -- no grep hit */
226 if (!S_ISREG(st.st_mode))
228 i = open(filename, O_RDONLY);
231 data = xmalloc(st.st_size + 1);
232 if (st.st_size != xread(i, data, st.st_size)) {
233 error("'%s': short read %s", filename, strerror(errno));
239 i = grep_buffer(opt, filename, data, st.st_size);
244 static int grep_cache(struct grep_opt *opt, struct rev_info *revs, int cached)
250 for (nr = 0; nr < active_nr; nr++) {
251 struct cache_entry *ce = active_cache[nr];
252 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
254 if (!pathspec_matches(&revs->diffopt, ce->name))
257 hit |= grep_sha1(opt, ce->sha1, ce->name);
259 hit |= grep_file(opt, ce->name);
264 static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
265 struct tree_desc *tree,
266 const char *tree_name, const char *base)
272 const unsigned char *sha1;
274 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
277 int offset = sprintf(path_buf, "%s:", tree_name);
278 down = path_buf + offset;
285 len = strlen(path_buf);
289 sha1 = tree_entry_extract(tree, &path, &mode);
290 pathlen = strlen(path);
291 strcpy(path_buf + len, path);
294 /* Match "abc/" against pathspec to
295 * decide if we want to descend into "abc"
298 strcpy(path_buf + len + pathlen, "/");
300 if (!pathspec_matches(&revs->diffopt, down))
302 else if (S_ISREG(mode))
303 hit |= grep_sha1(opt, sha1, path_buf);
304 else if (S_ISDIR(mode)) {
306 struct tree_desc sub;
308 data = read_sha1_file(sha1, type, &sub.size);
310 die("unable to read tree (%s)",
313 hit |= grep_tree(opt, revs, &sub, tree_name, down);
316 update_tree_entry(tree);
321 static int grep_object(struct grep_opt *opt, struct rev_info *revs,
322 struct object *obj, const char *name)
324 if (!strcmp(obj->type, blob_type))
325 return grep_sha1(opt, obj->sha1, name);
326 if (!strcmp(obj->type, commit_type) ||
327 !strcmp(obj->type, tree_type)) {
328 struct tree_desc tree;
331 data = read_object_with_reference(obj->sha1, tree_type,
334 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
336 hit = grep_tree(opt, revs, &tree, name, "");
340 die("unable to grep from object of type %s", obj->type);
343 static const char builtin_grep_usage[] =
344 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
346 int cmd_grep(int argc, const char **argv, char **envp)
349 const char **dst, **src;
354 int seen_noncommit = 0;
357 struct object_list *list;
359 memset(&opt, 0, sizeof(opt));
360 opt.regflags = REG_NEWLINE;
363 * Interpret and remove the grep options upfront. Sigh...
365 for (dst = src = &argv[1]; src < argc + argv; ) {
366 const char *arg = *src++;
368 if (!strcmp("--", arg)) {
373 if (!strcmp("--cached", arg)) {
377 if (!strcmp("-i", arg) ||
378 !strcmp("--ignore-case", arg)) {
379 opt.regflags |= REG_ICASE;
382 if (!strcmp("-v", arg) ||
383 !strcmp("--invert-match", arg)) {
387 if (!strcmp("-E", arg) ||
388 !strcmp("--extended-regexp", arg)) {
389 opt.regflags |= REG_EXTENDED;
392 if (!strcmp("-G", arg) ||
393 !strcmp("--basic-regexp", arg)) {
394 opt.regflags &= ~REG_EXTENDED;
397 if (!strcmp("-e", arg)) {
398 if (src < argc + argv) {
399 opt.pattern = *src++;
402 usage(builtin_grep_usage);
404 if (!strcmp("-n", arg)) {
408 if (!strcmp("-H", arg)) {
409 /* We always show the pathname, so this
414 if (!strcmp("-l", arg) ||
415 !strcmp("--files-with-matches", arg)) {
419 if (!strcmp("-A", arg) ||
420 !strcmp("-B", arg) ||
421 !strcmp("-C", arg)) {
423 if ((argc + argv <= src) ||
424 sscanf(*src++, "%u", &num) != 1)
425 usage(builtin_grep_usage);
428 opt.post_context = num;
431 opt.post_context = num;
433 opt.pre_context = num;
442 die("no pattern given.");
444 err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
447 regerror(err, &opt.regexp, errbuf, 1024);
448 regfree(&opt.regexp);
449 die("'%s': %s", opt.pattern, errbuf);
452 init_revisions(&rev);
454 argc = setup_revisions(dst - argv, argv, &rev, NULL);
457 * Do not walk "grep -e foo master next pu -- Documentation/"
458 * but do walk "grep -e foo master..next -- Documentation/".
459 * Ranged request mixed with a blob or tree object, like
460 * "grep -e foo v1.0.0:Documentation/ master..next"
461 * so detect that and complain.
463 for (list = rev.pending_objects; list; list = list->next) {
464 struct object *real_obj;
465 if (list->item->flags & UNINTERESTING)
467 real_obj = deref_tag(list->item, NULL, 0);
468 if (strcmp(real_obj->type, commit_type))
471 if (!rev.pending_objects)
472 return !grep_cache(&opt, &rev, cached);
474 die("both --cached and revisions given.");
476 if (seen_range && seen_noncommit)
477 die("both A..B and non commit are given.");
479 struct commit *commit;
480 prepare_revision_walk(&rev);
481 while ((commit = get_revision(&rev)) != NULL) {
482 unsigned char *sha1 = commit->object.sha1;
483 const char *n = find_unique_abbrev(sha1, rev.abbrev);
486 if (grep_object(&opt, &rev, &commit->object, rev_name))
488 commit->buffer = NULL;
493 /* all of them are non-commit; do not walk, and
494 * do not lose their names.
496 for (list = rev.pending_objects; list; list = list->next) {
497 struct object *real_obj;
498 real_obj = deref_tag(list->item, NULL, 0);
499 if (grep_object(&opt, &rev, real_obj, list->name))