Don't find objects in packs which aren't available anymore.
[gitweb.git] / builtin-grep.c
index 671878817386c72f16fe64cc62b6fa8daa62f102..2bfbdb71407dcd0ddfe970f6a614158d954cd4c0 100644 (file)
 #include "tag.h"
 #include "tree-walk.h"
 #include "builtin.h"
-#include <regex.h>
 #include "grep.h"
-#include <fnmatch.h>
-#include <sys/wait.h>
 
 /*
  * git grep pathspecs are somewhat different from diff-tree pathspecs;
@@ -139,7 +136,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
        if (i < 0)
                goto err_ret;
        data = xmalloc(st.st_size + 1);
-       if (st.st_size != xread(i, data, st.st_size)) {
+       if (st.st_size != read_in_full(i, data, st.st_size)) {
                error("'%s': short read %s", filename, strerror(errno));
                close(i);
                free(data);
@@ -268,7 +265,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce = active_cache[i];
                char *name;
-               if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+               if (!S_ISREG(ntohl(ce->ce_mode)))
                        continue;
                if (!pathspec_matches(paths, ce->name))
                        continue;
@@ -280,12 +277,19 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
                        memcpy(name + 2, ce->name, len + 1);
                }
                argv[argc++] = name;
-               if (argc < MAXARGS)
+               if (argc < MAXARGS && !ce_stage(ce))
                        continue;
                status = exec_grep(argc, argv);
                if (0 < status)
                        hit = 1;
                argc = nr;
+               if (ce_stage(ce)) {
+                       do {
+                               i++;
+                       } while (i < active_nr &&
+                                !strcmp(ce->name, active_cache[i]->name));
+                       i--; /* compensate for loop control */
+               }
        }
        if (argc > nr) {
                status = exec_grep(argc, argv);
@@ -316,15 +320,26 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 
        for (nr = 0; nr < active_nr; nr++) {
                struct cache_entry *ce = active_cache[nr];
-               if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+               if (!S_ISREG(ntohl(ce->ce_mode)))
                        continue;
                if (!pathspec_matches(paths, ce->name))
                        continue;
-               if (cached)
+               if (cached) {
+                       if (ce_stage(ce))
+                               continue;
                        hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
+               }
                else
                        hit |= grep_file(opt, ce->name);
+               if (ce_stage(ce)) {
+                       do {
+                               nr++;
+                       } while (nr < active_nr &&
+                                !strcmp(ce->name, active_cache[nr]->name));
+                       nr--; /* compensate for loop control */
+               }
        }
+       free_grep_patterns(opt);
        return hit;
 }
 
@@ -595,6 +610,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                                            GREP_CLOSE_PAREN);
                        continue;
                }
+               if (!strcmp("--all-match", arg)) {
+                       opt.all_match = 1;
+                       continue;
+               }
                if (!strcmp("-e", arg)) {
                        if (1 < argc) {
                                append_grep_pattern(&opt, argv[1],
@@ -694,5 +713,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                if (grep_object(&opt, paths, real_obj, list.objects[i].name))
                        hit = 1;
        }
+       free_grep_patterns(&opt);
        return !hit;
 }