@matheusbsbs / matheus-bagliano / commits / 40c813e00c
Fork 0

Handle relative paths in submodule .git files

Commit 842abf0 (Teach resolve_gitlink_ref() about the .git file, 2008-02-20) taught resolve_gitlink_ref() to call read_gitfile_gently() to resolve .git files. In this commit teach read_gitfile_gently() to interpret a relative path in a .git file with respect to the file location. This change allows update-index to recognize a submodule that uses a relative path in its .git file. It previously failed because the relative path was wrongly interpreted with respect to the superproject directory. Signed-off-by: Brad King <brad.king@kitware.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brad King committed Jan 8, 2010 at 22:36 UTC 40c813e00c8de5725c7a6bb127fadedcce1f788f
2 files changed +20 -4
setup.c
+19 -3
@@ -252,6 +252,8 @@ static int check_repository_format_gently(int *nongit_ok)
252 const char *read_gitfile_gently(const char *path)
253 {
254 char *buf;
255 + char *dir;
256 + const char *slash;
257 struct stat st;
258 int fd;
259 size_t len;
@@ -276,9 +278,23 @@ const char *read_gitfile_gently(const char *path)
278 if (len < 9)
279 die("No path in gitfile: %s", path);
280 buf[len] = '\0';
279 - if (!is_git_directory(buf + 8))
280 - die("Not a git repository: %s", buf + 8);
281 - path = make_absolute_path(buf + 8);
281 + dir = buf + 8;
282 +
283 + if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
284 + size_t pathlen = slash+1 - path;
285 + size_t dirlen = pathlen + len - 8;
286 + dir = xmalloc(dirlen + 1);
287 + strncpy(dir, path, pathlen);
288 + strncpy(dir + pathlen, buf + 8, len - 8);
289 + dir[dirlen] = '\0';
290 + free(buf);
291 + buf = dir;
292 + }
293 +
294 + if (!is_git_directory(dir))
295 + die("Not a git repository: %s", dir);
296 + path = make_absolute_path(dir);
297 +
298 free(buf);
299 return path;
300 }
t/t2104-update-index-gitfile.sh
+1 -1
@@ -31,7 +31,7 @@ test_expect_success 'submodule with relative .git file' '
31 test_commit first)
32 '
33
34 -test_expect_failure 'add gitlink to relative .git file' '
34 +test_expect_success 'add gitlink to relative .git file' '
35 git update-index --add -- sub2
36 '
37