Commit 56960b83 by Vicent Marti

Liike this

parent 2638a03a
......@@ -351,7 +351,9 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
* @param repo the repository
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reference_iterator_new(git_reference_iterator **out, git_repository *repo);
GIT_EXTERN(int) git_reference_iterator_new(
git_reference_iterator **out,
git_repository *repo);
/**
* Create an iterator for the repo's references that match the
......@@ -362,16 +364,19 @@ GIT_EXTERN(int) git_reference_iterator_new(git_reference_iterator **out, git_rep
* @param glob the glob to match against the reference names
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reference_iterator_glob_new(git_reference_iterator **out, git_repository *repo, const char *glob);
GIT_EXTERN(int) git_reference_iterator_glob_new(
git_reference_iterator **out,
git_repository *repo,
const char *glob);
/**
* Get the next reference name
* Get the next reference
*
* @param out pointer in which to store the string
* @param out pointer in which to store the reference
* @param iter the iterator
* @param 0, GIT_ITEROVER if there are no more; or an error code
*/
GIT_EXTERN(int) git_reference_next(const char **out, git_reference_iterator *iter);
GIT_EXTERN(int) git_reference_next(git_reference **out, git_reference_iterator *iter);
/**
* Free the iterator and its associated resources
......
......@@ -85,7 +85,7 @@ struct git_refdb_backend {
* A refdb implementation must provide this function.
*/
int (*next)(
const char **name,
git_reference **ref,
git_reference_iterator *iter);
/**
......
......@@ -131,28 +131,32 @@ int git_branch_foreach(
void *payload)
{
git_reference_iterator *iter;
const char *name;
git_reference *ref;
int error;
if (git_reference_iterator_new(&iter, repo) < 0)
return -1;
while ((error = git_reference_next(&name, iter)) == 0) {
while ((error = git_reference_next(&ref, iter)) == 0) {
if (list_flags & GIT_BRANCH_LOCAL &&
git__prefixcmp(name, GIT_REFS_HEADS_DIR) == 0) {
if (callback(name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, payload)) {
git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0) {
if (callback(ref->name + strlen(GIT_REFS_HEADS_DIR),
GIT_BRANCH_LOCAL, payload)) {
error = GIT_EUSER;
break;
}
}
if (list_flags & GIT_BRANCH_REMOTE &&
git__prefixcmp(name, GIT_REFS_REMOTES_DIR) == 0) {
if (callback(name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, payload)) {
git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) == 0) {
if (callback(ref->name + strlen(GIT_REFS_REMOTES_DIR),
GIT_BRANCH_REMOTE, payload)) {
error = GIT_EUSER;
break;
}
}
git_reference_free(ref);
}
if (error == GIT_ITEROVER)
......@@ -160,7 +164,6 @@ int git_branch_foreach(
git_reference_iterator_free(iter);
return error;
}
int git_branch_move(
......
......@@ -160,17 +160,26 @@ int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const c
return 0;
}
int git_refdb_next(const char **out, git_reference_iterator *iter)
int git_refdb_next(git_reference **out, git_reference_iterator *iter)
{
int error;
if (!iter->glob)
return iter->backend->next(out, iter);
if (!iter->glob) {
if ((error = iter->backend->next(out, iter)) < 0)
return error;
(*out)->db = iter->backend;
return 0;
}
/* If the iterator has a glob, we need to filter */
while ((error = iter->backend->next(out, iter)) == 0) {
if (!p_fnmatch(iter->glob, *out, 0))
break;
if (!p_fnmatch(iter->glob, (*out)->name, 0)) {
(*out)->db = iter->backend;
return 0;
}
git_reference_free(*out);
}
return error;
......
......@@ -28,7 +28,7 @@ int git_refdb_lookup(
int git_refdb_iterator(git_reference_iterator **out, git_refdb *db);
int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob);
int git_refdb_next(const char **out, git_reference_iterator *iter);
int git_refdb_next(git_reference **out, git_reference_iterator *iter);
void git_refdb_iterator_free(git_reference_iterator *iter);
int git_refdb_write(git_refdb *refdb, const git_reference *ref);
......
......@@ -645,16 +645,19 @@ static int iter_load_loose_paths(refdb_fs_iter *iter)
return 0;
}
static int refdb_fs_backend__next(const char **out, git_reference_iterator *_iter)
static int refdb_fs_backend__next(git_reference **out, git_reference_iterator *_iter)
{
refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.backend;
git_strmap *packfile = backend->refcache.packfile;
if (iter->loose_pos < iter->loose.length) {
while (iter->loose_pos < iter->loose.length) {
const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
*out = path;
if (loose_lookup(out, backend, path) == 0)
return 0;
giterr_clear();
}
if (iter->packed_pos < kh_end(packfile)) {
......@@ -671,7 +674,10 @@ static int refdb_fs_backend__next(const char **out, git_reference_iterator *_ite
iter->packed_pos++;
} while (ref->flags & PACKREF_SHADOWED);
*out = ref->name;
*out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
if (*out == NULL)
return -1;
return 0;
}
......
......@@ -664,7 +664,7 @@ int git_reference_iterator_glob_new(git_reference_iterator **out, git_repository
return git_refdb_iterator_glob(out, refdb, glob);
}
int git_reference_next(const char **out, git_reference_iterator *iter)
int git_reference_next(git_reference **out, git_reference_iterator *iter)
{
return git_refdb_next(out, iter);
}
......
......@@ -1239,30 +1239,25 @@ static int update_branch_remote_config_entry(
}
static int rename_one_remote_reference(
git_repository *repo,
const char *reference_name,
git_reference *reference,
const char *old_remote_name,
const char *new_remote_name)
{
int error = -1;
git_buf new_name = GIT_BUF_INIT;
git_reference *reference = NULL;
git_reference *newref = NULL;
if (git_buf_printf(
&new_name,
GIT_REFS_REMOTES_DIR "%s%s",
new_remote_name,
reference_name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
return -1;
if (git_reference_lookup(&reference, repo, reference_name) < 0)
goto cleanup;
/* TODO: can we make this NULL? */
error = git_reference_rename(&newref, reference, git_buf_cstr(&new_name), 0);
git_reference_free(reference);
cleanup:
git_reference_free(newref);
git_buf_free(&new_name);
return error;
......@@ -1273,46 +1268,28 @@ static int rename_remote_references(
const char *old_name,
const char *new_name)
{
git_vector refnames;
int error = -1;
unsigned int i;
char *name;
const char *refname;
git_reference *ref;
git_reference_iterator *iter;
if (git_vector_init(&refnames, 8, NULL) < 0)
return -1;
if (git_reference_iterator_new(&iter, repo) < 0)
goto cleanup;
return -1;
while ((error = git_reference_next(&refname, iter)) == 0) {
if (git__prefixcmp(refname, GIT_REFS_REMOTES_DIR))
while ((error = git_reference_next(&ref, iter)) == 0) {
if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR))
continue;
if ((error = git_vector_insert(&refnames, git__strdup(refname))) < 0)
break;
if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) {
git_reference_iterator_free(iter);
return error;
}
}
git_reference_iterator_free(iter);
if (error == GIT_ITEROVER)
error = 0;
else
goto cleanup;
git_vector_foreach(&refnames, i, name) {
if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0)
goto cleanup;
}
error = 0;
cleanup:
git_vector_foreach(&refnames, i, name) {
git__free(name);
}
if (error == GIT_ITEROVER)
return 0;
git_vector_free(&refnames);
return error;
}
......
......@@ -134,7 +134,7 @@ static int refdb_test_backend__iterator(git_reference_iterator **out, git_refdb_
return 0;
}
static int refdb_test_backend__next(const char **name, git_reference_iterator *_iter)
static int refdb_test_backend__next(git_reference **out, git_reference_iterator *_iter)
{
refdb_test_entry *entry;
refdb_test_backend *backend = (refdb_test_backend *) _iter->backend;
......@@ -144,9 +144,15 @@ static int refdb_test_backend__next(const char **name, git_reference_iterator *_
if (!entry)
return GIT_ITEROVER;
*name = entry->name;
iter->i++;
if (entry->type == GIT_REF_OID) {
*out = git_reference__alloc(entry->name, &entry->target.oid, NULL);
} else if (entry->type == GIT_REF_SYMBOLIC) {
*out = git_reference__alloc_symbolic(entry->name, entry->target.symbolic);
} else {
return -1;
}
iter->i++;
return 0;
}
......
......@@ -38,40 +38,43 @@ static const char *refnames[] = {
"refs/tags/wrapped_tag",
};
static int refcmp_cb(const void *a, const void *b)
{
const git_reference *refa = (const git_reference *)a;
const git_reference *refb = (const git_reference *)b;
return strcmp(refa->name, refb->name);
}
void test_refs_iterator__list(void)
{
git_reference_iterator *iter;
git_vector output;
char *refname;
git_reference *ref;
int error;
size_t i;
cl_git_pass(git_vector_init(&output, 32, git__strcmp_cb));
cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
cl_git_pass(git_reference_iterator_new(&iter, repo));
do {
const char *name;
error = git_reference_next(&name, iter);
error = git_reference_next(&ref, iter);
cl_assert(error == 0 || error == GIT_ITEROVER);
if (error != GIT_ITEROVER) {
char *dup = git__strdup(name);
cl_assert(dup != NULL);
cl_git_pass(git_vector_insert(&output, dup));
cl_git_pass(git_vector_insert(&output, ref));
}
} while (!error);
git_reference_iterator_free(iter);
cl_assert_equal_i(output.length, ARRAY_SIZE(refnames));
git_vector_sort(&output);
git_vector_foreach(&output, i, refname) {
cl_assert_equal_s(refname, refnames[i]);
}
git_reference_iterator_free(iter);
git_vector_foreach(&output, i, refname) {
git__free(refname);
git_vector_foreach(&output, i, ref) {
cl_assert_equal_s(ref->name, refnames[i]);
git_reference_free(ref);
}
git_vector_free(&output);
}
......@@ -79,14 +82,14 @@ void test_refs_iterator__empty(void)
{
git_reference_iterator *iter;
git_odb *odb;
const char *name;
git_reference *ref;
git_repository *empty;
cl_git_pass(git_odb_new(&odb));
cl_git_pass(git_repository_wrap_odb(&empty, odb));
cl_git_pass(git_reference_iterator_new(&iter, empty));
cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&name, iter));
cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&ref, iter));
git_reference_iterator_free(iter);
git_odb_free(odb);
......
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