]> git.ipfire.org Git - thirdparty/git.git/blobdiff - path.c
The 20th batch
[thirdparty/git.git] / path.c
diff --git a/path.c b/path.c
index 8bb223c92c91c2d963ab4fefa9efb0ac7c3b026c..93491bab141c5633691f5172e450052800da63ed 100644 (file)
--- a/path.c
+++ b/path.c
@@ -1,11 +1,13 @@
 /*
  * Utilities for paths and pathnames
  */
 /*
  * Utilities for paths and pathnames
  */
+
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "environment.h"
 #include "gettext.h"
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "environment.h"
 #include "gettext.h"
-#include "hex.h"
 #include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
@@ -28,9 +30,7 @@ static int get_st_mode_bits(const char *path, int *mode)
        return 0;
 }
 
        return 0;
 }
 
-static char bad_path[] = "/bad-path/";
-
-static struct strbuf *get_pathname(void)
+struct strbuf *get_pathname(void)
 {
        static struct strbuf pathname_array[4] = {
                STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
 {
        static struct strbuf pathname_array[4] = {
                STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
@@ -59,21 +59,6 @@ static void strbuf_cleanup_path(struct strbuf *sb)
                strbuf_remove(sb, 0, path - sb->buf);
 }
 
                strbuf_remove(sb, 0, path - sb->buf);
 }
 
-char *mksnpath(char *buf, size_t n, const char *fmt, ...)
-{
-       va_list args;
-       unsigned len;
-
-       va_start(args, fmt);
-       len = vsnprintf(buf, n, fmt, args);
-       va_end(args);
-       if (len >= n) {
-               strlcpy(buf, bad_path, n);
-               return buf;
-       }
-       return (char *)cleanup_path(buf);
-}
-
 static int dir_prefix(const char *buf, const char *dir)
 {
        int len = strlen(dir);
 static int dir_prefix(const char *buf, const char *dir)
 {
        int len = strlen(dir);
@@ -380,15 +365,15 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len,
                strbuf_addstr(buf, LOCK_SUFFIX);
 }
 
                strbuf_addstr(buf, LOCK_SUFFIX);
 }
 
-void report_linked_checkout_garbage(void)
+void report_linked_checkout_garbage(struct repository *r)
 {
        struct strbuf sb = STRBUF_INIT;
        const struct common_dir *p;
        int len;
 
 {
        struct strbuf sb = STRBUF_INIT;
        const struct common_dir *p;
        int len;
 
-       if (!the_repository->different_commondir)
+       if (!r->different_commondir)
                return;
                return;
-       strbuf_addf(&sb, "%s/", get_git_dir());
+       strbuf_addf(&sb, "%s/", r->gitdir);
        len = sb.len;
        for (p = common_list; p->path; p++) {
                const char *path = p->path;
        len = sb.len;
        for (p = common_list; p->path; p++) {
                const char *path = p->path;
@@ -432,9 +417,9 @@ static void strbuf_worktree_gitdir(struct strbuf *buf,
                strbuf_git_common_path(buf, repo, "worktrees/%s", wt->id);
 }
 
                strbuf_git_common_path(buf, repo, "worktrees/%s", wt->id);
 }
 
-static void do_git_path(const struct repository *repo,
-                       const struct worktree *wt, struct strbuf *buf,
-                       const char *fmt, va_list args)
+void repo_git_pathv(const struct repository *repo,
+                   const struct worktree *wt, struct strbuf *buf,
+                   const char *fmt, va_list args)
 {
        int gitdir_len;
        strbuf_worktree_gitdir(buf, repo, wt);
 {
        int gitdir_len;
        strbuf_worktree_gitdir(buf, repo, wt);
@@ -453,7 +438,7 @@ char *repo_git_path(const struct repository *repo,
        struct strbuf path = STRBUF_INIT;
        va_list args;
        va_start(args, fmt);
        struct strbuf path = STRBUF_INIT;
        va_list args;
        va_start(args, fmt);
-       do_git_path(repo, NULL, &path, fmt, args);
+       repo_git_pathv(repo, NULL, &path, fmt, args);
        va_end(args);
        return strbuf_detach(&path, NULL);
 }
        va_end(args);
        return strbuf_detach(&path, NULL);
 }
@@ -464,46 +449,8 @@ void strbuf_repo_git_path(struct strbuf *sb,
 {
        va_list args;
        va_start(args, fmt);
 {
        va_list args;
        va_start(args, fmt);
-       do_git_path(repo, NULL, sb, fmt, args);
-       va_end(args);
-}
-
-char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
-{
-       va_list args;
-       strbuf_reset(buf);
-       va_start(args, fmt);
-       do_git_path(the_repository, NULL, buf, fmt, args);
-       va_end(args);
-       return buf->buf;
-}
-
-void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
-{
-       va_list args;
-       va_start(args, fmt);
-       do_git_path(the_repository, NULL, sb, fmt, args);
-       va_end(args);
-}
-
-const char *git_path(const char *fmt, ...)
-{
-       struct strbuf *pathname = get_pathname();
-       va_list args;
-       va_start(args, fmt);
-       do_git_path(the_repository, NULL, pathname, fmt, args);
-       va_end(args);
-       return pathname->buf;
-}
-
-char *git_pathdup(const char *fmt, ...)
-{
-       struct strbuf path = STRBUF_INIT;
-       va_list args;
-       va_start(args, fmt);
-       do_git_path(the_repository, NULL, &path, fmt, args);
+       repo_git_pathv(repo, NULL, sb, fmt, args);
        va_end(args);
        va_end(args);
-       return strbuf_detach(&path, NULL);
 }
 
 char *mkpathdup(const char *fmt, ...)
 }
 
 char *mkpathdup(const char *fmt, ...)
@@ -527,12 +474,17 @@ const char *mkpath(const char *fmt, ...)
        return cleanup_path(pathname->buf);
 }
 
        return cleanup_path(pathname->buf);
 }
 
-const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
+const char *worktree_git_path(struct repository *r,
+                             const struct worktree *wt, const char *fmt, ...)
 {
        struct strbuf *pathname = get_pathname();
        va_list args;
 {
        struct strbuf *pathname = get_pathname();
        va_list args;
+
+       if (wt && wt->repo != r)
+               BUG("worktree not connected to expected repository");
+
        va_start(args, fmt);
        va_start(args, fmt);
-       do_git_path(the_repository, wt, pathname, fmt, args);
+       repo_git_pathv(r, wt, pathname, fmt, args);
        va_end(args);
        return pathname->buf;
 }
        va_end(args);
        return pathname->buf;
 }
@@ -632,26 +584,16 @@ int strbuf_git_path_submodule(struct strbuf *buf, const char *path,
        return err;
 }
 
        return err;
 }
 
-static void do_git_common_path(const struct repository *repo,
-                              struct strbuf *buf,
-                              const char *fmt,
-                              va_list args)
-{
-       strbuf_addstr(buf, repo->commondir);
-       if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
-               strbuf_addch(buf, '/');
-       strbuf_vaddf(buf, fmt, args);
-       strbuf_cleanup_path(buf);
-}
-
-const char *git_common_path(const char *fmt, ...)
+void repo_common_pathv(const struct repository *repo,
+                      struct strbuf *sb,
+                      const char *fmt,
+                      va_list args)
 {
 {
-       struct strbuf *pathname = get_pathname();
-       va_list args;
-       va_start(args, fmt);
-       do_git_common_path(the_repository, pathname, fmt, args);
-       va_end(args);
-       return pathname->buf;
+       strbuf_addstr(sb, repo->commondir);
+       if (sb->len && !is_dir_sep(sb->buf[sb->len - 1]))
+               strbuf_addch(sb, '/');
+       strbuf_vaddf(sb, fmt, args);
+       strbuf_cleanup_path(sb);
 }
 
 void strbuf_git_common_path(struct strbuf *sb,
 }
 
 void strbuf_git_common_path(struct strbuf *sb,
@@ -660,62 +602,10 @@ void strbuf_git_common_path(struct strbuf *sb,
 {
        va_list args;
        va_start(args, fmt);
 {
        va_list args;
        va_start(args, fmt);
-       do_git_common_path(repo, sb, fmt, args);
+       repo_common_pathv(repo, sb, fmt, args);
        va_end(args);
 }
 
        va_end(args);
 }
 
-int validate_headref(const char *path)
-{
-       struct stat st;
-       char buffer[256];
-       const char *refname;
-       struct object_id oid;
-       int fd;
-       ssize_t len;
-
-       if (lstat(path, &st) < 0)
-               return -1;
-
-       /* Make sure it is a "refs/.." symlink */
-       if (S_ISLNK(st.st_mode)) {
-               len = readlink(path, buffer, sizeof(buffer)-1);
-               if (len >= 5 && !memcmp("refs/", buffer, 5))
-                       return 0;
-               return -1;
-       }
-
-       /*
-        * Anything else, just open it and try to see if it is a symbolic ref.
-        */
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               return -1;
-       len = read_in_full(fd, buffer, sizeof(buffer)-1);
-       close(fd);
-
-       if (len < 0)
-               return -1;
-       buffer[len] = '\0';
-
-       /*
-        * Is it a symbolic ref?
-        */
-       if (skip_prefix(buffer, "ref:", &refname)) {
-               while (isspace(*refname))
-                       refname++;
-               if (starts_with(refname, "refs/"))
-                       return 0;
-       }
-
-       /*
-        * Is this a detached HEAD?
-        */
-       if (!get_oid_hex(buffer, &oid))
-               return 0;
-
-       return -1;
-}
-
 static struct passwd *getpw_str(const char *username, size_t len)
 {
        struct passwd *pw;
 static struct passwd *getpw_str(const char *username, size_t len)
 {
        struct passwd *pw;
@@ -846,6 +736,7 @@ const char *enter_repo(const char *path, int strict)
                if (!suffix[i])
                        return NULL;
                gitfile = read_gitfile(used_path.buf);
                if (!suffix[i])
                        return NULL;
                gitfile = read_gitfile(used_path.buf);
+               die_upon_dubious_ownership(gitfile, NULL, used_path.buf);
                if (gitfile) {
                        strbuf_reset(&used_path);
                        strbuf_addstr(&used_path, gitfile);
                if (gitfile) {
                        strbuf_reset(&used_path);
                        strbuf_addstr(&used_path, gitfile);
@@ -856,6 +747,7 @@ const char *enter_repo(const char *path, int strict)
        }
        else {
                const char *gitfile = read_gitfile(path);
        }
        else {
                const char *gitfile = read_gitfile(path);
+               die_upon_dubious_ownership(gitfile, NULL, path);
                if (gitfile)
                        path = gitfile;
                if (chdir(path))
                if (gitfile)
                        path = gitfile;
                if (chdir(path))