From: Duy Nguyen <pclouds@gmail.com>
To: Stan Hu <stanhu@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] sha1-name.c: Fix handling of revisions that contain paths with brackets
Date: Mon, 24 Dec 2018 09:06:51 +0100 [thread overview]
Message-ID: <20181224080651.GA12708@duynguyen.home> (raw)
In-Reply-To: <20181223234058.5834-1-stanhu@gmail.com>
On Sun, Dec 23, 2018 at 11:40:59PM +0000, Stan Hu wrote:
> Previously, calling ls-tree with a revision such as
> `master^{tree}:foo/{{path}}` would show the root tree instead of the
> correct tree pointed by foo/{{path}}. If a colon is present in the revision
> name, peel_onion() should assume that the presence of a bracket
> at the end of the string belongs to the filename.
>
> Signed-off-by: Stan Hu <stanhu@gmail.com>
> ---
> sha1-name.c | 14 +++++++++++++-
> t/t3104-ls-tree-braces.sh | 30 ++++++++++++++++++++++++++++++
> 2 files changed, 43 insertions(+), 1 deletion(-)
> create mode 100755 t/t3104-ls-tree-braces.sh
>
> diff --git a/sha1-name.c b/sha1-name.c
> index faa60f69e3..588b7a53cc 100644
> --- a/sha1-name.c
> +++ b/sha1-name.c
> @@ -1001,9 +1001,21 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
> * "ref^{commit}". "commit^{tree}" could be used to find the
> * top-level tree of the given commit.
> */
> - if (len < 4 || name[len-1] != '}')
> + if (len < 4)
> return -1;
>
> + /* Check for names in ref:path format in case the path includes
> + * brackets (e.g. ref^{type}:foo/{{bar}}).
> + */
> + for (sp = name; sp < name + len; sp++) {
> + if (*sp == ':')
> + return -1;
> + }
> +
> + if (name[len-1] != '}') {
> + return -1;
> + }
Instead of replacing one loose check (find '}' at the end) with
another one, how about tighten the parsing? peel_onion() is supposed
to consume all "len" characters or none so checking something like
this may be better.
Note that it also shows another corner case we need to be careful
about: master^{/regex} syntax _can_ contain colons in regex. I suppose
doing strchr to find the closing '}' here is better than what I did
below.
--8<--
diff --git a/sha1-name.c b/sha1-name.c
index faa60f69e3..c9e26206ce 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -989,7 +989,7 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
unsigned lookup_flags)
{
struct object_id outer;
- const char *sp;
+ const char *sp, *end;
unsigned int expected_type = 0;
struct object *o;
@@ -1013,21 +1013,26 @@ static int peel_onion(const char *name, int len, struct object_id *oid,
return -1;
sp++; /* beginning of type name, or closing brace for empty */
- if (starts_with(sp, "commit}"))
+ if (skip_prefix(sp, "commit}", &end))
expected_type = OBJ_COMMIT;
- else if (starts_with(sp, "tag}"))
+ else if (skip_prefix(sp, "tag}", &end))
expected_type = OBJ_TAG;
- else if (starts_with(sp, "tree}"))
+ else if (skip_prefix(sp, "tree}", &end))
expected_type = OBJ_TREE;
- else if (starts_with(sp, "blob}"))
+ else if (skip_prefix(sp, "blob}", &end))
expected_type = OBJ_BLOB;
- else if (starts_with(sp, "object}"))
+ else if (skip_prefix(sp, "object}", &end))
expected_type = OBJ_ANY;
- else if (sp[0] == '}')
+ else if (sp[0] == '}') {
expected_type = OBJ_NONE;
- else if (sp[0] == '/')
+ end = sp + 1;
+ } else if (sp[0] == '/') {
expected_type = OBJ_COMMIT;
- else
+ end = name + len;
+ } else
+ return -1;
+
+ if (end != name + len)
return -1;
lookup_flags &= ~GET_OID_DISAMBIGUATORS;
--8<--
--
Duy
next prev parent reply other threads:[~2018-12-24 8:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-23 23:27 [PATCH] sha1-name.c: Fix handling of revisions that contain paths with brackets Stan Hu
2018-12-23 23:37 ` Stan Hu
2018-12-23 23:40 ` [PATCH v2] " Stan Hu
2018-12-24 8:06 ` Duy Nguyen [this message]
2018-12-28 19:59 ` Junio C Hamano
2018-12-29 5:43 ` [PATCH v3] " Stan Hu
2019-01-02 20:35 ` Junio C Hamano
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=20181224080651.GA12708@duynguyen.home \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=stanhu@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).