[PATCH 1/2] clone: allow to clone from .git file

Subsystems: the rest

DORMANTno replies

4 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH 1/2] clone: allow to clone from .git file

From: Nguyễn Thái Ngọc Duy <hidden>
Date: 2016-06-15 22:51:51

Signed-off-by: Nguyễn Thái Ngọc Duy <redacted>
---
 builtin/clone.c  |   23 ++++++++++++++++++++++-
 t/t5601-clone.sh |    4 ++++
 2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 7663bc2..eff3b5e 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -103,10 +103,31 @@ static char *get_repo_path(const char *repo, int *is_bundle)
 	for (i = 0; i < ARRAY_SIZE(suffix); i++) {
 		const char *path;
 		path = mkpath("%s%s", repo, suffix[i]);
-		if (is_directory(path)) {
+		if (stat(path, &st))
+			continue;
+		if (S_ISDIR(st.st_mode)) {
 			*is_bundle = 0;
 			return xstrdup(absolute_path(path));
 		}
+		else if (S_ISREG(st.st_mode) && st.st_size > 8) {
+			/* Despite the name read_gitfile_gently can be
+			   cruel on non .git file, check for signature
+			   ourselves */
+			char signature[8];
+			int len, fd = open(path, O_RDONLY);
+			if (fd < 0)
+				continue;
+			len = read_in_full(fd, signature, 8);
+			close(fd);
+			if (len != 8 || strncmp(signature, "gitdir: ", 8))
+				continue;
+
+			path = read_gitfile_gently(path);
+			if (path) {
+				*is_bundle = 0;
+				return xstrdup(absolute_path(path));
+			}
+		}
 	}
 
 	for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh
index 151ea53..501bd3f 100755
--- a/t/t5601-clone.sh
+++ b/t/t5601-clone.sh
@@ -202,6 +202,10 @@ test_expect_success 'clone separate gitdir: output' '
 	test_cmp expected dst/.git
 '
 
+test_expect_success 'clone from .git file' '
+	git clone dst/.git dst2
+'
+
 test_expect_success 'clone separate gitdir where target already exists' '
 	rm -rf dst &&
 	test_must_fail git clone --separate-git-dir realgitdir src dst
-- 
1.7.4.74.g639db

[PATCH 2/2] Do not accept NUL in the path in .git file

From: Nguyễn Thái Ngọc Duy <hidden>
Date: 2016-06-15 22:51:51

Signed-off-by: Nguyễn Thái Ngọc Duy <redacted>
---
 setup.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/setup.c b/setup.c
index 2c51a9a..c1995b7 100644
--- a/setup.c
+++ b/setup.c
@@ -436,6 +436,8 @@ const char *read_gitfile_gently(const char *path)
 	if (len < 9)
 		die("No path in gitfile: %s", path);
 	buf[len] = '\0';
+	if (strlen(buf) < len)
+		die("invalid gitfile format: %s", path);
 	dir = buf + 8;
 
 	if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
-- 
1.7.4.74.g639db

Re: [PATCH 1/2] clone: allow to clone from .git file

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:51:51

Nguyễn Thái Ngọc Duy  [off-list ref] writes:
+		else if (S_ISREG(st.st_mode) && st.st_size > 8) {
+			/* Despite the name read_gitfile_gently can be
+			   cruel on non .git file, check for signature
+			   ourselves */
Didn't somebody add "is this '.git' thing a valid git metadirectory?" API
quite recently for exactly this?

Re: [PATCH 1/2] clone: allow to clone from .git file

From: Nguyen Thai Ngoc Duy <hidden>
Date: 2016-06-15 22:51:51

2011/8/22 Junio C Hamano [off-list ref]:
Nguyễn Thái Ngọc Duy  [off-list ref] writes:
quoted
+             else if (S_ISREG(st.st_mode) && st.st_size > 8) {
+                     /* Despite the name read_gitfile_gently can be
+                        cruel on non .git file, check for signature
+                        ourselves */
Didn't somebody add "is this '.git' thing a valid git metadirectory?" API
quite recently for exactly this?
You mean resolve_gitdir() in abc0682 (rev-parse: add option
--resolve-git-dir <path> - 2011-08-15)? That function would barf on a
bundle file. I'm not aware of any other functions.
-- 
Duy
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help