Blame src/refdb.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/object.h"
Packit Service 20376f
#include "git2/refs.h"
Packit Service 20376f
#include "git2/refdb.h"
Packit Service 20376f
#include "git2/sys/refdb_backend.h"
Packit Service 20376f
Packit Service 20376f
#include "hash.h"
Packit Service 20376f
#include "refdb.h"
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
#include "reflog.h"
Packit Service 20376f
Packit Service 20376f
int git_refdb_new(git_refdb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_refdb *db;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	db = git__calloc(1, sizeof(*db));
Packit Service 20376f
	GITERR_CHECK_ALLOC(db);
Packit Service 20376f
Packit Service 20376f
	db->repo = repo;
Packit Service 20376f
Packit Service 20376f
	*out = db;
Packit Service 20376f
	GIT_REFCOUNT_INC(db);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_open(git_refdb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_refdb *db;
Packit Service 20376f
	git_refdb_backend *dir;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (git_refdb_new(&db, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Add the default (filesystem) backend */
Packit Service 20376f
	if (git_refdb_backend_fs(&dir, repo) < 0) {
Packit Service 20376f
		git_refdb_free(db);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	db->repo = repo;
Packit Service 20376f
	db->backend = dir;
Packit Service 20376f
Packit Service 20376f
	*out = db;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void refdb_free_backend(git_refdb *db)
Packit Service 20376f
{
Packit Service 20376f
	if (db->backend)
Packit Service 20376f
		db->backend->free(db->backend);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
Packit Service 20376f
{
Packit Service 20376f
	refdb_free_backend(db);
Packit Service 20376f
	db->backend = backend;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_compress(git_refdb *db)
Packit Service 20376f
{
Packit Service 20376f
	assert(db);
Packit Service 20376f
Packit Service 20376f
	if (db->backend->compress)
Packit Service 20376f
		return db->backend->compress(db->backend);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_refdb__free(git_refdb *db)
Packit Service 20376f
{
Packit Service 20376f
	refdb_free_backend(db);
Packit Service 20376f
	git__memzero(db, sizeof(*db));
Packit Service 20376f
	git__free(db);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_refdb_free(git_refdb *db)
Packit Service 20376f
{
Packit Service 20376f
	if (db == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_DEC(db, git_refdb__free);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_exists(int *exists, git_refdb *refdb, const char *ref_name)
Packit Service 20376f
{
Packit Service 20376f
	assert(exists && refdb && refdb->backend);
Packit Service 20376f
Packit Service 20376f
	return refdb->backend->exists(exists, refdb->backend, ref_name);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *ref;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(db && db->backend && out && ref_name);
Packit Service 20376f
Packit Service 20376f
	error = db->backend->lookup(&ref, db->backend, ref_name);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(db);
Packit Service 20376f
	ref->db = db;
Packit Service 20376f
Packit Service 20376f
	*out = ref;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char *glob)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!db->backend || !db->backend->iterator) {
Packit Service 20376f
		giterr_set(GITERR_REFERENCE, "this backend doesn't support iterators");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = db->backend->iterator(out, db->backend, glob)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(db);
Packit Service 20376f
	(*out)->db = db;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_iterator_next(git_reference **out, git_reference_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = iter->next(out, iter)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(iter->db);
Packit Service 20376f
	(*out)->db = iter->db;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_iterator_next_name(const char **out, git_reference_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->next_name(out, iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_refdb_iterator_free(git_reference_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	GIT_REFCOUNT_DEC(iter->db, git_refdb__free);
Packit Service 20376f
	iter->free(iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_write(git_refdb *db, git_reference *ref, int force, const git_signature *who, const char *message, const git_oid *old_id, const char *old_target)
Packit Service 20376f
{
Packit Service 20376f
	assert(db && db->backend);
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(db);
Packit Service 20376f
	ref->db = db;
Packit Service 20376f
Packit Service 20376f
	return db->backend->write(db->backend, ref, force, who, message, old_id, old_target);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_rename(
Packit Service 20376f
	git_reference **out,
Packit Service 20376f
	git_refdb *db,
Packit Service 20376f
	const char *old_name,
Packit Service 20376f
	const char *new_name,
Packit Service 20376f
	int force,
Packit Service 20376f
	const git_signature *who,
Packit Service 20376f
	const char *message)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(db && db->backend);
Packit Service 20376f
	error = db->backend->rename(out, db->backend, old_name, new_name, force, who, message);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (out) {
Packit Service 20376f
		GIT_REFCOUNT_INC(db);
Packit Service 20376f
		(*out)->db = db;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_delete(struct git_refdb *db, const char *ref_name, const git_oid *old_id, const char *old_target)
Packit Service 20376f
{
Packit Service 20376f
	assert(db && db->backend);
Packit Service 20376f
	return db->backend->del(db->backend, ref_name, old_id, old_target);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_reflog_read(git_reflog **out, git_refdb *db,  const char *name)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(db && db->backend);
Packit Service 20376f
Packit Service 20376f
	if ((error = db->backend->reflog_read(out, db->backend, name)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(db);
Packit Service 20376f
	(*out)->db = db;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_has_log(git_refdb *db, const char *refname)
Packit Service 20376f
{
Packit Service 20376f
	assert(db && refname);
Packit Service 20376f
Packit Service 20376f
	return db->backend->has_log(db->backend, refname);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_ensure_log(git_refdb *db, const char *refname)
Packit Service 20376f
{
Packit Service 20376f
	assert(db && refname);
Packit Service 20376f
Packit Service 20376f
	return db->backend->ensure_log(db->backend, refname);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_init_backend(git_refdb_backend *backend, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit Service 20376f
		backend, version, git_refdb_backend, GIT_REFDB_BACKEND_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_lock(void **payload, git_refdb *db, const char *refname)
Packit Service 20376f
{
Packit Service 20376f
	assert(payload && db && refname);
Packit Service 20376f
Packit Service 20376f
	if (!db->backend->lock) {
Packit Service 20376f
		giterr_set(GITERR_REFERENCE, "backend does not support locking");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return db->backend->lock(payload, db->backend, refname);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_refdb_unlock(git_refdb *db, void *payload, int success, int update_reflog, const git_reference *ref, const git_signature *sig, const char *message)
Packit Service 20376f
{
Packit Service 20376f
	assert(db);
Packit Service 20376f
Packit Service 20376f
	return db->backend->unlock(db->backend, payload, success, update_reflog, ref, sig, message);
Packit Service 20376f
}