Skip to content

Commit 263830c

Browse files
committed
Merge branch 'rs/git-dir-cleanup'
* rs/git-dir-cleanup: Resurrect "git grep --no-index" setenv(GIT_DIR) clean-up Conflicts: builtin-grep.c t/t7002-grep.sh
2 parents 67eb538 + 59332d1 commit 263830c

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
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
/*
865893
* skip a -- separator; we know it cannot be
866894
* separating revisions from pathnames if
@@ -932,6 +960,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
932960
paths[1] = NULL;
933961
}
934962

963+
if (!use_index) {
964+
int hit;
965+
if (cached)
966+
die("--cached cannot be used with --no-index.");
967+
if (list.nr)
968+
die("--no-index cannot be used with revs.");
969+
hit = grep_directory(&opt, paths);
970+
if (use_threads)
971+
hit |= wait_all();
972+
return !hit;
973+
}
974+
935975
if (!list.nr) {
936976
int hit;
937977
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 },

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ char *enter_repo(char *path, int strict)
336336

337337
if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
338338
validate_headref("HEAD") == 0) {
339-
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
339+
set_git_dir(".");
340340
check_repository_format();
341341
return path;
342342
}

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
404404
inside_work_tree = 0;
405405
if (offset != len) {
406406
cwd[offset] = '\0';
407-
setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
407+
set_git_dir(cwd);
408408
} else
409-
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
409+
set_git_dir(".");
410410
check_repository_format_gently(nongit_ok);
411411
return NULL;
412412
}

t/t7002-grep.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,58 @@ 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_expect_success 'setup double-dash tests' '
438490
cat >double-dash <<EOF &&
439491
--

0 commit comments

Comments
 (0)