Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
git
提交
67f272c2
G
git
项目概览
李少辉-开发者
/
git
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
git
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
67f272c2
编写于
8月 10, 2009
作者:
J
Junio C Hamano
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'mk/grep-max-depth'
* mk/grep-max-depth: grep: Add --max-depth option.
上级
08ac6968
a91f453f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
103 addition
and
11 deletion
+103
-11
Documentation/git-grep.txt
Documentation/git-grep.txt
+5
-0
builtin-grep.c
builtin-grep.c
+46
-10
contrib/completion/git-completion.bash
contrib/completion/git-completion.bash
+1
-0
grep.h
grep.h
+1
-0
t/t7002-grep.sh
t/t7002-grep.sh
+50
-1
未找到文件。
Documentation/git-grep.txt
浏览文件 @
67f272c2
...
...
@@ -17,6 +17,7 @@ SYNOPSIS
[-l | --files-with-matches] [-L | --files-without-match]
[-z | --null]
[-c | --count] [--all-match]
[--max-depth <depth>]
[--color | --no-color]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-f <file>] [-e] <pattern>
...
...
@@ -47,6 +48,10 @@ OPTIONS
-I::
Don't match the pattern in binary files.
--max-depth <depth>::
For each pathspec given on command line, descend at most <depth>
levels of directories. A negative value means no limit.
-w::
--word-regexp::
Match the pattern only at word boundary (either begin at the
...
...
builtin-grep.c
浏览文件 @
67f272c2
...
...
@@ -52,26 +52,58 @@ static int grep_config(const char *var, const char *value, void *cb)
return
git_color_default_config
(
var
,
value
,
cb
);
}
/*
* Return non-zero if max_depth is negative or path has no more then max_depth
* slashes.
*/
static
int
accept_subdir
(
const
char
*
path
,
int
max_depth
)
{
if
(
max_depth
<
0
)
return
1
;
while
((
path
=
strchr
(
path
,
'/'
))
!=
NULL
)
{
max_depth
--
;
if
(
max_depth
<
0
)
return
0
;
path
++
;
}
return
1
;
}
/*
* Return non-zero if name is a subdirectory of match and is not too deep.
*/
static
int
is_subdir
(
const
char
*
name
,
int
namelen
,
const
char
*
match
,
int
matchlen
,
int
max_depth
)
{
if
(
matchlen
>
namelen
||
strncmp
(
name
,
match
,
matchlen
))
return
0
;
if
(
name
[
matchlen
]
==
'\0'
)
/* exact match */
return
1
;
if
(
!
matchlen
||
match
[
matchlen
-
1
]
==
'/'
||
name
[
matchlen
]
==
'/'
)
return
accept_subdir
(
name
+
matchlen
+
1
,
max_depth
);
return
0
;
}
/*
* git grep pathspecs are somewhat different from diff-tree pathspecs;
* pathname wildcards are allowed.
*/
static
int
pathspec_matches
(
const
char
**
paths
,
const
char
*
name
)
static
int
pathspec_matches
(
const
char
**
paths
,
const
char
*
name
,
int
max_depth
)
{
int
namelen
,
i
;
if
(
!
paths
||
!*
paths
)
return
1
;
return
accept_subdir
(
name
,
max_depth
)
;
namelen
=
strlen
(
name
);
for
(
i
=
0
;
paths
[
i
];
i
++
)
{
const
char
*
match
=
paths
[
i
];
int
matchlen
=
strlen
(
match
);
const
char
*
cp
,
*
meta
;
if
(
!
matchlen
||
((
matchlen
<=
namelen
)
&&
!
strncmp
(
name
,
match
,
matchlen
)
&&
(
match
[
matchlen
-
1
]
==
'/'
||
name
[
matchlen
]
==
'\0'
||
name
[
matchlen
]
==
'/'
)))
if
(
is_subdir
(
name
,
namelen
,
match
,
matchlen
,
max_depth
))
return
1
;
if
(
!
fnmatch
(
match
,
name
,
0
))
return
1
;
...
...
@@ -421,7 +453,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
int
kept
;
if
(
!
S_ISREG
(
ce
->
ce_mode
))
continue
;
if
(
!
pathspec_matches
(
paths
,
ce
->
name
))
if
(
!
pathspec_matches
(
paths
,
ce
->
name
,
opt
->
max_depth
))
continue
;
name
=
ce
->
name
;
if
(
name
[
0
]
==
'-'
)
{
...
...
@@ -478,7 +510,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
struct
cache_entry
*
ce
=
active_cache
[
nr
];
if
(
!
S_ISREG
(
ce
->
ce_mode
))
continue
;
if
(
!
pathspec_matches
(
paths
,
ce
->
name
))
if
(
!
pathspec_matches
(
paths
,
ce
->
name
,
opt
->
max_depth
))
continue
;
/*
* If CE_VALID is on, we assume worktree file and its cache entry
...
...
@@ -538,7 +570,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
strbuf_addch
(
&
pathbuf
,
'/'
);
down
=
pathbuf
.
buf
+
tn_len
;
if
(
!
pathspec_matches
(
paths
,
down
))
if
(
!
pathspec_matches
(
paths
,
down
,
opt
->
max_depth
))
;
else
if
(
S_ISREG
(
entry
.
mode
))
hit
|=
grep_sha1
(
opt
,
entry
.
sha1
,
pathbuf
.
buf
,
tn_len
);
...
...
@@ -692,6 +724,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
OPT_SET_INT
(
'I'
,
NULL
,
&
opt
.
binary
,
"don't match patterns in binary files"
,
GREP_BINARY_NOMATCH
),
{
OPTION_INTEGER
,
0
,
"max-depth"
,
&
opt
.
max_depth
,
"depth"
,
"descend at most <depth> levels"
,
PARSE_OPT_NONEG
,
NULL
,
1
},
OPT_GROUP
(
""
),
OPT_BIT
(
'E'
,
"extended-regexp"
,
&
opt
.
regflags
,
"use extended POSIX regular expressions"
,
REG_EXTENDED
),
...
...
@@ -768,6 +803,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
opt
.
pathname
=
1
;
opt
.
pattern_tail
=
&
opt
.
pattern_list
;
opt
.
regflags
=
REG_NEWLINE
;
opt
.
max_depth
=
-
1
;
strcpy
(
opt
.
color_match
,
GIT_COLOR_RED
GIT_COLOR_BOLD
);
opt
.
color
=
-
1
;
...
...
contrib/completion/git-completion.bash
浏览文件 @
67f272c2
...
...
@@ -1047,6 +1047,7 @@ _git_grep ()
--extended-regexp --basic-regexp --fixed-strings
--files-with-matches --name-only
--files-without-match
--max-depth
--count
--and --or --not --all-match
"
...
...
grep.h
浏览文件 @
67f272c2
...
...
@@ -79,6 +79,7 @@ struct grep_opt {
int
pathname
;
int
null_following_name
;
int
color
;
int
max_depth
;
int
funcname
;
char
color_match
[
COLOR_MAXLEN
];
const
char
*
color_external
;
...
...
t/t7002-grep.sh
浏览文件 @
67f272c2
...
...
@@ -25,13 +25,17 @@ test_expect_success setup '
echo foo mmap bar_mmap
echo foo_mmap bar mmap baz
} >file &&
echo vvv >v &&
echo ww w >w &&
echo x x xx x >x &&
echo y yy >y &&
echo zzz > z &&
mkdir t &&
echo test >t/t &&
git add file w x y z t/t hello.c &&
echo vvv >t/v &&
mkdir t/a &&
echo vvv >t/a/v &&
git add . &&
test_tick &&
git commit -m initial
'
...
...
@@ -132,6 +136,51 @@ do
! git grep -c test $H | grep /dev/null
'
test_expect_success
"grep --max-depth -1
$L
"
'
{
echo ${HC}t/a/v:1:vvv
echo ${HC}t/v:1:vvv
echo ${HC}v:1:vvv
} >expected &&
git grep --max-depth -1 -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success
"grep --max-depth 0
$L
"
'
{
echo ${HC}v:1:vvv
} >expected &&
git grep --max-depth 0 -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success
"grep --max-depth 0 -- '*'
$L
"
'
{
echo ${HC}t/a/v:1:vvv
echo ${HC}t/v:1:vvv
echo ${HC}v:1:vvv
} >expected &&
git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
test_cmp expected actual
'
test_expect_success
"grep --max-depth 1
$L
"
'
{
echo ${HC}t/v:1:vvv
echo ${HC}v:1:vvv
} >expected &&
git grep --max-depth 1 -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success
"grep --max-depth 0 -- t
$L
"
'
{
echo ${HC}t/v:1:vvv
} >expected &&
git grep --max-depth 0 -n -e vvv $H -- t >actual &&
test_cmp expected actual
'
done
cat
>
expected
<<
EOF
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录