Merge pull request #472 from libgit2/new-references

References! References! References!
This commit is contained in:
Vicent Martí 2011-11-07 04:38:12 -08:00
commit f8e8c8d44d
12 changed files with 953 additions and 766 deletions

View File

@ -23,8 +23,7 @@ GIT_BEGIN_DECL
/**
* Lookup a reference by its name in a repository.
*
* The generated reference is owned by the repository and
* should not be freed by the user.
* The generated reference must be freed by the user.
*
* @param reference_out pointer to the looked-up reference
* @param repo the repository to look up the reference
@ -39,8 +38,7 @@ GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_reposito
* The reference will be created in the repository and written
* to the disk.
*
* This reference is owned by the repository and shall not
* be free'd by the user.
* The generated reference must be freed by the user.
*
* If `force` is true and there already exists a reference
* with the same name, it will be overwritten.
@ -60,8 +58,7 @@ GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repos
* The reference will be created in the repository and written
* to the disk.
*
* This reference is owned by the repository and shall not
* be free'd by the user.
* The generated reference must be freed by the user.
*
* If `force` is true and there already exists a reference
* with the same name, it will be overwritten.
@ -119,8 +116,13 @@ GIT_EXTERN(const char *) git_reference_name(git_reference *ref);
* Thie method iteratively peels a symbolic reference
* until it resolves to a direct reference to an OID.
*
* The peeled reference is returned in the `resolved_ref`
* argument, and must be freed manually once it's no longer
* needed.
*
* If a direct reference is passed as an argument,
* that reference is returned immediately
* a copy of that reference is returned. This copy must
* be manually freed too.
*
* @param resolved_ref Pointer to the peeled reference
* @param ref The reference
@ -173,9 +175,19 @@ GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
* The new name will be checked for validity and may be
* modified into a normalized form.
*
* The refernece will be immediately renamed in-memory
* The given git_reference will be updated in place.
*
* The reference will be immediately renamed in-memory
* and on disk.
*
* If the `force` flag is not enabled, and there's already
* a reference with the given name, the renaming will fail.
*
* @param ref The reference to rename
* @param new_name The new name for the reference
* @param force Overwrite an existing reference
* @return GIT_SUCCESS or an error code
*
*/
GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name, int force);
@ -187,6 +199,8 @@ GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name, i
* The reference will be immediately removed on disk and from
* memory. The given reference pointer will no longer be valid.
*
* @param ref The reference to remove
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_reference_delete(git_reference *ref);
@ -200,9 +214,6 @@ GIT_EXTERN(int) git_reference_delete(git_reference *ref);
* Once the `packed-refs` file has been written properly,
* the loose references will be removed from disk.
*
* WARNING: calling this method may invalidate any existing
* references previously loaded on the cache.
*
* @param repo Repository where the loose refs will be packed
* @return GIT_SUCCESS or an error code
*/
@ -253,6 +264,41 @@ GIT_EXTERN(int) git_reference_listall(git_strarray *array, git_repository *repo,
*/
GIT_EXTERN(int) git_reference_foreach(git_repository *repo, unsigned int list_flags, int (*callback)(const char *, void *), 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 may become outdated if the Git
* repository is accessed simultaneously by other clients
* whilt the library is open.
*
* This method forces a reload of the reference from disk,
* to ensure that the provided information is still
* reliable.
*
* If the reload fails (e.g. the reference no longer exists
* on disk, or has become corrupted), an error code will be
* returned and the reference pointer will be invalidated.
*
* @param ref The reference to reload
* @return GIT_SUCCESS on success, or an error code
*/
GIT_EXTERN(int) git_reference_reload(git_reference *ref);
/**
* Free the given reference
*
* @param ref git_reference
*/
GIT_EXTERN(void) git_reference_free(git_reference *ref);
/** @} */
GIT_END_DECL
#endif

View File

@ -137,15 +137,18 @@ int git_commit_create(
if (error == GIT_SUCCESS && update_ref != NULL) {
git_reference *head;
git_reference *target;
error = git_reference_lookup(&head, repo, update_ref);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to create commit");
error = git_reference_resolve(&head, head);
error = git_reference_resolve(&target, head);
if (error < GIT_SUCCESS) {
if (error != GIT_ENOTFOUND)
if (error != GIT_ENOTFOUND) {
git_reference_free(head);
return git__rethrow(error, "Failed to create commit");
}
/*
* The target of the reference was not found. This can happen
* just after a repository has been initialized (the master
@ -153,10 +156,19 @@ int git_commit_create(
* point to) or after an orphan checkout, so if the target
* branch doesn't exist yet, create it and return.
*/
return git_reference_create_oid(&head, repo, git_reference_target(head), oid, 1);
error = git_reference_create_oid(&target, repo, git_reference_target(head), oid, 1);
git_reference_free(head);
if (error == GIT_SUCCESS)
git_reference_free(target);
return error;
}
error = git_reference_set_oid(head, oid);
error = git_reference_set_oid(target, oid);
git_reference_free(head);
git_reference_free(target);
}
if (error < GIT_SUCCESS)

View File

@ -215,23 +215,37 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
const git_oid *oid;
if ((error = git_reference_resolve(&r, ref)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to write reflog. Cannot resolve reference `%s`", ref->name);
return git__rethrow(error,
"Failed to write reflog. Cannot resolve reference `%s`", ref->name);
oid = git_reference_oid(r);
if (oid == NULL)
return git__throw(GIT_ERROR, "Failed to write reflog. Cannot resolve reference `%s`", r->name);
if (oid == NULL) {
git_reference_free(r);
return git__throw(GIT_ERROR,
"Failed to write reflog. Cannot resolve reference `%s`", r->name);
}
git_oid_to_string(new, GIT_OID_HEXSZ+1, oid);
git_path_join_n(log_path, 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
git_path_join_n(log_path, 3,
ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
git_reference_free(r);
if (git_futils_exists(log_path)) {
if ((error = git_futils_mkpath2file(log_path, GIT_REFLOG_DIR_MODE)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to write reflog. Cannot create reflog directory");
error = git_futils_mkpath2file(log_path, GIT_REFLOG_DIR_MODE);
if (error < GIT_SUCCESS)
return git__rethrow(error,
"Failed to write reflog. Cannot create reflog directory");
} else if (git_futils_isfile(log_path)) {
return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path);
} else if (oid_old == NULL)
return git__throw(GIT_ERROR, "Failed to write reflog. Old OID cannot be NULL for existing reference");
return git__throw(GIT_ERROR,
"Failed to write reflog. `%s` is directory", log_path);
} else if (oid_old == NULL) {
return git__throw(GIT_ERROR,
"Failed to write reflog. Old OID cannot be NULL for existing reference");
}
if (oid_old)
git_oid_to_string(old, GIT_OID_HEXSZ+1, oid_old);

1308
src/refs.c

File diff suppressed because it is too large Load Diff

View File

@ -33,21 +33,23 @@
#define GIT_REFNAME_MAX 1024
struct git_reference {
unsigned int flags;
git_repository *owner;
char *name;
unsigned int type;
time_t mtime;
union {
git_oid oid;
char *symbolic;
} target;
};
typedef struct {
git_hashtable *packfile;
git_hashtable *loose_cache;
time_t packfile_time;
} git_refcache;
void git_repository__refcache_free(git_refcache *refs);
int git_repository__refcache_init(git_refcache *refs);
int git_reference__normalize_name(char *buffer_out, size_t out_size, const char *name);
int git_reference__normalize_name_oid(char *buffer_out, size_t out_size, const char *name);

View File

@ -172,11 +172,6 @@ static git_repository *repository_alloc(void)
return NULL;
}
if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
git__free(repo);
return NULL;
}
return repo;
}
@ -603,8 +598,14 @@ static int repo_init_reinit(const char *repository_path, int is_bare)
static int repo_init_createhead(git_repository *repo)
{
int error;
git_reference *head_reference;
return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE, 0);
error = git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE, 0);
git_reference_free(head_reference);
return error;
}
static int repo_init_structure(const char *git_dir, int is_bare)
@ -715,10 +716,15 @@ int git_repository_head_detached(git_repository *repo)
if (error < GIT_SUCCESS)
return error;
if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
git_reference_free(ref);
return 0;
}
error = git_odb_read_header(&_size, &type, repo->db, git_reference_oid(ref));
git_reference_free(ref);
if (error < GIT_SUCCESS)
return error;
@ -730,7 +736,7 @@ int git_repository_head_detached(git_repository *repo)
int git_repository_head(git_reference **head_out, git_repository *repo)
{
git_reference *ref;
git_reference *ref, *resolved_ref;
int error;
*head_out = NULL;
@ -739,11 +745,15 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
if (error < GIT_SUCCESS)
return git__rethrow(GIT_ENOTAREPO, "Failed to locate the HEAD");
error = git_reference_resolve(&ref, ref);
if (error < GIT_SUCCESS)
error = git_reference_resolve(&resolved_ref, ref);
if (error < GIT_SUCCESS) {
git_reference_free(ref);
return git__rethrow(error, "Failed to resolve the HEAD");
}
*head_out = ref;
git_reference_free(ref);
*head_out = resolved_ref;
return GIT_SUCCESS;
}
@ -754,25 +764,36 @@ int git_repository_head_orphan(git_repository *repo)
error = git_repository_head(&ref, repo);
if (error == GIT_SUCCESS)
git_reference_free(ref);
return error == GIT_ENOTFOUND ? 1 : error;
}
int git_repository_is_empty(git_repository *repo)
{
git_reference *head, *branch;
git_reference *head = NULL, *branch = NULL;
int error;
error = git_reference_lookup(&head, repo, "HEAD");
if (error < GIT_SUCCESS)
return git__throw(error, "Corrupted repository. HEAD does not exist");
if (git_reference_type(head) != GIT_REF_SYMBOLIC)
if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
git_reference_free(head);
return 0;
}
if (strcmp(git_reference_target(head), "refs/heads/master") != 0)
if (strcmp(git_reference_target(head), "refs/heads/master") != 0) {
git_reference_free(head);
return 0;
}
error = git_reference_resolve(&branch, head);
git_reference_free(head);
git_reference_free(branch);
return error == GIT_ENOTFOUND ? 1 : error;
}

View File

@ -153,6 +153,8 @@ static int retrieve_tag_reference(git_reference **tag_reference_out, char *ref_n
git_reference *tag_ref;
int error;
*tag_reference_out = NULL;
git_path_join(ref_name_out, GIT_REFS_TAGS_DIR, tag_name);
error = git_reference_lookup(&tag_ref, repo, ref_name_out);
if (error < GIT_SUCCESS)
@ -224,6 +226,7 @@ static int git_tag_create__internal(
break;
default:
git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
@ -232,6 +235,7 @@ static int git_tag_create__internal(
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
git_reference_free(new_ref);
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
@ -239,8 +243,10 @@ static int git_tag_create__internal(
}
if (create_tag_annotation) {
if ((error = write_tag_annotation(oid, repo, tag_name, target, tagger, message)) < GIT_SUCCESS)
if ((error = write_tag_annotation(oid, repo, tag_name, target, tagger, message)) < GIT_SUCCESS) {
git_reference_free(new_ref);
return error;
}
} else
git_oid_cpy(oid, git_object_id(target));
@ -249,6 +255,8 @@ static int git_tag_create__internal(
else
error = git_reference_set_oid(new_ref, oid);
git_reference_free(new_ref);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create tag");
}
@ -281,7 +289,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
git_odb_stream *stream;
git_odb_object *target_obj;
git_reference *new_ref;
git_reference *new_ref = NULL;
char ref_name[GIT_REFNAME_MAX];
assert(oid && buffer);
@ -309,6 +317,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
break;
default:
git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
@ -317,6 +326,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
git_reference_free(new_ref);
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
@ -324,22 +334,28 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
}
/* write the buffer */
if ((error = git_odb_open_wstream(&stream, repo->db, strlen(buffer), GIT_OBJ_TAG)) < GIT_SUCCESS)
if ((error = git_odb_open_wstream(&stream, repo->db, strlen(buffer), GIT_OBJ_TAG)) < GIT_SUCCESS) {
git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
stream->write(stream, buffer, strlen(buffer));
error = stream->finalize_write(oid, stream);
stream->free(stream);
if (error < GIT_SUCCESS)
if (error < GIT_SUCCESS) {
git_reference_free(new_ref);
return git__rethrow(error, "Failed to create tag");
}
if (!should_update_ref)
error = git_reference_create_oid(&new_ref, repo, ref_name, oid, 0);
else
error = git_reference_set_oid(new_ref, oid);
git_reference_free(new_ref);
git_signature_free(tag.tagger);
git__free(tag.tag_name);
git__free(tag.message);

View File

@ -54,7 +54,7 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
{
const char peeled[] = "^{}";
git_remote_head *head;
git_reference *ref;
git_reference *ref, *resolved_ref;
git_object *obj = NULL;
int error = GIT_SUCCESS, peel_len, ret;
@ -72,7 +72,7 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
if (error < GIT_SUCCESS)
goto out;
error = git_reference_resolve(&ref, ref);
error = git_reference_resolve(&resolved_ref, ref);
if (error < GIT_SUCCESS)
goto out;
@ -111,6 +111,9 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
goto out;
out:
git_reference_free(ref);
git_reference_free(resolved_ref);
git_object_close(obj);
if (error < GIT_SUCCESS) {
git__free(head->name);

View File

@ -766,6 +766,8 @@ BEGIN_TEST(root0, "create a root commit")
git__free(head_old);
git_commit_close(commit);
git_repository_free(repo);
git_reference_free(head);
END_TEST
BEGIN_SUITE(commit)

View File

@ -197,7 +197,6 @@ BEGIN_TEST(write0, "write a tag to the repository and read it again")
git_tag_close(tag);
git_repository_free(repo);
END_TEST
BEGIN_TEST(write2, "Attempt to write a tag bearing the same name than an already existing tag")
@ -266,6 +265,7 @@ BEGIN_TEST(write3, "Replace an already existing tag")
close_temp_repo(repo);
git_reference_free(ref_tag);
END_TEST
BEGIN_TEST(write4, "write a lightweight tag to the repository and read it again")
@ -296,6 +296,8 @@ BEGIN_TEST(write4, "write a lightweight tag to the repository and read it again"
must_pass(git_tag_delete(repo, "light-tag"));
git_repository_free(repo);
git_reference_free(ref_tag);
END_TEST
BEGIN_TEST(write5, "Attempt to write a lightweight tag bearing the same name than an already existing tag")
@ -334,6 +336,8 @@ BEGIN_TEST(delete0, "Delete an already existing tag")
must_fail(git_reference_lookup(&ref_tag, repo, "refs/tags/e90810b"));
close_temp_repo(repo);
git_reference_free(ref_tag);
END_TEST
BEGIN_SUITE(tag)

View File

@ -42,8 +42,8 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
must_be_true(reference->type & GIT_REF_OID);
must_be_true((reference->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(reference) & GIT_REF_OID);
must_be_true(git_reference_is_packed(reference) == 0);
must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
must_pass(git_object_lookup(&object, repo, git_reference_oid(reference), GIT_OBJ_ANY));
@ -56,6 +56,8 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
git_object_close(object);
git_repository_free(repo);
git_reference_free(reference);
END_TEST
BEGIN_TEST(readtag1, "lookup a loose tag reference that doesn't exist")
@ -66,6 +68,8 @@ BEGIN_TEST(readtag1, "lookup a loose tag reference that doesn't exist")
must_fail(git_reference_lookup(&reference, repo, non_existing_tag_ref_name));
git_repository_free(repo);
git_reference_free(reference);
END_TEST
static const char *head_tracker_sym_ref_name = "head-tracker";
@ -81,12 +85,12 @@ BEGIN_TEST(readsym0, "lookup a symbolic reference")
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, GIT_HEAD_FILE));
must_be_true(reference->type & GIT_REF_SYMBOLIC);
must_be_true((reference->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(reference) & GIT_REF_SYMBOLIC);
must_be_true(git_reference_is_packed(reference) == 0);
must_be_true(strcmp(reference->name, GIT_HEAD_FILE) == 0);
must_pass(git_reference_resolve(&resolved_ref, reference));
must_be_true(resolved_ref->type == GIT_REF_OID);
must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
must_pass(git_object_lookup(&object, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
must_be_true(object != NULL);
@ -97,6 +101,9 @@ BEGIN_TEST(readsym0, "lookup a symbolic reference")
git_object_close(object);
git_repository_free(repo);
git_reference_free(reference);
git_reference_free(resolved_ref);
END_TEST
BEGIN_TEST(readsym1, "lookup a nested symbolic reference")
@ -108,12 +115,12 @@ BEGIN_TEST(readsym1, "lookup a nested symbolic reference")
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, head_tracker_sym_ref_name));
must_be_true(reference->type & GIT_REF_SYMBOLIC);
must_be_true((reference->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(reference) & GIT_REF_SYMBOLIC);
must_be_true(git_reference_is_packed(reference) == 0);
must_be_true(strcmp(reference->name, head_tracker_sym_ref_name) == 0);
must_pass(git_reference_resolve(&resolved_ref, reference));
must_be_true(resolved_ref->type == GIT_REF_OID);
must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
must_pass(git_object_lookup(&object, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
must_be_true(object != NULL);
@ -124,6 +131,9 @@ BEGIN_TEST(readsym1, "lookup a nested symbolic reference")
git_object_close(object);
git_repository_free(repo);
git_reference_free(reference);
git_reference_free(resolved_ref);
END_TEST
BEGIN_TEST(readsym2, "lookup the HEAD and resolve the master branch")
@ -133,17 +143,22 @@ BEGIN_TEST(readsym2, "lookup the HEAD and resolve the master branch")
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, head_tracker_sym_ref_name));
must_pass(git_reference_resolve(&resolved_ref, reference));
comp_base_ref = resolved_ref;
must_pass(git_reference_resolve(&comp_base_ref, reference));
git_reference_free(reference);
must_pass(git_reference_lookup(&reference, repo, GIT_HEAD_FILE));
must_pass(git_reference_resolve(&resolved_ref, reference));
must_pass(git_oid_cmp(git_reference_oid(comp_base_ref), git_reference_oid(resolved_ref)));
git_reference_free(reference);
git_reference_free(resolved_ref);
must_pass(git_reference_lookup(&reference, repo, current_head_target));
must_pass(git_reference_resolve(&resolved_ref, reference));
must_pass(git_oid_cmp(git_reference_oid(comp_base_ref), git_reference_oid(resolved_ref)));
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(comp_base_ref);
git_repository_free(repo);
END_TEST
@ -160,6 +175,10 @@ BEGIN_TEST(readsym3, "lookup the master branch and then the HEAD")
must_pass(git_oid_cmp(git_reference_oid(master_ref), git_reference_oid(resolved_ref)));
git_repository_free(repo);
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(master_ref);
END_TEST
static const char *packed_head_name = "refs/heads/packed";
@ -173,8 +192,8 @@ BEGIN_TEST(readpacked0, "lookup a packed reference")
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, packed_head_name));
must_be_true(reference->type & GIT_REF_OID);
must_be_true((reference->type & GIT_REF_PACKED) != 0);
must_be_true(git_reference_type(reference) & GIT_REF_OID);
must_be_true(git_reference_is_packed(reference));
must_be_true(strcmp(reference->name, packed_head_name) == 0);
must_pass(git_object_lookup(&object, repo, git_reference_oid(reference), GIT_OBJ_ANY));
@ -183,6 +202,8 @@ BEGIN_TEST(readpacked0, "lookup a packed reference")
git_object_close(object);
git_repository_free(repo);
git_reference_free(reference);
END_TEST
BEGIN_TEST(readpacked1, "assure that a loose reference is looked up before a packed reference")
@ -192,11 +213,13 @@ BEGIN_TEST(readpacked1, "assure that a loose reference is looked up before a pac
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&reference, repo, packed_head_name));
must_pass(git_reference_lookup(&reference, repo, packed_test_head_name));
must_be_true(reference->type & GIT_REF_OID);
must_be_true((reference->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(reference) & GIT_REF_OID);
must_be_true(git_reference_is_packed(reference) == 0);
must_be_true(strcmp(reference->name, packed_test_head_name) == 0);
git_repository_free(repo);
git_reference_free(reference);
END_TEST
BEGIN_TEST(create0, "create a new symbolic reference")
@ -219,13 +242,13 @@ BEGIN_TEST(create0, "create a new symbolic reference")
/* Ensure the reference can be looked-up... */
must_pass(git_reference_lookup(&looked_up_ref, repo, new_head_tracker));
must_be_true(looked_up_ref->type & GIT_REF_SYMBOLIC);
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
must_be_true(strcmp(looked_up_ref->name, new_head_tracker) == 0);
/* ...peeled.. */
must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
must_be_true(resolved_ref->type == GIT_REF_OID);
must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
/* ...and that it points to the current master tip */
must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
@ -240,6 +263,10 @@ BEGIN_TEST(create0, "create a new symbolic reference")
must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
close_temp_repo(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
END_TEST
BEGIN_TEST(create1, "create a deep symbolic reference")
@ -261,6 +288,10 @@ BEGIN_TEST(create1, "create a deep symbolic reference")
must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
close_temp_repo(repo);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
END_TEST
BEGIN_TEST(create2, "create a new OID reference")
@ -283,8 +314,8 @@ BEGIN_TEST(create2, "create a new OID reference")
/* Ensure the reference can be looked-up... */
must_pass(git_reference_lookup(&looked_up_ref, repo, new_head));
must_be_true(looked_up_ref->type & GIT_REF_OID);
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_type(looked_up_ref) & GIT_REF_OID);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
must_be_true(strcmp(looked_up_ref->name, new_head) == 0);
/* ...and that it points to the current master tip */
@ -299,6 +330,9 @@ BEGIN_TEST(create2, "create a new OID reference")
must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
close_temp_repo(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
END_TEST
BEGIN_TEST(create3, "Can not create a new OID reference which targets at an unknown id")
@ -349,6 +383,9 @@ BEGIN_TEST(overwrite0, "Overwrite an existing symbolic reference")
must_be_true(!strcmp(git_reference_target(ref), ref_master_name));
close_temp_repo(repo);
git_reference_free(ref);
git_reference_free(branch_ref);
END_TEST
BEGIN_TEST(overwrite1, "Overwrite an existing object id reference")
@ -359,14 +396,14 @@ BEGIN_TEST(overwrite1, "Overwrite an existing object id reference")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
/* Create it */
must_pass(git_reference_create_oid(&ref, repo, ref_name, &id, 0));
must_pass(git_reference_lookup(&ref, repo, ref_test_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
/* Ensure we can't overwrite unless we force it */
@ -378,6 +415,8 @@ BEGIN_TEST(overwrite1, "Overwrite an existing object id reference")
must_be_true(!git_oid_cmp(&id, git_reference_oid(ref)));
close_temp_repo(repo);
git_reference_free(ref);
END_TEST
BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symbolic one")
@ -388,7 +427,7 @@ BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symboli
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
must_pass(git_reference_create_oid(&ref, repo, ref_name, &id, 0));
@ -401,6 +440,8 @@ BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symboli
must_be_true(!strcmp(git_reference_target(ref), ref_master_name));
close_temp_repo(repo);
git_reference_free(ref);
END_TEST
BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object id one")
@ -411,7 +452,7 @@ BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
/* Create the symbolic ref */
@ -426,6 +467,8 @@ BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object
must_be_true(!git_oid_cmp(git_reference_oid(ref), &id));
close_temp_repo(repo);
git_reference_free(ref);
END_TEST
BEGIN_TEST(pack0, "create a packfile for an empty folder")
@ -451,7 +494,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
/* Ensure a known loose ref can be looked up */
must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
must_be_true((reference->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(reference) == 0);
must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
/*
@ -467,7 +510,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
/* Ensure the known ref can still be looked up but is now packed */
must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
must_be_true((reference->type & GIT_REF_PACKED) != 0);
must_be_true(git_reference_is_packed(reference));
must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
/* Ensure the known ref has been removed from the loose folder structure */
@ -475,6 +518,8 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
must_pass(!git_futils_exists(temp_path));
close_temp_repo(repo);
git_reference_free(reference);
END_TEST
BEGIN_TEST(rename0, "rename a loose reference")
@ -493,7 +538,7 @@ BEGIN_TEST(rename0, "rename a loose reference")
must_pass(git_reference_lookup(&looked_up_ref, repo, loose_tag_ref_name));
/* ... which is indeed loose */
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is renamed... */
must_pass(git_reference_rename(looked_up_ref, new_name, 0));
@ -507,14 +552,17 @@ BEGIN_TEST(rename0, "rename a loose reference")
must_be_true(!strcmp(another_looked_up_ref->name, new_name));
/* .. the ref is still loose... */
must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* ...and the ref can be found in the file system */
git_path_join(temp_path, repo->path_repository, new_name);
must_pass(git_futils_exists(temp_path));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
END_TEST
BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
@ -533,7 +581,7 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
/* .. and it's packed */
must_be_true((looked_up_ref->type & GIT_REF_PACKED) != 0);
must_be_true(git_reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
must_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
@ -547,14 +595,17 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
must_be_true(!strcmp(another_looked_up_ref->name, brand_new_name));
/* .. the ref is no longer packed... */
must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* ...and the ref now happily lives in the file system */
git_path_join(temp_path, repo->path_repository, brand_new_name);
must_pass(git_futils_exists(temp_path));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
END_TEST
BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference which happens to be in both loose and pack state")
@ -573,13 +624,13 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
/* Ensure it's loose */
must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
/* Lookup the reference to rename */
must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
/* Ensure it's packed */
must_be_true((looked_up_ref->type & GIT_REF_PACKED) != 0);
must_be_true(git_reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
must_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
@ -588,12 +639,15 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
/* Ensure it's loose */
must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
/* Ensure the other ref still exists on the file system */
must_pass(git_futils_exists(temp_path));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
END_TEST
BEGIN_TEST(rename3, "can not rename a reference with the name of an existing reference")
@ -613,6 +667,8 @@ BEGIN_TEST(rename3, "can not rename a reference with the name of an existing ref
must_be_true(!strcmp(looked_up_ref->name, packed_head_name));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
END_TEST
BEGIN_TEST(rename4, "can not rename a reference with an invalid name")
@ -635,6 +691,8 @@ BEGIN_TEST(rename4, "can not rename a reference with an invalid name")
must_be_true(!strcmp(looked_up_ref->name, packed_test_head_name));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
END_TEST
BEGIN_TEST(rename5, "can force-rename a packed reference with the name of an existing loose and packed reference")
@ -660,6 +718,8 @@ BEGIN_TEST(rename5, "can force-rename a packed reference with the name of an exi
must_fail(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
END_TEST
BEGIN_TEST(rename6, "can force-rename a loose reference with the name of an existing loose reference")
@ -685,6 +745,8 @@ BEGIN_TEST(rename6, "can force-rename a loose reference with the name of an exis
must_fail(git_reference_lookup(&looked_up_ref, repo, "refs/heads/br2"));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
END_TEST
static const char *ref_one_name = "refs/heads/one/branch";
@ -699,7 +761,7 @@ BEGIN_TEST(rename7, "can not overwrite name of existing reference")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
@ -717,6 +779,11 @@ BEGIN_TEST(rename7, "can not overwrite name of existing reference")
must_fail(git_reference_lookup(&ref_one_new, repo, ref_one_name_new));
close_temp_repo(repo);
git_reference_free(ref);
git_reference_free(ref_one);
git_reference_free(ref_one_new);
git_reference_free(ref_two);
END_TEST
static const char *ref_two_name_new = "refs/heads/two/two";
@ -729,7 +796,7 @@ BEGIN_TEST(rename8, "can be renamed to a new name prefixed with the old name")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
@ -748,6 +815,10 @@ BEGIN_TEST(rename8, "can be renamed to a new name prefixed with the old name")
must_fail(git_reference_lookup(&looked_up_ref, repo, ref_two_name));
close_temp_repo(repo);
git_reference_free(ref);
git_reference_free(ref_two);
git_reference_free(looked_up_ref);
END_TEST
BEGIN_TEST(rename9, "can move a reference to a upper reference hierarchy")
@ -758,7 +829,7 @@ BEGIN_TEST(rename9, "can move a reference to a upper reference hierarchy")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
must_pass(git_reference_lookup(&ref, repo, ref_master_name));
must_be_true(ref->type & GIT_REF_OID);
must_be_true(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
@ -794,7 +865,7 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name));
/* Ensure it's the loose version that has been found */
must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is deleted... */
must_pass(git_reference_delete(looked_up_ref));
@ -806,6 +877,42 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
must_pass(!git_futils_exists(temp_path));
close_temp_repo(repo);
git_reference_free(another_looked_up_ref);
END_TEST
BEGIN_TEST(delete1, "can delete a just packed reference")
git_reference *ref;
git_repository *repo;
git_oid id;
const char *new_ref = "refs/heads/new_ref";
git_oid_fromstr(&id, current_master_tip);
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Create and write the new object id reference */
must_pass(git_reference_create_oid(&ref, repo, new_ref, &id, 0));
/* Lookup the reference */
must_pass(git_reference_lookup(&ref, repo, new_ref));
/* Ensure it's a loose reference */
must_be_true(git_reference_is_packed(ref) == 0);
/* Pack all existing references */
must_pass(git_reference_packall(repo));
/* Reload the reference from disk */
must_pass(git_reference_reload(ref));
/* Ensure it's a packed reference */
must_be_true(git_reference_is_packed(ref) == 1);
/* This should pass */
must_pass(git_reference_delete(ref));
close_temp_repo(repo);
END_TEST
static int ensure_refname_normalized(int is_oid_ref, const char *input_refname, const char *expected_refname)
@ -1101,6 +1208,9 @@ BEGIN_TEST(reflog0, "write a reflog for a given reference and ensure it can be r
git_signature_free(committer);
git_reflog_free(reflog);
close_temp_repo(repo2);
git_reference_free(ref);
git_reference_free(lookedup_ref);
END_TEST
BEGIN_TEST(reflog1, "avoid writing an obviously wrong reflog")
@ -1129,6 +1239,8 @@ BEGIN_TEST(reflog1, "avoid writing an obviously wrong reflog")
git_signature_free(committer);
close_temp_repo(repo);
git_reference_free(ref);
END_TEST
BEGIN_SUITE(refs)
@ -1172,6 +1284,7 @@ BEGIN_SUITE(refs)
ADD_TEST(rename9);
ADD_TEST(delete0);
ADD_TEST(delete1);
ADD_TEST(list0);
ADD_TEST(list1);

View File

@ -286,6 +286,8 @@ BEGIN_TEST(detached0, "test if HEAD is detached")
must_be_true(git_repository_head_detached(repo) == 0);
git_repository_free(repo);
git_reference_free(ref);
END_TEST
BEGIN_TEST(orphan0, "test if HEAD is orphan")
@ -305,6 +307,8 @@ BEGIN_TEST(orphan0, "test if HEAD is orphan")
must_be_true(git_repository_head_orphan(repo) == 0);
git_repository_free(repo);
git_reference_free(ref);
END_TEST
#define DISCOVER_FOLDER TEMP_REPO_FOLDER "discover.git"