Skip to content

Commit 257ad08

Browse files
stefanhaRHgitster
authored andcommitted
grep: only add delimiter if there isn't one already
git-grep(1) output does not follow git's own syntax: $ git grep malloc v2.9.3:t/ v2.9.3:t/:test-lib.sh: setup_malloc_check () { $ git show v2.9.3:t/:test-lib.sh fatal: Path 't/:test-lib.sh' does not exist in 'v2.9.3' This patch avoids emitting the unnecessary ':' delimiter if the name already ends with ':' or '/': $ git grep malloc v2.9.3: v2.9.3:t/test-lib.sh: setup_malloc_check () { $ git show v2.9.3:t/test-lib.sh (succeeds) Signed-off-by: Stefan Hajnoczi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 787f75f commit 257ad08

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

builtin/grep.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,11 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
811811
strbuf_init(&base, PATH_MAX + len + 1);
812812
if (len) {
813813
strbuf_add(&base, name, len);
814-
strbuf_addch(&base, ':');
814+
815+
/* Add a delimiter if there isn't one already */
816+
if (name[len - 1] != '/' && name[len - 1] != ':') {
817+
strbuf_addch(&base, ':');
818+
}
815819
}
816820
init_tree_desc(&tree, data, size);
817821
hit = grep_tree(opt, pathspec, &tree, &base, base.len,

t/t7810-grep.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,4 +1435,25 @@ test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
14351435
test_cmp expected actual
14361436
'
14371437

1438+
cat >expected <<EOF
1439+
HEAD:t/a/v:vvv
1440+
HEAD:t/v:vvv
1441+
EOF
1442+
1443+
test_expect_success 'grep outputs valid <rev>:<path> for HEAD:t/' '
1444+
git grep vvv HEAD:t/ >actual &&
1445+
test_cmp expected actual
1446+
'
1447+
1448+
cat >expected <<EOF
1449+
HEAD:t/a/v:vvv
1450+
HEAD:t/v:vvv
1451+
HEAD:v:vvv
1452+
EOF
1453+
1454+
test_expect_success 'grep outputs valid <rev>:<path> for HEAD:' '
1455+
git grep vvv HEAD: >actual &&
1456+
test_cmp expected actual
1457+
'
1458+
14381459
test_done

0 commit comments

Comments
 (0)