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