Code

builtin-commit: Refresh cache after adding files.
[git.git] / setup.c
diff --git a/setup.c b/setup.c
index 4945eb3134c3e047f54e51db25cd0aa81d9c47d7..43cd3f94ea31a7a2a8f8a07cbb67598bb1e52c39 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -107,7 +107,7 @@ void verify_non_filename(const char *prefix, const char *arg)
        if (!lstat(name, &st))
                die("ambiguous argument '%s': both revision and filename\n"
                    "Use '--' to separate filenames from revisions", arg);
-       if (errno != ENOENT)
+       if (errno != ENOENT && errno != ENOTDIR)
                die("'%s': %s", arg, strerror(errno));
 }
 
@@ -189,53 +189,37 @@ int is_inside_work_tree(void)
 }
 
 /*
- * If no worktree was given, and we are outside of a default work tree,
- * now is the time to set it.
- *
- * In other words, if the user calls git with something like
- *
- *     git --git-dir=/some/where/else/.git bla
- *
- * default to /some/where/else as working directory; if the specified
- * git-dir does not end in "/.git", the cwd is used as working directory.
+ * set_work_tree() is only ever called if you set GIT_DIR explicitely.
+ * The old behaviour (which we retain here) is to set the work tree root
+ * to the cwd, unless overridden by the config, the command line, or
+ * GIT_WORK_TREE.
  */
-const char *set_work_tree(const char *dir)
+static const char *set_work_tree(const char *dir)
 {
-       char dir_buffer[PATH_MAX], *rel = NULL;
-       static char buffer[PATH_MAX + 1];
-       int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
-
-       /* strip the variable 'dir' of the postfix "/.git" if it has it */
-       len = strlen(dir);
-       if (len > suffix_len &&
-           !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
-               if ((len - suffix_len) >= sizeof(dir_buffer))
-                       die("directory name too long");
-               memcpy(dir_buffer, dir, len - suffix_len);
-               dir_buffer[len - suffix_len] = '\0';
-
-               /* are we inside the default work tree? */
-               rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
-       }
+       char buffer[PATH_MAX + 1];
 
-       /* if rel is set, the cwd is _not_ the current working tree */
-       if (rel && *rel) {
-               if (!is_absolute_path(dir))
-                       set_git_dir(make_absolute_path(dir));
-               dir = dir_buffer;
-               if (chdir(dir))
-                       die("cannot chdir to %s: %s", dir, strerror(errno));
-               else
-                       strcat(rel, "/");
-               inside_git_dir = 0;
-       } else {
-               rel = NULL;
-               dir = getcwd(buffer, sizeof(buffer));
-       }
-       git_work_tree_cfg = xstrdup(dir);
+       if (!getcwd(buffer, sizeof(buffer)))
+               die ("Could not get the current working directory");
+       git_work_tree_cfg = xstrdup(buffer);
        inside_work_tree = 1;
 
-       return rel;
+       return NULL;
+}
+
+void setup_work_tree(void)
+{
+       const char *work_tree, *git_dir;
+       static int initialized = 0;
+
+       if (initialized)
+               return;
+       work_tree = get_git_work_tree();
+       git_dir = get_git_dir();
+       if (!is_absolute_path(git_dir))
+               set_git_dir(make_absolute_path(git_dir));
+       if (!work_tree || chdir(work_tree))
+               die("This operation must be run in a work tree");
+       initialized = 1;
 }
 
 /*
@@ -259,9 +243,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
                if (PATH_MAX - 40 < strlen(gitdirenv))
                        die("'$%s' too big", GIT_DIR_ENVIRONMENT);
                if (is_git_directory(gitdirenv)) {
+                       static char buffer[1024 + 1];
+                       const char *retval;
+
                        if (!work_tree_env)
                                return set_work_tree(gitdirenv);
-                       return NULL;
+                       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
+                                       get_git_work_tree());
+                       if (!retval || !*retval)
+                               return NULL;
+                       set_git_dir(make_absolute_path(gitdirenv));
+                       if (chdir(work_tree_env) < 0)
+                               die ("Could not chdir to %s", work_tree_env);
+                       strcat(buffer, "/");
+                       return retval;
                }
                if (nongit_ok) {
                        *nongit_ok = 1;