Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 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" |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 11 | #include "tree-walk.h" |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 12 | #include "builtin.h" |
| 13 | #include <regex.h> |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 14 | #include <fnmatch.h> |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 15 | #include <sys/wait.h> |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 16 | |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 17 | /* |
| 18 | * git grep pathspecs are somewhat different from diff-tree pathspecs; |
| 19 | * pathname wildcards are allowed. |
| 20 | */ |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 21 | static int pathspec_matches(const char **paths, const char *name) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 22 | { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 23 | int namelen, i; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 24 | if (!paths || !*paths) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 25 | return 1; |
| 26 | namelen = strlen(name); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 27 | for (i = 0; paths[i]; i++) { |
| 28 | const char *match = paths[i]; |
| 29 | int matchlen = strlen(match); |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 30 | const char *cp, *meta; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 31 | |
Uwe Zeisberger | bb9e15a | 2006-06-21 11:04:12 +0200 | [diff] [blame] | 32 | if (!matchlen || |
| 33 | ((matchlen <= namelen) && |
| 34 | !strncmp(name, match, matchlen) && |
| 35 | (match[matchlen-1] == '/' || |
| 36 | name[matchlen] == '\0' || name[matchlen] == '/'))) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 37 | return 1; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 38 | if (!fnmatch(match, name, 0)) |
| 39 | return 1; |
| 40 | if (name[namelen-1] != '/') |
| 41 | continue; |
| 42 | |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 43 | /* We are being asked if the directory ("name") is worth |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 44 | * descending into. |
| 45 | * |
| 46 | * Find the longest leading directory name that does |
| 47 | * not have metacharacter in the pathspec; the name |
| 48 | * we are looking at must overlap with that directory. |
| 49 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 50 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 51 | char ch = *cp; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 52 | if (ch == '*' || ch == '[' || ch == '?') { |
| 53 | meta = cp; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 54 | break; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 55 | } |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 56 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 57 | if (!meta) |
| 58 | meta = cp; /* fully literal */ |
| 59 | |
| 60 | if (namelen <= meta - match) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 61 | /* Looking at "Documentation/" and |
| 62 | * the pattern says "Documentation/howto/", or |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 63 | * "Documentation/diff*.txt". The name we |
| 64 | * have should match prefix. |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 65 | */ |
| 66 | if (!memcmp(match, name, namelen)) |
| 67 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 68 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 69 | } |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 70 | |
| 71 | if (meta - match < namelen) { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 72 | /* Looking at "Documentation/howto/" and |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 73 | * the pattern says "Documentation/h*"; |
| 74 | * match up to "Do.../h"; this avoids descending |
| 75 | * into "Documentation/technical/". |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 76 | */ |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 77 | if (!memcmp(match, name, meta - match)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 78 | return 1; |
Junio C Hamano | 1e3d90e | 2006-05-02 17:27:07 -0700 | [diff] [blame] | 79 | continue; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 80 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 85 | enum grep_pat_token { |
| 86 | GREP_PATTERN, |
| 87 | GREP_AND, |
| 88 | GREP_OPEN_PAREN, |
| 89 | GREP_CLOSE_PAREN, |
| 90 | GREP_NOT, |
| 91 | GREP_OR, |
| 92 | }; |
| 93 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 94 | struct grep_pat { |
| 95 | struct grep_pat *next; |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 96 | const char *origin; |
| 97 | int no; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 98 | enum grep_pat_token token; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 99 | const char *pattern; |
| 100 | regex_t regexp; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 103 | enum grep_expr_node { |
| 104 | GREP_NODE_ATOM, |
| 105 | GREP_NODE_NOT, |
| 106 | GREP_NODE_AND, |
| 107 | GREP_NODE_OR, |
| 108 | }; |
| 109 | |
| 110 | struct grep_expr { |
| 111 | enum grep_expr_node node; |
| 112 | union { |
| 113 | struct grep_pat *atom; |
| 114 | struct grep_expr *unary; |
| 115 | struct { |
| 116 | struct grep_expr *left; |
| 117 | struct grep_expr *right; |
| 118 | } binary; |
| 119 | } u; |
| 120 | }; |
| 121 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 122 | struct grep_opt { |
| 123 | struct grep_pat *pattern_list; |
| 124 | struct grep_pat **pattern_tail; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 125 | struct grep_expr *pattern_expression; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 126 | int prefix_length; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 127 | regex_t regexp; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 128 | unsigned linenum:1; |
| 129 | unsigned invert:1; |
Junio C Hamano | df0e7aa | 2006-05-01 12:39:21 -0700 | [diff] [blame] | 130 | unsigned name_only:1; |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 131 | unsigned unmatch_name_only:1; |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 132 | unsigned count:1; |
Junio C Hamano | 7839a25 | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 133 | unsigned word_regexp:1; |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 134 | unsigned fixed:1; |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 135 | #define GREP_BINARY_DEFAULT 0 |
| 136 | #define GREP_BINARY_NOMATCH 1 |
| 137 | #define GREP_BINARY_TEXT 2 |
| 138 | unsigned binary:2; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 139 | unsigned extended:1; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 140 | unsigned relative:1; |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 141 | unsigned pathname:1; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 142 | int regflags; |
| 143 | unsigned pre_context; |
| 144 | unsigned post_context; |
| 145 | }; |
| 146 | |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 147 | static void add_pattern(struct grep_opt *opt, const char *pat, |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 148 | const char *origin, int no, enum grep_pat_token t) |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 149 | { |
| 150 | struct grep_pat *p = xcalloc(1, sizeof(*p)); |
| 151 | p->pattern = pat; |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 152 | p->origin = origin; |
| 153 | p->no = no; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 154 | p->token = t; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 155 | *opt->pattern_tail = p; |
| 156 | opt->pattern_tail = &p->next; |
| 157 | p->next = NULL; |
| 158 | } |
| 159 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 160 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
| 161 | { |
| 162 | int err = regcomp(&p->regexp, p->pattern, opt->regflags); |
| 163 | if (err) { |
| 164 | char errbuf[1024]; |
| 165 | char where[1024]; |
| 166 | if (p->no) |
| 167 | sprintf(where, "In '%s' at %d, ", |
| 168 | p->origin, p->no); |
| 169 | else if (p->origin) |
| 170 | sprintf(where, "%s, ", p->origin); |
| 171 | else |
| 172 | where[0] = 0; |
| 173 | regerror(err, &p->regexp, errbuf, 1024); |
| 174 | regfree(&p->regexp); |
| 175 | die("%s'%s': %s", where, p->pattern, errbuf); |
| 176 | } |
| 177 | } |
| 178 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 179 | static struct grep_expr *compile_pattern_expr(struct grep_pat **); |
| 180 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
| 181 | { |
| 182 | struct grep_pat *p; |
| 183 | struct grep_expr *x; |
| 184 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 185 | p = *list; |
| 186 | switch (p->token) { |
| 187 | case GREP_PATTERN: /* atom */ |
| 188 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 189 | x->node = GREP_NODE_ATOM; |
| 190 | x->u.atom = p; |
| 191 | *list = p->next; |
| 192 | return x; |
| 193 | case GREP_OPEN_PAREN: |
| 194 | *list = p->next; |
| 195 | x = compile_pattern_expr(list); |
| 196 | if (!x) |
| 197 | return NULL; |
| 198 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
| 199 | die("unmatched parenthesis"); |
| 200 | *list = (*list)->next; |
| 201 | return x; |
| 202 | default: |
| 203 | return NULL; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) |
| 208 | { |
| 209 | struct grep_pat *p; |
| 210 | struct grep_expr *x; |
| 211 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 212 | p = *list; |
| 213 | switch (p->token) { |
| 214 | case GREP_NOT: |
| 215 | if (!p->next) |
| 216 | die("--not not followed by pattern expression"); |
| 217 | *list = p->next; |
| 218 | x = xcalloc(1, sizeof (struct grep_expr)); |
| 219 | x->node = GREP_NODE_NOT; |
| 220 | x->u.unary = compile_pattern_not(list); |
| 221 | if (!x->u.unary) |
| 222 | die("--not followed by non pattern expression"); |
| 223 | return x; |
| 224 | default: |
| 225 | return compile_pattern_atom(list); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) |
| 230 | { |
| 231 | struct grep_pat *p; |
| 232 | struct grep_expr *x, *y, *z; |
| 233 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 234 | x = compile_pattern_not(list); |
| 235 | p = *list; |
| 236 | if (p && p->token == GREP_AND) { |
| 237 | if (!p->next) |
| 238 | die("--and not followed by pattern expression"); |
| 239 | *list = p->next; |
| 240 | y = compile_pattern_and(list); |
| 241 | if (!y) |
| 242 | die("--and not followed by pattern expression"); |
| 243 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 244 | z->node = GREP_NODE_AND; |
| 245 | z->u.binary.left = x; |
| 246 | z->u.binary.right = y; |
| 247 | return z; |
| 248 | } |
| 249 | return x; |
| 250 | } |
| 251 | |
| 252 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) |
| 253 | { |
| 254 | struct grep_pat *p; |
| 255 | struct grep_expr *x, *y, *z; |
| 256 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 257 | x = compile_pattern_and(list); |
| 258 | p = *list; |
| 259 | if (x && p && p->token != GREP_CLOSE_PAREN) { |
| 260 | y = compile_pattern_or(list); |
| 261 | if (!y) |
| 262 | die("not a pattern expression %s", p->pattern); |
| 263 | z = xcalloc(1, sizeof (struct grep_expr)); |
| 264 | z->node = GREP_NODE_OR; |
| 265 | z->u.binary.left = x; |
| 266 | z->u.binary.right = y; |
| 267 | return z; |
| 268 | } |
| 269 | return x; |
| 270 | } |
| 271 | |
| 272 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) |
| 273 | { |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 274 | return compile_pattern_or(list); |
| 275 | } |
| 276 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 277 | static void compile_patterns(struct grep_opt *opt) |
| 278 | { |
| 279 | struct grep_pat *p; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 280 | |
| 281 | /* First compile regexps */ |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 282 | for (p = opt->pattern_list; p; p = p->next) { |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 283 | if (p->token == GREP_PATTERN) |
| 284 | compile_regexp(p, opt); |
| 285 | else |
| 286 | opt->extended = 1; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 287 | } |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 288 | |
| 289 | if (!opt->extended) |
| 290 | return; |
| 291 | |
| 292 | /* Then bundle them up in an expression. |
| 293 | * A classic recursive descent parser would do. |
| 294 | */ |
| 295 | p = opt->pattern_list; |
| 296 | opt->pattern_expression = compile_pattern_expr(&p); |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 297 | if (p) |
| 298 | die("incomplete pattern expression: %s", p->pattern); |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 301 | static char *end_of_line(char *cp, unsigned long *left) |
| 302 | { |
| 303 | unsigned long l = *left; |
| 304 | while (l && *cp != '\n') { |
| 305 | l--; |
| 306 | cp++; |
| 307 | } |
| 308 | *left = l; |
| 309 | return cp; |
| 310 | } |
| 311 | |
Junio C Hamano | 7839a25 | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 312 | static int word_char(char ch) |
| 313 | { |
| 314 | return isalnum(ch) || ch == '_'; |
| 315 | } |
| 316 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 317 | static void show_line(struct grep_opt *opt, const char *bol, const char *eol, |
| 318 | const char *name, unsigned lno, char sign) |
| 319 | { |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 320 | if (opt->pathname) |
| 321 | printf("%s%c", name, sign); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 322 | if (opt->linenum) |
| 323 | printf("%d%c", lno, sign); |
Junio C Hamano | a24f1e2 | 2006-05-02 01:28:02 -0700 | [diff] [blame] | 324 | printf("%.*s\n", (int)(eol-bol), bol); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 327 | /* |
| 328 | * NEEDSWORK: share code with diff.c |
| 329 | */ |
| 330 | #define FIRST_FEW_BYTES 8000 |
| 331 | static int buffer_is_binary(const char *ptr, unsigned long size) |
| 332 | { |
| 333 | if (FIRST_FEW_BYTES < size) |
| 334 | size = FIRST_FEW_BYTES; |
David Rientjes | b756776 | 2006-08-14 13:22:15 -0700 | [diff] [blame] | 335 | return !!memchr(ptr, 0, size); |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 338 | static int fixmatch(const char *pattern, char *line, regmatch_t *match) |
| 339 | { |
| 340 | char *hit = strstr(line, pattern); |
| 341 | if (!hit) { |
| 342 | match->rm_so = match->rm_eo = -1; |
| 343 | return REG_NOMATCH; |
| 344 | } |
| 345 | else { |
| 346 | match->rm_so = hit - line; |
| 347 | match->rm_eo = match->rm_so + strlen(pattern); |
| 348 | return 0; |
| 349 | } |
| 350 | } |
| 351 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 352 | static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol) |
| 353 | { |
| 354 | int hit = 0; |
Junio C Hamano | f25b793 | 2006-08-04 22:16:42 -0700 | [diff] [blame] | 355 | int at_true_bol = 1; |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 356 | regmatch_t pmatch[10]; |
| 357 | |
Junio C Hamano | f25b793 | 2006-08-04 22:16:42 -0700 | [diff] [blame] | 358 | again: |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 359 | if (!opt->fixed) { |
| 360 | regex_t *exp = &p->regexp; |
| 361 | hit = !regexec(exp, bol, ARRAY_SIZE(pmatch), |
| 362 | pmatch, 0); |
| 363 | } |
| 364 | else { |
| 365 | hit = !fixmatch(p->pattern, bol, pmatch); |
| 366 | } |
| 367 | |
| 368 | if (hit && opt->word_regexp) { |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 369 | if ((pmatch[0].rm_so < 0) || |
| 370 | (eol - bol) <= pmatch[0].rm_so || |
| 371 | (pmatch[0].rm_eo < 0) || |
| 372 | (eol - bol) < pmatch[0].rm_eo) |
| 373 | die("regexp returned nonsense"); |
Junio C Hamano | f25b793 | 2006-08-04 22:16:42 -0700 | [diff] [blame] | 374 | |
| 375 | /* Match beginning must be either beginning of the |
| 376 | * line, or at word boundary (i.e. the last char must |
| 377 | * not be a word char). Similarly, match end must be |
| 378 | * either end of the line, or at word boundary |
| 379 | * (i.e. the next char must not be a word char). |
| 380 | */ |
| 381 | if ( ((pmatch[0].rm_so == 0 && at_true_bol) || |
| 382 | !word_char(bol[pmatch[0].rm_so-1])) && |
| 383 | ((pmatch[0].rm_eo == (eol-bol)) || |
| 384 | !word_char(bol[pmatch[0].rm_eo])) ) |
| 385 | ; |
| 386 | else |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 387 | hit = 0; |
Junio C Hamano | f25b793 | 2006-08-04 22:16:42 -0700 | [diff] [blame] | 388 | |
| 389 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
| 390 | /* There could be more than one match on the |
| 391 | * line, and the first match might not be |
| 392 | * strict word match. But later ones could be! |
| 393 | */ |
| 394 | bol = pmatch[0].rm_so + bol + 1; |
| 395 | at_true_bol = 0; |
| 396 | goto again; |
| 397 | } |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 398 | } |
| 399 | return hit; |
| 400 | } |
| 401 | |
| 402 | static int match_expr_eval(struct grep_opt *opt, |
| 403 | struct grep_expr *x, |
| 404 | char *bol, char *eol) |
| 405 | { |
| 406 | switch (x->node) { |
| 407 | case GREP_NODE_ATOM: |
| 408 | return match_one_pattern(opt, x->u.atom, bol, eol); |
| 409 | break; |
| 410 | case GREP_NODE_NOT: |
| 411 | return !match_expr_eval(opt, x->u.unary, bol, eol); |
| 412 | case GREP_NODE_AND: |
| 413 | return (match_expr_eval(opt, x->u.binary.left, bol, eol) && |
| 414 | match_expr_eval(opt, x->u.binary.right, bol, eol)); |
| 415 | case GREP_NODE_OR: |
| 416 | return (match_expr_eval(opt, x->u.binary.left, bol, eol) || |
| 417 | match_expr_eval(opt, x->u.binary.right, bol, eol)); |
| 418 | } |
| 419 | die("Unexpected node type (internal error) %d\n", x->node); |
| 420 | } |
| 421 | |
| 422 | static int match_expr(struct grep_opt *opt, char *bol, char *eol) |
| 423 | { |
| 424 | struct grep_expr *x = opt->pattern_expression; |
| 425 | return match_expr_eval(opt, x, bol, eol); |
| 426 | } |
| 427 | |
| 428 | static int match_line(struct grep_opt *opt, char *bol, char *eol) |
| 429 | { |
| 430 | struct grep_pat *p; |
| 431 | if (opt->extended) |
| 432 | return match_expr(opt, bol, eol); |
| 433 | for (p = opt->pattern_list; p; p = p->next) { |
| 434 | if (match_one_pattern(opt, p, bol, eol)) |
| 435 | return 1; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 440 | static int grep_buffer(struct grep_opt *opt, const char *name, |
| 441 | char *buf, unsigned long size) |
| 442 | { |
| 443 | char *bol = buf; |
| 444 | unsigned long left = size; |
| 445 | unsigned lno = 1; |
| 446 | struct pre_context_line { |
| 447 | char *bol; |
| 448 | char *eol; |
| 449 | } *prev = NULL, *pcl; |
| 450 | unsigned last_hit = 0; |
| 451 | unsigned last_shown = 0; |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 452 | int binary_match_only = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 453 | const char *hunk_mark = ""; |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 454 | unsigned count = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 455 | |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 456 | if (buffer_is_binary(buf, size)) { |
| 457 | switch (opt->binary) { |
| 458 | case GREP_BINARY_DEFAULT: |
| 459 | binary_match_only = 1; |
| 460 | break; |
| 461 | case GREP_BINARY_NOMATCH: |
| 462 | return 0; /* Assume unmatch */ |
| 463 | break; |
| 464 | default: |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 469 | if (opt->pre_context) |
| 470 | prev = xcalloc(opt->pre_context, sizeof(*prev)); |
| 471 | if (opt->pre_context || opt->post_context) |
| 472 | hunk_mark = "--\n"; |
| 473 | |
| 474 | while (left) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 475 | char *eol, ch; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 476 | int hit = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 477 | |
| 478 | eol = end_of_line(bol, &left); |
| 479 | ch = *eol; |
| 480 | *eol = 0; |
| 481 | |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 482 | hit = match_line(opt, bol, eol); |
Junio C Hamano | 7839a25 | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 483 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 484 | /* "grep -v -e foo -e bla" should list lines |
| 485 | * that do not have either, so inversion should |
| 486 | * be done outside. |
| 487 | */ |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 488 | if (opt->invert) |
| 489 | hit = !hit; |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 490 | if (opt->unmatch_name_only) { |
| 491 | if (hit) |
| 492 | return 0; |
| 493 | goto next_line; |
| 494 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 495 | if (hit) { |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 496 | count++; |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 497 | if (binary_match_only) { |
| 498 | printf("Binary file %s matches\n", name); |
| 499 | return 1; |
| 500 | } |
Junio C Hamano | df0e7aa | 2006-05-01 12:39:21 -0700 | [diff] [blame] | 501 | if (opt->name_only) { |
| 502 | printf("%s\n", name); |
| 503 | return 1; |
| 504 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 505 | /* Hit at this line. If we haven't shown the |
| 506 | * pre-context lines, we would need to show them. |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 507 | * When asked to do "count", this still show |
| 508 | * the context which is nonsense, but the user |
| 509 | * deserves to get that ;-). |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 510 | */ |
| 511 | if (opt->pre_context) { |
| 512 | unsigned from; |
| 513 | if (opt->pre_context < lno) |
| 514 | from = lno - opt->pre_context; |
| 515 | else |
| 516 | from = 1; |
| 517 | if (from <= last_shown) |
| 518 | from = last_shown + 1; |
| 519 | if (last_shown && from != last_shown + 1) |
| 520 | printf(hunk_mark); |
| 521 | while (from < lno) { |
| 522 | pcl = &prev[lno-from-1]; |
| 523 | show_line(opt, pcl->bol, pcl->eol, |
| 524 | name, from, '-'); |
| 525 | from++; |
| 526 | } |
| 527 | last_shown = lno-1; |
| 528 | } |
| 529 | if (last_shown && lno != last_shown + 1) |
| 530 | printf(hunk_mark); |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 531 | if (!opt->count) |
| 532 | show_line(opt, bol, eol, name, lno, ':'); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 533 | last_shown = last_hit = lno; |
| 534 | } |
| 535 | else if (last_hit && |
| 536 | lno <= last_hit + opt->post_context) { |
| 537 | /* If the last hit is within the post context, |
| 538 | * we need to show this line. |
| 539 | */ |
| 540 | if (last_shown && lno != last_shown + 1) |
| 541 | printf(hunk_mark); |
| 542 | show_line(opt, bol, eol, name, lno, '-'); |
| 543 | last_shown = lno; |
| 544 | } |
| 545 | if (opt->pre_context) { |
| 546 | memmove(prev+1, prev, |
| 547 | (opt->pre_context-1) * sizeof(*prev)); |
| 548 | prev->bol = bol; |
| 549 | prev->eol = eol; |
| 550 | } |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 551 | |
| 552 | next_line: |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 553 | *eol = ch; |
| 554 | bol = eol + 1; |
Junio C Hamano | 7ed36f5 | 2006-05-03 21:03:25 -0700 | [diff] [blame] | 555 | if (!left) |
| 556 | break; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 557 | left--; |
| 558 | lno++; |
| 559 | } |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 560 | |
| 561 | if (opt->unmatch_name_only) { |
| 562 | /* We did not see any hit, so we want to show this */ |
| 563 | printf("%s\n", name); |
| 564 | return 1; |
| 565 | } |
| 566 | |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 567 | /* NEEDSWORK: |
| 568 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, |
| 569 | * which feels mostly useless but sometimes useful. Maybe |
| 570 | * make it another option? For now suppress them. |
| 571 | */ |
| 572 | if (opt->count && count) |
| 573 | printf("%s:%u\n", name, count); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 574 | return !!last_hit; |
| 575 | } |
| 576 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 577 | static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 578 | { |
| 579 | unsigned long size; |
| 580 | char *data; |
| 581 | char type[20]; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 582 | char *to_free = NULL; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 583 | int hit; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 584 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 585 | data = read_sha1_file(sha1, type, &size); |
| 586 | if (!data) { |
| 587 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); |
| 588 | return 0; |
| 589 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 590 | if (opt->relative && opt->prefix_length) { |
| 591 | static char name_buf[PATH_MAX]; |
| 592 | char *cp; |
| 593 | int name_len = strlen(name) - opt->prefix_length + 1; |
| 594 | |
| 595 | if (!tree_name_len) |
| 596 | name += opt->prefix_length; |
| 597 | else { |
| 598 | if (ARRAY_SIZE(name_buf) <= name_len) |
| 599 | cp = to_free = xmalloc(name_len); |
| 600 | else |
| 601 | cp = name_buf; |
| 602 | memcpy(cp, name, tree_name_len); |
| 603 | strcpy(cp + tree_name_len, |
| 604 | name + tree_name_len + opt->prefix_length); |
| 605 | name = cp; |
| 606 | } |
| 607 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 608 | hit = grep_buffer(opt, name, data, size); |
| 609 | free(data); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 610 | free(to_free); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 611 | return hit; |
| 612 | } |
| 613 | |
| 614 | static int grep_file(struct grep_opt *opt, const char *filename) |
| 615 | { |
| 616 | struct stat st; |
| 617 | int i; |
| 618 | char *data; |
| 619 | if (lstat(filename, &st) < 0) { |
| 620 | err_ret: |
| 621 | if (errno != ENOENT) |
| 622 | error("'%s': %s", filename, strerror(errno)); |
| 623 | return 0; |
| 624 | } |
| 625 | if (!st.st_size) |
| 626 | return 0; /* empty file -- no grep hit */ |
| 627 | if (!S_ISREG(st.st_mode)) |
| 628 | return 0; |
| 629 | i = open(filename, O_RDONLY); |
| 630 | if (i < 0) |
| 631 | goto err_ret; |
| 632 | data = xmalloc(st.st_size + 1); |
| 633 | if (st.st_size != xread(i, data, st.st_size)) { |
| 634 | error("'%s': short read %s", filename, strerror(errno)); |
| 635 | close(i); |
| 636 | free(data); |
| 637 | return 0; |
| 638 | } |
| 639 | close(i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 640 | if (opt->relative && opt->prefix_length) |
| 641 | filename += opt->prefix_length; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 642 | i = grep_buffer(opt, filename, data, st.st_size); |
| 643 | free(data); |
| 644 | return i; |
| 645 | } |
| 646 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 647 | static int exec_grep(int argc, const char **argv) |
| 648 | { |
| 649 | pid_t pid; |
| 650 | int status; |
| 651 | |
| 652 | argv[argc] = NULL; |
| 653 | pid = fork(); |
| 654 | if (pid < 0) |
| 655 | return pid; |
| 656 | if (!pid) { |
| 657 | execvp("grep", (char **) argv); |
| 658 | exit(255); |
| 659 | } |
| 660 | while (waitpid(pid, &status, 0) < 0) { |
| 661 | if (errno == EINTR) |
| 662 | continue; |
| 663 | return -1; |
| 664 | } |
| 665 | if (WIFEXITED(status)) { |
| 666 | if (!WEXITSTATUS(status)) |
| 667 | return 1; |
| 668 | return 0; |
| 669 | } |
| 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | #define MAXARGS 1000 |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 674 | #define ARGBUF 4096 |
| 675 | #define push_arg(a) do { \ |
| 676 | if (nr < MAXARGS) argv[nr++] = (a); \ |
| 677 | else die("maximum number of args exceeded"); \ |
| 678 | } while (0) |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 679 | |
| 680 | static int external_grep(struct grep_opt *opt, const char **paths, int cached) |
| 681 | { |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 682 | int i, nr, argc, hit, len, status; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 683 | const char *argv[MAXARGS+1]; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 684 | char randarg[ARGBUF]; |
| 685 | char *argptr = randarg; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 686 | struct grep_pat *p; |
| 687 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 688 | if (opt->extended || (opt->relative && opt->prefix_length)) |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 689 | return -1; |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 690 | len = nr = 0; |
| 691 | push_arg("grep"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 692 | if (opt->fixed) |
Linus Torvalds | f664751 | 2006-05-15 17:54:01 -0700 | [diff] [blame] | 693 | push_arg("-F"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 694 | if (opt->linenum) |
| 695 | push_arg("-n"); |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 696 | if (!opt->pathname) |
| 697 | push_arg("-h"); |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 698 | if (opt->regflags & REG_EXTENDED) |
| 699 | push_arg("-E"); |
Robert Fitzsimons | 3026402 | 2006-06-06 23:15:16 +0000 | [diff] [blame] | 700 | if (opt->regflags & REG_ICASE) |
| 701 | push_arg("-i"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 702 | if (opt->word_regexp) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 703 | push_arg("-w"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 704 | if (opt->name_only) |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 705 | push_arg("-l"); |
| 706 | if (opt->unmatch_name_only) |
| 707 | push_arg("-L"); |
| 708 | if (opt->count) |
| 709 | push_arg("-c"); |
| 710 | if (opt->post_context || opt->pre_context) { |
| 711 | if (opt->post_context != opt->pre_context) { |
| 712 | if (opt->pre_context) { |
| 713 | push_arg("-B"); |
| 714 | len += snprintf(argptr, sizeof(randarg)-len, |
| 715 | "%u", opt->pre_context); |
| 716 | if (sizeof(randarg) <= len) |
| 717 | die("maximum length of args exceeded"); |
| 718 | push_arg(argptr); |
| 719 | argptr += len; |
| 720 | } |
| 721 | if (opt->post_context) { |
| 722 | push_arg("-A"); |
| 723 | len += snprintf(argptr, sizeof(randarg)-len, |
| 724 | "%u", opt->post_context); |
| 725 | if (sizeof(randarg) <= len) |
| 726 | die("maximum length of args exceeded"); |
| 727 | push_arg(argptr); |
| 728 | argptr += len; |
| 729 | } |
| 730 | } |
| 731 | else { |
| 732 | push_arg("-C"); |
| 733 | len += snprintf(argptr, sizeof(randarg)-len, |
| 734 | "%u", opt->post_context); |
| 735 | if (sizeof(randarg) <= len) |
| 736 | die("maximum length of args exceeded"); |
| 737 | push_arg(argptr); |
| 738 | argptr += len; |
| 739 | } |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 740 | } |
Junio C Hamano | ffa0a7a | 2006-05-15 13:28:01 -0700 | [diff] [blame] | 741 | for (p = opt->pattern_list; p; p = p->next) { |
| 742 | push_arg("-e"); |
| 743 | push_arg(p->pattern); |
| 744 | } |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * To make sure we get the header printed out when we want it, |
| 748 | * add /dev/null to the paths to grep. This is unnecessary |
| 749 | * (and wrong) with "-l" or "-L", which always print out the |
| 750 | * name anyway. |
| 751 | * |
| 752 | * GNU grep has "-H", but this is portable. |
| 753 | */ |
| 754 | if (!opt->name_only && !opt->unmatch_name_only) |
| 755 | push_arg("/dev/null"); |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 756 | |
| 757 | hit = 0; |
| 758 | argc = nr; |
| 759 | for (i = 0; i < active_nr; i++) { |
| 760 | struct cache_entry *ce = active_cache[i]; |
Alex Riesen | fbd01ab | 2006-05-21 22:45:46 +0200 | [diff] [blame] | 761 | char *name; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 762 | if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode))) |
| 763 | continue; |
| 764 | if (!pathspec_matches(paths, ce->name)) |
| 765 | continue; |
Linus Torvalds | bbb66c6 | 2006-05-17 11:12:22 -0700 | [diff] [blame] | 766 | name = ce->name; |
| 767 | if (name[0] == '-') { |
| 768 | int len = ce_namelen(ce); |
| 769 | name = xmalloc(len + 3); |
| 770 | memcpy(name, "./", 2); |
| 771 | memcpy(name + 2, ce->name, len + 1); |
| 772 | } |
| 773 | argv[argc++] = name; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 774 | if (argc < MAXARGS) |
| 775 | continue; |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 776 | status = exec_grep(argc, argv); |
| 777 | if (0 < status) |
| 778 | hit = 1; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 779 | argc = nr; |
| 780 | } |
Junio C Hamano | fcfe34b | 2006-07-04 02:43:40 -0700 | [diff] [blame] | 781 | if (argc > nr) { |
| 782 | status = exec_grep(argc, argv); |
| 783 | if (0 < status) |
| 784 | hit = 1; |
| 785 | } |
| 786 | return hit; |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 789 | static int grep_cache(struct grep_opt *opt, const char **paths, int cached) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 790 | { |
| 791 | int hit = 0; |
| 792 | int nr; |
| 793 | read_cache(); |
| 794 | |
Linus Torvalds | 1e2398d | 2006-05-14 20:49:15 -0700 | [diff] [blame] | 795 | #ifdef __unix__ |
| 796 | /* |
| 797 | * Use the external "grep" command for the case where |
| 798 | * we grep through the checked-out files. It tends to |
| 799 | * be a lot more optimized |
| 800 | */ |
| 801 | if (!cached) { |
| 802 | hit = external_grep(opt, paths, cached); |
| 803 | if (hit >= 0) |
| 804 | return hit; |
| 805 | } |
| 806 | #endif |
| 807 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 808 | for (nr = 0; nr < active_nr; nr++) { |
| 809 | struct cache_entry *ce = active_cache[nr]; |
| 810 | if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode))) |
| 811 | continue; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 812 | if (!pathspec_matches(paths, ce->name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 813 | continue; |
| 814 | if (cached) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 815 | hit |= grep_sha1(opt, ce->sha1, ce->name, 0); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 816 | else |
| 817 | hit |= grep_file(opt, ce->name); |
| 818 | } |
| 819 | return hit; |
| 820 | } |
| 821 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 822 | static int grep_tree(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 823 | struct tree_desc *tree, |
| 824 | const char *tree_name, const char *base) |
| 825 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 826 | int len; |
| 827 | int hit = 0; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 828 | struct name_entry entry; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 829 | char *down; |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 830 | int tn_len = strlen(tree_name); |
| 831 | char *path_buf = xmalloc(PATH_MAX + tn_len + 100); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 832 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 833 | if (tn_len) { |
| 834 | tn_len = sprintf(path_buf, "%s:", tree_name); |
| 835 | down = path_buf + tn_len; |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 836 | strcat(down, base); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 837 | } |
| 838 | else { |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 839 | down = path_buf; |
| 840 | strcpy(down, base); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 841 | } |
| 842 | len = strlen(path_buf); |
| 843 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 844 | while (tree_entry(tree, &entry)) { |
| 845 | strcpy(path_buf + len, entry.path); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 846 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 847 | if (S_ISDIR(entry.mode)) |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 848 | /* Match "abc/" against pathspec to |
| 849 | * decide if we want to descend into "abc" |
| 850 | * directory. |
| 851 | */ |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 852 | strcpy(path_buf + len + entry.pathlen, "/"); |
Junio C Hamano | e0eb889 | 2006-05-01 12:27:56 -0700 | [diff] [blame] | 853 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 854 | if (!pathspec_matches(paths, down)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 855 | ; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 856 | else if (S_ISREG(entry.mode)) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 857 | hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 858 | else if (S_ISDIR(entry.mode)) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 859 | char type[20]; |
| 860 | struct tree_desc sub; |
| 861 | void *data; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 862 | data = read_sha1_file(entry.sha1, type, &sub.size); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 863 | if (!data) |
| 864 | die("unable to read tree (%s)", |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 865 | sha1_to_hex(entry.sha1)); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 866 | sub.buf = data; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 867 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 868 | free(data); |
| 869 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 870 | } |
| 871 | return hit; |
| 872 | } |
| 873 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 874 | static int grep_object(struct grep_opt *opt, const char **paths, |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 875 | struct object *obj, const char *name) |
| 876 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 877 | if (obj->type == OBJ_BLOB) |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 878 | return grep_sha1(opt, obj->sha1, name, 0); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 879 | if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 880 | struct tree_desc tree; |
| 881 | void *data; |
| 882 | int hit; |
| 883 | data = read_object_with_reference(obj->sha1, tree_type, |
| 884 | &tree.size, NULL); |
| 885 | if (!data) |
| 886 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); |
| 887 | tree.buf = data; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 888 | hit = grep_tree(opt, paths, &tree, name, ""); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 889 | free(data); |
| 890 | return hit; |
| 891 | } |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 892 | die("unable to grep from object of type %s", typename(obj->type)); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static const char builtin_grep_usage[] = |
| 896 | "git-grep <option>* <rev>* [-e] <pattern> [<path>...]"; |
| 897 | |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 898 | static const char emsg_invalid_context_len[] = |
| 899 | "%s: invalid context length argument"; |
| 900 | static const char emsg_missing_context_len[] = |
| 901 | "missing context length argument"; |
| 902 | static const char emsg_missing_argument[] = |
| 903 | "option requires an argument -%s"; |
| 904 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 905 | int cmd_grep(int argc, const char **argv, const char *prefix) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 906 | { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 907 | int hit = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 908 | int cached = 0; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 909 | int seen_dashdash = 0; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 910 | struct grep_opt opt; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 911 | struct object_array list = { 0, 0, NULL }; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 912 | const char **paths = NULL; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 913 | int i; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 914 | |
| 915 | memset(&opt, 0, sizeof(opt)); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 916 | opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; |
| 917 | opt.relative = 1; |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 918 | opt.pathname = 1; |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 919 | opt.pattern_tail = &opt.pattern_list; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 920 | opt.regflags = REG_NEWLINE; |
| 921 | |
| 922 | /* |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 923 | * If there is no -- then the paths must exist in the working |
| 924 | * tree. If there is no explicit pattern specified with -e or |
| 925 | * -f, we take the first unrecognized non option to be the |
| 926 | * pattern, but then what follows it must be zero or more |
| 927 | * valid refs up to the -- (if exists), and then existing |
| 928 | * paths. If there is an explicit pattern, then the first |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 929 | * unrecognized non option is the beginning of the refs list |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 930 | * that continues up to the -- (if exists), and then paths. |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 931 | */ |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 932 | |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 933 | while (1 < argc) { |
| 934 | const char *arg = argv[1]; |
| 935 | argc--; argv++; |
| 936 | if (!strcmp("--cached", arg)) { |
| 937 | cached = 1; |
| 938 | continue; |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 939 | } |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 940 | if (!strcmp("-a", arg) || |
| 941 | !strcmp("--text", arg)) { |
| 942 | opt.binary = GREP_BINARY_TEXT; |
| 943 | continue; |
| 944 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 945 | if (!strcmp("-i", arg) || |
| 946 | !strcmp("--ignore-case", arg)) { |
| 947 | opt.regflags |= REG_ICASE; |
| 948 | continue; |
| 949 | } |
Junio C Hamano | b8d0f5a | 2006-05-03 21:05:29 -0700 | [diff] [blame] | 950 | if (!strcmp("-I", arg)) { |
| 951 | opt.binary = GREP_BINARY_NOMATCH; |
| 952 | continue; |
| 953 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 954 | if (!strcmp("-v", arg) || |
| 955 | !strcmp("--invert-match", arg)) { |
| 956 | opt.invert = 1; |
| 957 | continue; |
| 958 | } |
| 959 | if (!strcmp("-E", arg) || |
| 960 | !strcmp("--extended-regexp", arg)) { |
| 961 | opt.regflags |= REG_EXTENDED; |
| 962 | continue; |
| 963 | } |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 964 | if (!strcmp("-F", arg) || |
| 965 | !strcmp("--fixed-strings", arg)) { |
| 966 | opt.fixed = 1; |
| 967 | continue; |
| 968 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 969 | if (!strcmp("-G", arg) || |
| 970 | !strcmp("--basic-regexp", arg)) { |
| 971 | opt.regflags &= ~REG_EXTENDED; |
| 972 | continue; |
| 973 | } |
| 974 | if (!strcmp("-n", arg)) { |
| 975 | opt.linenum = 1; |
| 976 | continue; |
| 977 | } |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 978 | if (!strcmp("-h", arg)) { |
| 979 | opt.pathname = 0; |
| 980 | continue; |
| 981 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 982 | if (!strcmp("-H", arg)) { |
Linus Torvalds | 7977f0e | 2006-09-14 10:45:12 -0700 | [diff] [blame^] | 983 | opt.pathname = 1; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 984 | continue; |
| 985 | } |
| 986 | if (!strcmp("-l", arg) || |
| 987 | !strcmp("--files-with-matches", arg)) { |
| 988 | opt.name_only = 1; |
| 989 | continue; |
| 990 | } |
Junio C Hamano | e23d2d6 | 2006-05-03 21:46:29 -0700 | [diff] [blame] | 991 | if (!strcmp("-L", arg) || |
| 992 | !strcmp("--files-without-match", arg)) { |
| 993 | opt.unmatch_name_only = 1; |
| 994 | continue; |
| 995 | } |
Junio C Hamano | 2c866cf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 996 | if (!strcmp("-c", arg) || |
| 997 | !strcmp("--count", arg)) { |
| 998 | opt.count = 1; |
| 999 | continue; |
| 1000 | } |
Junio C Hamano | 7839a25 | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 1001 | if (!strcmp("-w", arg) || |
| 1002 | !strcmp("--word-regexp", arg)) { |
| 1003 | opt.word_regexp = 1; |
| 1004 | continue; |
| 1005 | } |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 1006 | if (!strncmp("-A", arg, 2) || |
| 1007 | !strncmp("-B", arg, 2) || |
| 1008 | !strncmp("-C", arg, 2) || |
| 1009 | (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) { |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1010 | unsigned num; |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 1011 | const char *scan; |
| 1012 | switch (arg[1]) { |
| 1013 | case 'A': case 'B': case 'C': |
| 1014 | if (!arg[2]) { |
| 1015 | if (argc <= 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 1016 | die(emsg_missing_context_len); |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 1017 | scan = *++argv; |
| 1018 | argc--; |
| 1019 | } |
| 1020 | else |
| 1021 | scan = arg + 2; |
| 1022 | break; |
| 1023 | default: |
| 1024 | scan = arg + 1; |
| 1025 | break; |
| 1026 | } |
| 1027 | if (sscanf(scan, "%u", &num) != 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 1028 | die(emsg_invalid_context_len, scan); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1029 | switch (arg[1]) { |
| 1030 | case 'A': |
| 1031 | opt.post_context = num; |
| 1032 | break; |
Junio C Hamano | f462ebb | 2006-05-02 15:17:05 -0700 | [diff] [blame] | 1033 | default: |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1034 | case 'C': |
| 1035 | opt.post_context = num; |
| 1036 | case 'B': |
| 1037 | opt.pre_context = num; |
| 1038 | break; |
| 1039 | } |
| 1040 | continue; |
| 1041 | } |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1042 | if (!strcmp("-f", arg)) { |
| 1043 | FILE *patterns; |
| 1044 | int lno = 0; |
| 1045 | char buf[1024]; |
| 1046 | if (argc <= 1) |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 1047 | die(emsg_missing_argument, arg); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1048 | patterns = fopen(argv[1], "r"); |
| 1049 | if (!patterns) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1050 | die("'%s': %s", argv[1], strerror(errno)); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1051 | while (fgets(buf, sizeof(buf), patterns)) { |
| 1052 | int len = strlen(buf); |
| 1053 | if (buf[len-1] == '\n') |
| 1054 | buf[len-1] = 0; |
| 1055 | /* ignore empty line like grep does */ |
| 1056 | if (!buf[0]) |
| 1057 | continue; |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 1058 | add_pattern(&opt, xstrdup(buf), argv[1], ++lno, |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 1059 | GREP_PATTERN); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1060 | } |
| 1061 | fclose(patterns); |
| 1062 | argv++; |
| 1063 | argc--; |
| 1064 | continue; |
| 1065 | } |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 1066 | if (!strcmp("--not", arg)) { |
| 1067 | add_pattern(&opt, arg, "command line", 0, GREP_NOT); |
| 1068 | continue; |
| 1069 | } |
| 1070 | if (!strcmp("--and", arg)) { |
| 1071 | add_pattern(&opt, arg, "command line", 0, GREP_AND); |
| 1072 | continue; |
| 1073 | } |
| 1074 | if (!strcmp("--or", arg)) |
| 1075 | continue; /* no-op */ |
| 1076 | if (!strcmp("(", arg)) { |
| 1077 | add_pattern(&opt, arg, "command line", 0, GREP_OPEN_PAREN); |
| 1078 | continue; |
| 1079 | } |
| 1080 | if (!strcmp(")", arg)) { |
| 1081 | add_pattern(&opt, arg, "command line", 0, GREP_CLOSE_PAREN); |
| 1082 | continue; |
| 1083 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1084 | if (!strcmp("-e", arg)) { |
| 1085 | if (1 < argc) { |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 1086 | add_pattern(&opt, argv[1], "-e option", 0, |
| 1087 | GREP_PATTERN); |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 1088 | argv++; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1089 | argc--; |
| 1090 | continue; |
| 1091 | } |
Junio C Hamano | 088b084 | 2006-07-04 02:44:48 -0700 | [diff] [blame] | 1092 | die(emsg_missing_argument, arg); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1093 | } |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 1094 | if (!strcmp("--full-name", arg)) { |
| 1095 | opt.relative = 0; |
| 1096 | continue; |
| 1097 | } |
Junio C Hamano | 5390590 | 2006-07-04 02:31:50 -0700 | [diff] [blame] | 1098 | if (!strcmp("--", arg)) { |
| 1099 | /* later processing wants to have this at argv[1] */ |
| 1100 | argv--; |
| 1101 | argc++; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1102 | break; |
Junio C Hamano | 5390590 | 2006-07-04 02:31:50 -0700 | [diff] [blame] | 1103 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1104 | if (*arg == '-') |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1105 | usage(builtin_grep_usage); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1106 | |
| 1107 | /* First unrecognized non-option token */ |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 1108 | if (!opt.pattern_list) { |
Junio C Hamano | 79d3696 | 2006-06-30 03:04:05 -0700 | [diff] [blame] | 1109 | add_pattern(&opt, arg, "command line", 0, |
| 1110 | GREP_PATTERN); |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1111 | break; |
| 1112 | } |
| 1113 | else { |
| 1114 | /* We are looking at the first path or rev; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1115 | * it is found at argv[1] after leaving the |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1116 | * loop. |
| 1117 | */ |
| 1118 | argc++; argv--; |
| 1119 | break; |
| 1120 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1121 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1122 | |
Junio C Hamano | f9b9faf | 2006-05-02 15:40:49 -0700 | [diff] [blame] | 1123 | if (!opt.pattern_list) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1124 | die("no pattern given."); |
Junio C Hamano | 07ea91d | 2006-05-09 18:28:41 -0700 | [diff] [blame] | 1125 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
| 1126 | die("cannot mix --fixed-strings and regexp"); |
| 1127 | if (!opt.fixed) |
| 1128 | compile_patterns(&opt); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1129 | |
| 1130 | /* Check revs and then paths */ |
| 1131 | for (i = 1; i < argc; i++) { |
| 1132 | const char *arg = argv[i]; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1133 | unsigned char sha1[20]; |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1134 | /* Is it a rev? */ |
| 1135 | if (!get_sha1(arg, sha1)) { |
| 1136 | struct object *object = parse_object(sha1); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1137 | if (!object) |
| 1138 | die("bad object %s", arg); |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1139 | add_object_array(object, arg, &list); |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1140 | continue; |
| 1141 | } |
| 1142 | if (!strcmp(arg, "--")) { |
| 1143 | i++; |
| 1144 | seen_dashdash = 1; |
| 1145 | } |
| 1146 | break; |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1147 | } |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* The rest are paths */ |
| 1150 | if (!seen_dashdash) { |
| 1151 | int j; |
Junio C Hamano | c39c4f4 | 2006-05-09 18:15:21 -0700 | [diff] [blame] | 1152 | for (j = i; j < argc; j++) |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1153 | verify_filename(prefix, argv[j]); |
| 1154 | } |
| 1155 | |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 1156 | if (i < argc) { |
Junio C Hamano | 5acd64e | 2006-05-08 23:55:47 -0700 | [diff] [blame] | 1157 | paths = get_pathspec(prefix, argv + i); |
Junio C Hamano | 0d042fe | 2006-08-11 00:44:42 -0700 | [diff] [blame] | 1158 | if (opt.prefix_length && opt.relative) { |
| 1159 | /* Make sure we do not get outside of paths */ |
| 1160 | for (i = 0; paths[i]; i++) |
| 1161 | if (strncmp(prefix, paths[i], opt.prefix_length)) |
| 1162 | die("git-grep: cannot generate relative filenames containing '..'"); |
| 1163 | } |
| 1164 | } |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1165 | else if (prefix) { |
| 1166 | paths = xcalloc(2, sizeof(const char *)); |
| 1167 | paths[0] = prefix; |
| 1168 | paths[1] = NULL; |
| 1169 | } |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1170 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1171 | if (!list.nr) |
Junio C Hamano | 1362671 | 2006-05-01 15:58:29 -0700 | [diff] [blame] | 1172 | return !grep_cache(&opt, paths, cached); |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1173 | |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1174 | if (cached) |
Junio C Hamano | aa8c79a | 2006-05-08 13:28:27 -0700 | [diff] [blame] | 1175 | die("both --cached and trees are given."); |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1176 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1177 | for (i = 0; i < list.nr; i++) { |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1178 | struct object *real_obj; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 1179 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
| 1180 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) |
Junio C Hamano | 5010cb5 | 2006-04-30 23:28:15 -0700 | [diff] [blame] | 1181 | hit = 1; |
| 1182 | } |
| 1183 | return !hit; |
| 1184 | } |