Skip to content

Commit 0a93fb8

Browse files
committed
grep: teach --untracked and --exclude-standard options
In a working tree of a git managed repository, "grep --untracked" would find the specified patterns from files in untracked files in addition to its usual behaviour of finding them in the tracked files. By default, when working with "--no-index" option, "grep" does not pay attention to .gitignore mechanism. "grep --no-index --exclude-standard" can be used to tell the command to use .gitignore and stop reporting hits from files that would be ignored. Also, when working without "--no-index", "grep" honors .gitignore mechanism, and "grep --no-exclude-standard" can be used to tell the command to include hits from files that are ignored. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a9e6436 commit 0a93fb8

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

Documentation/git-grep.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-grep - Print lines matching a pattern
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git grep' [--cached]
12+
'git grep' [--cached] [--untracked] [--exclude-standard]
1313
[-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
1414
[-v | --invert-match] [-h|-H] [--full-name]
1515
[-E | --extended-regexp] [-G | --basic-regexp]
@@ -36,6 +36,19 @@ OPTIONS
3636
Instead of searching in the working tree files, check
3737
the blobs registered in the index file.
3838

39+
--untracked::
40+
In addition to searching in the tracked files in the working
41+
tree, search also in untracked files.
42+
43+
--no-exclude-standard::
44+
Also search in ignored files by not honoring the `.gitignore`
45+
mechanism. Only useful with `--untracked`.
46+
47+
--exclude-standard::
48+
Do not pay attention to ignored files specified via the `.gitignore`
49+
mechanism. Only useful when searching files in the current directory
50+
with `--no-index`.
51+
3952
-a::
4053
--text::
4154
Process binary files as if they were text.

builtin-grep.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,14 @@ static int grep_object(struct grep_opt *opt, const char **paths,
646646
die("unable to grep from object of type %s", typename(obj->type));
647647
}
648648

649-
static int grep_directory(struct grep_opt *opt, const char **paths)
649+
static int grep_directory(struct grep_opt *opt, const char **paths, int exc_std)
650650
{
651651
struct dir_struct dir;
652652
int i, hit = 0;
653653

654654
memset(&dir, 0, sizeof(dir));
655+
if (exc_std)
656+
setup_standard_excludes(&dir);
655657

656658
fill_directory(&dir, paths);
657659
for (i = 0; i < dir.nr; i++) {
@@ -749,7 +751,7 @@ static int help_callback(const struct option *opt, const char *arg, int unset)
749751
int cmd_grep(int argc, const char **argv, const char *prefix)
750752
{
751753
int hit = 0;
752-
int cached = 0;
754+
int cached = 0, untracked = 0, opt_exclude = -1;
753755
int seen_dashdash = 0;
754756
int external_grep_allowed__ignored;
755757
struct grep_opt opt;
@@ -764,6 +766,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
764766
{ OPTION_BOOLEAN, 0, "index", &use_index, NULL,
765767
"finds in contents not managed by git",
766768
PARSE_OPT_NOARG | PARSE_OPT_NEGHELP },
769+
OPT_BOOLEAN(0, "untracked", &untracked,
770+
"search in both tracked and untracked files"),
771+
OPT_SET_INT(0, "exclude-standard", &opt_exclude,
772+
"search also in ignored files", 1),
767773
OPT_GROUP(""),
768774
OPT_BOOLEAN('v', "invert-match", &opt.invert,
769775
"show non-matching lines"),
@@ -950,18 +956,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
950956
paths[1] = NULL;
951957
}
952958

953-
if (!use_index) {
959+
if (!use_index && (untracked || cached))
960+
die("--cached or --untracked cannot be used with --no-index.");
961+
962+
if (!use_index || untracked) {
954963
int hit;
955-
if (cached)
956-
die("--cached cannot be used with --no-index.");
964+
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
957965
if (list.nr)
958-
die("--no-index cannot be used with revs.");
959-
hit = grep_directory(&opt, paths);
966+
die("--no-index or --untracked cannot be used with revs.");
967+
hit = grep_directory(&opt, paths, use_exclude);
960968
if (use_threads)
961969
hit |= wait_all();
962970
return !hit;
963971
}
964972

973+
if (0 <= opt_exclude)
974+
die("--exclude or --no-exclude cannot be used for tracked contents.");
975+
965976
if (!list.nr) {
966977
int hit;
967978
if (!cached)

t/t7002-grep.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,23 @@ test_expect_success 'outside of git repository' '
455455
test_must_fail git grep o &&
456456
git grep --no-index o >../../actual.sub &&
457457
test_cmp ../../expect.sub ../../actual.sub
458+
) &&
459+
460+
echo ".*o*" >non/git/.gitignore &&
461+
(
462+
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
463+
export GIT_CEILING_DIRECTORIES &&
464+
cd non/git &&
465+
test_must_fail git grep o &&
466+
git grep --no-index --exclude-standard o >../actual.full &&
467+
test_cmp ../expect.full ../actual.full &&
468+
469+
{
470+
echo ".gitignore:.*o*"
471+
cat ../expect.full
472+
} >../expect.with.ignored &&
473+
git grep --no-index --no-exclude o >../actual.full &&
474+
test_cmp ../expect.with.ignored ../actual.full
458475
)
459476
'
460477

@@ -465,9 +482,12 @@ test_expect_success 'inside git repository but with --no-index' '
465482
echo world >is/git/sub/file2 &&
466483
echo ".*o*" >is/git/.gitignore &&
467484
{
468-
echo ".gitignore:.*o*" &&
469485
echo file1:hello &&
470486
echo sub/file2:world
487+
} >is/expect.unignored &&
488+
{
489+
echo ".gitignore:.*o*" &&
490+
cat is/expect.unignored
471491
} >is/expect.full &&
472492
: >is/expect.empty &&
473493
echo file2:world >is/expect.sub
@@ -476,12 +496,24 @@ test_expect_success 'inside git repository but with --no-index' '
476496
git init &&
477497
test_must_fail git grep o >../actual.full &&
478498
test_cmp ../expect.empty ../actual.full &&
499+
500+
git grep --untracked o >../actual.unignored &&
501+
test_cmp ../expect.unignored ../actual.unignored &&
502+
479503
git grep --no-index o >../actual.full &&
480504
test_cmp ../expect.full ../actual.full &&
505+
506+
git grep --no-index --exclude-standard o >../actual.unignored &&
507+
test_cmp ../expect.unignored ../actual.unignored &&
508+
481509
cd sub &&
482510
test_must_fail git grep o >../../actual.sub &&
483511
test_cmp ../../expect.empty ../../actual.sub &&
512+
484513
git grep --no-index o >../../actual.sub &&
514+
test_cmp ../../expect.sub ../../actual.sub &&
515+
516+
git grep --untracked o >../../actual.sub &&
485517
test_cmp ../../expect.sub ../../actual.sub
486518
)
487519
'

0 commit comments

Comments
 (0)