repository.con commit repository.c: delete dead functions (0ac5af5)
   1#include "cache.h"
   2#include "repository.h"
   3#include "config.h"
   4#include "submodule-config.h"
   5
   6/* The main repository */
   7static struct repository the_repo;
   8struct repository *the_repository;
   9
  10void initialize_the_repository(void)
  11{
  12        the_repository = &the_repo;
  13
  14        the_repo.index = &the_index;
  15        repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
  16}
  17
  18static void expand_base_dir(char **out, const char *in,
  19                            const char *base_dir, const char *def_in)
  20{
  21        free(*out);
  22        if (in)
  23                *out = xstrdup(in);
  24        else
  25                *out = xstrfmt("%s/%s", base_dir, def_in);
  26}
  27
  28static void repo_set_commondir(struct repository *repo,
  29                               const char *commondir)
  30{
  31        struct strbuf sb = STRBUF_INIT;
  32
  33        free(repo->commondir);
  34
  35        if (commondir) {
  36                repo->different_commondir = 1;
  37                repo->commondir = xstrdup(commondir);
  38                return;
  39        }
  40
  41        repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
  42        repo->commondir = strbuf_detach(&sb, NULL);
  43}
  44
  45void repo_set_gitdir(struct repository *repo,
  46                     const char *root,
  47                     const struct set_gitdir_args *o)
  48{
  49        const char *gitfile = read_gitfile(root);
  50        /*
  51         * repo->gitdir is saved because the caller could pass "root"
  52         * that also points to repo->gitdir. We want to keep it alive
  53         * until after xstrdup(root). Then we can free it.
  54         */
  55        char *old_gitdir = repo->gitdir;
  56
  57        repo->gitdir = xstrdup(gitfile ? gitfile : root);
  58        free(old_gitdir);
  59
  60        repo_set_commondir(repo, o->commondir);
  61        expand_base_dir(&repo->objectdir, o->object_dir,
  62                        repo->commondir, "objects");
  63        expand_base_dir(&repo->graft_file, o->graft_file,
  64                        repo->commondir, "info/grafts");
  65        expand_base_dir(&repo->index_file, o->index_file,
  66                        repo->gitdir, "index");
  67}
  68
  69void repo_set_hash_algo(struct repository *repo, int hash_algo)
  70{
  71        repo->hash_algo = &hash_algos[hash_algo];
  72}
  73
  74/*
  75 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
  76 * Return 0 upon success and a non-zero value upon failure.
  77 */
  78static int repo_init_gitdir(struct repository *repo, const char *gitdir)
  79{
  80        int ret = 0;
  81        int error = 0;
  82        char *abspath = NULL;
  83        const char *resolved_gitdir;
  84        struct set_gitdir_args args = { NULL };
  85
  86        abspath = real_pathdup(gitdir, 0);
  87        if (!abspath) {
  88                ret = -1;
  89                goto out;
  90        }
  91
  92        /* 'gitdir' must reference the gitdir directly */
  93        resolved_gitdir = resolve_gitdir_gently(abspath, &error);
  94        if (!resolved_gitdir) {
  95                ret = -1;
  96                goto out;
  97        }
  98
  99        repo_set_gitdir(repo, resolved_gitdir, &args);
 100
 101out:
 102        free(abspath);
 103        return ret;
 104}
 105
 106void repo_set_worktree(struct repository *repo, const char *path)
 107{
 108        repo->worktree = real_pathdup(path, 1);
 109}
 110
 111static int read_and_verify_repository_format(struct repository_format *format,
 112                                             const char *commondir)
 113{
 114        int ret = 0;
 115        struct strbuf sb = STRBUF_INIT;
 116
 117        strbuf_addf(&sb, "%s/config", commondir);
 118        read_repository_format(format, sb.buf);
 119        strbuf_reset(&sb);
 120
 121        if (verify_repository_format(format, &sb) < 0) {
 122                warning("%s", sb.buf);
 123                ret = -1;
 124        }
 125
 126        strbuf_release(&sb);
 127        return ret;
 128}
 129
 130/*
 131 * Initialize 'repo' based on the provided 'gitdir'.
 132 * Return 0 upon success and a non-zero value upon failure.
 133 */
 134static int repo_init(struct repository *repo,
 135                     const char *gitdir,
 136                     const char *worktree)
 137{
 138        struct repository_format format;
 139        memset(repo, 0, sizeof(*repo));
 140
 141        repo->ignore_env = 1;
 142
 143        if (repo_init_gitdir(repo, gitdir))
 144                goto error;
 145
 146        if (read_and_verify_repository_format(&format, repo->commondir))
 147                goto error;
 148
 149        repo_set_hash_algo(repo, format.hash_algo);
 150
 151        if (worktree)
 152                repo_set_worktree(repo, worktree);
 153
 154        return 0;
 155
 156error:
 157        repo_clear(repo);
 158        return -1;
 159}
 160
 161/*
 162 * Initialize 'submodule' as the submodule given by 'path' in parent repository
 163 * 'superproject'.
 164 * Return 0 upon success and a non-zero value upon failure.
 165 */
 166int repo_submodule_init(struct repository *submodule,
 167                        struct repository *superproject,
 168                        const char *path)
 169{
 170        const struct submodule *sub;
 171        struct strbuf gitdir = STRBUF_INIT;
 172        struct strbuf worktree = STRBUF_INIT;
 173        int ret = 0;
 174
 175        sub = submodule_from_cache(superproject, &null_oid, path);
 176        if (!sub) {
 177                ret = -1;
 178                goto out;
 179        }
 180
 181        strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
 182        strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
 183
 184        if (repo_init(submodule, gitdir.buf, worktree.buf)) {
 185                /*
 186                 * If initilization fails then it may be due to the submodule
 187                 * not being populated in the superproject's worktree.  Instead
 188                 * we can try to initilize the submodule by finding it's gitdir
 189                 * in the superproject's 'modules' directory.  In this case the
 190                 * submodule would not have a worktree.
 191                 */
 192                strbuf_reset(&gitdir);
 193                strbuf_repo_git_path(&gitdir, superproject,
 194                                     "modules/%s", sub->name);
 195
 196                if (repo_init(submodule, gitdir.buf, NULL)) {
 197                        ret = -1;
 198                        goto out;
 199                }
 200        }
 201
 202        submodule->submodule_prefix = xstrfmt("%s%s/",
 203                                              superproject->submodule_prefix ?
 204                                              superproject->submodule_prefix :
 205                                              "", path);
 206
 207out:
 208        strbuf_release(&gitdir);
 209        strbuf_release(&worktree);
 210        return ret;
 211}
 212
 213void repo_clear(struct repository *repo)
 214{
 215        FREE_AND_NULL(repo->gitdir);
 216        FREE_AND_NULL(repo->commondir);
 217        FREE_AND_NULL(repo->objectdir);
 218        FREE_AND_NULL(repo->graft_file);
 219        FREE_AND_NULL(repo->index_file);
 220        FREE_AND_NULL(repo->worktree);
 221        FREE_AND_NULL(repo->submodule_prefix);
 222
 223        if (repo->config) {
 224                git_configset_clear(repo->config);
 225                FREE_AND_NULL(repo->config);
 226        }
 227
 228        if (repo->submodule_cache) {
 229                submodule_cache_free(repo->submodule_cache);
 230                repo->submodule_cache = NULL;
 231        }
 232
 233        if (repo->index) {
 234                discard_index(repo->index);
 235                FREE_AND_NULL(repo->index);
 236        }
 237}
 238
 239int repo_read_index(struct repository *repo)
 240{
 241        if (!repo->index)
 242                repo->index = xcalloc(1, sizeof(*repo->index));
 243
 244        return read_index_from(repo->index, repo->index_file, repo->gitdir);
 245}