Skip to content

Commit 135daf8

Browse files
committed
Merge branch 'sh/grep-tree-obj-tweak-output' into pu
"git grep", when fed a tree-ish as an input, shows each hit prefixed with "<tree-ish>:<path>:<lineno>:". As <tree-ish> is almost always either a commit or a tag that points at a commit, the early part of the output "<tree-ish>:<path>" can be used as the name of the blob and given to "git show". When <tree-ish> is a tree given in the extended SHA-1 syntax (e.g. "<commit>:", or "<commit>:<dir>"), however, this results in a string that does not name a blob (e.g. "<commit>::<path>" or "<commit>:<dir>:<path>"). "git grep" has been taught to be a bit more intelligent about these cases and omit a colon (in the former case) or use slash (in the latter case) to produce "<commit>:<path>" and "<commit>:<dir>/<path>" that can be used as the name of a blob. * sh/grep-tree-obj-tweak-output: grep: use '/' delimiter for paths grep: only add delimiter if there isn't one already
2 parents 5b71ad2 + 9905382 commit 135daf8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

builtin/grep.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,13 @@ 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+
/* rev: or rev:path/ */
818+
char delim = obj->type == OBJ_COMMIT ? ':' : '/';
819+
strbuf_addch(&base, delim);
820+
}
815821
}
816822
init_tree_desc(&tree, data, size);
817823
hit = grep_tree(opt, pathspec, &tree, &base, base.len,

t/t7810-grep.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,4 +1439,30 @@ test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
14391439
test_cmp expected actual
14401440
'
14411441

1442+
cat >expected <<EOF
1443+
HEAD:t/a/v:vvv
1444+
HEAD:t/v:vvv
1445+
EOF
1446+
1447+
test_expect_success 'grep outputs valid <rev>:<path> for HEAD:t/' '
1448+
git grep vvv HEAD:t/ >actual &&
1449+
test_cmp expected actual
1450+
'
1451+
1452+
test_expect_success 'grep outputs valid <rev>:<path> for HEAD:t' '
1453+
git grep vvv HEAD:t >actual &&
1454+
test_cmp expected actual
1455+
'
1456+
1457+
cat >expected <<EOF
1458+
HEAD:t/a/v:vvv
1459+
HEAD:t/v:vvv
1460+
HEAD:v:vvv
1461+
EOF
1462+
1463+
test_expect_success 'grep outputs valid <rev>:<path> for HEAD:' '
1464+
git grep vvv HEAD: >actual &&
1465+
test_cmp expected actual
1466+
'
1467+
14421468
test_done

0 commit comments

Comments
 (0)