git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
From: Stan Hu <stanhu@gmail.com>
To: git@vger.kernel.org, stanhu@gmail.com
Subject: [PATCH v3] sha1-name.c: Fix handling of revisions that contain paths with brackets
Date: Fri, 28 Dec 2018 21:43:58 -0800	[thread overview]
Message-ID: <20181229054357.11716-1-stanhu@gmail.com> (raw)
In-Reply-To: <20181224080651.GA12708@duynguyen.home>

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}}. We improve this by ensuring
peel_onion() consumes all "len" characters or none.

Note that it's also possible to have directories named ^{tree} and other
patterns that look like type restrictions. To handle those cases, scan
from the beginning of the name instead of the end.

Signed-off-by: Stan Hu <stanhu@gmail.com>
---
 sha1-name.c               | 35 ++++++++++--------
 t/t3104-ls-tree-braces.sh | 75 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 14 deletions(-)
 create mode 100755 t/t3104-ls-tree-braces.sh

diff --git a/sha1-name.c b/sha1-name.c
index faa60f69e3..ab372a90be 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;
 
@@ -1001,33 +1001,40 @@ 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;
 
-	for (sp = name + len - 1; name <= sp; sp--) {
+	for (sp = name; sp < name + len; sp++) {
 		int ch = *sp;
-		if (ch == '{' && name < sp && sp[-1] == '^')
+		if (ch == '^' && sp < name + len && sp[1] == '{')
 			break;
 	}
-	if (sp <= name)
+
+	if (sp == name + len)
 		return -1;
 
-	sp++; /* beginning of type name, or closing brace for empty */
-	if (starts_with(sp, "commit}"))
+	sp += 2; /* beginning of type name, or closing brace for empty */
+
+	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;
diff --git a/t/t3104-ls-tree-braces.sh b/t/t3104-ls-tree-braces.sh
new file mode 100755
index 0000000000..adeed5bb49
--- /dev/null
+++ b/t/t3104-ls-tree-braces.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+test_description='ls-tree with folders with brackets'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir -p "newdir/{{curly}}" &&
+	mkdir -p "newdir/^{tree}" &&
+	touch "newdir/{{curly}}/one" &&
+	touch "newdir/^{tree}/two" &&
+	git add "newdir/{{curly}}/one" &&
+	git add "newdir/^{tree}/two" &&
+	git commit -m "Test message: search me"
+'
+
+test_expect_success 'ls-tree with curly brace folder' '
+	cat >expect <<-EOF &&
+	100644 blob $EMPTY_BLOB	one
+	EOF
+	git ls-tree -r "HEAD:newdir/{{curly}}" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-tree with type restriction and curly brace folder' '
+	cat >expect <<-EOF &&
+	100644 blob $EMPTY_BLOB	one
+	EOF
+	git ls-tree "HEAD^{tree}:newdir/{{curly}}" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-tree with type restriction and folder named ^{tree}' '
+	cat >expect <<-EOF &&
+	100644 blob $EMPTY_BLOB	two
+	EOF
+	git ls-tree "HEAD^{tree}:newdir/^{tree}" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-tree with unknown type restriction' '
+	(git ls-tree HEAD^{foobar} >actual || true) &&
+	test_must_be_empty actual
+'
+
+test_output () {
+	sed -e "s/ $OID_REGEX	/ X	/" <actual >check
+	test_cmp expect check
+}
+
+test_expect_success 'ls-tree with regex' '
+	cat >expect <<-EOF &&
+	040000 tree X	newdir
+	EOF
+	git ls-tree "HEAD^{/message}" >actual &&
+	test_output
+'
+
+test_expect_success 'ls-tree with regex with a colon' '
+	cat >expect <<-EOF &&
+	040000 tree X	newdir
+	EOF
+	git ls-tree "HEAD^{/message: search}" >actual &&
+	test_output
+'
+
+test_expect_success 'ls-tree with regex and curly brace folder' '
+	cat >expect <<-EOF &&
+	100644 blob $EMPTY_BLOB	one
+	EOF
+	git ls-tree "HEAD^{/message: search}:newdir/{{curly}}" >actual &&
+	test_cmp expect actual
+'
+
+test_done
-- 
2.19.1


  parent reply	other threads:[~2018-12-29  5:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-23 23:27 [PATCH] " 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
2018-12-28 19:59       ` Junio C Hamano
2018-12-29  5:43       ` Stan Hu [this message]
2019-01-02 20:35         ` [PATCH v3] " 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=20181229054357.11716-1-stanhu@gmail.com \
    --to=stanhu@gmail.com \
    --cc=git@vger.kernel.org \
    --subject='Re: [PATCH v3] sha1-name.c: Fix handling of revisions that contain paths with brackets' \
    /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

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git