Merge branch 'jc/grep-untracked-exclude'
authorJunio C Hamano <gitster@pobox.com>
Fri, 14 Oct 2011 02:03:23 +0000 (19:03 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Oct 2011 02:03:23 +0000 (19:03 -0700)
* jc/grep-untracked-exclude:
grep: teach --untracked and --exclude-standard options

1  2 
builtin/grep.c
diff --combined builtin/grep.c
index 91c3a852f6eb88c9c56a75dec6239d9043683764,5e6d3c335224ce5acb38c9aea8bee30b04ea09e2..024b87868aaf6b57b4af01feecf671cad664433c
@@@ -598,11 -598,8 +598,11 @@@ static int grep_object(struct grep_opt 
                struct strbuf base;
                int hit, len;
  
 +              read_sha1_lock();
                data = read_object_with_reference(obj->sha1, tree_type,
                                                  &size, NULL);
 +              read_sha1_unlock();
 +
                if (!data)
                        die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1));
  
@@@ -640,12 -637,15 +640,15 @@@ static int grep_objects(struct grep_op
        return hit;
  }
  
- static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec)
+ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
+                         int exc_std)
  {
        struct dir_struct dir;
        int i, hit = 0;
  
        memset(&dir, 0, sizeof(dir));
+       if (exc_std)
+               setup_standard_excludes(&dir);
  
        fill_directory(&dir, pathspec->raw);
        for (i = 0; i < dir.nr; i++) {
@@@ -752,7 -752,7 +755,7 @@@ static int help_callback(const struct o
  int cmd_grep(int argc, const char **argv, const char *prefix)
  {
        int hit = 0;
-       int cached = 0;
+       int cached = 0, untracked = 0, opt_exclude = -1;
        int seen_dashdash = 0;
        int external_grep_allowed__ignored;
        const char *show_in_pager = NULL, *default_pager = "dummy";
                { OPTION_BOOLEAN, 0, "index", &use_index, NULL,
                        "finds in contents not managed by git",
                        PARSE_OPT_NOARG | PARSE_OPT_NEGHELP },
+               OPT_BOOLEAN(0, "untracked", &untracked,
+                       "search in both tracked and untracked files"),
+               OPT_SET_INT(0, "exclude-standard", &opt_exclude,
+                           "search also in ignored files", 1),
                OPT_GROUP(""),
                OPT_BOOLEAN('v', "invert-match", &opt.invert,
                        "show non-matching lines"),
        if (!show_in_pager)
                setup_pager();
  
+       if (!use_index && (untracked || cached))
+               die(_("--cached or --untracked cannot be used with --no-index."));
  
-       if (!use_index) {
-               if (cached)
-                       die(_("--cached cannot be used with --no-index."));
+       if (!use_index || untracked) {
+               int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
                if (list.nr)
-                       die(_("--no-index cannot be used with revs."));
-               hit = grep_directory(&opt, &pathspec);
+                       die(_("--no-index or --untracked cannot be used with revs."));
+               hit = grep_directory(&opt, &pathspec, use_exclude);
+       } else if (0 <= opt_exclude) {
+               die(_("--exclude or --no-exclude cannot be used for tracked contents."));
        } else if (!list.nr) {
                if (!cached)
                        setup_work_tree();