Skip to content

Commit 3081623

Browse files
committed
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>
1 parent 7e62265 commit 3081623

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

builtin-grep.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "userdiff.h"
1515
#include "grep.h"
1616
#include "quote.h"
17+
#include "dir.h"
1718

1819
static char const * const grep_usage[] = {
1920
"git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
@@ -320,6 +321,21 @@ static int grep_object(struct grep_opt *opt, const char **paths,
320321
die("unable to grep from object of type %s", typename(obj->type));
321322
}
322323

324+
static int grep_directory(struct grep_opt *opt, const char **paths)
325+
{
326+
struct dir_struct dir;
327+
int i, hit = 0;
328+
329+
memset(&dir, 0, sizeof(dir));
330+
setup_standard_excludes(&dir);
331+
332+
fill_directory(&dir, paths);
333+
for (i = 0; i < dir.nr; i++)
334+
hit |= grep_file(opt, dir.entries[i]->name);
335+
free_grep_patterns(opt);
336+
return hit;
337+
}
338+
323339
static int context_callback(const struct option *opt, const char *arg,
324340
int unset)
325341
{
@@ -418,6 +434,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
418434
struct option options[] = {
419435
OPT_BOOLEAN(0, "cached", &cached,
420436
"search in index instead of in the work tree"),
437+
OPT_BOOLEAN(0, "index", &use_index,
438+
"--no-index finds in contents not managed by git"),
421439
OPT_GROUP(""),
422440
OPT_BOOLEAN('v', "invert-match", &opt.invert,
423441
"show non-matching lines"),
@@ -591,6 +609,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
591609
paths[1] = NULL;
592610
}
593611

612+
if (!use_index) {
613+
if (cached)
614+
die("--cached cannot be used with --no-index.");
615+
if (list.nr)
616+
die("--no-index cannot be used with revs.");
617+
return !grep_directory(&opt, paths);
618+
}
619+
594620
if (!list.nr) {
595621
if (!cached)
596622
setup_work_tree();

t/t7002-grep.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,56 @@ test_expect_success 'grep -Fi' '
426426
test_cmp expected actual
427427
'
428428

429+
test_expect_success 'outside of git repository' '
430+
rm -fr non &&
431+
mkdir -p non/git/sub &&
432+
echo hello >non/git/file1 &&
433+
echo world >non/git/sub/file2 &&
434+
echo ".*o*" >non/git/.gitignore &&
435+
{
436+
echo file1:hello &&
437+
echo sub/file2:world
438+
} >non/expect.full &&
439+
echo file2:world >non/expect.sub
440+
(
441+
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
442+
export GIT_CEILING_DIRECTORIES &&
443+
cd non/git &&
444+
test_must_fail git grep o &&
445+
git grep --no-index o >../actual.full &&
446+
test_cmp ../expect.full ../actual.full
447+
cd sub &&
448+
test_must_fail git grep o &&
449+
git grep --no-index o >../../actual.sub &&
450+
test_cmp ../../expect.sub ../../actual.sub
451+
)
452+
'
453+
454+
test_expect_success 'inside git repository but with --no-index' '
455+
rm -fr is &&
456+
mkdir -p is/git/sub &&
457+
echo hello >is/git/file1 &&
458+
echo world >is/git/sub/file2 &&
459+
echo ".*o*" >is/git/.gitignore &&
460+
{
461+
echo file1:hello &&
462+
echo sub/file2:world
463+
} >is/expect.full &&
464+
: >is/expect.empty &&
465+
echo file2:world >is/expect.sub
466+
(
467+
cd is/git &&
468+
git init &&
469+
test_must_fail git grep o >../actual.full &&
470+
test_cmp ../expect.empty ../actual.full &&
471+
git grep --no-index o >../actual.full &&
472+
test_cmp ../expect.full ../actual.full &&
473+
cd sub &&
474+
test_must_fail git grep o >../../actual.sub &&
475+
test_cmp ../../expect.empty ../../actual.sub &&
476+
git grep --no-index o >../../actual.sub &&
477+
test_cmp ../../expect.sub ../../actual.sub
478+
)
479+
'
480+
429481
test_done

0 commit comments

Comments
 (0)