git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] sha1-name.c: Fix handling of revisions that contain paths with brackets

From
Stan Hu <stanhu@gmail.com>
Date
Dec 23, 2018, 23:37 UTC
Message-ID
<20181223233736.10306-1-stanhu@gmail.com>
In-Reply-To
<20181223232716.6339-1-stanhu@gmail.com>

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 100644 t/t3104-ls-tree-braces.sh
diff --git a/sha1-name.c b/sha1-name.c
index faa60f69e3..69ecf0a26c 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 (sp == name && name[len-1] != '}') {
+		return -1;
+	}
+
 	for (sp = name + len - 1; name <= sp; sp--) {
 		int ch = *sp;
 		if (ch == '{' && name < sp && sp[-1] == '^')
diff --git a/t/t3104-ls-tree-braces.sh b/t/t3104-ls-tree-braces.sh
new file mode 100644
index 0000000000..3ead86c4fe
--- /dev/null
+++ b/t/t3104-ls-tree-braces.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='ls-tree with a folder with braces'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir -p "newdir/{{curly}}" &&
+	touch "newdir/{{curly}}/one" &&
+	git add "newdir/{{curly}}/one" &&
+	git commit -m test
+'
+
+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_done
-- 
2.19.0
Previous: Stan HuNext: Stan Hu
Message 2 of 7 in “sha1-name.c: Fix handling of revisions that contain paths with brackets”
  1. sha1-name.c: Fix handling of revisions that contain paths with bracketsStan Hu, Dec 23, 2018
  2. sha1-name.c: Fix handling of revisions that contain paths with bracketsStan Hu, Dec 23, 2018
  3. sha1-name.c: Fix handling of revisions that contain paths with bracketsStan Hu, Dec 23, 2018
  4. Duy NguyenDec 24, 2018
  5. Junio C HamanoDec 28, 2018
  6. sha1-name.c: Fix handling of revisions that contain paths with bracketsStan Hu, Dec 29, 2018
  7. Junio C HamanoJan 2, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.