Skip to content

Commit 59332d1

Browse files
committed
Resurrect "git grep --no-index"
This reverts commit 3c8f6c8 (Revert 3081623 and 7e62265, 2010-02-05) as the issue has been sorted out.
1 parent 717c397 commit 59332d1

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

builtin-grep.c

Lines changed: 40 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
#ifndef NO_PTHREADS
1920
#include "thread-utils.h"
@@ -645,6 +646,24 @@ static int grep_object(struct grep_opt *opt, const char **paths,
645646
die("unable to grep from object of type %s", typename(obj->type));
646647
}
647648

649+
static int grep_directory(struct grep_opt *opt, const char **paths)
650+
{
651+
struct dir_struct dir;
652+
int i, hit = 0;
653+
654+
memset(&dir, 0, sizeof(dir));
655+
setup_standard_excludes(&dir);
656+
657+
fill_directory(&dir, paths);
658+
for (i = 0; i < dir.nr; i++) {
659+
hit |= grep_file(opt, dir.entries[i]->name);
660+
if (hit && opt->status_only)
661+
break;
662+
}
663+
free_grep_patterns(opt);
664+
return hit;
665+
}
666+
648667
static int context_callback(const struct option *opt, const char *arg,
649668
int unset)
650669
{
@@ -739,9 +758,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
739758
const char **paths = NULL;
740759
int i;
741760
int dummy;
761+
int nongit = 0, use_index = 1;
742762
struct option options[] = {
743763
OPT_BOOLEAN(0, "cached", &cached,
744764
"search in index instead of in the work tree"),
765+
OPT_BOOLEAN(0, "index", &use_index,
766+
"--no-index finds in contents not managed by git"),
745767
OPT_GROUP(""),
746768
OPT_BOOLEAN('v', "invert-match", &opt.invert,
747769
"show non-matching lines"),
@@ -824,6 +846,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
824846
OPT_END()
825847
};
826848

849+
prefix = setup_git_directory_gently(&nongit);
850+
827851
/*
828852
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
829853
* to show usage information and exit.
@@ -861,6 +885,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
861885
PARSE_OPT_STOP_AT_NON_OPTION |
862886
PARSE_OPT_NO_INTERNAL_HELP);
863887

888+
if (use_index && nongit)
889+
/* die the same way as if we did it at the beginning */
890+
setup_git_directory();
891+
864892
/* First unrecognized non-option token */
865893
if (argc > 0 && !opt.pattern_list) {
866894
append_grep_pattern(&opt, argv[0], "command line", 0,
@@ -922,6 +950,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
922950
paths[1] = NULL;
923951
}
924952

953+
if (!use_index) {
954+
int hit;
955+
if (cached)
956+
die("--cached cannot be used with --no-index.");
957+
if (list.nr)
958+
die("--no-index cannot be used with revs.");
959+
hit = grep_directory(&opt, paths);
960+
if (use_threads)
961+
hit |= wait_all();
962+
return !hit;
963+
}
964+
925965
if (!list.nr) {
926966
int hit;
927967
if (!cached)

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
317317
{ "fsck-objects", cmd_fsck, RUN_SETUP },
318318
{ "gc", cmd_gc, RUN_SETUP },
319319
{ "get-tar-commit-id", cmd_get_tar_commit_id },
320-
{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
320+
{ "grep", cmd_grep, USE_PAGER },
321321
{ "hash-object", cmd_hash_object },
322322
{ "help", cmd_help },
323323
{ "index-pack", cmd_index_pack },

t/t7002-grep.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,56 @@ test_expect_success 'grep -Fi' '
434434
test_cmp expected actual
435435
'
436436

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

0 commit comments

Comments
 (0)