summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@redhat.com>2017-01-20 17:11:25 +0000
committerJunio C Hamano <gitster@pobox.com>2017-01-20 14:13:35 -0800
commit257ad080657f995fb3ed03eff98634a190777e53 (patch)
treec0ca534ea847649285ce0404aaee7a0acd83b32f
parent787f75f0567aa8c7347544c65e9d3bc6640a27d4 (diff)
downloadgit-257ad080657f995fb3ed03eff98634a190777e53.tar.gz
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 <stefanha@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin/grep.c6
-rwxr-xr-xt/t7810-grep.sh21
2 files changed, 26 insertions, 1 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index 2c727ef499..a57aebbfb4 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -811,7 +811,11 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
strbuf_init(&base, PATH_MAX + len + 1);
if (len) {
strbuf_add(&base, name, len);
- strbuf_addch(&base, ':');
+
+ /* Add a delimiter if there isn't one already */
+ if (name[len - 1] != '/' && name[len - 1] != ':') {
+ strbuf_addch(&base, ':');
+ }
}
init_tree_desc(&tree, data, size);
hit = grep_tree(opt, pathspec, &tree, &base, base.len,
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index de2405ccba..e804a3f323 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -1435,4 +1435,25 @@ test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
test_cmp expected actual
'
+cat >expected <<EOF
+HEAD:t/a/v:vvv
+HEAD:t/v:vvv
+EOF
+
+test_expect_success 'grep outputs valid <rev>:<path> for HEAD:t/' '
+ git grep vvv HEAD:t/ >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+HEAD:t/a/v:vvv
+HEAD:t/v:vvv
+HEAD:v:vvv
+EOF
+
+test_expect_success 'grep outputs valid <rev>:<path> for HEAD:' '
+ git grep vvv HEAD: >actual &&
+ test_cmp expected actual
+'
+
test_done