git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Re* [PATCH 2/2] grep --no-index: don't use git standard exclusions

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
Sep 28, 2011, 19:19 UTC
Message-ID
<CAKPyHN3=mbtEkyFUBdZAJCEVXfJhwPVhVFWkNNfX-yhtw9498w@mail.gmail.com>
In-Reply-To
<7vty7xwrsf.fsf_-_@alter.siamese.dyndns.org>
On Wed, Sep 28, 2011 at 00:21, Junio C Hamano <gitster@pobox.com> wrote:
Show 10 quoted lines
> Bert Wesarg <bert.wesarg@googlemail.com> writes:
>
>> Would '--untracked-too' only be a synonym for '--no-index
>> --exclude-standard', i.e. the current behavior?
>
> That basically would be the idea. Perhaps something like this on top of
> a9e6436 (grep --no-index: don't use git standard exclusions, 2011-09-15).
>
> -- >8 --
> Subject: [PATCH 1/2] grep: teach --untracked and --exclude options

I would still vote to name the option --exclude-standard, like it is done in 'git ls-files'. Which also has a --exclude=<pattern> option. And I think a --exclude=<pattern> option would be useful for 'git grep', too.

Else:
Acked-by: Bert Wesarg <bert.wesarg@googlemail.com>
Thanks.
Bert
Show 125 quoted lines
>
> 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" 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" can be used
> to tell the command to include hits from files that are ignored.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  Documentation/git-grep.txt |   15 ++++++++++++++-
>  builtin-grep.c             |   25 ++++++++++++++++++-------
>  2 files changed, 32 insertions(+), 8 deletions(-)
>
> diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
> index e019e76..2ccfb90 100644
> --- a/Documentation/git-grep.txt
> +++ b/Documentation/git-grep.txt
> @@ -9,7 +9,7 @@ git-grep - Print lines matching a pattern
>  SYNOPSIS
>  --------
>  [verse]
> -'git grep' [--cached]
> +'git grep' [--cached] [--untracked] [--excludes]
>           [-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
>           [-v | --invert-match] [-h|-H] [--full-name]
>           [-E | --extended-regexp] [-G | --basic-regexp]
> @@ -36,6 +36,19 @@ OPTIONS
>        Instead of searching in the working tree files, check
>        the blobs registered in the index file.
>
> +--untracked::
> +       In addition to searching in the tracked files in the working
> +       tree, search also in untracked files.
> +
> +--no-excludes::
> +       Also search in ignored files by not honoring the `.gitignore`
> +       mechanism. Only useful with `--untracked`.
> +
> +--excludes::
> +       Do not pay attention to ignored files specified via the `.gitignore`
> +       mechanism.  Only useful when searching files in the current directory
> +       with `--no-index`.
> +
>  -a::
>  --text::
>        Process binary files as if they were text.
> diff --git a/builtin-grep.c b/builtin-grep.c
> index a10946d..c6cfdf8 100644
> --- a/builtin-grep.c
> +++ b/builtin-grep.c
> @@ -646,12 +646,14 @@ static int grep_object(struct grep_opt *opt, const char **paths,
>        die("unable to grep from object of type %s", typename(obj->type));
>  }
>
> -static int grep_directory(struct grep_opt *opt, const char **paths)
> +static int grep_directory(struct grep_opt *opt, const char **paths, int exc_std)
>  {
>        struct dir_struct dir;
>        int i, hit = 0;
>
>        memset(&dir, 0, sizeof(dir));
> +       if (exc_std)
> +               setup_standard_excludes(&dir);
>
>        fill_directory(&dir, paths);
>        for (i = 0; i < dir.nr; i++) {
> @@ -749,7 +751,7 @@ static int help_callback(const struct option *opt, const char *arg, int unset)
>  int cmd_grep(int argc, const char **argv, const char *prefix)
>  {
>        int hit = 0;
> -       int cached = 0;
> +       int cached = 0, untracked = 0, opt_exclude = -1;
>        int seen_dashdash = 0;
>        int external_grep_allowed__ignored;
>        struct grep_opt opt;
> @@ -764,6 +766,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>                { OPTION_BOOLEAN, 0, "index", &use_index, NULL,
>                        "finds in contents not managed by git",
>                        PARSE_OPT_NOARG | PARSE_OPT_NEGHELP },
> +               OPT_BOOLEAN(0, "untracked", &untracked,
> +                       "search in both tracked and untracked files"),
> +               OPT_SET_INT(0, "exclude", &opt_exclude,
> +                           "search also in ignored files", 1),
>                OPT_GROUP(""),
>                OPT_BOOLEAN('v', "invert-match", &opt.invert,
>                        "show non-matching lines"),
> @@ -950,18 +956,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>                paths[1] = NULL;
>        }
>
> -       if (!use_index) {
> +       if (!use_index && (untracked || cached))
> +               die("--cached or --untracked cannot be used with --no-index.");
> +
> +       if (!use_index || untracked) {
>                int hit;
> -               if (cached)
> -                       die("--cached cannot be used with --no-index.");
> +               int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
>                if (list.nr)
> -                       die("--no-index cannot be used with revs.");
> -               hit = grep_directory(&opt, paths);
> +                       die("--no-index or --untracked cannot be used with revs.");
> +               hit = grep_directory(&opt, paths, use_exclude);
>                if (use_threads)
>                        hit |= wait_all();
>                return !hit;
>        }
>
> +       if (0 <= opt_exclude)
> +               die("--exclude or --no-exclude cannot be used for tracked contents.");
> +
>        if (!list.nr) {
>                int hit;
>                if (!cached)
> --
> 1.7.7.rc3.4.g8d714
>
>
>
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 8 in “grep: do not use --index in the short usage output”
  1. 1/2 grep: do not use --index in the short usage outputBert Wesarg, Sep 15, 2011
  2. 2/2 grep --no-index: don't use git standard exclusionsBert Wesarg, Sep 15, 2011
  3. Junio C HamanoSep 15, 2011
  4. Bert WesargSep 16, 2011
  5. Re* [PATCH 2/2] grep --no-index: don't use git standard exclusionsJunio C Hamano, Sep 27, 2011
  6. 2/2 grep: --untracked and --exclude testsJunio C Hamano, Sep 27, 2011
  7. Bert WesargSep 28, 2011
  8. Junio C HamanoSep 28, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.