{"thread":{"id":"14027","subject":"[PATCH] Make git_dir a path relative to work_tree in setup_work_tree()","startedAt":"2008-06-19T01:55:45Z","lastAt":"2008-06-19T03:03:13Z","messageCount":2,"participants":["Daniel Barkalow","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"80281","messageId":"alpine.LNX.1.00.0806181949160.19665@iabervon.org","threadId":"14027","inReplyTo":null,"subject":"[PATCH] Make git_dir a path relative to work_tree in setup_work_tree()","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-06-19T01:55:45Z","receivedAt":"2008-06-19T01:55:45Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"Once we find the absolute paths for git_dir and work_tree, we can make\ngit_dir a relative path since we know pwd will be work_tree. This should\nsave the kernel some time traversing the path to work_tree all the time\nif git_dir is inside work_tree.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\nI think this should do what Linus wanted without losing the ability to \nhave unrelated work tree and git dir locations. I'm not entirely sure if \nthere's any meaningful difference between this and setting git_dir to an \nabsolute path if it or work_tree is provided, and otherwise just using \n\".git\", but this is a relatively small and obvious change that should also \nhelp if the user, for some reason, specifies the directories explicitly \nsuch that they're in the usual relationship.\n\n cache.h |    1 +\n path.c  |   14 ++++++++++++++\n setup.c |    9 ++++++---\n 3 files changed, 21 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex eab1a17..3465c94 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -524,6 +524,7 @@ static inline int is_absolute_path(const char *path)\n \treturn path[0] == '/';\n }\n const char *make_absolute_path(const char *path);\n+const char *make_relative_path(const char *abs, const char *base);\n \n /* Read and unpack a sha1 file into memory, write memory to a sha1 file */\n extern int sha1_object_info(const unsigned char *, unsigned long *);\ndiff --git a/path.c b/path.c\nindex b7c24a2..8066bbc 100644\n--- a/path.c\n+++ b/path.c\n@@ -294,6 +294,20 @@ int adjust_shared_perm(const char *path)\n /* We allow \"recursive\" symbolic links. Only within reason, though. */\n #define MAXDEPTH 5\n \n+const char *make_relative_path(const char *abs, const char *base)\n+{\n+\tstatic char buf[PATH_MAX + 1];\n+\tint baselen = strlen(base);\n+\tif (prefixcmp(abs, base))\n+\t\treturn abs;\n+\tif (abs[baselen] == '/')\n+\t\tbaselen++;\n+\telse if (base[baselen - 1] != '/')\n+\t\treturn abs;\n+\tstrcpy(buf, abs + baselen);\n+\treturn buf;\n+}\n+\n const char *make_absolute_path(const char *path)\n {\n \tstatic char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];\ndiff --git a/setup.c b/setup.c\nindex d630e37..a4ceae9 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -284,18 +284,21 @@ static const char *set_work_tree(const char *dir)\n \n void setup_work_tree(void)\n {\n-\tconst char *work_tree, *git_dir;\n+\tconst char *git_dir;\n+\tchar *work_tree;\n \tstatic int initialized = 0;\n \n \tif (initialized)\n \t\treturn;\n-\twork_tree = get_git_work_tree();\n+\twork_tree = xstrdup(make_absolute_path(get_git_work_tree()));\n \tgit_dir = get_git_dir();\n \tif (!is_absolute_path(git_dir))\n-\t\tset_git_dir(make_absolute_path(git_dir));\n+\t\tset_git_dir(make_relative_path(make_absolute_path(git_dir),\n+\t\t\t\t\t       work_tree));\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n \tinitialized = 1;\n+\tfree(work_tree);\n }\n \n static int check_repository_format_gently(int *nongit_ok)\n-- \n1.5.4.5\n"},{"id":"80283","messageId":"7vr6auw2ha.fsf@gitster.siamese.dyndns.org","threadId":"14027","inReplyTo":"alpine.LNX.1.00.0806181949160.19665@iabervon.org","subject":"Re: [PATCH] Make git_dir a path relative to work_tree in setup_work_tree()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-06-19T03:03:13Z","receivedAt":"2008-06-19T03:03:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n>  \tif (initialized)\n>  \t\treturn;\n> -\twork_tree = get_git_work_tree();\n> +\twork_tree = xstrdup(make_absolute_path(get_git_work_tree()));\n\nMight be a nice try, but Oops, I suspect.\n\nDoesn't the presense of \"if (!work_tree...\" below tell you that the\nfunction you are calling can legitimately return NULL?\n"}]}