[PATCH 0/2] Support relative .git file in a submodule

DORMANTno replies

Revision v1 of 2 in this series.

3 messages, 1 author, 2016-06-15 · open the first message on its own page

[PATCH 0/2] Support relative .git file in a submodule

From: Brad King <hidden>
Date: 2016-06-15 22:47:59

While experimenting with submodules I discovered that if a submodule
has a .git file "symlink" with a relative path to the real submodule
repository then it cannot be added to the superproject:

$ git init
$ mkdir sub
$ cd sub
$ git init
$ mv .git .real
$ echo 'gitdir: .real' > .git
$ echo a > a
$ git add a
$ git commit -m a
$ cd ..
$ git add sub
fatal: Not a git repository: .real

This patch series adds a test demonstrating the problem, and then
fixes it.

Brad King (2):
  Test update-index for a gitlink to a .git file
  Handle relative paths in submodule .git files

 setup.c                         |   17 +++++++++++++++++
 t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100755 t/t2104-update-index-gitfile.sh

[PATCH 1/2] Test update-index for a gitlink to a .git file

From: Brad King <hidden>
Date: 2016-06-15 22:47:59

Check that update-index recognizes a submodule that uses a .git file.
Currently it works when the .git file specifies an absolute path, but
not when it specifies a relative path.

Signed-off-by: Brad King <redacted>
---
 t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
 create mode 100755 t/t2104-update-index-gitfile.sh
diff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh
new file mode 100755
index 0000000..ba71984
--- /dev/null
+++ b/t/t2104-update-index-gitfile.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Brad King
+#
+
+test_description='git update-index for gitlink to .git file.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'submodule with absolute .git file' '
+	mkdir sub1 &&
+	(cd sub1 &&
+	 git init &&
+	 REAL="$(pwd)/.real" &&
+	 mv .git "$REAL"
+	 echo "gitdir: $REAL" >.git &&
+	 test_commit first)
+'
+
+test_expect_success 'add gitlink to absolute .git file' '
+	git update-index --add -- sub1
+'
+
+test_expect_success 'submodule with relative .git file' '
+	mkdir sub2 &&
+	(cd sub2 &&
+	 git init &&
+	 mv .git .real &&
+	 echo "gitdir: .real" >.git &&
+	 test_commit first)
+'
+
+test_expect_failure 'add gitlink to relative .git file' '
+	git update-index --add -- sub2
+'
+
+test_done
-- 
1.6.5

[PATCH 2/2] Handle relative paths in submodule .git files

From: Brad King <hidden>
Date: 2016-06-15 22:47:59

Commit 842abf06f36b5b31050db6406265972e3e1cc189 taught
resolve_gitlink_ref() to call read_gitfile_gently() to resolve .git
files.  However, read_gitfile_gently() needs to chdir over to the
directory containing the .git file to resolve relative paths correctly.

Signed-off-by: Brad King <redacted>
---
 setup.c                         |   17 +++++++++++++++++
 t/t2104-update-index-gitfile.sh |    2 +-
 2 files changed, 18 insertions(+), 1 deletions(-)
diff --git a/setup.c b/setup.c
index 2cf0f19..a233e01 100644
--- a/setup.c
+++ b/setup.c
@@ -255,6 +255,8 @@ const char *read_gitfile_gently(const char *path)
 	struct stat st;
 	int fd;
 	size_t len;
+	char cwd[1024] = "";
+	const char *slash;
 
 	if (stat(path, &st))
 		return NULL;
@@ -276,9 +278,24 @@ const char *read_gitfile_gently(const char *path)
 	if (len < 9)
 		die("No path in gitfile: %s", path);
 	buf[len] = '\0';
+
+	slash = strrchr(path, '/');
+	if (slash) {
+		char *dir = xstrndup(path, slash - path);
+		if (!getcwd(cwd, sizeof(cwd)))
+			die_errno ("Could not get current working directory");
+		if (chdir(dir))
+			die_errno ("Could not switch to '%s'", dir);
+		free(dir);
+	}
+
 	if (!is_git_directory(buf + 8))
 		die("Not a git repository: %s", buf + 8);
 	path = make_absolute_path(buf + 8);
+
+	if (*cwd && chdir(cwd))
+		die_errno ("Could not change back to '%s'", cwd);
+
 	free(buf);
 	return path;
 }
diff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh
index ba71984..641607d 100755
--- a/t/t2104-update-index-gitfile.sh
+++ b/t/t2104-update-index-gitfile.sh
@@ -31,7 +31,7 @@ test_expect_success 'submodule with relative .git file' '
 	 test_commit first)
 '
 
-test_expect_failure 'add gitlink to relative .git file' '
+test_expect_success 'add gitlink to relative .git file' '
 	git update-index --add -- sub2
 '
 
-- 
1.6.5
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help