Merge branch 'jk/prune-mtime'
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 3324014cb7ac825fa9703cc6568190460e9cd87d..d34372520b05a2acee36f02ddc2ef68dde4b65fd 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -100,10 +100,7 @@ char *prefix_path_gently(const char *prefix, int len,
                        return NULL;
                }
        } else {
-               sanitized = xmalloc(len + strlen(path) + 1);
-               if (len)
-                       memcpy(sanitized, prefix, len);
-               strcpy(sanitized + len, path);
+               sanitized = xstrfmt("%.*s%s", len, prefix, path);
                if (remaining_prefix)
                        *remaining_prefix = len;
                if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
@@ -229,15 +226,22 @@ void verify_non_filename(const char *prefix, const char *arg)
 }
 
 int get_common_dir(struct strbuf *sb, const char *gitdir)
+{
+       const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
+       if (git_env_common_dir) {
+               strbuf_addstr(sb, git_env_common_dir);
+               return 1;
+       } else {
+               return get_common_dir_noenv(sb, gitdir);
+       }
+}
+
+int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
 {
        struct strbuf data = STRBUF_INIT;
        struct strbuf path = STRBUF_INIT;
-       const char *git_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
        int ret = 0;
-       if (git_common_dir) {
-               strbuf_addstr(sb, git_common_dir);
-               return 1;
-       }
+
        strbuf_addf(&path, "%s/commondir", gitdir);
        if (file_exists(path.buf)) {
                if (strbuf_read_file(&data, path.buf, 0) <= 0)
@@ -501,11 +505,8 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
 
        if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
                size_t pathlen = slash+1 - path;
-               size_t dirlen = pathlen + len - 8;
-               dir = xmalloc(dirlen + 1);
-               strncpy(dir, path, pathlen);
-               strncpy(dir + pathlen, buf + 8, len - 8);
-               dir[dirlen] = '\0';
+               dir = xstrfmt("%.*s%.*s", (int)pathlen, path,
+                             (int)(len - 8), buf + 8);
                free(buf);
                buf = dir;
        }