{"thread":{"id":"22567","subject":"[PATCH] Support working directory located at root","startedAt":"2010-02-08T14:53:30Z","lastAt":"2010-02-09T02:49:54Z","messageCount":5,"participants":["Nguyễn Thái Ngọc Duy","Junio C Hamano","Johannes Sixt","Nguyen Thai Ngoc Duy"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"133923","messageId":"1265640810-6361-1-git-send-email-pclouds@gmail.com","threadId":"22567","inReplyTo":null,"subject":"[PATCH] Support working directory located at root","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-02-08T14:53:30Z","receivedAt":"2010-02-08T14:53:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Git should work regardless where the working directory is located,\neven at root. This patch fixes two places where it assumes working\ndirectory always have parent directory.\n\nIn setup_git_directory_gently(), when Git goes up to root and finds\n.git there, it happily sets worktree to \"\".\n\nIn prefix_path(), loosen the outside repo check a little bit. Usually\nwhen a path XXX is inside worktree /foo, it must be either \"/foo\", or\n\"/foo/...\". When worktree is simply \"/\", we can safely ignore the\ncheck: we have a slash at the beginning already.\n\nNot related to worktree, but also set gitdir correctly if a bare repo\nis placed (insanely?) at root.\n\nThanks João Carlos Mendes Luís for pointing out this problem.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Tell me if this patch is rejected, I'll send another one that makes\n setup_git_* die() if worktree/gitdir is to be set empty. I don't think we\n expect gitdir or worktree to be empty anywhere.\n\n setup.c |    6 ++++--\n 1 files changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex b38cbee..0fcd2fd 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -25,7 +25,7 @@ const char *prefix_path(const char *prefix, int len, const char *path)\n \t\tlen = strlen(work_tree);\n \t\ttotal = strlen(sanitized) + 1;\n \t\tif (strncmp(sanitized, work_tree, len) ||\n-\t\t    (sanitized[len] != '\\0' && sanitized[len] != '/')) {\n+\t\t    (len > 1 && sanitized[len] != '\\0' && sanitized[len] != '/')) {\n \t\terror_out:\n \t\t\tdie(\"'%s' is outside repository\", orig);\n \t\t}\n@@ -403,7 +403,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\tif (!work_tree_env)\n \t\t\t\tinside_work_tree = 0;\n \t\t\tif (offset != len) {\n-\t\t\t\tcwd[offset] = '\\0';\n+\t\t\t\tcwd[offset ? offset : 1] = '\\0';\n \t\t\t\tset_git_dir(cwd);\n \t\t\t} else\n \t\t\t\tset_git_dir(\".\");\n@@ -427,6 +427,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \tinside_git_dir = 0;\n \tif (!work_tree_env)\n \t\tinside_work_tree = 1;\n+\tif (offset == 0) /* reached root, set worktree to '/' */\n+\t\toffset = 1;\n \tgit_work_tree_cfg = xstrndup(cwd, offset);\n \tif (check_repository_format_gently(nongit_ok))\n \t\treturn NULL;\n-- \n1.7.0.rc0.54.gd33ef\n"},{"id":"133933","messageId":"7v8wb3pqqw.fsf@alter.siamese.dyndns.org","threadId":"22567","inReplyTo":"1265640810-6361-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] Support working directory located at root","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-02-08T17:43:51Z","receivedAt":"2010-02-08T17:43:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:\n\n> Git should work regardless where the working directory is located,\n> even at root. This patch fixes two places where it assumes working\n> directory always have parent directory.\n>\n> In setup_git_directory_gently(), when Git goes up to root and finds\n> .git there, it happily sets worktree to \"\".\n\nIf you mean \"instead set it to \"/\" and things will work much better.\"\nI agree with the reasoning (not suggesting to reword---just trying to\nmake sure I understood what you meant).\n\n> In prefix_path(), loosen the outside repo check a little bit. Usually\n> when a path XXX is inside worktree /foo, it must be either \"/foo\", or\n> \"/foo/...\". When worktree is simply \"/\", we can safely ignore the\n> check: we have a slash at the beginning already.\n\nThe logic for the \"are we inside?\" check above sounds correct.  When\nwork_tree is at root, have \"/\" in it, and len inside the \"if orig is\nabsolute\" block is 1, so memmove() strips out the leading '/' and makes\nthe result relative to the root level.  Am I reading the code right?\n\n> Not related to worktree, but also set gitdir correctly if a bare repo\n> is placed (insanely?) at root.\n\nYuck, but looks correct.\n"},{"id":"133954","messageId":"201002082229.43024.j6t@kdbg.org","threadId":"22567","inReplyTo":"1265640810-6361-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] Support working directory located at root","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2010-02-08T21:29:41Z","receivedAt":"2010-02-08T21:29:41Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Montag, 8. Februar 2010, Nguyễn Thái Ngọc Duy wrote:\n> @@ -25,7 +25,7 @@ const char *prefix_path(const char *prefix, int len,\n> const char *path) len = strlen(work_tree);\n>  \t\ttotal = strlen(sanitized) + 1;\n>  \t\tif (strncmp(sanitized, work_tree, len) ||\n> -\t\t    (sanitized[len] != '\\0' && sanitized[len] != '/')) {\n> +\t\t    (len > 1 && sanitized[len] != '\\0' && sanitized[len] != '/')) {\n>  \t\terror_out:\n>  \t\t\tdie(\"'%s' is outside repository\", orig);\n>  \t\t}\n> @@ -403,7 +403,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n>  \t\t\tif (!work_tree_env)\n>  \t\t\t\tinside_work_tree = 0;\n>  \t\t\tif (offset != len) {\n> -\t\t\t\tcwd[offset] = '\\0';\n> +\t\t\t\tcwd[offset ? offset : 1] = '\\0';\n>  \t\t\t\tset_git_dir(cwd);\n>  \t\t\t} else\n>  \t\t\t\tset_git_dir(\".\");\n> @@ -427,6 +427,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n>  \tinside_git_dir = 0;\n>  \tif (!work_tree_env)\n>  \t\tinside_work_tree = 1;\n> +\tif (offset == 0) /* reached root, set worktree to '/' */\n> +\t\toffset = 1;\n>  \tgit_work_tree_cfg = xstrndup(cwd, offset);\n>  \tif (check_repository_format_gently(nongit_ok))\n>  \t\treturn NULL;\n\nDoes not work:\n\netc@master:1028> ~/Src/git/git/git add resolv.conf\nfatal: pathspec 'tc/resolv.conf' did not match any files\n\nI wonder how this works on Windows where we do not want to strip the slash \nfrom C:/ either.\n\n-- Hannes\n"},{"id":"133981","messageId":"fcaeb9bf1002081818s53f8646ev80afbaa37f039e50@mail.gmail.com","threadId":"22567","inReplyTo":"7v8wb3pqqw.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] Support working directory located at root","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-02-09T02:18:22Z","receivedAt":"2010-02-09T02:18:22Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2010/2/9 Junio C Hamano <gitster@pobox.com>:\n> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:\n>\n>> Git should work regardless where the working directory is located,\n>> even at root. This patch fixes two places where it assumes working\n>> directory always have parent directory.\n>>\n>> In setup_git_directory_gently(), when Git goes up to root and finds\n>> .git there, it happily sets worktree to \"\".\n>\n> If you mean \"instead set it to \"/\" and things will work much better.\"\n> I agree with the reasoning (not suggesting to reword---just trying to\n> make sure I understood what you meant).\n\nYes.\n\n>\n>> In prefix_path(), loosen the outside repo check a little bit. Usually\n>> when a path XXX is inside worktree /foo, it must be either \"/foo\", or\n>> \"/foo/...\". When worktree is simply \"/\", we can safely ignore the\n>> check: we have a slash at the beginning already.\n>\n> The logic for the \"are we inside?\" check above sounds correct.  When\n> work_tree is at root, have \"/\" in it, and len inside the \"if orig is\n> absolute\" block is 1, so memmove() strips out the leading '/' and makes\n> the result relative to the root level.  Am I reading the code right?\n\nYou are. But I suspect my change in this code is not enough and caused\nthe problem (on msys?) for Hannes. If the worktree somehow is '//' and\nsanitized is '/etc/resolv.conf', then we could end up eating two\nchars, leading to \"'tc/resolv.conf' not match\" error.\n\nHannes, can you put a \"printf(\"%s\\n\", work_tree);\" in prefix_path() to\nsee if it's the case?\n-- \nDuy\n"},{"id":"133982","messageId":"fcaeb9bf1002081849m5aacc55fo52e5119548d8d704@mail.gmail.com","threadId":"22567","inReplyTo":"fcaeb9bf1002081818s53f8646ev80afbaa37f039e50@mail.gmail.com","subject":"Re: [PATCH] Support working directory located at root","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-02-09T02:49:54Z","receivedAt":"2010-02-09T02:49:54Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2010/2/9 Nguyen Thai Ngoc Duy <pclouds@gmail.com>:\n> But I suspect my change in this code is not enough and caused\n> the problem (on msys?) for Hannes. If the worktree somehow is '//' and\n> sanitized is '/etc/resolv.conf', then we could end up eating two\n> chars, leading to \"'tc/resolv.conf' not match\" error.\n>\n> Hannes, can you put a \"printf(\"%s\\n\", work_tree);\" in prefix_path() to\n> see if it's the case?\n\nSilly me. You used relative path, so that code path was untouched.\n-- \nDuy\n"}]}