{"thread":{"id":"22138","subject":"[PATCH 1/2] Test update-index for a gitlink to a .git file","startedAt":"2010-01-08T22:36:46Z","lastAt":"2010-01-09T17:20:18Z","messageCount":9,"participants":["Brad King","Junio C Hamano","Avery Pennarun"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"131139","messageId":"1262990208-15554-1-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":null,"subject":"[PATCH 0/2] Support relative .git file in a submodule","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-08T22:36:46Z","receivedAt":"2010-01-08T22:36:46Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"While experimenting with submodules I discovered that if a submodule\nhas a .git file \"symlink\" with a relative path to the real submodule\nrepository then it cannot be added to the superproject:\n\n$ git init\n$ mkdir sub\n$ cd sub\n$ git init\n$ mv .git .real\n$ echo 'gitdir: .real' > .git\n$ echo a > a\n$ git add a\n$ git commit -m a\n$ cd ..\n$ git add sub\nfatal: Not a git repository: .real\n\nThis patch series adds a test demonstrating the problem, and then\nfixes it.\n\nBrad King (2):\n  Test update-index for a gitlink to a .git file\n  Handle relative paths in submodule .git files\n\n setup.c                         |   17 +++++++++++++++++\n t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++\n 2 files changed, 55 insertions(+), 0 deletions(-)\n create mode 100755 t/t2104-update-index-gitfile.sh\n"},{"id":"131138","messageId":"1262990208-15554-2-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":"1262990208-15554-1-git-send-email-brad.king@kitware.com","subject":"[PATCH 1/2] Test update-index for a gitlink to a .git file","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-08T22:36:47Z","receivedAt":"2010-01-08T22:36:47Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"Check that update-index recognizes a submodule that uses a .git file.\nCurrently it works when the .git file specifies an absolute path, but\nnot when it specifies a relative path.\n\nSigned-off-by: Brad King <brad.king@kitware.com>\n---\n t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++\n 1 files changed, 38 insertions(+), 0 deletions(-)\n create mode 100755 t/t2104-update-index-gitfile.sh\n\ndiff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh\nnew file mode 100755\nindex 0000000..ba71984\n--- /dev/null\n+++ b/t/t2104-update-index-gitfile.sh\n@@ -0,0 +1,38 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Brad King\n+#\n+\n+test_description='git update-index for gitlink to .git file.\n+'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'submodule with absolute .git file' '\n+\tmkdir sub1 &&\n+\t(cd sub1 &&\n+\t git init &&\n+\t REAL=\"$(pwd)/.real\" &&\n+\t mv .git \"$REAL\"\n+\t echo \"gitdir: $REAL\" >.git &&\n+\t test_commit first)\n+'\n+\n+test_expect_success 'add gitlink to absolute .git file' '\n+\tgit update-index --add -- sub1\n+'\n+\n+test_expect_success 'submodule with relative .git file' '\n+\tmkdir sub2 &&\n+\t(cd sub2 &&\n+\t git init &&\n+\t mv .git .real &&\n+\t echo \"gitdir: .real\" >.git &&\n+\t test_commit first)\n+'\n+\n+test_expect_failure 'add gitlink to relative .git file' '\n+\tgit update-index --add -- sub2\n+'\n+\n+test_done\n-- \n1.6.5\n"},{"id":"131140","messageId":"1262990208-15554-3-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":"1262990208-15554-1-git-send-email-brad.king@kitware.com","subject":"[PATCH 2/2] Handle relative paths in submodule .git files","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-08T22:36:48Z","receivedAt":"2010-01-08T22:36:48Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"Commit 842abf06f36b5b31050db6406265972e3e1cc189 taught\nresolve_gitlink_ref() to call read_gitfile_gently() to resolve .git\nfiles.  However, read_gitfile_gently() needs to chdir over to the\ndirectory containing the .git file to resolve relative paths correctly.\n\nSigned-off-by: Brad King <brad.king@kitware.com>\n---\n setup.c                         |   17 +++++++++++++++++\n t/t2104-update-index-gitfile.sh |    2 +-\n 2 files changed, 18 insertions(+), 1 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 2cf0f19..a233e01 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -255,6 +255,8 @@ const char *read_gitfile_gently(const char *path)\n \tstruct stat st;\n \tint fd;\n \tsize_t len;\n+\tchar cwd[1024] = \"\";\n+\tconst char *slash;\n \n \tif (stat(path, &st))\n \t\treturn NULL;\n@@ -276,9 +278,24 @@ const char *read_gitfile_gently(const char *path)\n \tif (len < 9)\n \t\tdie(\"No path in gitfile: %s\", path);\n \tbuf[len] = '\\0';\n+\n+\tslash = strrchr(path, '/');\n+\tif (slash) {\n+\t\tchar *dir = xstrndup(path, slash - path);\n+\t\tif (!getcwd(cwd, sizeof(cwd)))\n+\t\t\tdie_errno (\"Could not get current working directory\");\n+\t\tif (chdir(dir))\n+\t\t\tdie_errno (\"Could not switch to '%s'\", dir);\n+\t\tfree(dir);\n+\t}\n+\n \tif (!is_git_directory(buf + 8))\n \t\tdie(\"Not a git repository: %s\", buf + 8);\n \tpath = make_absolute_path(buf + 8);\n+\n+\tif (*cwd && chdir(cwd))\n+\t\tdie_errno (\"Could not change back to '%s'\", cwd);\n+\n \tfree(buf);\n \treturn path;\n }\ndiff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh\nindex ba71984..641607d 100755\n--- a/t/t2104-update-index-gitfile.sh\n+++ b/t/t2104-update-index-gitfile.sh\n@@ -31,7 +31,7 @@ test_expect_success 'submodule with relative .git file' '\n \t test_commit first)\n '\n \n-test_expect_failure 'add gitlink to relative .git file' '\n+test_expect_success 'add gitlink to relative .git file' '\n \tgit update-index --add -- sub2\n '\n \n-- \n1.6.5\n"},{"id":"131142","messageId":"7vocl4urc6.fsf@alter.siamese.dyndns.org","threadId":"22138","inReplyTo":"1262990208-15554-1-git-send-email-brad.king@kitware.com","subject":"Re: [PATCH 0/2] Support relative .git file in a submodule","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-01-08T23:09:13Z","receivedAt":"2010-01-08T23:09:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Brad King <brad.king@kitware.com> writes:\n\n> ... if a submodule\n> has a .git file \"symlink\" with a relative path to the real submodule\n> repository then ...\n\n... then I've always thought that is simply a misconfiguration (t0002\nseems to use full path for this exact reason).  Is there a reason why\nrelative path should be used/usable here, other than \"being able to is\nbetter than not being able to\"???\n\nI don't like my process randomly chdir'ing around assuming they can chdir\nback safely very much, and would prefer not to add such codepaths unless\nabsolutely necessary.\n"},{"id":"131147","messageId":"32541b131001081524g43d54a44i582dd286c1dfe7a5@mail.gmail.com","threadId":"22138","inReplyTo":"7vocl4urc6.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 0/2] Support relative .git file in a submodule","fromName":"Avery Pennarun","fromEmail":"apenwarr@gmail.com","sentAt":"2010-01-08T23:24:13Z","receivedAt":"2010-01-08T23:24:13Z","isPatch":true,"sender":{"key":"apenwarr@gmail.com","avatar":"https://avatars.githubusercontent.com/u/20592?v=4"},"body":"On Fri, Jan 8, 2010 at 6:09 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Brad King <brad.king@kitware.com> writes:\n>\n>> ... if a submodule\n>> has a .git file \"symlink\" with a relative path to the real submodule\n>> repository then ...\n>\n> ... then I've always thought that is simply a misconfiguration (t0002\n> seems to use full path for this exact reason).  Is there a reason why\n> relative path should be used/usable here, other than \"being able to is\n> better than not being able to\"???\n\nIf I have a bunch of git repos in ~/src, and I decide I'd rather\nrename it all to ~/source, it seems like it would be nice for all my\nlinks not to be broken.  This sort of thing can also happen if you\nhave NFS-mounted home directories on a farm of machines, and some of\nthem automount in /u/username and others use /home/username, for\nexample.  I think this is the same reason that common sysadmin advice\nis to use relative symlinks instead of absolute links.\n\nThis problem seems especially true with submodules.  If the\nsubmodule's repo is something like supermodule/.git/submodule.git, a\nrelative path would almost always be a appropriate, no?\n\nHave fun,\n\nAvery\n"},{"id":"131163","messageId":"1263008201-27429-1-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":"32541b131001081524g43d54a44i582dd286c1dfe7a5@mail.gmail.com","subject":"[PATCH v2 0/2] Support relative .git file in a submodule","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-09T03:36:39Z","receivedAt":"2010-01-09T03:36:39Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"Junio C Hamano wrote:\n> then I've always thought that is simply a misconfiguration (t0002\n> seems to use full path for this exact reason).\n\nEverything in that test works with REAL=.real except the line\n\n  test \"$REAL\" = \"$(git rev-parse --git-dir)\"\n\nbecause --git-dir returns an absolute path.\n\n> Is there a reason why relative path should be used/usable here, other\n> than \"being able to is better than not being able to\"???\n\nSimilar configurations already work:\n\n - A .git file with a relative path works inside its work tree\n - A .git symlink with a relative path works inside its work tree\n - A submodule whose .git is a real symlink with a relative path works\n\nMy patch just fixes an intuitive combination of these cases.\n\n> I don't like my process randomly chdir'ing around assuming they can\n> chdir back safely very much, and would prefer not to add such\n> codepaths unless absolutely necessary.\n\nHere is a new patch series.  Patch 1/2 is unchanged.  Patch 2/2 has been\nre-written to avoid chdir.\n\nAvery Pennarun wrote:\n> This problem seems especially true with submodules.  If the\n> submodule's repo is something like supermodule/.git/submodule.git, a\n> relative path would almost always be a appropriate, no?\n\nExactly.  In fact the experiment I was doing involved creating submodule\nrepos inside the main .git and linking to them from the work tree\nsubdirectories.  I'm looking into combining the approach with that of\ngit-new-workdir to keep submodules in the same object database.\n\nBrad King (2):\n  Test update-index for a gitlink to a .git file\n  Handle relative paths in submodule .git files\n\n setup.c                         |   22 +++++++++++++++++++---\n t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++\n 2 files changed, 57 insertions(+), 3 deletions(-)\n create mode 100755 t/t2104-update-index-gitfile.sh\n"},{"id":"131162","messageId":"1263008201-27429-2-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":"32541b131001081524g43d54a44i582dd286c1dfe7a5@mail.gmail.com","subject":"[PATCH v2 1/2] Test update-index for a gitlink to a .git file","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-09T03:36:40Z","receivedAt":"2010-01-09T03:36:40Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"Check that update-index recognizes a submodule that uses a .git file.\nCurrently it works when the .git file specifies an absolute path, but\nnot when it specifies a relative path.\n\nSigned-off-by: Brad King <brad.king@kitware.com>\n---\n t/t2104-update-index-gitfile.sh |   38 ++++++++++++++++++++++++++++++++++++++\n 1 files changed, 38 insertions(+), 0 deletions(-)\n create mode 100755 t/t2104-update-index-gitfile.sh\n\ndiff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh\nnew file mode 100755\nindex 0000000..ba71984\n--- /dev/null\n+++ b/t/t2104-update-index-gitfile.sh\n@@ -0,0 +1,38 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Brad King\n+#\n+\n+test_description='git update-index for gitlink to .git file.\n+'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'submodule with absolute .git file' '\n+\tmkdir sub1 &&\n+\t(cd sub1 &&\n+\t git init &&\n+\t REAL=\"$(pwd)/.real\" &&\n+\t mv .git \"$REAL\"\n+\t echo \"gitdir: $REAL\" >.git &&\n+\t test_commit first)\n+'\n+\n+test_expect_success 'add gitlink to absolute .git file' '\n+\tgit update-index --add -- sub1\n+'\n+\n+test_expect_success 'submodule with relative .git file' '\n+\tmkdir sub2 &&\n+\t(cd sub2 &&\n+\t git init &&\n+\t mv .git .real &&\n+\t echo \"gitdir: .real\" >.git &&\n+\t test_commit first)\n+'\n+\n+test_expect_failure 'add gitlink to relative .git file' '\n+\tgit update-index --add -- sub2\n+'\n+\n+test_done\n-- \n1.6.5\n"},{"id":"131164","messageId":"1263008201-27429-3-git-send-email-brad.king@kitware.com","threadId":"22138","inReplyTo":"32541b131001081524g43d54a44i582dd286c1dfe7a5@mail.gmail.com","subject":"[PATCH v2 2/2] Handle relative paths in submodule .git files","fromName":"Brad King","fromEmail":"brad.king@kitware.com","sentAt":"2010-01-09T03:36:41Z","receivedAt":"2010-01-09T03:36:41Z","isPatch":true,"sender":{"key":"brad.king@kitware.com","avatar":"https://avatars.githubusercontent.com/u/87268?v=4"},"body":"Commit 842abf06f36b5b31050db6406265972e3e1cc189 taught\nresolve_gitlink_ref() to call read_gitfile_gently() to resolve .git\nfiles.  In this commit teach read_gitfile_gently() to interpret a\nrelative path in a .git file with respect to the file location.\n\nThis change allows update-index to recognize a submodule that uses a\nrelative path in its .git file.  It previously failed because the\nrelative path was wrongly interpreted with respect to the superproject\ndirectory.\n\nSigned-off-by: Brad King <brad.king@kitware.com>\n---\n setup.c                         |   22 +++++++++++++++++++---\n t/t2104-update-index-gitfile.sh |    2 +-\n 2 files changed, 20 insertions(+), 4 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 2cf0f19..f10e2dd 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -252,6 +252,8 @@ static int check_repository_format_gently(int *nongit_ok)\n const char *read_gitfile_gently(const char *path)\n {\n \tchar *buf;\n+\tchar* dir;\n+\tconst char *slash;\n \tstruct stat st;\n \tint fd;\n \tsize_t len;\n@@ -276,9 +278,23 @@ const char *read_gitfile_gently(const char *path)\n \tif (len < 9)\n \t\tdie(\"No path in gitfile: %s\", path);\n \tbuf[len] = '\\0';\n-\tif (!is_git_directory(buf + 8))\n-\t\tdie(\"Not a git repository: %s\", buf + 8);\n-\tpath = make_absolute_path(buf + 8);\n+\tdir = buf + 8;\n+\n+\tif (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {\n+\t\tsize_t pathlen = slash+1 - path;\n+\t\tsize_t dirlen = pathlen + len - 8;\n+\t\tdir = xmalloc(dirlen + 1);\n+\t\tstrncpy(dir, path, pathlen);\n+\t\tstrncpy(dir + pathlen, buf + 8, len - 8);\n+\t\tdir[dirlen] = '\\0';\n+\t\tfree(buf);\n+\t\tbuf = dir;\n+\t}\n+\n+\tif (!is_git_directory(dir))\n+\t\tdie(\"Not a git repository: %s\", dir);\n+\tpath = make_absolute_path(dir);\n+\n \tfree(buf);\n \treturn path;\n }\ndiff --git a/t/t2104-update-index-gitfile.sh b/t/t2104-update-index-gitfile.sh\nindex ba71984..641607d 100755\n--- a/t/t2104-update-index-gitfile.sh\n+++ b/t/t2104-update-index-gitfile.sh\n@@ -31,7 +31,7 @@ test_expect_success 'submodule with relative .git file' '\n \t test_commit first)\n '\n \n-test_expect_failure 'add gitlink to relative .git file' '\n+test_expect_success 'add gitlink to relative .git file' '\n \tgit update-index --add -- sub2\n '\n \n-- \n1.6.5\n"},{"id":"131186","messageId":"7vhbqvry99.fsf@alter.siamese.dyndns.org","threadId":"22138","inReplyTo":"1263008201-27429-1-git-send-email-brad.king@kitware.com","subject":"Re: [PATCH v2 0/2] Support relative .git file in a submodule","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-01-09T17:20:18Z","receivedAt":"2010-01-09T17:20:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Brad King <brad.king@kitware.com> writes:\n\n> Exactly.  In fact the experiment I was doing involved creating submodule\n> repos inside the main .git and linking to them from the work tree\n> subdirectories.  I'm looking into combining the approach with that of\n> git-new-workdir to keep submodules in the same object database.\n\nNice.\n\nI recall we discussed design ideas to allow moving the submodule worktree\nand repository more easily using \"gitdir:\" when we first discussed the\npatch series.  I am glad to see finally it is going forward.\n"}]}