Skip to contentSkip to conversation

clone: allow to clone from .git file

PATCH In review Nguyễn Thái Ngọc Duy 2 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

---
 builtin/clone.c  |   23 ++++++++++++++++++++++-
 t/t5601-clone.sh |    4 ++++
 2 files changed, 26 insertions(+), 1 deletions(-)
builtin/clone.c +22 -1
index 7663bc2..eff3b5e 100644
@@ -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++) {
t/t5601-clone.sh +4 -0
index 151ea53..501bd3f 100755
@@ -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
Signature · 1 line
1.7.4.74.g639db
#1 lore raw

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

---
 setup.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
setup.c +2 -0
index 2c51a9a..c1995b7 100644
@@ -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, '/'))) {
Signature · 1 line
1.7.4.74.g639db
#2 lore raw
In reply to Nguyễn Thái Ngọc Duy

builtin/clone.c static char *get_repo_path(const char *repo, int *is_bundle) · line 115

Quoted 4 lines · Nguyễn Thái Ngọc Duy
builtin/clone.c +4 -0
+		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?

#3 lore raw

2011/8/22 Junio C Hamano <[email protected]>:

Quoted 10 lines
Quoted 4 lines · Nguyễn Thái Ngọc Duy

+             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.

Signature · 1 line
Duy
#4 lore raw
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 3 lines

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.

Well, then it has to be fixed, perhaps to return NULL instead as other failure cases, as read_gitfile_GENTLY() should be gentle to the callers so that they can notice error cases and deal with them themselves.

#5 lore raw

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

Quoted 5 lines · Junio C Hamano
Quoted 3 lines · Nguyen Thai Ngoc Duy
Quoted 2 lines

Didn't somebody add "is this '.git' thing a valid git metadirectory?" API quite recently for exactly this?

Quoted 7 lines
Quoted 3 lines

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.

Well, then it has to be fixed, perhaps to return NULL instead as other failure cases, as read_gitfile_GENTLY() should be gentle to the callers so that they can notice error cases and deal with them themselves.

The attached patch would be such a fix. While I think it is a necessary thing to do independently from the issue you are trying to solve, I do not think it is appropriate, as it tries to _read_ the whole thing before deciding if it is a valid gitfile. You need a way to cheaply probe if it is a bundle or a gitfile without reading the whole thing for your topic.

-- >8 --
Subject: [PATCH] fix misnamed read_gitfile_gently()

The function was not gentle at all to the callers and died without giving them a chance to deal with possible errors. Rename it to read_gitfile(), update all the callers, and add "gently" variant.

As nobody needs gently variant right now, it may be a good idea to just rename the function without any other change for now, though...

---

 builtin/init-db.c |    2 +-
 cache.h           |    9 +++++++-
 environment.c     |    2 +-
 path.c            |    2 +-
 refs.c            |    2 +-
 setup.c           |   59 ++++++++++++++++++++++++++++++++++++++++------------
 submodule.c       |    6 ++--
 7 files changed, 60 insertions(+), 22 deletions(-)
builtin/init-db.c +1 -1
index 025aa47..d07554c 100644
@@ -347,7 +347,7 @@ static void separate_git_dir(const char *git_dir)
 		const char *src;
 
 		if (S_ISREG(st.st_mode))
-			src = read_gitfile_gently(git_link);
+			src = read_gitfile(git_link);
 		else if (S_ISDIR(st.st_mode))
 			src = git_link;
 		else
cache.h +8 -1
index e11cf6a..9ce04a5 100644
@@ -420,7 +420,14 @@ extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
-extern const char *read_gitfile_gently(const char *path);
+extern const char *read_gitfile(const char *path);
+enum {
+	ERROR_READ_GITFILE_CANTOPEN = 1,
+	ERROR_READ_GITFILE_READFAIL,
+	ERROR_READ_GITFILE_FORMAT,
+	ERROR_READ_GITFILE_BADDIR
+};
+extern const char *read_gitfile_gently(const char *path, int *status);
 extern void set_git_work_tree(const char *tree);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
environment.c +1 -1
index 94d58fd..2228c4e 100644
@@ -91,7 +91,7 @@ static void setup_git_env(void)
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
 	git_dir = git_dir ? xstrdup(git_dir) : NULL;
 	if (!git_dir) {
-		git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
+		git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
 		git_dir = git_dir ? xstrdup(git_dir) : NULL;
 	}
 	if (!git_dir)
path.c +1 -1
index 4d73cc9..6f3f5d5 100644
@@ -139,7 +139,7 @@ char *git_path_submodule(const char *path, const char *fmt, ...)
 		strbuf_addch(&buf, '/');
 	strbuf_addstr(&buf, ".git");
 
-	git_dir = read_gitfile_gently(buf.buf);
+	git_dir = read_gitfile(buf.buf);
 	if (git_dir) {
 		strbuf_reset(&buf);
 		strbuf_addstr(&buf, git_dir);
refs.c +1 -1
index b10419a..c98c006 100644
@@ -451,7 +451,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
 	memcpy(gitdir + len, "/.git", 6);
 	len += 5;
 
-	tmp = read_gitfile_gently(gitdir);
+	tmp = read_gitfile(gitdir);
 	if (tmp) {
 		free(gitdir);
 		len = strlen(tmp);
setup.c +45 -14

102 lines not on this page · raw message

submodule.c +3 -3
index b6dec70..b8b0326 100644
@@ -32,7 +32,7 @@ static int add_submodule_odb(const char *path)
 	const char *git_dir;
 
 	strbuf_addf(&objects_directory, "%s/.git", path);
-	git_dir = read_gitfile_gently(objects_directory.buf);
+	git_dir = read_gitfile(objects_directory.buf);
 	if (git_dir) {
 		strbuf_reset(&objects_directory);
 		strbuf_addstr(&objects_directory, git_dir);
@@ -478,7 +478,7 @@ int fetch_populated_submodules(int num_options, const char **options,
 		strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
 		strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
 		strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
-		git_dir = read_gitfile_gently(submodule_git_dir.buf);
+		git_dir = read_gitfile(submodule_git_dir.buf);
 		if (!git_dir)
 			git_dir = submodule_git_dir.buf;
 		if (is_directory(git_dir)) {
@@ -516,7 +516,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 	const char *git_dir;
 
 	strbuf_addf(&buf, "%s/.git", path);
-	git_dir = read_gitfile_gently(buf.buf);
+	git_dir = read_gitfile(buf.buf);
 	if (!git_dir)
 		git_dir = buf.buf;
 	if (!is_directory(git_dir)) {
#6 lore raw

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

setup.c const char *read_gitfile_gently(const char *path) · line 392

Quoted 8 lines · Junio C Hamano
setup.c +1 -1
-const char *read_gitfile_gently(const char *path)
+const char *read_gitfile_gently(const char *path, int *status)

 {
       char *buf;
       char *dir;

setup.c +0 -0
@@ -389,20 +391,39 @@ const char *read_gitfile_gently(const char *path)
        if (!S_ISREG(st.st_mode))
                return NULL;

Set *status here too? I assume we need valid *status whenever NULL is returned.

Quoted 12 lines
setup.c +6 -2
        fd = open(path, O_RDONLY);
-       if (fd < 0)
-               die_errno("Error opening '%s'", path);
+       if (fd < 0) {
+               if (!status)
+                       die_errno("Error opening '%s'", path);
+               *status = ERROR_READ_GITFILE_CANTOPEN;
+               return NULL;
+       }
        buf = xmalloc(st.st_size + 1);
        len = read_in_full(fd, buf, st.st_size);
        close(fd);
Signature · 1 line
Duy
#7 lore raw

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

Quoted 1 line · Nguyen Thai Ngoc Duy

Set *status here too? I assume we need valid *status whenever NULL is returned.

You are absolutely correct.

But I ended up deciding not to add "gently" variant for now, as nobody needs it, and if we were to have "gently", we should also rethink the earlier checks in the function if they should give diagnosis. Please check what is queued near the tip of tonight's 'pu'.

Thanks.

#8 lore raw

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

Quoted 4 lines · Junio C Hamano

But I ended up deciding not to add "gently" variant for now, as nobody needs it, and if we were to have "gently", we should also rethink the earlier checks in the function if they should give diagnosis. Please check what is queued near the tip of tonight's 'pu'.

Looks OK.

Signature · 1 line
Duy