{"thread":{"id":"10330","subject":"[PATCH] Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE","startedAt":"2007-10-16T23:37:36Z","lastAt":"2007-10-16T23:37:36Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"56147","messageId":"Pine.LNX.4.64.0710170037030.25221@racer.site","threadId":"10330","inReplyTo":null,"subject":"[PATCH] Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-10-16T23:37:36Z","receivedAt":"2007-10-16T23:37:36Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nThere are a few programs, such as config and diff, which allow running\nwithout a git repository.  Therefore, they have to call\nsetup_git_directory_gently().\n\nHowever, when GIT_DIR and GIT_WORK_TREE were set, and the current\ndirectory was a subdirectory of the work tree,\nsetup_git_directory_gently() would return a bogus NULL prefix.\n\nThis patch fixes that.\n\nNoticed by REPLeffect on IRC.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n setup.c             |   13 ++++++++++++-\n t/t1501-worktree.sh |    9 +++++++++\n 2 files changed, 21 insertions(+), 1 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 06004f1..145eca5 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\tif (PATH_MAX - 40 < strlen(gitdirenv))\n \t\t\tdie(\"'$%s' too big\", GIT_DIR_ENVIRONMENT);\n \t\tif (is_git_directory(gitdirenv)) {\n+\t\t\tstatic char buffer[1024 + 1];\n+\t\t\tconst char *retval;\n+\n \t\t\tif (!work_tree_env)\n \t\t\t\treturn set_work_tree(gitdirenv);\n-\t\t\treturn NULL;\n+\t\t\tretval = get_relative_cwd(buffer, sizeof(buffer) - 1,\n+\t\t\t\t\tget_git_work_tree());\n+\t\t\tif (!retval || !*retval)\n+\t\t\t\treturn NULL;\n+\t\t\tset_git_dir(make_absolute_path(gitdirenv));\n+\t\t\tif (chdir(work_tree_env) < 0)\n+\t\t\t\tdie (\"Could not chdir to %s\", work_tree_env);\n+\t\t\tstrcat(buffer, \"/\");\n+\t\t\treturn retval;\n \t\t}\n \t\tif (nongit_ok) {\n \t\t\t*nongit_ok = 1;\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 7322161..7ee3820 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -103,4 +103,13 @@ test_expect_success 'repo finds its work tree from work tree, too' '\n \t test sub/dir/tracked = \"$(git ls-files)\")\n '\n \n+test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '\n+\tcd repo.git/work/sub/dir &&\n+\tGIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \\\n+\t\tgit diff --exit-code tracked &&\n+\techo changed > tracked &&\n+\t! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \\\n+\t\tgit diff --exit-code tracked\n+'\n+\n test_done\n-- \n1.5.3.4.1223.ga973c\n"}]}