GIT 1.5.6
[gitweb.git] / builtin-grep.c
index bbf747fc7b66f6b4f19d9dc62a9ba10965199917..ef299108f529fa5fa931d765828f04535c3a6351 100644 (file)
 #include "builtin.h"
 #include "grep.h"
 
+#ifndef NO_EXTERNAL_GREP
+#ifdef __unix__
+#define NO_EXTERNAL_GREP 0
+#else
+#define NO_EXTERNAL_GREP 1
+#endif
+#endif
+
 /*
  * git grep pathspecs are somewhat different from diff-tree pathspecs;
  * pathname wildcards are allowed.
@@ -153,7 +161,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
        return i;
 }
 
-#ifdef __unix__
+#if !NO_EXTERNAL_GREP
 static int exec_grep(int argc, const char **argv)
 {
        pid_t pid;
@@ -331,7 +339,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
                struct cache_entry *ce = active_cache[i];
                char *name;
                int kept;
-               if (!S_ISREG(ntohl(ce->ce_mode)))
+               if (!S_ISREG(ce->ce_mode))
                        continue;
                if (!pathspec_matches(paths, ce->name))
                        continue;
@@ -343,12 +351,12 @@ 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)
-                       continue;
-               status = flush_grep(opt, argc, nr, argv, &kept);
-               if (0 < status)
-                       hit = 1;
-               argc = nr + kept;
+               if (MAXARGS <= argc) {
+                       status = flush_grep(opt, argc, nr, argv, &kept);
+                       if (0 < status)
+                               hit = 1;
+                       argc = nr + kept;
+               }
                if (ce_stage(ce)) {
                        do {
                                i++;
@@ -372,7 +380,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
        int nr;
        read_cache();
 
-#ifdef __unix__
+#if !NO_EXTERNAL_GREP
        /*
         * Use the external "grep" command for the case where
         * we grep through the checked-out files. It tends to
@@ -387,7 +395,7 @@ 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 (!S_ISREG(ntohl(ce->ce_mode)))
+               if (!S_ISREG(ce->ce_mode))
                        continue;
                if (!pathspec_matches(paths, ce->name))
                        continue;
@@ -578,6 +586,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        continue;
                }
                if (!strcmp("-l", arg) ||
+                   !strcmp("--name-only", arg) ||
                    !strcmp("--files-with-matches", arg)) {
                        opt.name_only = 1;
                        continue;
@@ -644,7 +653,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                                die("'%s': %s", argv[1], strerror(errno));
                        while (fgets(buf, sizeof(buf), patterns)) {
                                int len = strlen(buf);
-                               if (buf[len-1] == '\n')
+                               if (len && buf[len-1] == '\n')
                                        buf[len-1] = 0;
                                /* ignore empty line like grep does */
                                if (!buf[0])