summaryrefslogtreecommitdiff
path: root/builtin-grep.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2010-01-15 20:52:40 (GMT)
committerJunio C Hamano <gitster@pobox.com>2010-01-15 20:54:02 (GMT)
commit308162372d0aa202ff45743e02253e20a4fac4d7 (patch)
treef4d6de4099ce1a6213b5aadec23177ee26bf5e0c /builtin-grep.c
parent7e622650d756955a94f546866eddd51506aa93a3 (diff)
downloadgit-308162372d0aa202ff45743e02253e20a4fac4d7.zip
git-308162372d0aa202ff45743e02253e20a4fac4d7.tar.gz
git-308162372d0aa202ff45743e02253e20a4fac4d7.tar.bz2
grep --no-index: allow use of "git grep" outside a git repository
Just like some people wanted diff features that are not found in other people's diff implementations outside of a git repository and added --no-index mode to the command, this adds --no-index mode to the "git grep" command. Also, inside a git repository, --no-index mode allows you to grep in untracked (but not ignored) files. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-grep.c')
-rw-r--r--builtin-grep.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/builtin-grep.c b/builtin-grep.c
index 229555d..1283373 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -14,6 +14,7 @@
#include "userdiff.h"
#include "grep.h"
#include "quote.h"
+#include "dir.h"
static char const * const grep_usage[] = {
"git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
@@ -320,6 +321,21 @@ static int grep_object(struct grep_opt *opt, const char **paths,
die("unable to grep from object of type %s", typename(obj->type));
}
+static int grep_directory(struct grep_opt *opt, const char **paths)
+{
+ struct dir_struct dir;
+ int i, hit = 0;
+
+ memset(&dir, 0, sizeof(dir));
+ setup_standard_excludes(&dir);
+
+ fill_directory(&dir, paths);
+ for (i = 0; i < dir.nr; i++)
+ hit |= grep_file(opt, dir.entries[i]->name);
+ free_grep_patterns(opt);
+ return hit;
+}
+
static int context_callback(const struct option *opt, const char *arg,
int unset)
{
@@ -418,6 +434,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
struct option options[] = {
OPT_BOOLEAN(0, "cached", &cached,
"search in index instead of in the work tree"),
+ OPT_BOOLEAN(0, "index", &use_index,
+ "--no-index finds in contents not managed by git"),
OPT_GROUP(""),
OPT_BOOLEAN('v', "invert-match", &opt.invert,
"show non-matching lines"),
@@ -591,6 +609,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
paths[1] = NULL;
}
+ if (!use_index) {
+ if (cached)
+ die("--cached cannot be used with --no-index.");
+ if (list.nr)
+ die("--no-index cannot be used with revs.");
+ return !grep_directory(&opt, paths);
+ }
+
if (!list.nr) {
if (!cached)
setup_work_tree();