Merge branch 'mm/push-default-switch-warning'
[gitweb.git] / path.c
diff --git a/path.c b/path.c
index b6f71d1086981dc41bdbbc8954eccd9e9b719f98..66acd24fa8821861d032fc216f6ea6fdd711a969 100644 (file)
--- a/path.c
+++ b/path.c
@@ -87,6 +87,21 @@ char *git_pathdup(const char *fmt, ...)
        return xstrdup(path);
 }
 
+char *mkpathdup(const char *fmt, ...)
+{
+       char *path;
+       struct strbuf sb = STRBUF_INIT;
+       va_list args;
+
+       va_start(args, fmt);
+       strbuf_vaddf(&sb, fmt, args);
+       va_end(args);
+       path = xstrdup(cleanup_path(sb.buf));
+
+       strbuf_release(&sb);
+       return path;
+}
+
 char *mkpath(const char *fmt, ...)
 {
        va_list args;
@@ -122,6 +137,32 @@ char *git_path(const char *fmt, ...)
        return cleanup_path(pathname);
 }
 
+void home_config_paths(char **global, char **xdg, char *file)
+{
+       char *xdg_home = getenv("XDG_CONFIG_HOME");
+       char *home = getenv("HOME");
+       char *to_free = NULL;
+
+       if (!home) {
+               if (global)
+                       *global = NULL;
+       } else {
+               if (!xdg_home) {
+                       to_free = mkpathdup("%s/.config", home);
+                       xdg_home = to_free;
+               }
+               if (global)
+                       *global = mkpathdup("%s/.gitconfig", home);
+       }
+
+       if (!xdg_home)
+               *xdg = NULL;
+       else
+               *xdg = mkpathdup("%s/git/%s", xdg_home, file);
+
+       free(to_free);
+}
+
 char *git_path_submodule(const char *path, const char *fmt, ...)
 {
        char *pathname = get_pathname();
@@ -293,7 +334,7 @@ const char *enter_repo(const char *path, int strict)
 
        if (!strict) {
                static const char *suffix[] = {
-                       ".git/.git", "/.git", ".git", "", NULL,
+                       "/.git", "", ".git/.git", ".git", NULL,
                };
                const char *gitfile;
                int len = strlen(path);
@@ -324,8 +365,11 @@ const char *enter_repo(const char *path, int strict)
                        return NULL;
                len = strlen(used_path);
                for (i = 0; suffix[i]; i++) {
+                       struct stat st;
                        strcpy(used_path + len, suffix[i]);
-                       if (!access(used_path, F_OK)) {
+                       if (!stat(used_path, &st) &&
+                           (S_ISREG(st.st_mode) ||
+                           (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
                                strcat(validated_path, suffix[i]);
                                break;
                        }