Hash :
51fc5e89
Author :
Date :
2013-05-04T15:16:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "posix.h"
#include "git2/object.h"
#include "git2/refs.h"
#include "git2/refdb.h"
#include "git2/sys/refdb_backend.h"
#include "hash.h"
#include "refdb.h"
#include "refs.h"
int git_refdb_new(git_refdb **out, git_repository *repo)
{
git_refdb *db;
assert(out && repo);
db = git__calloc(1, sizeof(*db));
GITERR_CHECK_ALLOC(db);
db->repo = repo;
*out = db;
GIT_REFCOUNT_INC(db);
return 0;
}
int git_refdb_open(git_refdb **out, git_repository *repo)
{
git_refdb *db;
git_refdb_backend *dir;
assert(out && repo);
*out = NULL;
if (git_refdb_new(&db, repo) < 0)
return -1;
/* Add the default (filesystem) backend */
if (git_refdb_backend_fs(&dir, repo) < 0) {
git_refdb_free(db);
return -1;
}
db->repo = repo;
db->backend = dir;
*out = db;
return 0;
}
static void refdb_free_backend(git_refdb *db)
{
if (db->backend) {
if (db->backend->free)
db->backend->free(db->backend);
else
git__free(db->backend);
}
}
int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
{
refdb_free_backend(db);
db->backend = backend;
return 0;
}
int git_refdb_compress(git_refdb *db)
{
assert(db);
if (db->backend->compress)
return db->backend->compress(db->backend);
return 0;
}
static void refdb_free(git_refdb *db)
{
refdb_free_backend(db);
git__free(db);
}
void git_refdb_free(git_refdb *db)
{
if (db == NULL)
return;
GIT_REFCOUNT_DEC(db, refdb_free);
}
int git_refdb_exists(int *exists, git_refdb *refdb, const char *ref_name)
{
assert(exists && refdb && refdb->backend);
return refdb->backend->exists(exists, refdb->backend, ref_name);
}
int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
{
git_reference *ref;
int error;
assert(db && db->backend && out && ref_name);
if (!(error = db->backend->lookup(&ref, db->backend, ref_name))) {
ref->db = db;
*out = ref;
} else {
*out = NULL;
}
return error;
}
int git_refdb_iterator(git_reference_iterator **out, git_refdb *db)
{
git_reference_iterator *iter;
if (!db->backend || !db->backend->iterator) {
giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators");
return -1;
}
if (db->backend->iterator(&iter, db->backend) < 0) {
git__free(iter);
return -1;
}
*out = iter;
return 0;
}
int git_refdb_next(const char **out, git_reference_iterator *iter)
{
return iter->backend->next(out, iter);
}
void git_refdb_iterator_free(git_reference_iterator *iter)
{
iter->backend->iterator_free(iter);
}
int git_refdb_foreach(
git_refdb *db,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload)
{
assert(db && db->backend);
return db->backend->foreach(db->backend, list_flags, callback, payload);
}
struct glob_cb_data {
const char *glob;
git_reference_foreach_cb callback;
void *payload;
};
static int fromglob_cb(const char *reference_name, void *payload)
{
struct glob_cb_data *data = (struct glob_cb_data *)payload;
if (!p_fnmatch(data->glob, reference_name, 0))
return data->callback(reference_name, data->payload);
return 0;
}
int git_refdb_foreach_glob(
git_refdb *db,
const char *glob,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload)
{
int error;
struct glob_cb_data data;
assert(db && db->backend && glob && callback);
if(db->backend->foreach_glob != NULL)
error = db->backend->foreach_glob(db->backend,
glob, list_flags, callback, payload);
else {
data.glob = glob;
data.callback = callback;
data.payload = payload;
error = db->backend->foreach(db->backend,
list_flags, fromglob_cb, &data);
}
return error;
}
int git_refdb_write(git_refdb *db, const git_reference *ref)
{
assert(db && db->backend);
return db->backend->write(db->backend, ref);
}
int git_refdb_delete(struct git_refdb *db, const git_reference *ref)
{
assert(db && db->backend);
return db->backend->delete(db->backend, ref);
}