From: Jeff King <peff@peff.net>
To: Phil Hord <hordp@cisco.com>
Cc: git@vger.kernel.org, phil.hord@gmail.com,
Junio C Hamano <gitster@pobox.com>,
Jonathan Nieder <jrnieder@gmail.com>
Subject: [PATCH 2/2] grep: use slash for path delimiter, not colon
Date: Mon, 26 Aug 2013 15:56:16 -0400 [thread overview]
Message-ID: <20130826195616.GB21074@sigill.intra.peff.net> (raw)
In-Reply-To: <20130826195331.GA21051@sigill.intra.peff.net>
From: Phil Hord <hordp@cisco.com>
When a commit is grepped and matching filenames are printed, grep-objects
creates the filename by prefixing the original cmdline argument to the
matched path separated by a colon. Normally this forms a valid blob
reference to the filename, like this:
git grep -l foo HEAD
HEAD:some/path/to/foo.txt
^
But a tree path may be given to grep instead; in this case the colon is
not a valid delimiter to use since it is placed inside a path.
git grep -l foo HEAD:some
HEAD:some:path/to/foo.txt
^
The slash path delimiter should be used instead. Fix git grep to
discern the correct delimiter so it can report valid object names.
git grep -l foo HEAD:some
HEAD:some/path/to/foo.txt
^
Also, prevent the delimiter being added twice, as happens now in these
examples:
git grep -l foo HEAD:
HEAD::some/path/to/foo.txt
^
git grep -l foo HEAD:some/
HEAD:some/:path/to/foo.txt
^
Add a test to confirm correct path forming.
Signed-off-by: Jeff King <peff@peff.net>
---
I left the author as you, since you have done all the hard work; this is
really just me rebasing your patch on top of mine. But note that you did
not signoff the original.
builtin/grep.c | 13 +++++++++----
t/t7810-grep.sh | 15 +++++++++++++++
2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index ee47d49..2df7986 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -26,6 +26,7 @@ struct object_to_grep {
struct object_to_grep {
struct object *item;
const char *name;
+ unsigned has_path:1;
};
static int use_threads = 1;
@@ -462,7 +463,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
}
static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
- struct object *obj, const char *name)
+ struct object *obj, const char *name, char delimiter)
{
if (obj->type == OBJ_BLOB)
return grep_sha1(opt, obj->sha1, name, 0, NULL);
@@ -485,7 +486,8 @@ 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, ':');
+ if (name[len-1] != delimiter)
+ strbuf_addch(&base, delimiter);
}
init_tree_desc(&tree, data, size);
hit = grep_tree(opt, pathspec, &tree, &base, base.len,
@@ -506,7 +508,8 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
for (i = 0; i < nr; i++) {
struct object *real_obj;
real_obj = deref_tag(list[i].item, NULL, 0);
- if (grep_object(opt, pathspec, real_obj, list[i].name)) {
+ if (grep_object(opt, pathspec, real_obj, list[i].name,
+ list[i].has_path ? '/' : ':')) {
hit = 1;
if (opt->status_only)
break;
@@ -822,8 +825,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
unsigned char sha1[20];
+ struct object_context oc;
/* Is it a rev? */
- if (!get_sha1(arg, sha1)) {
+ if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
struct object *object = parse_object_or_die(sha1, arg);
if (!seen_dashdash)
verify_non_filename(prefix, arg);
@@ -831,6 +835,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
ALLOC_GROW(list, list_nr+1, list_alloc);
list[list_nr].item = object;
list[list_nr].name = arg;
+ list[list_nr].has_path = !!oc.path[0];
list_nr++;
continue;
}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index f698001..2494bfc 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -886,6 +886,21 @@ cat >expected <<EOF
'
cat >expected <<EOF
+HEAD:t/a/v:1:vvv
+HEAD:t/v:1:vvv
+EOF
+
+test_expect_success "grep HEAD -- path/" '
+ git grep -n -e vvv HEAD -- t/ >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success "grep HEAD:path" '
+ git grep -n -e vvv HEAD:t/ >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
hello.c:int main(int argc, const char **argv)
hello.c: printf("Hello world.\n");
EOF
--
1.8.4.2.g87d4a77
next prev parent reply other threads:[~2013-08-26 19:56 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-26 14:46 [PATCHv2] grep: use slash for path delimiter, not colon Phil Hord
2013-08-26 19:28 ` Jeff King
2013-08-26 19:53 ` Jeff King
2013-08-26 19:54 ` [PATCH 1/2] grep: stop using object_array Jeff King
2013-08-26 19:56 ` Jeff King [this message]
2013-08-26 20:13 ` [PATCH 2/2] grep: use slash for path delimiter, not colon Johannes Sixt
2013-08-26 20:52 ` Phil Hord
2013-08-26 20:52 ` Jeff King
2013-08-26 21:03 ` Phil Hord
2013-08-26 21:13 ` Jeff King
2013-08-27 3:37 ` [PATCHv2] " Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2013-09-22 19:15 [PATCH 2/2] " Jonathon Mah
2013-09-24 6:57 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130826195616.GB21074@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=hordp@cisco.com \
--cc=jrnieder@gmail.com \
--cc=phil.hord@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).