Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
grep: turn off gitlink detection for --no-index
If we are running "git grep --no-index" outside of a git
repository, we behave roughly like "grep -r", examining all
files in the current directory and its subdirectories.
However, because we use fill_directory() to do the
recursion, it will skip over any directories which look like
sub-repositories.

For a normal git operation (like "git grep" in a repository)
this makes sense; we do not want to cross the boundary out
of our current repository into a submodule. But for
"--no-index" without a repository, we should look at all
files, including embedded repositories.

There is one exception, though: we probably should _not_
descend into ".git" directories. Doing so is inefficient and
unlikely to turn up useful hits.

This patch drops our use of dir.c's gitlink-detection, but
we do still avoid ".git". That makes us more like tools such
as "ack" or "ag", which also know to avoid cruft in .git.

As a bonus, this also drops our usage of the ref code
when we are outside of a repository, making the transition
to pluggable ref backends cleaner.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Mar 7, 2016
1 parent 5735dc5 commit 85975c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions builtin/grep.c
Expand Up @@ -522,12 +522,14 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
}

static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
int exc_std)
int exc_std, int use_index)
{
struct dir_struct dir;
int i, hit = 0;

memset(&dir, 0, sizeof(dir));
if (!use_index)
dir.flags |= DIR_NO_GITLINKS;
if (exc_std)
setup_standard_excludes(&dir);

Expand Down Expand Up @@ -902,7 +904,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
if (list.nr)
die(_("--no-index or --untracked cannot be used with revs."));
hit = grep_directory(&opt, &pathspec, use_exclude);
hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
} else if (0 <= opt_exclude) {
die(_("--[no-]exclude-standard cannot be used for tracked contents."));
} else if (!list.nr) {
Expand Down
27 changes: 27 additions & 0 deletions t/t7810-grep.sh
Expand Up @@ -905,6 +905,33 @@ test_expect_success 'inside git repository but with --no-index' '
)
'

test_expect_success 'grep --no-index descends into repos, but not .git' '
rm -fr non &&
mkdir -p non/git &&
(
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
echo magic >file &&
git init repo &&
(
cd repo &&
echo magic >file &&
git add file &&
git commit -m foo &&
echo magic >.git/file
) &&
cat >expect <<-\EOF &&
file
repo/file
EOF
git grep -l --no-index magic >actual &&
test_cmp expect actual
)
'

test_expect_success 'setup double-dash tests' '
cat >double-dash <<EOF &&
--
Expand Down

0 comments on commit 85975c0

Please sign in to comment.