{"thread":{"id":"30229","subject":"[PATCH] properly keep track of current working directory","startedAt":"2012-04-12T23:11:36Z","lastAt":"2012-04-12T23:11:36Z","messageCount":1,"participants":["Clemens Buchacher"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"189159","messageId":"20120412231136.GA17585@ecki","threadId":"30229","inReplyTo":null,"subject":"[PATCH] properly keep track of current working directory","fromName":"Clemens Buchacher","fromEmail":"drizzd@aon.at","sentAt":"2012-04-12T23:11:36Z","receivedAt":"2012-04-12T23:11:36Z","isPatch":true,"sender":{"key":"drizzd@gmx.net","avatar":"https://avatars.githubusercontent.com/u/59082?v=4"},"body":"Various failure modes in the repository detection code path currently\nquote the wrong directory in their error message. The working directory\nis changed iteratively to the parent directory until a git repository is\nfound. If the working directory cannot be changed to the parent\ndirectory for some reason, the detection gives up and prints an error\nmessage. THe error message should report the current working directory.\n\nInstead of continually updating the 'cwd' variable, which is actually\nused to remember the original working directory, the 'offset' variable\nis used to keep track of the current working directory. At the point\nwhere the affected error handling code is called, 'offset' already\npoints to the end of the parent of the working directory, rather than\nthe current working directory.\n\nFix this by explicitly using a variable 'offset_parent' and update\n'offset' concurrently with the call to chdir.\n\nIn a similar fashion, the function get_device_or_die() would print the\noriginal working directory in case of a failure, rather than the current\nworking directory. Fix this as well by making use of the 'offset'\nvariable.\n\nLastly, replace the phrase 'mount parent' with 'mount point'. The former\nappears to be a typo.\n\nSigned-off-by: Clemens Buchacher <drizzd@aon.at>\n---\n setup.c |   22 +++++++++++++---------\n 1 file changed, 13 insertions(+), 9 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 7a3618f..731851a 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -569,13 +569,15 @@ static const char *setup_nongit(const char *cwd, int *nongit_ok)\n \treturn NULL;\n }\n \n-static dev_t get_device_or_die(const char *path, const char *prefix)\n+static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)\n {\n \tstruct stat buf;\n-\tif (stat(path, &buf))\n-\t\tdie_errno(\"failed to stat '%s%s%s'\",\n+\tif (stat(path, &buf)) {\n+\t\tdie_errno(\"failed to stat '%*s%s%s'\",\n+\t\t\t\tprefix_len,\n \t\t\t\tprefix ? prefix : \"\",\n \t\t\t\tprefix ? \"/\" : \"\", path);\n+\t}\n \treturn buf.st_dev;\n }\n \n@@ -589,7 +591,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \tstatic char cwd[PATH_MAX+1];\n \tconst char *gitdirenv, *ret;\n \tchar *gitfile;\n-\tint len, offset, ceil_offset;\n+\tint len, offset, offset_parent, ceil_offset;\n \tdev_t current_device = 0;\n \tint one_filesystem = 1;\n \n@@ -631,7 +633,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t */\n \tone_filesystem = !git_env_bool(\"GIT_DISCOVERY_ACROSS_FILESYSTEM\", 0);\n \tif (one_filesystem)\n-\t\tcurrent_device = get_device_or_die(\".\", NULL);\n+\t\tcurrent_device = get_device_or_die(\".\", NULL, 0);\n \tfor (;;) {\n \t\tgitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);\n \t\tif (gitfile)\n@@ -653,11 +655,12 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\tif (is_git_directory(\".\"))\n \t\t\treturn setup_bare_git_dir(cwd, offset, len, nongit_ok);\n \n-\t\twhile (--offset > ceil_offset && cwd[offset] != '/');\n-\t\tif (offset <= ceil_offset)\n+\t\toffset_parent = offset;\n+\t\twhile (--offset_parent > ceil_offset && cwd[offset_parent] != '/');\n+\t\tif (offset_parent <= ceil_offset)\n \t\t\treturn setup_nongit(cwd, nongit_ok);\n \t\tif (one_filesystem) {\n-\t\t\tdev_t parent_device = get_device_or_die(\"..\", cwd);\n+\t\t\tdev_t parent_device = get_device_or_die(\"..\", cwd, offset);\n \t\t\tif (parent_device != current_device) {\n \t\t\t\tif (nongit_ok) {\n \t\t\t\t\tif (chdir(cwd))\n@@ -666,7 +669,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\t\t\treturn NULL;\n \t\t\t\t}\n \t\t\t\tcwd[offset] = '\\0';\n-\t\t\t\tdie(\"Not a git repository (or any parent up to mount parent %s)\\n\"\n+\t\t\t\tdie(\"Not a git repository (or any parent up to mount point %s)\\n\"\n \t\t\t\t\"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\", cwd);\n \t\t\t}\n \t\t}\n@@ -674,6 +677,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tcwd[offset] = '\\0';\n \t\t\tdie_errno(\"Cannot change to '%s/..'\", cwd);\n \t\t}\n+\t\toffset = offset_parent;\n \t}\n }\n \n-- \n1.7.9.6\n"}]}