{"thread":{"id":"8847","subject":"[PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","startedAt":"2007-07-03T22:49:19Z","lastAt":"2007-07-04T17:31:09Z","messageCount":7,"participants":["Matthias Lederhofer","Johannes Sixt","Johannes Schindelin","Linus Torvalds","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"46420","messageId":"20070703224919.GA22578@moooo.ath.cx","threadId":"8847","inReplyTo":null,"subject":"[PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Matthias Lederhofer","fromEmail":"matled@gmx.net","sentAt":"2007-07-03T22:49:19Z","receivedAt":"2007-07-03T22:49:19Z","isPatch":true,"sender":{"key":"matled@gmx.net","avatar":null},"body":"Now you can do the following to create a repository which\nhas a separate working tree:\n\n    /tmp/foo$ export GIT_DIR=/tmp/bar\n    /tmp/foo$ git --work-tree . init\n    Initialized empty Git repository in /tmp/bar/\n    /tmp/foo$ git config core.worktree\n    /tmp/foo\n\nSigned-off-by: Matthias Lederhofer <matled@gmx.net>\n---\nWithout this I found it quite complicated to create a non bare\nrepository which is not in a .git directory.\n\nSome stuff I was wondering about while writing this patch:\n\n * Should we have a function similar to err(3) which prints\n   \": \" and strerror(errno) after the supplied error message?\n   This function could be used instead of die whenever errno contains\n   some meaningful value.\n\n * Should git init create the whole path to the repository and not\n   only the last component?  I.e. git --git-dir /tmp/a init suceeds\n   but git --git-dir /tmp/a/b/c init fails (unless /tmp/a/b exists).\n\n * Should git init create the working tree if it does not exist?\n\n * Is this something which should have a test?  git init doesn't seem\n   to have any tests yet beside basic tests if the created repository\n   looks ok.\n---\n builtin-init-db.c |   40 +++++++++++++++++++++++++++++++++++++---\n 1 files changed, 37 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin-init-db.c b/builtin-init-db.c\nindex 976f47b..ff5c13b 100644\n--- a/builtin-init-db.c\n+++ b/builtin-init-db.c\n@@ -174,7 +174,36 @@ static void copy_templates(const char *git_dir, int len, const char *template_di\n \tclosedir(dir);\n }\n \n-static int create_default_files(const char *git_dir, const char *template_path)\n+/*\n+ * Get the full path to the working tree specified in $GIT_WORK_TREE\n+ * or NULL if no working tree is specified.\n+ */\n+static const char *get_work_tree(void)\n+{\n+\tconst char *git_work_tree;\n+\tchar cwd[PATH_MAX];\n+\tstatic char worktree[PATH_MAX];\n+\n+\tgit_work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);\n+\tif (!git_work_tree)\n+\t\treturn NULL;\n+\tif (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n+\t\tdie(\"Unable to read current working directory\");\n+\tif (chdir(git_work_tree))\n+\t\tdie(\"Cannot change directory to specified working tree '%s'\",\n+\t\t\tgit_work_tree);\n+\tif (git_work_tree[0] != '/') {\n+\t\tif (!getcwd(worktree, sizeof(worktree)) || worktree[0] != '/')\n+\t\t\tdie(\"Unable to read current working directory\");\n+\t\tgit_work_tree = worktree;\n+\t}\n+\tif (chdir(cwd))\n+\t\tdie(\"Cannot come back to cwd\");\n+\treturn git_work_tree;\n+}\n+\n+static int create_default_files(const char *git_dir, const char *git_work_tree,\n+\tconst char *template_path)\n {\n \tunsigned len = strlen(git_dir);\n \tstatic char path[PATH_MAX];\n@@ -253,7 +282,7 @@ static int create_default_files(const char *git_dir, const char *template_path)\n \t}\n \tgit_config_set(\"core.filemode\", filemode ? \"true\" : \"false\");\n \n-\tif (is_bare_repository()) {\n+\tif (is_bare_repository() && !git_work_tree) {\n \t\tgit_config_set(\"core.bare\", \"true\");\n \t}\n \telse {\n@@ -261,6 +290,8 @@ static int create_default_files(const char *git_dir, const char *template_path)\n \t\t/* allow template config file to override the default */\n \t\tif (log_all_ref_updates == -1)\n \t\t    git_config_set(\"core.logallrefupdates\", \"true\");\n+\t\tif (git_work_tree)\n+\t\t\tgit_config_set(\"core.worktree\", git_work_tree);\n \t}\n \treturn reinit;\n }\n@@ -277,6 +308,7 @@ static const char init_db_usage[] =\n int cmd_init_db(int argc, const char **argv, const char *prefix)\n {\n \tconst char *git_dir;\n+\tconst char *git_work_tree;\n \tconst char *sha1_dir;\n \tconst char *template_dir = NULL;\n \tchar *path;\n@@ -294,6 +326,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\t\tusage(init_db_usage);\n \t}\n \n+\tgit_work_tree = get_work_tree();\n+\n \t/*\n \t * Set up the default .git directory contents\n \t */\n@@ -309,7 +343,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t */\n \tcheck_repository_format();\n \n-\treinit = create_default_files(git_dir, template_dir);\n+\treinit = create_default_files(git_dir, git_work_tree, template_dir);\n \n \t/*\n \t * And set up the object store.\n-- \n1.5.2.2.646.g71e55-dirty\n"},{"id":"46456","messageId":"20070704092915.GA18597@moooo.ath.cx","threadId":"8847","inReplyTo":"20070703224919.GA22578@moooo.ath.cx","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Matthias Lederhofer","fromEmail":"matled@gmx.net","sentAt":"2007-07-04T09:29:15Z","receivedAt":"2007-07-04T09:29:15Z","isPatch":true,"sender":{"key":"matled@gmx.net","avatar":null},"body":"> +\tif (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n> +\t\tdie(\"Unable to read current working directory\");\n\nDscho just pointed out that this causes problems on windows.  The same\nis also in setup_git_directory_gently and was there before I touched\nit, introduced by Linus in d288a700.  What was the original reason to\ndo this?  Are there implementations of getcwd which return a relative\npath?\n\nAdditionally there are other places which need to check if some user\nsupplied path is absolute.  Should we have a macro/function for that\nwhich depends on the os and checks for path[0] = '/' on unix and\nperhaps path[1] = ':' on windows?\n"},{"id":"46460","messageId":"468B793D.53661D60@eudaptics.com","threadId":"8847","inReplyTo":"20070704092915.GA18597@moooo.ath.cx","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Johannes Sixt","fromEmail":"j.sixt@eudaptics.com","sentAt":"2007-07-04T10:41:01Z","receivedAt":"2007-07-04T10:41:01Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Matthias Lederhofer wrote:\n> \n> > +     if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n> > +             die(\"Unable to read current working directory\");\n> \n> Dscho just pointed out that this causes problems on windows.  The same\n> is also in setup_git_directory_gently and was there before I touched\n> it, introduced by Linus in d288a700.  What was the original reason to\n> do this?  Are there implementations of getcwd which return a relative\n> path?\n> \n> Additionally there are other places which need to check if some user\n> supplied path is absolute.  Should we have a macro/function for that\n> which depends on the os and checks for path[0] = '/' on unix and\n> perhaps path[1] = ':' on windows?\n\nI've modified some places (that check for an absolute path) in the MinGW\nport to read (path[0] == '/' || path[1] == ':'). I don't think that it's\nnecessary that you cater for this case in your code - I'll take care of\nit in the MinGW port. Of course, a hint that there is another place to\nwatch out for, or even better a test case in t/, is most appreciated.\n\nI still hope to find some time to rework the MinGW port. Then such\ndetails will be hidden behind a is_path_absolute() function. Or maybe\ngetcwd_or_die() ;)\n\n-- Hannes\n"},{"id":"46471","messageId":"Pine.LNX.4.64.0707041255510.4071@racer.site","threadId":"8847","inReplyTo":"468B793D.53661D60@eudaptics.com","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-07-04T11:57:32Z","receivedAt":"2007-07-04T11:57:32Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Wed, 4 Jul 2007, Johannes Sixt wrote:\n\n> Matthias Lederhofer wrote:\n> > \n> > > +     if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n> > > +             die(\"Unable to read current working directory\");\n> > \n> > Dscho just pointed out that this causes problems on windows.  The same\n> > is also in setup_git_directory_gently and was there before I touched\n> > it, introduced by Linus in d288a700.  What was the original reason to\n> > do this?  Are there implementations of getcwd which return a relative\n> > path?\n> > \n> > Additionally there are other places which need to check if some user\n> > supplied path is absolute.  Should we have a macro/function for that\n> > which depends on the os and checks for path[0] = '/' on unix and\n> > perhaps path[1] = ':' on windows?\n> \n> I've modified some places (that check for an absolute path) in the MinGW\n> port to read (path[0] == '/' || path[1] == ':'). I don't think that it's\n> necessary that you cater for this case in your code - I'll take care of\n> it in the MinGW port. Of course, a hint that there is another place to\n> watch out for, or even better a test case in t/, is most appreciated.\n> \n> I still hope to find some time to rework the MinGW port. Then such\n> details will be hidden behind a is_path_absolute() function. Or maybe\n> getcwd_or_die() ;)\n\nYes, but why not start early? is_path_absolute() makes perfect sense.\n\nHowever, I was asking (out of lack of knowledge): is there any way \ngetcwd() does not return an absolute path?\n\nCiao,\nDscho\n"},{"id":"46493","messageId":"alpine.LFD.0.98.0707040920520.9434@woody.linux-foundation.org","threadId":"8847","inReplyTo":"20070704092915.GA18597@moooo.ath.cx","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2007-07-04T16:23:17Z","receivedAt":"2007-07-04T16:23:17Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Wed, 4 Jul 2007, Matthias Lederhofer wrote:\n\n> > +\tif (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n> > +\t\tdie(\"Unable to read current working directory\");\n> \n> Dscho just pointed out that this causes problems on windows.  The same\n> is also in setup_git_directory_gently and was there before I touched\n> it, introduced by Linus in d288a700.  What was the original reason to\n> do this?  Are there implementations of getcwd which return a relative\n> path?\n\nJust remove the check for cwd[0] being '/'.\n\nIt's just me being too kernel-oriented - inside the kernel, a d_path() \nreturn value pathname can be either a real path, or something like \n\"pipe:[8003]\", and the difference is the '/' at the beginning.\n\nIn user space, and for getcwd(), the check doesn't make sense. So please \njust remove it, and sorry for my idiotic \"I've worked with the kernel for \ntoo damn long\" programming mistakes.\n\n\t\tLinus\n"},{"id":"46499","messageId":"7vsl84gkrz.fsf@assigned-by-dhcp.cox.net","threadId":"8847","inReplyTo":"alpine.LFD.0.98.0707040920520.9434@woody.linux-foundation.org","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-07-04T17:07:28Z","receivedAt":"2007-07-04T17:07:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Linus Torvalds <torvalds@linux-foundation.org> writes:\n\n> On Wed, 4 Jul 2007, Matthias Lederhofer wrote:\n>\n>> > +\tif (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')\n>> > +\t\tdie(\"Unable to read current working directory\");\n>> \n>> Dscho just pointed out that this causes problems on windows.  The same\n>> is also in setup_git_directory_gently and was there before I touched\n>> it, introduced by Linus in d288a700.  What was the original reason to\n>> do this?  Are there implementations of getcwd which return a relative\n>> path?\n>\n> Just remove the check for cwd[0] being '/'.\n\nOk, will do this.\n\n-- >8 --\nFrom: Linus Torvalds <torvalds@linux-foundation.org>\nDate: Wed, 4 Jul 2007 09:23:17 -0700 (PDT)\nSubject: Do not check if getcwd() result begins with a slash\n\nIt's just me being too kernel-oriented - inside the kernel, a d_path() \nreturn value pathname can be either a real path, or something like \n\"pipe:[8003]\", and the difference is the '/' at the beginning.\n\nIn user space, and for getcwd(), the check doesn't make sense. So please \njust remove it, and sorry for my idiotic \"I've worked with the kernel for \ntoo damn long\" programming mistakes.\n\nSigned-off-by: Linus Torvalds <torvalds@linux-foundation.org>\nForged-signature-by: Junio C Hamano <gitster@pobox.com>\n\n---\n\n setup.c |    8 ++++----\n 1 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 01f74d4..bb26f3a 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -211,7 +211,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \tif (!gitdirenv) {\n \t\tint len, offset;\n \n-\t\tif (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')\n+\t\tif (!getcwd(cwd, sizeof(cwd)-1))\n \t\t\tdie(\"Unable to read current working directory\");\n \n \t\toffset = len = strlen(cwd);\n@@ -271,7 +271,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\tdie(\"Not a git repository: '%s'\", gitdirenv);\n \t}\n \n-\tif (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')\n+\tif (!getcwd(cwd, sizeof(cwd)-1))\n \t\tdie(\"Unable to read current working directory\");\n \tif (chdir(gitdirenv)) {\n \t\tif (nongit_ok) {\n@@ -281,7 +281,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\tdie(\"Cannot change directory to $%s '%s'\",\n \t\t\tGIT_DIR_ENVIRONMENT, gitdirenv);\n \t}\n-\tif (!getcwd(gitdir, sizeof(gitdir)-1) || gitdir[0] != '/')\n+\tif (!getcwd(gitdir, sizeof(gitdir)-1))\n \t\tdie(\"Unable to read current working directory\");\n \tif (chdir(cwd))\n \t\tdie(\"Cannot come back to cwd\");\n@@ -340,7 +340,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\tdie(\"Cannot change directory to working tree '%s'\",\n \t\t\t\tgitworktree);\n \t}\n-\tif (!getcwd(worktree, sizeof(worktree)-1) || worktree[0] != '/')\n+\tif (!getcwd(worktree, sizeof(worktree)-1))\n \t\tdie(\"Unable to read current working directory\");\n \tstrcat(worktree, \"/\");\n \tinside_work_tree = !prefixcmp(cwd, worktree);\n"},{"id":"46500","messageId":"alpine.LFD.0.98.0707041027500.9434@woody.linux-foundation.org","threadId":"8847","inReplyTo":"7vsl84gkrz.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] git-init: set core.worktree if GIT_WORK_TREE is specified","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2007-07-04T17:31:09Z","receivedAt":"2007-07-04T17:31:09Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Wed, 4 Jul 2007, Junio C Hamano wrote:\n> \n> Ok, will do this.\n\nAck.\n\n> From: Linus Torvalds <torvalds@linux-foundation.org>\n\nI don't think you need to credit me, or forge my sign-off for stuff like \nthis where I didn't actually send out a patch.\n\nI'd suggest instead just committing it as yours, but maybe with the \nexplanation saying something like \"Linus explains: <explanation>\" instead.\n\nBut hey, you can do it your way too, your choice.\n\n\t\tLinus\n"}]}