Merge branches 'lh/submodules' and 'pb/am'
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 2ae57f7c94e304ef3468a27cd245c314a08046ed..a45ea8309a9773160597f142f1208a6129885499 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -95,6 +95,8 @@ void verify_non_filename(const char *prefix, const char *arg)
        const char *name;
        struct stat st;
 
+       if (is_inside_git_dir())
+               return;
        if (*arg == '-')
                return; /* flag */
        name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
@@ -138,7 +140,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
  *    GIT_OBJECT_DIRECTORY environment variable
  *  - a refs/ directory
  *  - either a HEAD symlink or a HEAD file that is formatted as
- *    a proper "ref:".
+ *    a proper "ref:", or a regular file HEAD that has a properly
+ *    formatted sha1 object name.
  */
 static int is_git_directory(const char *suspect)
 {
@@ -161,12 +164,34 @@ static int is_git_directory(const char *suspect)
                return 0;
 
        strcpy(path + len, "/HEAD");
-       if (validate_symref(path))
+       if (validate_headref(path))
                return 0;
 
        return 1;
 }
 
+static int inside_git_dir = -1;
+
+int is_inside_git_dir(void)
+{
+       if (inside_git_dir < 0) {
+               char buffer[1024];
+
+               if (is_bare_repository())
+                       return (inside_git_dir = 1);
+               if (getcwd(buffer, sizeof(buffer))) {
+                       const char *git_dir = get_git_dir(), *cwd = buffer;
+                       while (*git_dir && *git_dir == *cwd) {
+                               git_dir++;
+                               cwd++;
+                       }
+                       inside_git_dir = !*git_dir;
+               } else
+                       inside_git_dir = 0;
+       }
+       return inside_git_dir;
+}
+
 const char *setup_git_directory_gently(int *nongit_ok)
 {
        static char cwd[PATH_MAX+1];
@@ -191,7 +216,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                die("Not a git repository: '%s'", gitdirenv);
        }
 
-       if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
+       if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')
                die("Unable to read current working directory");
 
        offset = len = strlen(cwd);
@@ -205,6 +230,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                                        if (chdir(cwd))
                                                die("Cannot come back to cwd");
                                        setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
+                                       inside_git_dir = 1;
                                        return NULL;
                                }
                                if (nongit_ok) {
@@ -225,6 +251,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
        offset++;
        cwd[len++] = '/';
        cwd[len] = 0;
+       inside_git_dir = !prefixcmp(cwd + offset, ".git/");
        return cwd + offset;
 }