From: Nguyen Thai Ngoc Duy Date: Sun, 11 Mar 2007 12:42:02 GMT Subject: Re: [RFC] introduce GIT_WORK_DIR environment variable Message-ID: In-Reply-To: <20070311043250.GA21331@moooo.ath.cx> On 3/11/07, Matthias Lederhofer wrote: > + gitwd = getenv(GIT_WORKING_DIR_ENVIRONMENT); > + if (!gitwd || stat(gitwd, &st_work)) > return NULL; I propose the following instead of the last two lines: if (!gitwd) return NULL; if (stat(gitwd, &st_work)) die("Unable to stat git working directory %s",gitwd); > + if (inside_git_dir == -1 && stat(gitdirenv, &st_git)) > + die("Unable to stat git directory"); > + if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/') > + die("Unable to read current working directory"); > + len = strlen(cwd); > + > + prefix = cwd+len; > + for (;;) { > + c = *prefix; > + *prefix = '\0'; > + if (stat(cwd, &st)) > + die("Unable to stat '%s'", cwd); > + if (st_work.st_dev == st.st_dev && > + st_work.st_ino == st.st_ino) > + break; > + if (inside_git_dir == -1 && > + st_git.st_dev == st.st_dev && > + st_git.st_ino == st.st_ino) > + inside_git_dir = 1; > + *prefix = c; > + > + if (prefix == cwd+1) { > + has_working_directory = 0; > + return NULL; My case seems a bit complicated than usual. The working directory (/home/pclouds/blog/data) was not a prefix of cwd (/home/pclouds/blog) so the code failed silently at this line. If I replace "has_working_directory = 0; return NULL;" with "strcpy(cwd,gitwd);c = 0;break;", it may work but see below > + } > + while (*(--prefix) != '/') > + ; /* do nothing */ > + if (prefix == cwd) > + prefix++; > + } > + > + if (chdir(cwd)) > + die("Cannot change directory to '%s'", cwd); > + If cwd changed and GIT_DIR is a relative path, git can no longer access GIT_DIR properly. -- Duy