Add callback data to for_each_ref() family.
[gitweb.git] / builtin-grep.c
index 0bd517b2649af37d9980f85e784f9a00c3263922..ed87a5550c7dab6de56327b8acc0e3e2c897e39e 100644 (file)
@@ -138,6 +138,7 @@ struct grep_opt {
        unsigned binary:2;
        unsigned extended:1;
        unsigned relative:1;
+       unsigned pathname:1;
        int regflags;
        unsigned pre_context;
        unsigned post_context;
@@ -293,9 +294,6 @@ static void compile_patterns(struct grep_opt *opt)
         */
        p = opt->pattern_list;
        opt->pattern_expression = compile_pattern_expr(&p);
-#if DEBUG
-       dump_pattern_exp(opt->pattern_expression, 0);
-#endif
        if (p)
                die("incomplete pattern expression: %s", p->pattern);
 }
@@ -319,7 +317,8 @@ static int word_char(char ch)
 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
                      const char *name, unsigned lno, char sign)
 {
-       printf("%s%c", name, sign);
+       if (opt->pathname)
+               printf("%s%c", name, sign);
        if (opt->linenum)
                printf("%d%c", lno, sign);
        printf("%.*s\n", (int)(eol-bol), bol);
@@ -694,6 +693,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
                push_arg("-F");
        if (opt->linenum)
                push_arg("-n");
+       if (!opt->pathname)
+               push_arg("-h");
        if (opt->regflags & REG_EXTENDED)
                push_arg("-E");
        if (opt->regflags & REG_ICASE)
@@ -914,6 +915,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        memset(&opt, 0, sizeof(opt));
        opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
        opt.relative = 1;
+       opt.pathname = 1;
        opt.pattern_tail = &opt.pattern_list;
        opt.regflags = REG_NEWLINE;
 
@@ -973,10 +975,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        opt.linenum = 1;
                        continue;
                }
+               if (!strcmp("-h", arg)) {
+                       opt.pathname = 0;
+                       continue;
+               }
                if (!strcmp("-H", arg)) {
-                       /* We always show the pathname, so this
-                        * is a noop.
-                        */
+                       opt.pathname = 1;
                        continue;
                }
                if (!strcmp("-l", arg) ||
@@ -1051,7 +1055,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                                /* ignore empty line like grep does */
                                if (!buf[0])
                                        continue;
-                               add_pattern(&opt, strdup(buf), argv[1], ++lno,
+                               add_pattern(&opt, xstrdup(buf), argv[1], ++lno,
                                            GREP_PATTERN);
                        }
                        fclose(patterns);