ident.c: add support for IPv6
[gitweb.git] / path.c
diff --git a/path.c b/path.c
index a7ceea26fbdee5b8f94c5023d1a8de5f1f4a9fa8..58d5620b98c22f85e252f48817e8cf58ffcf4dd3 100644 (file)
--- a/path.c
+++ b/path.c
@@ -4,6 +4,7 @@
 #include "cache.h"
 #include "strbuf.h"
 #include "string-list.h"
+#include "dir.h"
 
 static int get_st_mode_bits(const char *path, int *mode)
 {
@@ -60,29 +61,136 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
        return cleanup_path(buf);
 }
 
-static void vsnpath(struct strbuf *buf, const char *fmt, va_list args)
+static int dir_prefix(const char *buf, const char *dir)
 {
-       const char *git_dir = get_git_dir();
-       strbuf_addstr(buf, git_dir);
+       int len = strlen(dir);
+       return !strncmp(buf, dir, len) &&
+               (is_dir_sep(buf[len]) || buf[len] == '\0');
+}
+
+/* $buf =~ m|$dir/+$file| but without regex */
+static int is_dir_file(const char *buf, const char *dir, const char *file)
+{
+       int len = strlen(dir);
+       if (strncmp(buf, dir, len) || !is_dir_sep(buf[len]))
+               return 0;
+       while (is_dir_sep(buf[len]))
+               len++;
+       return !strcmp(buf + len, file);
+}
+
+static void replace_dir(struct strbuf *buf, int len, const char *newdir)
+{
+       int newlen = strlen(newdir);
+       int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) &&
+               !is_dir_sep(newdir[newlen - 1]);
+       if (need_sep)
+               len--;   /* keep one char, to be replaced with '/'  */
+       strbuf_splice(buf, 0, len, newdir, newlen);
+       if (need_sep)
+               buf->buf[newlen] = '/';
+}
+
+static const char *common_list[] = {
+       "/branches", "/hooks", "/info", "!/logs", "/lost-found",
+       "/objects", "/refs", "/remotes", "/worktrees", "/rr-cache", "/svn",
+       "config", "!gc.pid", "packed-refs", "shallow",
+       NULL
+};
+
+static void update_common_dir(struct strbuf *buf, int git_dir_len)
+{
+       char *base = buf->buf + git_dir_len;
+       const char **p;
+
+       if (is_dir_file(base, "logs", "HEAD") ||
+           is_dir_file(base, "info", "sparse-checkout"))
+               return; /* keep this in $GIT_DIR */
+       for (p = common_list; *p; p++) {
+               const char *path = *p;
+               int is_dir = 0;
+               if (*path == '!')
+                       path++;
+               if (*path == '/') {
+                       path++;
+                       is_dir = 1;
+               }
+               if (is_dir && dir_prefix(base, path)) {
+                       replace_dir(buf, git_dir_len, get_git_common_dir());
+                       return;
+               }
+               if (!is_dir && !strcmp(base, path)) {
+                       replace_dir(buf, git_dir_len, get_git_common_dir());
+                       return;
+               }
+       }
+}
+
+void report_linked_checkout_garbage(void)
+{
+       struct strbuf sb = STRBUF_INIT;
+       const char **p;
+       int len;
+
+       if (!git_common_dir_env)
+               return;
+       strbuf_addf(&sb, "%s/", get_git_dir());
+       len = sb.len;
+       for (p = common_list; *p; p++) {
+               const char *path = *p;
+               if (*path == '!')
+                       continue;
+               strbuf_setlen(&sb, len);
+               strbuf_addstr(&sb, path);
+               if (file_exists(sb.buf))
+                       report_garbage("unused in linked checkout", sb.buf);
+       }
+       strbuf_release(&sb);
+}
+
+static void adjust_git_path(struct strbuf *buf, int git_dir_len)
+{
+       const char *base = buf->buf + git_dir_len;
+       if (git_graft_env && is_dir_file(base, "info", "grafts"))
+               strbuf_splice(buf, 0, buf->len,
+                             get_graft_file(), strlen(get_graft_file()));
+       else if (git_index_env && !strcmp(base, "index"))
+               strbuf_splice(buf, 0, buf->len,
+                             get_index_file(), strlen(get_index_file()));
+       else if (git_db_env && dir_prefix(base, "objects"))
+               replace_dir(buf, git_dir_len + 7, get_object_directory());
+       else if (git_common_dir_env)
+               update_common_dir(buf, git_dir_len);
+}
+
+static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
+{
+       int gitdir_len;
+       strbuf_addstr(buf, get_git_dir());
        if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
                strbuf_addch(buf, '/');
+       gitdir_len = buf->len;
        strbuf_vaddf(buf, fmt, args);
+       adjust_git_path(buf, gitdir_len);
        strbuf_cleanup_path(buf);
 }
 
-char *git_snpath(char *buf, size_t n, const char *fmt, ...)
+void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
 {
-       struct strbuf sb = STRBUF_INIT;
        va_list args;
        va_start(args, fmt);
-       vsnpath(&sb, fmt, args);
+       do_git_path(sb, fmt, args);
        va_end(args);
-       if (sb.len >= n)
-               strlcpy(buf, bad_path, n);
-       else
-               memcpy(buf, sb.buf, sb.len + 1);
-       strbuf_release(&sb);
-       return buf;
+}
+
+const char *git_path(const char *fmt, ...)
+{
+       struct strbuf *pathname = get_pathname();
+       va_list args;
+       va_start(args, fmt);
+       do_git_path(pathname, fmt, args);
+       va_end(args);
+       return pathname->buf;
 }
 
 char *git_pathdup(const char *fmt, ...)
@@ -90,7 +198,7 @@ char *git_pathdup(const char *fmt, ...)
        struct strbuf path = STRBUF_INIT;
        va_list args;
        va_start(args, fmt);
-       vsnpath(&path, fmt, args);
+       do_git_path(&path, fmt, args);
        va_end(args);
        return strbuf_detach(&path, NULL);
 }
@@ -116,49 +224,10 @@ const char *mkpath(const char *fmt, ...)
        return cleanup_path(pathname->buf);
 }
 
-const char *git_path(const char *fmt, ...)
+static void do_submodule_path(struct strbuf *buf, const char *path,
+                             const char *fmt, va_list args)
 {
-       struct strbuf *pathname = get_pathname();
-       va_list args;
-       va_start(args, fmt);
-       vsnpath(pathname, fmt, args);
-       va_end(args);
-       return pathname->buf;
-}
-
-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) {
-               if (!xdg_home)
-                       *xdg = NULL;
-               else
-                       *xdg = mkpathdup("%s/git/%s", xdg_home, file);
-       }
-
-       free(to_free);
-}
-
-const char *git_path_submodule(const char *path, const char *fmt, ...)
-{
-       struct strbuf *buf = get_pathname();
        const char *git_dir;
-       va_list args;
 
        strbuf_addstr(buf, path);
        if (buf->len && buf->buf[buf->len - 1] != '/')
@@ -172,11 +241,27 @@ const char *git_path_submodule(const char *path, const char *fmt, ...)
        }
        strbuf_addch(buf, '/');
 
-       va_start(args, fmt);
        strbuf_vaddf(buf, fmt, args);
-       va_end(args);
        strbuf_cleanup_path(buf);
-       return buf->buf;
+}
+
+char *git_pathdup_submodule(const char *path, const char *fmt, ...)
+{
+       va_list args;
+       struct strbuf buf = STRBUF_INIT;
+       va_start(args, fmt);
+       do_submodule_path(&buf, path, fmt, args);
+       va_end(args);
+       return strbuf_detach(&buf, NULL);
+}
+
+void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
+                              const char *fmt, ...)
+{
+       va_list args;
+       va_start(args, fmt);
+       do_submodule_path(buf, path, fmt, args);
+       va_end(args);
 }
 
 int validate_headref(const char *path)
@@ -285,14 +370,9 @@ char *expand_user_path(const char *path)
  * (3) "relative/path" to mean cwd relative directory; or
  * (4) "/absolute/path" to mean absolute directory.
  *
- * Unless "strict" is given, we try access() for existence of "%s.git/.git",
- * "%s/.git", "%s.git", "%s" in this order.  The first one that exists is
- * what we try.
- *
- * Second, we try chdir() to that.  Upon failure, we return NULL.
- *
- * Then, we try if the current directory is a valid git repository.
- * Upon failure, we return NULL.
+ * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
+ * in this order. We select the first one that is a valid git repository, and
+ * chdir() to it. If none match, or we fail to chdir, we return NULL.
  *
  * If all goes well, we return the directory we used to chdir() (but
  * before ~user is expanded), avoiding getcwd() resolving symbolic
@@ -596,6 +676,11 @@ const char *remove_leading_path(const char *in, const char *prefix)
  * normalized, any time "../" eats up to the prefix_len part,
  * prefix_len is reduced. In the end prefix_len is the remaining
  * prefix that has not been overridden by user pathspec.
+ *
+ * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'.
+ * For everything but the root folder itself, the normalized path should not
+ * end with a '/', then the callers need to be fixed up accordingly.
+ *
  */
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
 {
@@ -805,3 +890,61 @@ int daemon_avoid_alias(const char *p)
                }
        }
 }
+
+static int only_spaces_and_periods(const char *path, size_t len, size_t skip)
+{
+       if (len < skip)
+               return 0;
+       len -= skip;
+       path += skip;
+       while (len-- > 0) {
+               char c = *(path++);
+               if (c != ' ' && c != '.')
+                       return 0;
+       }
+       return 1;
+}
+
+int is_ntfs_dotgit(const char *name)
+{
+       int len;
+
+       for (len = 0; ; len++)
+               if (!name[len] || name[len] == '\\' || is_dir_sep(name[len])) {
+                       if (only_spaces_and_periods(name, len, 4) &&
+                                       !strncasecmp(name, ".git", 4))
+                               return 1;
+                       if (only_spaces_and_periods(name, len, 5) &&
+                                       !strncasecmp(name, "git~1", 5))
+                               return 1;
+                       if (name[len] != '\\')
+                               return 0;
+                       name += len + 1;
+                       len = -1;
+               }
+}
+
+char *xdg_config_home(const char *filename)
+{
+       const char *home, *config_home;
+
+       assert(filename);
+       config_home = getenv("XDG_CONFIG_HOME");
+       if (config_home && *config_home)
+               return mkpathdup("%s/git/%s", config_home, filename);
+
+       home = getenv("HOME");
+       if (home)
+               return mkpathdup("%s/.config/git/%s", home, filename);
+       return NULL;
+}
+
+GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
+GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
+GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")
+GIT_PATH_FUNC(git_path_merge_msg, "MERGE_MSG")
+GIT_PATH_FUNC(git_path_merge_rr, "MERGE_RR")
+GIT_PATH_FUNC(git_path_merge_mode, "MERGE_MODE")
+GIT_PATH_FUNC(git_path_merge_head, "MERGE_HEAD")
+GIT_PATH_FUNC(git_path_fetch_head, "FETCH_HEAD")
+GIT_PATH_FUNC(git_path_shallow, "shallow")