Skip to content

Commit dbfae86

Browse files
committed
Merge branch 'jc/maint-grep-untracked-exclude' into jc/grep-untracked-exclude
* jc/maint-grep-untracked-exclude: grep: teach --untracked and --exclude-standard options grep --no-index: don't use git standard exclusions grep: do not use --index in the short usage output Conflicts: Documentation/git-grep.txt builtin/grep.c
2 parents 703f05a + 0a93fb8 commit dbfae86

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

Documentation/git-grep.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SYNOPSIS
2323
[-A <post-context>] [-B <pre-context>] [-C <context>]
2424
[-f <file>] [-e] <pattern>
2525
[--and|--or|--not|(|)|-e <pattern>...]
26-
[--cached | --no-index | <tree>...]
26+
[ [--exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
2727
[--] [<pathspec>...]
2828

2929
DESCRIPTION
@@ -49,7 +49,20 @@ OPTIONS
4949
blobs registered in the index file.
5050

5151
--no-index::
52-
Search files in the current directory, not just those tracked by git.
52+
Search files in the current directory that is not managed by git.
53+
54+
--untracked::
55+
In addition to searching in the tracked files in the working
56+
tree, search also in untracked files.
57+
58+
--no-exclude-standard::
59+
Also search in ignored files by not honoring the `.gitignore`
60+
mechanism. Only useful with `--untracked`.
61+
62+
--exclude-standard::
63+
Do not pay attention to ignored files specified via the `.gitignore`
64+
mechanism. Only useful when searching files in the current directory
65+
with `--no-index`.
5366

5467
-a::
5568
--text::

builtin/grep.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,15 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
637637
return hit;
638638
}
639639

640-
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec)
640+
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
641+
int exc_std)
641642
{
642643
struct dir_struct dir;
643644
int i, hit = 0;
644645

645646
memset(&dir, 0, sizeof(dir));
646-
setup_standard_excludes(&dir);
647+
if (exc_std)
648+
setup_standard_excludes(&dir);
647649

648650
fill_directory(&dir, pathspec->raw);
649651
for (i = 0; i < dir.nr; i++) {
@@ -750,7 +752,7 @@ static int help_callback(const struct option *opt, const char *arg, int unset)
750752
int cmd_grep(int argc, const char **argv, const char *prefix)
751753
{
752754
int hit = 0;
753-
int cached = 0;
755+
int cached = 0, untracked = 0, opt_exclude = -1;
754756
int seen_dashdash = 0;
755757
int external_grep_allowed__ignored;
756758
const char *show_in_pager = NULL, *default_pager = "dummy";
@@ -774,8 +776,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
774776
struct option options[] = {
775777
OPT_BOOLEAN(0, "cached", &cached,
776778
"search in index instead of in the work tree"),
777-
OPT_BOOLEAN(0, "index", &use_index,
778-
"--no-index finds in contents not managed by git"),
779+
{ OPTION_BOOLEAN, 0, "index", &use_index, NULL,
780+
"finds in contents not managed by git",
781+
PARSE_OPT_NOARG | PARSE_OPT_NEGHELP },
782+
OPT_BOOLEAN(0, "untracked", &untracked,
783+
"search in both tracked and untracked files"),
784+
OPT_SET_INT(0, "exclude-standard", &opt_exclude,
785+
"search also in ignored files", 1),
779786
OPT_GROUP(""),
780787
OPT_BOOLEAN('v', "invert-match", &opt.invert,
781788
"show non-matching lines"),
@@ -1045,13 +1052,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10451052
if (!show_in_pager)
10461053
setup_pager();
10471054

1055+
if (!use_index && (untracked || cached))
1056+
die(_("--cached or --untracked cannot be used with --no-index."));
10481057

1049-
if (!use_index) {
1050-
if (cached)
1051-
die(_("--cached cannot be used with --no-index."));
1058+
if (!use_index || untracked) {
1059+
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
10521060
if (list.nr)
1053-
die(_("--no-index cannot be used with revs."));
1054-
hit = grep_directory(&opt, &pathspec);
1061+
die(_("--no-index or --untracked cannot be used with revs."));
1062+
hit = grep_directory(&opt, &pathspec, use_exclude);
1063+
} else if (0 <= opt_exclude) {
1064+
die(_("--exclude or --no-exclude cannot be used for tracked contents."));
10551065
} else if (!list.nr) {
10561066
if (!cached)
10571067
setup_work_tree();

t/t7810-grep.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ test_expect_success 'outside of git repository' '
554554
mkdir -p non/git/sub &&
555555
echo hello >non/git/file1 &&
556556
echo world >non/git/sub/file2 &&
557-
echo ".*o*" >non/git/.gitignore &&
558557
{
559558
echo file1:hello &&
560559
echo sub/file2:world
@@ -571,6 +570,23 @@ test_expect_success 'outside of git repository' '
571570
test_must_fail git grep o &&
572571
git grep --no-index o >../../actual.sub &&
573572
test_cmp ../../expect.sub ../../actual.sub
573+
) &&
574+
575+
echo ".*o*" >non/git/.gitignore &&
576+
(
577+
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
578+
export GIT_CEILING_DIRECTORIES &&
579+
cd non/git &&
580+
test_must_fail git grep o &&
581+
git grep --no-index --exclude-standard o >../actual.full &&
582+
test_cmp ../expect.full ../actual.full &&
583+
584+
{
585+
echo ".gitignore:.*o*"
586+
cat ../expect.full
587+
} >../expect.with.ignored &&
588+
git grep --no-index --no-exclude o >../actual.full &&
589+
test_cmp ../expect.with.ignored ../actual.full
574590
)
575591
'
576592

@@ -583,6 +599,10 @@ test_expect_success 'inside git repository but with --no-index' '
583599
{
584600
echo file1:hello &&
585601
echo sub/file2:world
602+
} >is/expect.unignored &&
603+
{
604+
echo ".gitignore:.*o*" &&
605+
cat is/expect.unignored
586606
} >is/expect.full &&
587607
: >is/expect.empty &&
588608
echo file2:world >is/expect.sub &&
@@ -591,12 +611,24 @@ test_expect_success 'inside git repository but with --no-index' '
591611
git init &&
592612
test_must_fail git grep o >../actual.full &&
593613
test_cmp ../expect.empty ../actual.full &&
614+
615+
git grep --untracked o >../actual.unignored &&
616+
test_cmp ../expect.unignored ../actual.unignored &&
617+
594618
git grep --no-index o >../actual.full &&
595619
test_cmp ../expect.full ../actual.full &&
620+
621+
git grep --no-index --exclude-standard o >../actual.unignored &&
622+
test_cmp ../expect.unignored ../actual.unignored &&
623+
596624
cd sub &&
597625
test_must_fail git grep o >../../actual.sub &&
598626
test_cmp ../../expect.empty ../../actual.sub &&
627+
599628
git grep --no-index o >../../actual.sub &&
629+
test_cmp ../../expect.sub ../../actual.sub &&
630+
631+
git grep --untracked o >../../actual.sub &&
600632
test_cmp ../../expect.sub ../../actual.sub
601633
)
602634
'

0 commit comments

Comments
 (0)