builtin-grep: allow -<n> and -[ABC]<n> notation for context lines.
[git/git.git] / builtin-grep.c
CommitLineData
5010cb5f
JH
1/*
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"
1362671f 11#include "tree-walk.h"
5010cb5f
JH
12#include "builtin.h"
13#include <regex.h>
e0eb889f 14#include <fnmatch.h>
5010cb5f 15
e0eb889f
JH
16/*
17 * git grep pathspecs are somewhat different from diff-tree pathspecs;
18 * pathname wildcards are allowed.
19 */
1362671f 20static int pathspec_matches(const char **paths, const char *name)
5010cb5f 21{
e0eb889f 22 int namelen, i;
1362671f 23 if (!paths || !*paths)
5010cb5f
JH
24 return 1;
25 namelen = strlen(name);
1362671f
JH
26 for (i = 0; paths[i]; i++) {
27 const char *match = paths[i];
28 int matchlen = strlen(match);
e0eb889f
JH
29 const char *slash, *cp;
30
31 if ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/'))
35 return 1;
36 if (!fnmatch(match, name, 0))
37 return 1;
38 if (name[namelen-1] != '/')
5010cb5f 39 continue;
e0eb889f
JH
40
41 /* We are being asked if the name directory is worth
42 * 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.
5010cb5f 47 */
e0eb889f
JH
48 for (cp = match, slash = NULL; cp - match < matchlen; cp++) {
49 char ch = *cp;
50 if (ch == '/')
51 slash = cp;
52 if (ch == '*' || ch == '[')
5010cb5f 53 break;
e0eb889f
JH
54 }
55 if (!slash)
56 slash = match; /* toplevel */
57 else
58 slash++;
59 if (namelen <= slash - match) {
60 /* Looking at "Documentation/" and
61 * the pattern says "Documentation/howto/", or
62 * "Documentation/diff*.txt".
63 */
64 if (!memcmp(match, name, namelen))
65 return 1;
66 }
67 else {
68 /* Looking at "Documentation/howto/" and
69 * the pattern says "Documentation/h*".
70 */
71 if (!memcmp(match, name, slash - match))
72 return 1;
73 }
5010cb5f
JH
74 }
75 return 0;
76}
77
78struct grep_opt {
79 const char *pattern;
80 regex_t regexp;
81 unsigned linenum:1;
82 unsigned invert:1;
df0e7aa8 83 unsigned name_only:1;
5010cb5f
JH
84 int regflags;
85 unsigned pre_context;
86 unsigned post_context;
87};
88
89static char *end_of_line(char *cp, unsigned long *left)
90{
91 unsigned long l = *left;
92 while (l && *cp != '\n') {
93 l--;
94 cp++;
95 }
96 *left = l;
97 return cp;
98}
99
100static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
101 const char *name, unsigned lno, char sign)
102{
103 printf("%s%c", name, sign);
104 if (opt->linenum)
105 printf("%d%c", lno, sign);
a24f1e25 106 printf("%.*s\n", (int)(eol-bol), bol);
5010cb5f
JH
107}
108
109static int grep_buffer(struct grep_opt *opt, const char *name,
110 char *buf, unsigned long size)
111{
112 char *bol = buf;
113 unsigned long left = size;
114 unsigned lno = 1;
115 struct pre_context_line {
116 char *bol;
117 char *eol;
118 } *prev = NULL, *pcl;
119 unsigned last_hit = 0;
120 unsigned last_shown = 0;
121 const char *hunk_mark = "";
122
123 if (opt->pre_context)
124 prev = xcalloc(opt->pre_context, sizeof(*prev));
125 if (opt->pre_context || opt->post_context)
126 hunk_mark = "--\n";
127
128 while (left) {
129 regmatch_t pmatch[10];
130 char *eol, ch;
131 int hit;
132
133 eol = end_of_line(bol, &left);
134 ch = *eol;
135 *eol = 0;
136
137 hit = !regexec(&opt->regexp, bol, ARRAY_SIZE(pmatch),
138 pmatch, 0);
139 if (opt->invert)
140 hit = !hit;
141 if (hit) {
df0e7aa8
JH
142 if (opt->name_only) {
143 printf("%s\n", name);
144 return 1;
145 }
5010cb5f
JH
146 /* Hit at this line. If we haven't shown the
147 * pre-context lines, we would need to show them.
148 */
149 if (opt->pre_context) {
150 unsigned from;
151 if (opt->pre_context < lno)
152 from = lno - opt->pre_context;
153 else
154 from = 1;
155 if (from <= last_shown)
156 from = last_shown + 1;
157 if (last_shown && from != last_shown + 1)
158 printf(hunk_mark);
159 while (from < lno) {
160 pcl = &prev[lno-from-1];
161 show_line(opt, pcl->bol, pcl->eol,
162 name, from, '-');
163 from++;
164 }
165 last_shown = lno-1;
166 }
167 if (last_shown && lno != last_shown + 1)
168 printf(hunk_mark);
169 show_line(opt, bol, eol, name, lno, ':');
170 last_shown = last_hit = lno;
171 }
172 else if (last_hit &&
173 lno <= last_hit + opt->post_context) {
174 /* If the last hit is within the post context,
175 * we need to show this line.
176 */
177 if (last_shown && lno != last_shown + 1)
178 printf(hunk_mark);
179 show_line(opt, bol, eol, name, lno, '-');
180 last_shown = lno;
181 }
182 if (opt->pre_context) {
183 memmove(prev+1, prev,
184 (opt->pre_context-1) * sizeof(*prev));
185 prev->bol = bol;
186 prev->eol = eol;
187 }
188 *eol = ch;
189 bol = eol + 1;
190 left--;
191 lno++;
192 }
193 return !!last_hit;
194}
195
196static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
197{
198 unsigned long size;
199 char *data;
200 char type[20];
201 int hit;
202 data = read_sha1_file(sha1, type, &size);
203 if (!data) {
204 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
205 return 0;
206 }
207 hit = grep_buffer(opt, name, data, size);
208 free(data);
209 return hit;
210}
211
212static int grep_file(struct grep_opt *opt, const char *filename)
213{
214 struct stat st;
215 int i;
216 char *data;
217 if (lstat(filename, &st) < 0) {
218 err_ret:
219 if (errno != ENOENT)
220 error("'%s': %s", filename, strerror(errno));
221 return 0;
222 }
223 if (!st.st_size)
224 return 0; /* empty file -- no grep hit */
225 if (!S_ISREG(st.st_mode))
226 return 0;
227 i = open(filename, O_RDONLY);
228 if (i < 0)
229 goto err_ret;
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));
233 close(i);
234 free(data);
235 return 0;
236 }
237 close(i);
238 i = grep_buffer(opt, filename, data, st.st_size);
239 free(data);
240 return i;
241}
242
1362671f 243static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
5010cb5f
JH
244{
245 int hit = 0;
246 int nr;
247 read_cache();
248
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)))
252 continue;
1362671f 253 if (!pathspec_matches(paths, ce->name))
5010cb5f
JH
254 continue;
255 if (cached)
256 hit |= grep_sha1(opt, ce->sha1, ce->name);
257 else
258 hit |= grep_file(opt, ce->name);
259 }
260 return hit;
261}
262
1362671f 263static int grep_tree(struct grep_opt *opt, const char **paths,
5010cb5f
JH
264 struct tree_desc *tree,
265 const char *tree_name, const char *base)
266{
267 unsigned mode;
268 int len;
269 int hit = 0;
270 const char *path;
271 const unsigned char *sha1;
e0eb889f 272 char *down;
5010cb5f
JH
273 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
274
275 if (tree_name[0]) {
276 int offset = sprintf(path_buf, "%s:", tree_name);
e0eb889f
JH
277 down = path_buf + offset;
278 strcat(down, base);
5010cb5f
JH
279 }
280 else {
e0eb889f
JH
281 down = path_buf;
282 strcpy(down, base);
5010cb5f
JH
283 }
284 len = strlen(path_buf);
285
286 while (tree->size) {
287 int pathlen;
288 sha1 = tree_entry_extract(tree, &path, &mode);
289 pathlen = strlen(path);
290 strcpy(path_buf + len, path);
291
e0eb889f
JH
292 if (S_ISDIR(mode))
293 /* Match "abc/" against pathspec to
294 * decide if we want to descend into "abc"
295 * directory.
296 */
297 strcpy(path_buf + len + pathlen, "/");
298
1362671f 299 if (!pathspec_matches(paths, down))
5010cb5f
JH
300 ;
301 else if (S_ISREG(mode))
302 hit |= grep_sha1(opt, sha1, path_buf);
303 else if (S_ISDIR(mode)) {
304 char type[20];
305 struct tree_desc sub;
306 void *data;
307 data = read_sha1_file(sha1, type, &sub.size);
308 if (!data)
309 die("unable to read tree (%s)",
310 sha1_to_hex(sha1));
5010cb5f 311 sub.buf = data;
1362671f 312 hit |= grep_tree(opt, paths, &sub, tree_name, down);
5010cb5f
JH
313 free(data);
314 }
315 update_tree_entry(tree);
316 }
317 return hit;
318}
319
1362671f 320static int grep_object(struct grep_opt *opt, const char **paths,
5010cb5f
JH
321 struct object *obj, const char *name)
322{
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;
328 void *data;
329 int hit;
330 data = read_object_with_reference(obj->sha1, tree_type,
331 &tree.size, NULL);
332 if (!data)
333 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
334 tree.buf = data;
1362671f 335 hit = grep_tree(opt, paths, &tree, name, "");
5010cb5f
JH
336 free(data);
337 return hit;
338 }
339 die("unable to grep from object of type %s", obj->type);
340}
341
342static const char builtin_grep_usage[] =
343"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
344
345int cmd_grep(int argc, const char **argv, char **envp)
346{
5010cb5f
JH
347 int err;
348 int hit = 0;
1362671f 349 int no_more_flags = 0;
5010cb5f
JH
350 int seen_noncommit = 0;
351 int cached = 0;
352 struct grep_opt opt;
1362671f
JH
353 struct object_list *list, **tail, *object_list = NULL;
354 const char *prefix = setup_git_directory();
355 const char **paths = NULL;
5010cb5f
JH
356
357 memset(&opt, 0, sizeof(opt));
358 opt.regflags = REG_NEWLINE;
359
360 /*
1362671f 361 * No point using rev_info, really.
5010cb5f 362 */
1362671f
JH
363 while (1 < argc) {
364 const char *arg = argv[1];
365 argc--; argv++;
366 if (!strcmp("--cached", arg)) {
367 cached = 1;
368 continue;
369 }
370 if (!strcmp("-i", arg) ||
371 !strcmp("--ignore-case", arg)) {
372 opt.regflags |= REG_ICASE;
373 continue;
374 }
375 if (!strcmp("-v", arg) ||
376 !strcmp("--invert-match", arg)) {
377 opt.invert = 1;
378 continue;
379 }
380 if (!strcmp("-E", arg) ||
381 !strcmp("--extended-regexp", arg)) {
382 opt.regflags |= REG_EXTENDED;
383 continue;
384 }
385 if (!strcmp("-G", arg) ||
386 !strcmp("--basic-regexp", arg)) {
387 opt.regflags &= ~REG_EXTENDED;
388 continue;
389 }
390 if (!strcmp("-n", arg)) {
391 opt.linenum = 1;
392 continue;
393 }
394 if (!strcmp("-H", arg)) {
395 /* We always show the pathname, so this
396 * is a noop.
397 */
398 continue;
399 }
400 if (!strcmp("-l", arg) ||
401 !strcmp("--files-with-matches", arg)) {
402 opt.name_only = 1;
403 continue;
404 }
f462ebb4
JH
405 if (!strncmp("-A", arg, 2) ||
406 !strncmp("-B", arg, 2) ||
407 !strncmp("-C", arg, 2) ||
408 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
1362671f 409 unsigned num;
f462ebb4
JH
410 const char *scan;
411 switch (arg[1]) {
412 case 'A': case 'B': case 'C':
413 if (!arg[2]) {
414 if (argc <= 1)
415 usage(builtin_grep_usage);
416 scan = *++argv;
417 argc--;
418 }
419 else
420 scan = arg + 2;
421 break;
422 default:
423 scan = arg + 1;
424 break;
425 }
426 if (sscanf(scan, "%u", &num) != 1)
5010cb5f 427 usage(builtin_grep_usage);
1362671f
JH
428 switch (arg[1]) {
429 case 'A':
430 opt.post_context = num;
431 break;
f462ebb4 432 default:
1362671f
JH
433 case 'C':
434 opt.post_context = num;
435 case 'B':
436 opt.pre_context = num;
437 break;
5010cb5f 438 }
1362671f
JH
439 continue;
440 }
441 if (!strcmp("-e", arg)) {
442 if (1 < argc) {
443 /* We probably would want to do
444 * -e pat1 -e pat2 as well later...
5010cb5f 445 */
1362671f
JH
446 if (opt.pattern)
447 die("more than one pattern?");
448 opt.pattern = *++argv;
449 argc--;
5010cb5f
JH
450 continue;
451 }
1362671f
JH
452 usage(builtin_grep_usage);
453 }
454 if (!strcmp("--", arg)) {
455 no_more_flags = 1;
456 continue;
457 }
458 /* Either unrecognized option or a single pattern */
459 if (!no_more_flags && *arg == '-')
460 usage(builtin_grep_usage);
461 if (!opt.pattern) {
462 opt.pattern = arg;
463 break;
464 }
465 else {
466 /* We are looking at the first path or rev;
467 * it is found at argv[0] after leaving the
468 * loop.
469 */
470 argc++; argv--;
471 break;
5010cb5f 472 }
5010cb5f
JH
473 }
474 if (!opt.pattern)
475 die("no pattern given.");
5010cb5f
JH
476 err = regcomp(&opt.regexp, opt.pattern, opt.regflags);
477 if (err) {
478 char errbuf[1024];
479 regerror(err, &opt.regexp, errbuf, 1024);
480 regfree(&opt.regexp);
481 die("'%s': %s", opt.pattern, errbuf);
482 }
1362671f
JH
483 tail = &object_list;
484 while (1 < argc) {
485 struct object *object;
486 struct object_list *elem;
487 const char *arg = argv[1];
488 unsigned char sha1[20];
489 if (get_sha1(arg, sha1) < 0)
490 break;
491 object = parse_object(sha1);
492 if (!object)
493 die("bad object %s", arg);
494 elem = object_list_insert(object, tail);
495 elem->name = arg;
496 tail = &elem->next;
497 argc--; argv++;
498 }
499 if (1 < argc)
500 paths = get_pathspec(prefix, argv + 1);
501 else if (prefix) {
502 paths = xcalloc(2, sizeof(const char *));
503 paths[0] = prefix;
504 paths[1] = NULL;
505 }
5010cb5f 506
1362671f
JH
507 if (!object_list)
508 return !grep_cache(&opt, paths, cached);
5010cb5f
JH
509 /*
510 * Do not walk "grep -e foo master next pu -- Documentation/"
511 * but do walk "grep -e foo master..next -- Documentation/".
512 * Ranged request mixed with a blob or tree object, like
513 * "grep -e foo v1.0.0:Documentation/ master..next"
514 * so detect that and complain.
515 */
1362671f 516 for (list = object_list; list; list = list->next) {
5010cb5f 517 struct object *real_obj;
5010cb5f
JH
518 real_obj = deref_tag(list->item, NULL, 0);
519 if (strcmp(real_obj->type, commit_type))
520 seen_noncommit = 1;
521 }
5010cb5f
JH
522 if (cached)
523 die("both --cached and revisions given.");
524
1362671f 525 for (list = object_list; list; list = list->next) {
5010cb5f
JH
526 struct object *real_obj;
527 real_obj = deref_tag(list->item, NULL, 0);
1362671f 528 if (grep_object(&opt, paths, real_obj, list->name))
5010cb5f
JH
529 hit = 1;
530 }
531 return !hit;
532}