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