Commit d00d5464 by Edward Thomson

immutable references and a pluggable ref database

parent 6a9ef012
......@@ -50,11 +50,11 @@ GIT_BEGIN_DECL
* pointing to the provided target commit.
*/
GIT_EXTERN(int) git_branch_create(
git_reference **out,
git_repository *repo,
const char *branch_name,
const git_commit *target,
int force);
git_reference **out,
git_repository *repo,
const char *branch_name,
const git_commit *target,
int force);
/**
* Delete an existing branch reference.
......@@ -67,6 +67,11 @@ GIT_EXTERN(int) git_branch_create(
*/
GIT_EXTERN(int) git_branch_delete(git_reference *branch);
typedef int (*git_branch_foreach_cb)(
const char *branch_name,
git_branch_t branch_type,
void *payload);
/**
* Loop over all the branches and issue a callback for each one.
*
......@@ -85,14 +90,10 @@ GIT_EXTERN(int) git_branch_delete(git_reference *branch);
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_branch_foreach(
git_repository *repo,
unsigned int list_flags,
int (*branch_cb)(
const char *branch_name,
git_branch_t branch_type,
void *payload),
void *payload
);
git_repository *repo,
unsigned int list_flags,
git_branch_foreach_cb branch_cb,
void *payload);
/**
* Move/rename an existing local branch reference.
......@@ -110,9 +111,10 @@ GIT_EXTERN(int) git_branch_foreach(
* @return 0 on success, GIT_EINVALIDSPEC or an error code.
*/
GIT_EXTERN(int) git_branch_move(
git_reference *branch,
const char *new_branch_name,
int force);
git_reference **out,
git_reference *branch,
const char *new_branch_name,
int force);
/**
* Lookup a branch by its name in a repository.
......@@ -136,10 +138,10 @@ GIT_EXTERN(int) git_branch_move(
* exists, GIT_EINVALIDSPEC, otherwise an error code.
*/
GIT_EXTERN(int) git_branch_lookup(
git_reference **out,
git_repository *repo,
const char *branch_name,
git_branch_t branch_type);
git_reference **out,
git_repository *repo,
const char *branch_name,
git_branch_t branch_type);
/**
* Return the name of the given local or remote branch.
......@@ -172,8 +174,8 @@ GIT_EXTERN(int) git_branch_name(const char **out,
* reference exists, otherwise an error code.
*/
GIT_EXTERN(int) git_branch_tracking(
git_reference **out,
git_reference *branch);
git_reference **out,
git_reference *branch);
/**
* Return the name of the reference supporting the remote tracking branch,
......@@ -208,7 +210,7 @@ GIT_EXTERN(int) git_branch_tracking_name(
* error code otherwise.
*/
GIT_EXTERN(int) git_branch_is_head(
git_reference *branch);
git_reference *branch);
/**
* Return the name of remote that the remote tracking branch belongs to.
......
/*
* 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.
*/
#ifndef INCLUDE_git_refdb_h__
#define INCLUDE_git_refdb_h__
#include "common.h"
#include "types.h"
#include "oid.h"
#include "refs.h"
/**
* @file git2/refdb.h
* @brief Git custom refs backend functions
* @defgroup git_refdb Git custom refs backend API
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Create a new reference. Either an oid or a symbolic target must be
* specified.
*
* @param refdb the reference database to associate with this reference
* @param name the reference name
* @param oid the object id for a direct reference
* @param symbolic the target for a symbolic reference
* @return the created git_reference or NULL on error
*/
git_reference *git_reference__alloc(
git_refdb *refdb,
const char *name,
const git_oid *oid,
const char *symbolic);
/**
* Create a new reference database with no backends.
*
* Before the Ref DB can be used for read/writing, a custom database
* backend must be manually set using `git_refdb_set_backend()`
*
* @param out location to store the database pointer, if opened.
* Set to NULL if the open failed.
* @param repo the repository
* @return 0 or an error code
*/
GIT_EXTERN(int) git_refdb_new(git_refdb **out, git_repository *repo);
/**
* Create a new reference database and automatically add
* the default backends:
*
* - git_refdb_dir: read and write loose and packed refs
* from disk, assuming the repository dir as the folder
*
* @param out location to store the database pointer, if opened.
* Set to NULL if the open failed.
* @param repo the repository
* @return 0 or an error code
*/
GIT_EXTERN(int) git_refdb_open(git_refdb **out, git_repository *repo);
/**
* Suggests that the given refdb compress or optimize its references.
* This mechanism is implementation specific. For on-disk reference
* databases, for example, this may pack all loose references.
*/
GIT_EXTERN(int) git_refdb_compress(git_refdb *refdb);
/**
* Close an open reference database.
*
* @param refdb reference database pointer or NULL
*/
GIT_EXTERN(void) git_refdb_free(git_refdb *refdb);
/**
* Sets the custom backend to an existing reference DB
*
* Read <refdb_backends.h> for more information.
*
* @param refdb database to add the backend to
* @param backend pointer to a git_refdb_backend instance
* @param priority Value for ordering the backends queue
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_refdb_set_backend(
git_refdb *refdb,
git_refdb_backend *backend);
/** @} */
GIT_END_DECL
#endif
/*
* 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.
*/
#ifndef INCLUDE_git_refdb_backend_h__
#define INCLUDE_git_refdb_backend_h__
#include "common.h"
#include "types.h"
#include "oid.h"
/**
* @file git2/refdb_backend.h
* @brief Git custom refs backend functions
* @defgroup git_refdb_backend Git custom refs backend API
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/** An instance for a custom backend */
struct git_refdb_backend {
unsigned int version;
/**
* Queries the refdb backend to determine if the given ref_name
* exists. A refdb implementation must provide this function.
*/
int (*exists)(
int *exists,
struct git_refdb_backend *backend,
const char *ref_name);
/**
* Queries the refdb backend for a given reference. A refdb
* implementation must provide this function.
*/
int (*lookup)(
git_reference **out,
struct git_refdb_backend *backend,
const char *ref_name);
/**
* Enumerates each reference in the refdb. A refdb implementation must
* provide this function.
*/
int (*foreach)(
struct git_refdb_backend *backend,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload);
/**
* Enumerates each reference in the refdb that matches the given
* glob string. A refdb implementation may provide this function;
* if it is not provided, foreach will be used and the results filtered
* against the glob.
*/
int (*foreach_glob)(
struct git_refdb_backend *backend,
const char *glob,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload);
/**
* Writes the given reference to the refdb. A refdb implementation
* must provide this function.
*/
int (*write)(struct git_refdb_backend *backend, const git_reference *ref);
/**
* Deletes the given reference from the refdb. A refdb implementation
* must provide this function.
*/
int (*delete)(struct git_refdb_backend *backend, const git_reference *ref);
/**
* Suggests that the given refdb compress or optimize its references.
* This mechanism is implementation specific. (For on-disk reference
* databases, this may pack all loose references.) A refdb
* implementation may provide this function; if it is not provided,
* nothing will be done.
*/
int (*compress)(struct git_refdb_backend *backend);
/**
* Frees any resources held by the refdb. A refdb implementation may
* provide this function; if it is not provided, nothing will be done.
*/
void (*free)(struct git_refdb_backend *backend);
};
#define GIT_ODB_BACKEND_VERSION 1
#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
/**
* Constructors for default refdb backends.
*/
GIT_EXTERN(int) git_refdb_backend_fs(
struct git_refdb_backend **backend_out,
git_repository *repo,
git_refdb *refdb);
GIT_END_DECL
#endif
......@@ -189,33 +189,41 @@ GIT_EXTERN(int) git_reference_resolve(git_reference **out, const git_reference *
GIT_EXTERN(git_repository *) git_reference_owner(const git_reference *ref);
/**
* Set the symbolic target of a reference.
* Create a new reference with the same name as the given reference but a
* different symbolic target. The reference must be a symbolic reference,
* otherwise this will fail.
*
* The reference must be a symbolic reference, otherwise this will fail.
*
* The reference will be automatically updated in memory and on disk.
* The new reference will be written to disk, overwriting the given reference.
*
* The target name will be checked for validity.
* See `git_reference_create_symbolic()` for rules about valid names.
*
* @param out Pointer to the newly created reference
* @param ref The reference
* @param target The new target for the reference
* @return 0 on success, EINVALIDSPEC or an error code
*/
GIT_EXTERN(int) git_reference_symbolic_set_target(git_reference *ref, const char *target);
GIT_EXTERN(int) git_reference_symbolic_set_target(
git_reference **out,
git_reference *ref,
const char *target);
/**
* Set the OID target of a reference.
* Create a new reference with the same name as the given reference but a
* different OID target. The reference must be a direct reference, otherwise
* this will fail.
*
* The reference must be a direct reference, otherwise this will fail.
*
* The reference will be automatically updated in memory and on disk.
* The new reference will be written to disk, overwriting the given reference.
*
* @param out Pointer to the newly created reference
* @param ref The reference
* @param id The new target OID for the reference
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id);
GIT_EXTERN(int) git_reference_set_target(
git_reference **out,
git_reference *ref,
const git_oid *id);
/**
* Rename an existing reference.
......@@ -225,7 +233,8 @@ GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id);
* The new name will be checked for validity.
* See `git_reference_create_symbolic()` for rules about valid names.
*
* The given git_reference will be updated in place.
* On success, the given git_reference will be deleted from disk and a
* new `git_reference` will be returned.
*
* The reference will be immediately renamed in-memory and on disk.
*
......@@ -243,15 +252,18 @@ GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const git_oid *id);
* @return 0 on success, EINVALIDSPEC, EEXISTS or an error code
*
*/
GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *name, int force);
GIT_EXTERN(int) git_reference_rename(
git_reference **out,
git_reference *ref,
const char *new_name,
int force);
/**
* Delete an existing reference.
*
* This method works for both direct and symbolic references.
*
* The reference will be immediately removed on disk and from memory
* (i.e. freed). The given reference pointer will no longer be valid.
* This method works for both direct and symbolic references. The reference
* will be immediately removed on disk but the memory will not be freed.
* Callers must call `git_reference_free`.
*
* @param ref The reference to remove
* @return 0 or an error code
......@@ -259,21 +271,6 @@ GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *name, int f
GIT_EXTERN(int) git_reference_delete(git_reference *ref);
/**
* Pack all the loose references in the repository.
*
* This method will load into the cache all the loose
* references on the repository and update the
* `packed-refs` file with them.
*
* Once the `packed-refs` file has been written properly,
* the loose references will be removed from disk.
*
* @param repo Repository where the loose refs will be packed
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reference_packall(git_repository *repo);
/**
* Fill a list with all the references that can be found in a repository.
*
* Using the `list_flags` parameter, the listed references may be filtered
......@@ -323,14 +320,6 @@ GIT_EXTERN(int) git_reference_foreach(
void *payload);
/**
* Check if a reference has been loaded from a packfile.
*
* @param ref A git reference
* @return 0 in case it's not packed; 1 otherwise
*/
GIT_EXTERN(int) git_reference_is_packed(git_reference *ref);
/**
* Reload a reference from disk.
*
* Reference pointers can become outdated if the Git repository is
......
......@@ -434,6 +434,39 @@ GIT_EXTERN(int) git_repository_odb(git_odb **out, git_repository *repo);
GIT_EXTERN(void) git_repository_set_odb(git_repository *repo, git_odb *odb);
/**
* Get the Reference Database Backend for this repository.
*
* If a custom refsdb has not been set, the default database for
* the repository will be returned (the one that manipulates loose
* and packed references in the `.git` directory).
*
* The refdb must be freed once it's no longer being used by
* the user.
*
* @param out Pointer to store the loaded refdb
* @param repo A repository object
* @return 0, or an error code
*/
GIT_EXTERN(int) git_repository_refdb(git_refdb **out, git_repository *repo);
/**
* Set the Reference Database Backend for this repository
*
* The refdb will be used for all reference related operations
* involving this repository.
*
* The repository will keep a reference to the refdb; the user
* must still free the refdb object after setting it to the
* repository, or it will leak.
*
* @param repo A repository object
* @param refdb An refdb object
*/
GIT_EXTERN(void) git_repository_set_refdb(
git_repository *repo,
git_refdb *refdb);
/**
* Get the Index file for this repository.
*
* If a custom index has not been set, the default
......
......@@ -92,6 +92,12 @@ typedef struct git_odb_stream git_odb_stream;
/** A stream to write a packfile to the ODB */
typedef struct git_odb_writepack git_odb_writepack;
/** An open refs database handle. */
typedef struct git_refdb git_refdb;
/** A custom backend for refs */
typedef struct git_refdb_backend git_refdb_backend;
/**
* Representation of an existing git repository,
* including all its object contents
......@@ -164,9 +170,7 @@ typedef enum {
GIT_REF_INVALID = 0, /** Invalid reference */
GIT_REF_OID = 1, /** A reference which points at an object id */
GIT_REF_SYMBOLIC = 2, /** A reference which points at another reference */
GIT_REF_PACKED = 4,
GIT_REF_HAS_PEEL = 8,
GIT_REF_LISTALL = GIT_REF_OID|GIT_REF_SYMBOLIC|GIT_REF_PACKED,
GIT_REF_LISTALL = GIT_REF_OID|GIT_REF_SYMBOLIC,
} git_ref_t;
/** Basic type of any Git branch. */
......
......@@ -54,11 +54,11 @@ static int not_a_local_branch(const char *reference_name)
}
int git_branch_create(
git_reference **ref_out,
git_repository *repository,
const char *branch_name,
const git_commit *commit,
int force)
git_reference **ref_out,
git_repository *repository,
const char *branch_name,
const git_commit *commit,
int force)
{
git_reference *branch = NULL;
git_buf canonical_branch_name = GIT_BUF_INIT;
......@@ -124,10 +124,7 @@ on_error:
}
typedef struct {
int (*branch_cb)(
const char *branch_name,
git_branch_t branch_type,
void *payload);
git_branch_foreach_cb branch_cb;
void *callback_payload;
unsigned int branch_type;
} branch_foreach_filter;
......@@ -148,14 +145,10 @@ static int branch_foreach_cb(const char *branch_name, void *payload)
}
int git_branch_foreach(
git_repository *repo,
unsigned int list_flags,
int (*branch_cb)(
const char *branch_name,
git_branch_t branch_type,
void *payload),
void *payload
)
git_repository *repo,
unsigned int list_flags,
git_branch_foreach_cb branch_cb,
void *payload)
{
branch_foreach_filter filter;
......@@ -167,6 +160,7 @@ int git_branch_foreach(
}
int git_branch_move(
git_reference **out,
git_reference *branch,
const char *new_branch_name,
int force)
......@@ -181,28 +175,20 @@ int git_branch_move(
if (!git_reference_is_branch(branch))
return not_a_local_branch(git_reference_name(branch));
if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
goto cleanup;
if (git_buf_printf(
&old_config_section,
"branch.%s",
git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
goto cleanup;
if ((error = git_reference_rename(branch, git_buf_cstr(&new_reference_name), force)) < 0)
goto cleanup;
if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0 ||
(error = git_buf_printf(&old_config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0) ||
(error = git_buf_printf(&new_config_section, "branch.%s", new_branch_name)) < 0)
goto done;
if (git_buf_printf(&new_config_section, "branch.%s", new_branch_name) < 0)
goto cleanup;
if ((error = git_config_rename_section(
git_reference_owner(branch),
if ((error = git_config_rename_section(git_reference_owner(branch),
git_buf_cstr(&old_config_section),
git_buf_cstr(&new_config_section))) < 0)
goto cleanup;
goto done;
if ((error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force)) < 0)
goto done;
cleanup:
done:
git_buf_free(&new_reference_name);
git_buf_free(&old_config_section);
git_buf_free(&new_config_section);
......@@ -211,10 +197,10 @@ cleanup:
}
int git_branch_lookup(
git_reference **ref_out,
git_repository *repo,
const char *branch_name,
git_branch_t branch_type)
git_reference **ref_out,
git_repository *repo,
const char *branch_name,
git_branch_t branch_type)
{
assert(ref_out && repo && branch_name);
......
......@@ -121,7 +121,7 @@ int git_commit_create(
git_buf_free(&commit);
if (update_ref != NULL)
return git_reference__update(repo, oid, update_ref);
return git_reference__update_terminal(repo, update_ref, oid);
return 0;
......
......@@ -16,7 +16,6 @@
#include "refs.h"
#include "repository.h"
int git_fetchhead_ref_cmp(const void *a, const void *b)
{
const git_fetchhead_ref *one = (const git_fetchhead_ref *)a;
......
/*
* 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 "hash.h"
#include "refdb.h"
#include "refs.h"
#include "git2/refdb_backend.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, db) < 0) {
git_refdb_free(db);
return -1;
}
db->repo = repo;
db->backend = dir;
*out = db;
return 0;
}
int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
{
if (db->backend) {
if(db->backend->free)
db->backend->free(db->backend);
else
git__free(db->backend);
}
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;
}
void git_refdb_free(git_refdb *db)
{
if (db->backend) {
if(db->backend->free)
db->backend->free(db->backend);
else
git__free(db->backend);
}
git__free(db);
}
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)
{
assert(db && db->backend && ref_name);
return db->backend->lookup(out, db->backend, ref_name);
}
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);
}
/*
* 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.
*/
#ifndef INCLUDE_refdb_h__
#define INCLUDE_refdb_h__
#include "git2/refdb.h"
#include "repository.h"
struct git_refdb {
git_refcount rc;
git_repository *repo;
git_refdb_backend *backend;
};
int git_refdb_exists(
int *exists,
git_refdb *refdb,
const char *ref_name);
int git_refdb_lookup(
git_reference **out,
git_refdb *refdb,
const char *ref_name);
int git_refdb_foreach(
git_refdb *refdb,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload);
int git_refdb_foreach_glob(
git_refdb *refdb,
const char *glob,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload);
int git_refdb_write(git_refdb *refdb, const git_reference *ref);
int git_refdb_delete(struct git_refdb *refdb, const git_reference *ref);
#endif
This diff is collapsed. Click to expand it.
/*
* 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.
*/
#ifndef INCLUDE_refdb_fs_h__
#define INCLUDE_refdb_fs_h__
typedef struct {
git_strmap *packfile;
time_t packfile_time;
} git_refcache;
#endif
......@@ -10,6 +10,7 @@
#include "common.h"
#include "git2/oid.h"
#include "git2/refs.h"
#include "git2/refdb.h"
#include "strmap.h"
#include "buffer.h"
......@@ -47,28 +48,22 @@
#define GIT_REFNAME_MAX 1024
struct git_reference {
unsigned int flags;
git_repository *owner;
char *name;
time_t mtime;
git_refdb *db;
git_ref_t type;
union {
git_oid oid;
char *symbolic;
} target;
char name[0];
};
typedef struct {
git_strmap *packfile;
time_t packfile_time;
} git_refcache;
void git_repository__refcache_free(git_refcache *refs);
int git_reference__normalize_name_lax(char *buffer_out, size_t out_size, const char *name);
int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags);
int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid);
int git_reference__is_valid_name(const char *refname, unsigned int flags);
int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name);
int git_reference__is_branch(const char *ref_name);
int git_reference__is_remote(const char *ref_name);
......
......@@ -1175,6 +1175,7 @@ static int rename_one_remote_reference(
int error = -1;
git_buf new_name = GIT_BUF_INIT;
git_reference *reference = NULL;
git_reference *newref = NULL;
if (git_buf_printf(
&new_name,
......@@ -1186,10 +1187,11 @@ static int rename_one_remote_reference(
if (git_reference_lookup(&reference, repo, reference_name) < 0)
goto cleanup;
error = git_reference_rename(reference, git_buf_cstr(&new_name), 0);
error = git_reference_rename(&newref, reference, git_buf_cstr(&new_name), 0);
git_reference_free(reference);
cleanup:
git_reference_free(reference);
git_reference_free(newref);
git_buf_free(&new_name);
return error;
}
......
......@@ -8,6 +8,7 @@
#include <ctype.h>
#include "git2/object.h"
#include "git2/refdb.h"
#include "common.h"
#include "repository.h"
......@@ -39,6 +40,15 @@ static void drop_odb(git_repository *repo)
}
}
static void drop_refdb(git_repository *repo)
{
if (repo->_refdb != NULL) {
GIT_REFCOUNT_OWN(repo->_refdb, NULL);
git_refdb_free(repo->_refdb);
repo->_refdb = NULL;
}
}
static void drop_config(git_repository *repo)
{
if (repo->_config != NULL) {
......@@ -65,7 +75,6 @@ void git_repository_free(git_repository *repo)
return;
git_cache_free(&repo->objects);
git_repository__refcache_free(&repo->references);
git_attr_cache_flush(repo);
git_submodule_config_free(repo);
......@@ -75,6 +84,7 @@ void git_repository_free(git_repository *repo)
drop_config(repo);
drop_index(repo);
drop_odb(repo);
drop_refdb(repo);
git__free(repo);
}
......@@ -600,6 +610,45 @@ void git_repository_set_odb(git_repository *repo, git_odb *odb)
GIT_REFCOUNT_INC(odb);
}
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
{
assert(out && repo);
if (repo->_refdb == NULL) {
int res;
res = git_refdb_open(&repo->_refdb, repo);
if (res < 0)
return -1;
GIT_REFCOUNT_OWN(repo->_refdb, repo);
}
*out = repo->_refdb;
return 0;
}
int git_repository_refdb(git_refdb **out, git_repository *repo)
{
if (git_repository_refdb__weakptr(out, repo) < 0)
return -1;
GIT_REFCOUNT_INC(*out);
return 0;
}
void git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
{
assert (repo && refdb);
drop_refdb(repo);
repo->_refdb = refdb;
GIT_REFCOUNT_OWN(repo->_refdb, repo);
GIT_REFCOUNT_INC(refdb);
}
int git_repository_index__weakptr(git_index **out, git_repository *repo)
{
assert(out && repo);
......
......@@ -21,6 +21,7 @@
#include "object.h"
#include "attr.h"
#include "strmap.h"
#include "refdb.h"
#define DOT_GIT ".git"
#define GIT_DIR DOT_GIT "/"
......@@ -79,11 +80,11 @@ enum {
/** Internal structure for repository object */
struct git_repository {
git_odb *_odb;
git_refdb *_refdb;
git_config *_config;
git_index *_index;
git_cache objects;
git_refcache references;
git_attr_cache attrcache;
git_strmap *submodules;
......@@ -112,6 +113,7 @@ int git_repository_head_tree(git_tree **tree, git_repository *repo);
*/
int git_repository_config__weakptr(git_config **out, git_repository *repo);
int git_repository_odb__weakptr(git_odb **out, git_repository *repo);
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo);
int git_repository_index__weakptr(git_index **out, git_repository *repo);
/*
......
......@@ -13,48 +13,10 @@
#include "git2/reset.h"
#include "git2/checkout.h"
#include "git2/merge.h"
#include "git2/refs.h"
#define ERROR_MSG "Cannot perform reset"
static int update_head(git_repository *repo, git_object *commit)
{
int error;
git_reference *head = NULL, *target = NULL;
error = git_repository_head(&head, repo);
if (error < 0 && error != GIT_EORPHANEDHEAD)
return error;
if (error == GIT_EORPHANEDHEAD) {
giterr_clear();
/*
* TODO: This is a bit weak as this doesn't support chained
* symbolic references. yet.
*/
if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
goto cleanup;
if ((error = git_reference_create(
&target,
repo,
git_reference_symbolic_target(head),
git_object_id(commit), 0)) < 0)
goto cleanup;
} else {
if ((error = git_reference_set_target(head, git_object_id(commit))) < 0)
goto cleanup;
}
error = 0;
cleanup:
git_reference_free(head);
git_reference_free(target);
return error;
}
int git_reset_default(
git_repository *repo,
git_object *target,
......@@ -167,7 +129,8 @@ int git_reset(
}
/* move HEAD to the new target */
if ((error = update_head(repo, commit)) < 0)
if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
git_object_id(commit))) < 0)
goto cleanup;
if (reset_type == GIT_RESET_HARD) {
......
......@@ -10,6 +10,7 @@
#include "common.h"
#include "buffer.h"
#include "tree.h"
#include "refdb.h"
#include "git2.h"
......@@ -656,7 +657,7 @@ static int object_from_reference(git_object **object, git_reference *reference)
if (git_reference_resolve(&resolved, reference) < 0)
return -1;
error = git_object_lookup(object, reference->owner, git_reference_target(resolved), GIT_OBJ_ANY);
error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJ_ANY);
git_reference_free(resolved);
return error;
......
......@@ -645,13 +645,15 @@ int git_stash_drop(
if (max == 1) {
error = git_reference_delete(stash);
git_reference_free(stash);
stash = NULL;
} else if (index == 0) {
const git_reflog_entry *entry;
entry = git_reflog_entry_byindex(reflog, 0);
error = git_reference_set_target(stash, &entry->oid_cur);
git_reference_free(stash);
error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, &entry->oid_cur, 1);
}
cleanup:
......
......@@ -376,9 +376,9 @@ on_error:
int git_tag_delete(git_repository *repo, const char *tag_name)
{
int error;
git_reference *tag_ref;
git_buf ref_name = GIT_BUF_INIT;
int error;
error = retrieve_tag_reference(&tag_ref, &ref_name, repo, tag_name);
......@@ -387,7 +387,10 @@ int git_tag_delete(git_repository *repo, const char *tag_name)
if (error < 0)
return error;
return git_reference_delete(tag_ref);
if ((error = git_reference_delete(tag_ref)) == 0)
git_reference_free(tag_ref);
return error;
}
int git_tag__parse(git_tag *tag, git_odb_object *obj)
......@@ -426,8 +429,7 @@ int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data)
data.cb_data = cb_data;
data.repo = repo;
return git_reference_foreach(
repo, GIT_REF_OID | GIT_REF_PACKED, &tags_cb, &data);
return git_reference_foreach(repo, GIT_REF_OID, &tags_cb, &data);
}
typedef struct {
......
......@@ -114,8 +114,9 @@ void test_commit_write__root(void)
cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
head_old = git__strdup(git_reference_symbolic_target(head));
cl_assert(head_old != NULL);
cl_git_pass(git_reference_symbolic_set_target(head, branch_name));
git_reference_free(head);
cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
......
......@@ -60,6 +60,7 @@ void test_object_tag_write__basic(void)
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
cl_git_pass(git_reference_delete(ref_tag));
git_reference_free(ref_tag);
git_tag_free(tag);
}
......
#include "clar_libgit2.h"
#include "refdb.h"
#include "repository.h"
#include "testdb.h"
#define TEST_REPO_PATH "testrepo"
static git_repository *repo;
static git_refdb *refdb;
static git_refdb_backend *refdb_backend;
int unlink_ref(void *payload, git_buf *file)
{
GIT_UNUSED(payload);
return p_unlink(git_buf_cstr(file));
}
int empty(void *payload, git_buf *file)
{
GIT_UNUSED(payload);
GIT_UNUSED(file);
return -1;
}
int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *filename))
{
const char *repo_path;
git_buf repo_refs_dir = GIT_BUF_INIT;
int error = 0;
repo_path = git_repository_path(repo);
git_buf_joinpath(&repo_refs_dir, repo_path, "HEAD");
if (git_path_exists(git_buf_cstr(&repo_refs_dir)) &&
cb(NULL, &repo_refs_dir) < 0)
return -1;
git_buf_joinpath(&repo_refs_dir, repo_path, "refs");
git_buf_joinpath(&repo_refs_dir, git_buf_cstr(&repo_refs_dir), "heads");
if (git_path_direach(&repo_refs_dir, cb, NULL) != 0)
return -1;
git_buf_joinpath(&repo_refs_dir, repo_path, "packed-refs");
if (git_path_exists(git_buf_cstr(&repo_refs_dir)) &&
cb(NULL, &repo_refs_dir) < 0)
return -1;
git_buf_free(&repo_refs_dir);
return error;
}
void test_refdb_inmemory__initialize(void)
{
git_buf repo_refs_dir = GIT_BUF_INIT;
repo = cl_git_sandbox_init(TEST_REPO_PATH);
cl_git_pass(git_repository_refdb(&refdb, repo));
cl_git_pass(refdb_backend_test(&refdb_backend, repo));
cl_git_pass(git_refdb_set_backend(refdb, refdb_backend));
ref_file_foreach(repo, unlink_ref);
git_buf_free(&repo_refs_dir);
}
void test_refdb_inmemory__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refdb_inmemory__doesnt_write_ref_file(void)
{
git_reference *ref;
git_oid oid;
cl_git_pass(git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_reference_create(&ref, repo, GIT_REFS_HEADS_DIR "test1", &oid, 0));
ref_file_foreach(repo, empty);
git_reference_free(ref);
}
void test_refdb_inmemory__read(void)
{
git_reference *write1, *write2, *write3, *read1, *read2, *read3;
git_oid oid1, oid2, oid3;
cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
cl_git_pass(git_reference_lookup(&read1, repo, GIT_REFS_HEADS_DIR "test1"));
cl_assert(strcmp(git_reference_name(read1), git_reference_name(write1)) == 0);
cl_assert(git_oid_cmp(git_reference_target(read1), git_reference_target(write1)) == 0);
cl_git_pass(git_reference_lookup(&read2, repo, GIT_REFS_HEADS_DIR "test2"));
cl_assert(strcmp(git_reference_name(read2), git_reference_name(write2)) == 0);
cl_assert(git_oid_cmp(git_reference_target(read2), git_reference_target(write2)) == 0);
cl_git_pass(git_reference_lookup(&read3, repo, GIT_REFS_HEADS_DIR "test3"));
cl_assert(strcmp(git_reference_name(read3), git_reference_name(write3)) == 0);
cl_assert(git_oid_cmp(git_reference_target(read3), git_reference_target(write3)) == 0);
git_reference_free(write1);
git_reference_free(write2);
git_reference_free(write3);
git_reference_free(read1);
git_reference_free(read2);
git_reference_free(read3);
}
int foreach_test(const char *ref_name, void *payload)
{
git_reference *ref;
git_oid expected;
int *i = payload;
cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
if (*i == 0)
cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
else if (*i == 1)
cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));
else if (*i == 2)
cl_git_pass(git_oid_fromstr(&expected, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_assert(git_oid_cmp(&expected, &ref->target.oid) == 0);
++(*i);
git_reference_free(ref);
return 0;
}
void test_refdb_inmemory__foreach(void)
{
git_reference *write1, *write2, *write3;
git_oid oid1, oid2, oid3;
size_t i = 0;
cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, foreach_test, &i));
cl_assert(i == 3);
git_reference_free(write1);
git_reference_free(write2);
git_reference_free(write3);
}
int delete_test(const char *ref_name, void *payload)
{
git_reference *ref;
git_oid expected;
int *i = payload;
cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));
cl_assert(git_oid_cmp(&expected, &ref->target.oid) == 0);
++(*i);
git_reference_free(ref);
return 0;
}
void test_refdb_inmemory__delete(void)
{
git_reference *write1, *write2, *write3;
git_oid oid1, oid2, oid3;
size_t i = 0;
cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
git_reference_delete(write1);
git_reference_free(write1);
git_reference_delete(write3);
git_reference_free(write3);
cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i));
cl_assert(i == 1);
git_reference_free(write2);
}
#include "common.h"
#include "vector.h"
#include "util.h"
#include <git2/refdb.h>
#include <git2/refdb_backend.h>
#include <git2/errors.h>
#include <git2/repository.h>
typedef struct refdb_test_backend {
git_refdb_backend parent;
git_repository *repo;
git_refdb *refdb;
git_vector refs;
} refdb_test_backend;
typedef struct refdb_test_entry {
char *name;
git_ref_t type;
union {
git_oid oid;
char *symbolic;
} target;
} refdb_test_entry;
static int ref_name_cmp(const void *a, const void *b)
{
return strcmp(git_reference_name((git_reference *)a),
git_reference_name((git_reference *)b));
}
static int refdb_test_backend__exists(
int *exists,
git_refdb_backend *_backend,
const char *ref_name)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
size_t i;
assert(_backend);
backend = (refdb_test_backend *)_backend;
*exists = 0;
git_vector_foreach(&backend->refs, i, entry) {
if (strcmp(entry->name, ref_name) == 0) {
*exists = 1;
break;
}
}
return 0;
}
static int refdb_test_backend__write(
git_refdb_backend *_backend,
const git_reference *ref)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
assert(_backend);
backend = (refdb_test_backend *)_backend;
entry = git__calloc(1, sizeof(refdb_test_entry));
GITERR_CHECK_ALLOC(entry);
entry->name = git__strdup(git_reference_name(ref));
GITERR_CHECK_ALLOC(entry->name);
entry->type = git_reference_type(ref);
if (entry->type == GIT_REF_OID)
git_oid_cpy(&entry->target.oid, git_reference_target(ref));
else {
entry->target.symbolic = git__strdup(git_reference_symbolic_target(ref));
GITERR_CHECK_ALLOC(entry->target.symbolic);
}
git_vector_insert(&backend->refs, entry);
return 0;
}
static int refdb_test_backend__lookup(
git_reference **out,
git_refdb_backend *_backend,
const char *ref_name)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
size_t i;
assert(_backend);
backend = (refdb_test_backend *)_backend;
git_vector_foreach(&backend->refs, i, entry) {
if (strcmp(entry->name, ref_name) == 0) {
const git_oid *oid =
entry->type == GIT_REF_OID ? &entry->target.oid : NULL;
const char *symbolic =
entry->type == GIT_REF_SYMBOLIC ? entry->target.symbolic : NULL;
if ((*out = git_reference__alloc(backend->refdb, ref_name, oid, symbolic)) == NULL)
return -1;
return 0;
}
}
return GIT_ENOTFOUND;
}
static int refdb_test_backend__foreach(
git_refdb_backend *_backend,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
size_t i;
assert(_backend);
backend = (refdb_test_backend *)_backend;
git_vector_foreach(&backend->refs, i, entry) {
if (entry->type == GIT_REF_OID && (list_flags & GIT_REF_OID) == 0)
continue;
if (entry->type == GIT_REF_SYMBOLIC && (list_flags & GIT_REF_SYMBOLIC) == 0)
continue;
if (callback(entry->name, payload) != 0)
return GIT_EUSER;
}
return 0;
}
static void refdb_test_entry_free(refdb_test_entry *entry)
{
if (entry->type == GIT_REF_SYMBOLIC)
git__free(entry->target.symbolic);
git__free(entry->name);
git__free(entry);
}
static int refdb_test_backend__delete(
git_refdb_backend *_backend,
const git_reference *ref)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
size_t i;
assert(_backend);
backend = (refdb_test_backend *)_backend;
git_vector_foreach(&backend->refs, i, entry) {
if (strcmp(entry->name, git_reference_name(ref)) == 0) {
git_vector_remove(&backend->refs, i);
refdb_test_entry_free(entry);
}
}
return GIT_ENOTFOUND;
}
static void refdb_test_backend__free(git_refdb_backend *_backend)
{
refdb_test_backend *backend;
refdb_test_entry *entry;
size_t i;
assert(_backend);
backend = (refdb_test_backend *)_backend;
git_vector_foreach(&backend->refs, i, entry)
refdb_test_entry_free(entry);
git_vector_free(&backend->refs);
git__free(backend);
}
int refdb_backend_test(
git_refdb_backend **backend_out,
git_repository *repo)
{
refdb_test_backend *backend;
git_refdb *refdb;
int error = 0;
if ((error = git_repository_refdb(&refdb, repo)) < 0)
return error;
backend = git__calloc(1, sizeof(refdb_test_backend));
GITERR_CHECK_ALLOC(backend);
git_vector_init(&backend->refs, 0, ref_name_cmp);
backend->repo = repo;
backend->refdb = refdb;
backend->parent.exists = &refdb_test_backend__exists;
backend->parent.lookup = &refdb_test_backend__lookup;
backend->parent.foreach = &refdb_test_backend__foreach;
backend->parent.write = &refdb_test_backend__write;
backend->parent.delete = &refdb_test_backend__delete;
backend->parent.free = &refdb_test_backend__free;
*backend_out = (git_refdb_backend *)backend;
return 0;
}
int refdb_backend_test(
git_refdb_backend **backend_out,
git_repository *repo);
......@@ -50,9 +50,11 @@ void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
......@@ -63,6 +65,7 @@ void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
......@@ -79,6 +82,7 @@ void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_local_branch(void)
......@@ -86,6 +90,7 @@ void test_refs_branches_delete__can_delete_a_local_branch(void)
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_remote_branch(void)
......@@ -93,6 +98,7 @@ void test_refs_branches_delete__can_delete_a_remote_branch(void)
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
......@@ -104,6 +110,7 @@ void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
......
......@@ -3,20 +3,14 @@
#include "config/config_helpers.h"
static git_repository *repo;
static git_reference *ref;
void test_refs_branches_move__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/br2"));
}
void test_refs_branches_move__cleanup(void)
{
git_reference_free(ref);
ref = NULL;
cl_git_sandbox_cleanup();
}
......@@ -24,56 +18,99 @@ void test_refs_branches_move__cleanup(void)
void test_refs_branches_move__can_move_a_local_branch(void)
{
cl_git_pass(git_branch_move(ref, NEW_BRANCH_NAME, 0));
cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(ref));
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(new_ref));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(ref, "somewhere/" NEW_BRANCH_NAME, 0));
cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(ref, "br2", 0));
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(ref, "br2/" NEW_BRANCH_NAME, 0));
cl_git_pass(git_branch_move(&new_ref, original_ref, "br2/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(ref, "br2", 0));
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
cl_assert_equal_i(GIT_EEXISTS, git_branch_move(ref, "master", 0));
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0));
git_reference_free(original_ref);
}
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(ref, "Inv@{id", 0));
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
git_reference_free(original_ref);
}
void test_refs_branches_move__can_not_move_a_non_branch(void)
{
git_reference *tag;
git_reference *tag, *new_ref;
cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_move(tag, NEW_BRANCH_NAME, 0));
cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
git_reference_free(tag);
}
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
{
cl_git_pass(git_branch_move(ref, "master", 1));
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
......@@ -82,23 +119,26 @@ void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(v
assert_config_entry_existence(repo, "branch.moved.remote", false);
assert_config_entry_existence(repo, "branch.moved.merge", false);
cl_git_pass(git_branch_move(branch, "moved", 0));
cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
assert_config_entry_existence(repo, "branch.moved.remote", true);
assert_config_entry_existence(repo, "branch.moved.merge", true);
git_reference_free(branch);
git_reference_free(new_branch);
}
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_move(branch, "master2", 0));
cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
git_reference_free(branch);
git_reference_free(new_branch);
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
......
......@@ -10,8 +10,11 @@ void test_refs_crashes__double_free(void)
cl_git_pass(git_reference_symbolic_create(&ref, repo, REFNAME, "refs/heads/master", 0));
cl_git_pass(git_reference_lookup(&ref2, repo, REFNAME));
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
/* reference is gone from disk, so reloading it will fail */
cl_git_fail(git_reference_reload(ref2));
cl_git_fail(git_reference_lookup(&ref2, repo, REFNAME));
git_repository_free(repo);
}
......@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
static const char *current_head_target = "refs/heads/master";
......@@ -36,7 +37,7 @@ void test_refs_create__symbolic(void)
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
/* ...peeled.. */
......@@ -99,7 +100,7 @@ void test_refs_create__oid(void)
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID);
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head);
/* ...and that it points to the current master tip */
......
......@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
......@@ -37,10 +38,11 @@ void test_refs_delete__packed_loose(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's the loose version that has been found */
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
cl_assert(reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is deleted... */
cl_git_pass(git_reference_delete(looked_up_ref));
git_reference_free(looked_up_ref);
/* Looking up the reference once again should not retrieve it */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
......@@ -56,6 +58,7 @@ void test_refs_delete__packed_only(void)
{
// can delete a just packed reference
git_reference *ref;
git_refdb *refdb;
git_oid id;
const char *new_ref = "refs/heads/new_ref";
......@@ -69,17 +72,20 @@ void test_refs_delete__packed_only(void)
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a loose reference */
cl_assert(git_reference_is_packed(ref) == 0);
cl_assert(reference_is_packed(ref) == 0);
/* Pack all existing references */
cl_git_pass(git_reference_packall(g_repo));
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
/* Reload the reference from disk */
cl_git_pass(git_reference_reload(ref));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a packed reference */
cl_assert(git_reference_is_packed(ref) == 1);
cl_assert(reference_is_packed(ref) == 1);
/* This should pass */
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
}
......@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
......@@ -18,6 +19,14 @@ void test_refs_pack__cleanup(void)
cl_git_sandbox_cleanup();
}
void packall()
{
git_refdb *refdb;
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
}
void test_refs_pack__empty(void)
{
// create a packfile for an empty folder
......@@ -27,7 +36,7 @@ void test_refs_pack__empty(void)
cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
git_buf_free(&temp_path);
cl_git_pass(git_reference_packall(g_repo));
packall();
}
void test_refs_pack__loose(void)
......@@ -38,7 +47,7 @@ void test_refs_pack__loose(void)
/* Ensure a known loose ref can be looked up */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
git_reference_free(reference);
......@@ -47,7 +56,7 @@ void test_refs_pack__loose(void)
* called `points_to_blob`, to make sure we can properly
* pack weak tags
*/
cl_git_pass(git_reference_packall(g_repo));
packall();
/* Ensure the packed-refs file exists */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE));
......@@ -55,7 +64,7 @@ void test_refs_pack__loose(void)
/* Ensure the known ref can still be looked up but is now packed */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_is_packed(reference));
cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, loose_tag_ref_name);
/* Ensure the known ref has been removed from the loose folder structure */
......
......@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
......@@ -34,7 +35,7 @@ void test_refs_read__loose_tag(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
......@@ -71,7 +72,7 @@ void test_refs_read__symbolic(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, GIT_HEAD_FILE);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
......@@ -99,7 +100,7 @@ void test_refs_read__nested_symbolic(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, head_tracker_sym_ref_name);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
......@@ -167,7 +168,7 @@ void test_refs_read__packed(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference));
cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, packed_head_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
......@@ -188,7 +189,7 @@ void test_refs_read__loose_first(void)
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, packed_test_head_name);
git_reference_free(reference);
......
#include "git2/repository.h"
#include "git2/refs.h"
#include "common.h"
#include "util.h"
#include "buffer.h"
#include "path.h"
int reference_is_packed(git_reference *ref)
{
git_buf ref_path = GIT_BUF_INIT;
int packed;
assert(ref);
if (git_buf_joinpath(&ref_path,
git_repository_path(git_reference_owner(ref)),
git_reference_name(ref)) < 0)
return -1;
packed = !git_path_isfile(ref_path.ptr);
git_buf_free(&ref_path);
return packed;
}
int reference_is_packed(git_reference *ref);
......@@ -90,7 +90,7 @@ void test_refs_reflog_reflog__append_then_read(void)
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
{
git_reference *master;
git_reference *master, *new_master;
git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
......@@ -102,12 +102,13 @@ void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path)));
cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_rename(master, "refs/moved", 0));
cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
git_reference_free(master);
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path)));
git_reference_free(master);
git_reference_free(new_master);
git_buf_free(&moved_log_path);
git_buf_free(&master_log_path);
}
......@@ -152,7 +153,7 @@ void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_re
void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
{
git_reference *master;
git_reference *master, *new_master;
git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
git_reflog *reflog;
......@@ -161,12 +162,13 @@ void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
cl_git_pass(git_reflog_write(reflog));
cl_git_pass(git_reference_rename(master, "refs/moved", 0));
cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
git_reference_free(master);
cl_git_fail(git_reflog_write(reflog));
git_reflog_free(reflog);
git_reference_free(master);
git_reference_free(new_master);
git_buf_free(&moved_log_path);
git_buf_free(&master_log_path);
}
......
......@@ -227,7 +227,7 @@ void test_refs_revparse__previous_head(void)
static void create_fake_stash_reference_and_reflog(git_repository *repo)
{
git_reference *master;
git_reference *master, *new_master;
git_buf log_path = GIT_BUF_INIT;
git_buf_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
......@@ -235,12 +235,13 @@ static void create_fake_stash_reference_and_reflog(git_repository *repo)
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&log_path)));
cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
cl_git_pass(git_reference_rename(master, "refs/fakestash", 0));
cl_git_pass(git_reference_rename(&new_master, master, "refs/fakestash", 0));
git_reference_free(master);
cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path)));
git_buf_free(&log_path);
git_reference_free(master);
git_reference_free(new_master);
}
void test_refs_revparse__reflog_of_a_ref_under_refs(void)
......
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "git2/refs.h"
static const char *ref_name = "refs/heads/other";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_test_name = "refs/heads/test";
static git_repository *g_repo;
void test_refs_setter__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_setter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_setter__update_direct(void)
{
git_reference *ref, *test_ref, *new_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REF_OID);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
cl_assert(git_reference_type(test_ref) == GIT_REF_OID);
cl_git_pass(git_reference_set_target(&new_ref, test_ref, &id));
git_reference_free(test_ref);
git_reference_free(new_ref);
cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
cl_assert(git_reference_type(test_ref) == GIT_REF_OID);
cl_assert(git_oid_cmp(&id, git_reference_target(test_ref)) == 0);
git_reference_free(test_ref);
}
void test_refs_setter__update_symbolic(void)
{
git_reference *head, *new_head;
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
cl_assert(strcmp(git_reference_symbolic_target(head), ref_master_name) == 0);
cl_git_pass(git_reference_symbolic_set_target(&new_head, head, ref_test_name));
git_reference_free(new_head);
git_reference_free(head);
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
cl_assert(strcmp(git_reference_symbolic_target(head), ref_test_name) == 0);
git_reference_free(head);
}
void test_refs_setter__cant_update_direct_with_symbolic(void)
{
// Overwrite an existing object id reference with a symbolic one
git_reference *ref, *new;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REF_OID);
git_oid_cpy(&id, git_reference_target(ref));
cl_git_fail(git_reference_symbolic_set_target(&new, ref, ref_name));
git_reference_free(ref);
}
void test_refs_setter__cant_update_symbolic_with_direct(void)
{
// Overwrite an existing symbolic reference with an object id one
git_reference *ref, *new;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REF_OID);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
/* Create the symbolic ref */
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0));
/* Can't set an OID on a direct ref */
cl_git_fail(git_reference_set_target(&new, ref, &id));
git_reference_free(ref);
}
......@@ -19,11 +19,8 @@ void test_refs_update__updating_the_target_of_a_symref_with_an_invalid_name_retu
git_reference *head;
cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_set_target(
head, "refs/heads/inv@{id"));
git_reference_free(head);
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(&head, g_repo, GIT_HEAD_FILE, "refs/heads/inv@{id", 1));
}
......@@ -193,8 +193,7 @@ void test_stash_save__cannot_stash_against_an_unborn_branch(void)
{
git_reference *head;
cl_git_pass(git_reference_lookup(&head, repo, "HEAD"));
cl_git_pass(git_reference_symbolic_set_target(head, "refs/heads/unborn"));
cl_git_pass(git_reference_symbolic_create(&head, repo, "HEAD", "refs/heads/unborn", 1));
cl_assert_equal_i(GIT_EORPHANEDHEAD,
git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment