Thread (21 messages) flat view 21 messages, 4 authors, 2016-06-15

Re: [RFC] introduce GIT_WORK_DIR environment variable

From: Nguyen Thai Ngoc Duy <hidden>
Date: 2016-06-15 22:42:59

On 3/11/07, Matthias Lederhofer [off-list ref] 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help