Skip to content

Commit

Permalink
[RPC] grep: introduce --scope option
Browse files Browse the repository at this point in the history
Because we may want to restrict grep's file scope
to sparse specification, Apply the --scope option
we implemented in diff to grep as well.

`--scope=sparse` mean that the search file scope
restrict to sparse specification when we grep
something in commit history, and `--scope=all`
mean that the search file scope will be full-tree.

Note that `--scope` option only oly takes effect
when "git grep <tree>" is specified.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
  • Loading branch information
adlternative committed Nov 29, 2022
1 parent 115d128 commit 1d1b661
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Documentation/git-grep.txt
Expand Up @@ -28,6 +28,7 @@ SYNOPSIS
[-f <file>] [-e] <pattern>
[--and|--or|--not|(|)|-e <pattern>...]
[--recurse-submodules] [--parent-basename <basename>]
[--scope=(sparse|all)]
[ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
[--] [<pathspec>...]

Expand Down Expand Up @@ -296,6 +297,29 @@ question doesn't support them.
Do not output matched lines; instead, exit with status 0 when
there is a match and with non-zero status when there isn't.

--scope=(sparse|all)::
Restrict or not restrict grep path scope in sparse specification.
The variants are as follows:

+
--
`sparse`;;
When grep in commit history, restrict the scope of file path
to the sparse specification. See sparse specification in
link:technical/sparse-checkout.html [the sparse-checkout design
document] for more information.
`all`;;
When grep in commit history, the file path scope is full-tree.
This is consistent with the current default behavior.
--
+

Note that `--scope` option only take effect if git command specify `<tree>`.

The behavior of this `--scope` option is experimental and may change
in the future. See link:technical/sparse-checkout.html [the sparse-checkout
design document] for more information.

<tree>...::
Instead of searching tracked files in the working tree, search
blobs in the given trees.
Expand Down
10 changes: 10 additions & 0 deletions builtin/grep.c
Expand Up @@ -640,6 +640,15 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
}

strbuf_add(base, entry.path, te_len);
if (opt->scope == SPARSE_SCOPE_SPARSE &&
base->len != tn_len &&
!path_in_sparse_patterns(base->buf + tn_len,
S_ISDIR(entry.mode) ||
S_ISGITLINK(entry.mode))) {
strbuf_setlen(base, old_baselen);
continue;
}


if (S_ISREG(entry.mode)) {
hit |= grep_oid(opt, &entry.oid, base->buf, tn_len,
Expand Down Expand Up @@ -999,6 +1008,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
PARSE_OPT_NOCOMPLETE),
OPT_INTEGER('m', "max-count", &opt.max_count,
N_("maximum number of results per file")),
OPT_SPARSE_SCOPE(&opt.scope),
OPT_END()
};
grep_prefix = prefix;
Expand Down
2 changes: 2 additions & 0 deletions grep.h
Expand Up @@ -22,6 +22,7 @@ typedef int pcre2_general_context;
#endif
#include "thread-utils.h"
#include "userdiff.h"
#include "cache.h"

struct repository;

Expand Down Expand Up @@ -175,6 +176,7 @@ struct grep_opt {

void (*output)(struct grep_opt *opt, const void *data, size_t size);
void *output_priv;
enum sparse_scope scope;
};

#define GREP_OPT_INIT { \
Expand Down
27 changes: 27 additions & 0 deletions t/t1090-sparse-checkout-scope.sh
Expand Up @@ -382,4 +382,31 @@ M in
test_cmp expected actual
'

# git grep TREE

test_expect_success 'git grep --scope=all' '
reset_sparse_checkout_state &&
cat >expected <<-EOF &&
HEAD~:in/1
HEAD~:out1/1
HEAD~:out1/2
HEAD~:out1/3
HEAD~:out1/4
HEAD~:out1/5
HEAD~:out1/6
HEAD~:out2/1
EOF
git -C repo grep --name-only --scope=all 1 HEAD~ >actual &&
test_cmp expected actual
'

test_expect_success 'git grep --scope=sparse' '
reset_sparse_checkout_state &&
cat >expected <<-EOF &&
HEAD~:in/1
EOF
git -C repo grep --name-only --scope=sparse 1 HEAD~ >actual &&
test_cmp expected actual
'

test_done

0 comments on commit 1d1b661

Please sign in to comment.