Commit 1be680c4 by Carlos Martín Nieto

refspec: unify the string and parsed data

It used to be separate as an attempt to make the querying easier, but
it didn't work out that way, so put all the data together.

Add git_refspec_string() as well to get the original string, which is
now stored alongside the independent parts.
parent bc6374ea
...@@ -36,6 +36,14 @@ GIT_EXTERN(const char *) git_refspec_src(const git_refspec *refspec); ...@@ -36,6 +36,14 @@ GIT_EXTERN(const char *) git_refspec_src(const git_refspec *refspec);
GIT_EXTERN(const char *) git_refspec_dst(const git_refspec *refspec); GIT_EXTERN(const char *) git_refspec_dst(const git_refspec *refspec);
/** /**
* Get the refspec's string
*
* @param refspec the refspec
* @returns the refspec's original string
*/
GIT_EXTERN(const char *) git_refspec_string(const git_refspec *refspec);
/**
* Get the force update setting * Get the force update setting
* *
* @param refspec the refspec * @param refspec the refspec
......
...@@ -120,6 +120,9 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -120,6 +120,9 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
} }
} }
refspec->string = git__strdup(input);
GITERR_CHECK_ALLOC(refspec->string);
return 0; return 0;
invalid: invalid:
...@@ -133,6 +136,7 @@ void git_refspec__free(git_refspec *refspec) ...@@ -133,6 +136,7 @@ void git_refspec__free(git_refspec *refspec)
git__free(refspec->src); git__free(refspec->src);
git__free(refspec->dst); git__free(refspec->dst);
git__free(refspec->string);
} }
const char *git_refspec_src(const git_refspec *refspec) const char *git_refspec_src(const git_refspec *refspec)
...@@ -145,6 +149,11 @@ const char *git_refspec_dst(const git_refspec *refspec) ...@@ -145,6 +149,11 @@ const char *git_refspec_dst(const git_refspec *refspec)
return refspec == NULL ? NULL : refspec->dst; return refspec == NULL ? NULL : refspec->dst;
} }
const char *git_refspec_string(const git_refspec *refspec)
{
return refspec == NULL ? NULL : refspec->string;
}
int git_refspec_force(const git_refspec *refspec) int git_refspec_force(const git_refspec *refspec)
{ {
assert(refspec); assert(refspec);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "buffer.h" #include "buffer.h"
struct git_refspec { struct git_refspec {
char *string;
char *src; char *src;
char *dst; char *dst;
unsigned int force :1, unsigned int force :1,
......
...@@ -21,32 +21,24 @@ ...@@ -21,32 +21,24 @@
static int add_refspec(git_remote *remote, const char *string, bool is_fetch) static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
{ {
char *name_dup;
git_refspec *spec; git_refspec *spec;
spec = git__calloc(1, sizeof(git_refspec)); spec = git__calloc(1, sizeof(git_refspec));
GITERR_CHECK_ALLOC(spec); GITERR_CHECK_ALLOC(spec);
name_dup = git__strdup(string); if (git_refspec__parse(spec, string, is_fetch) < 0) {
if (!name_dup) git__free(spec);
goto on_error; return -1;
}
if (git_refspec__parse(spec, string, is_fetch) < 0)
goto on_error;
spec->push = !is_fetch; spec->push = !is_fetch;
if (git_vector_insert(&remote->refspec_strings, name_dup) < 0) if (git_vector_insert(&remote->refspecs, spec) < 0) {
goto on_error; git_refspec__free(spec);
git__free(spec);
if (git_vector_insert(&remote->refspecs, spec) < 0) return -1;
goto on_error; }
return 0; return 0;
on_error:
git__free(spec);
git__free(name_dup);
return -1;
} }
static int download_tags_value(git_remote *remote, git_config *cfg) static int download_tags_value(git_remote *remote, git_config *cfg)
...@@ -245,8 +237,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -245,8 +237,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
GITERR_CHECK_ALLOC(remote->name); GITERR_CHECK_ALLOC(remote->name);
if ((git_vector_init(&remote->refs, 32, NULL) < 0) || if ((git_vector_init(&remote->refs, 32, NULL) < 0) ||
(git_vector_init(&remote->refspecs, 2, NULL)) || (git_vector_init(&remote->refspecs, 2, NULL))) {
(git_vector_init(&remote->refspec_strings, 2, NULL))) {
error = -1; error = -1;
goto cleanup; goto cleanup;
} }
...@@ -360,15 +351,13 @@ static int update_config_refspec(const git_remote *remote, git_config *config, i ...@@ -360,15 +351,13 @@ static int update_config_refspec(const git_remote *remote, git_config *config, i
if (error != GIT_ENOTFOUND) if (error != GIT_ENOTFOUND)
return error; return error;
for (i = 0; i < remote->refspec_strings.length; i++) { for (i = 0; i < remote->refspecs.length; i++) {
git_refspec *spec = git_vector_get(&remote->refspecs, i); git_refspec *spec = git_vector_get(&remote->refspecs, i);
const char *str = git_vector_get(&remote->refspec_strings, i);
assert(spec && str);
if (spec->push != push) if (spec->push != push)
continue; continue;
if ((error = git_config_set_multivar(config, git_buf_cstr(&name), "", str)) < 0) { if ((error = git_config_set_multivar(config, git_buf_cstr(&name), "", spec->string)) < 0) {
goto cleanup; goto cleanup;
} }
} }
...@@ -947,7 +936,6 @@ void git_remote_disconnect(git_remote *remote) ...@@ -947,7 +936,6 @@ void git_remote_disconnect(git_remote *remote)
void git_remote_free(git_remote *remote) void git_remote_free(git_remote *remote)
{ {
git_refspec *spec; git_refspec *spec;
char *str;
size_t i; size_t i;
if (remote == NULL) if (remote == NULL)
...@@ -968,10 +956,6 @@ void git_remote_free(git_remote *remote) ...@@ -968,10 +956,6 @@ void git_remote_free(git_remote *remote)
} }
git_vector_free(&remote->refspecs); git_vector_free(&remote->refspecs);
git_vector_foreach(&remote->refspec_strings, i, str)
git__free(str);
git_vector_free(&remote->refspec_strings);
git__free(remote->url); git__free(remote->url);
git__free(remote->pushurl); git__free(remote->pushurl);
git__free(remote->name); git__free(remote->name);
...@@ -1263,23 +1247,20 @@ static int rename_fetch_refspecs( ...@@ -1263,23 +1247,20 @@ static int rename_fetch_refspecs(
{ {
git_config *config; git_config *config;
git_buf base = GIT_BUF_INIT, var = GIT_BUF_INIT, val = GIT_BUF_INIT; git_buf base = GIT_BUF_INIT, var = GIT_BUF_INIT, val = GIT_BUF_INIT;
const char *refspec; const git_refspec *spec;
size_t i; size_t i;
int error = -1; int error = -1;
if (git_buf_printf(&base, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0) if (git_buf_printf(&base, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0)
goto cleanup; goto cleanup;
git_vector_foreach(&remote->refspec_strings, i, refspec) { git_vector_foreach(&remote->refspecs, i, spec) {
git_refspec *spec = git_vector_get(&remote->refspecs, i);
assert(spec);
if (spec->push) if (spec->push)
continue; continue;
/* Every refspec is a problem refspec for an in-memory remote */ /* Every refspec is a problem refspec for an in-memory remote */
if (!remote->name) { if (!remote->name) {
if (callback(refspec, payload) < 0) { if (callback(spec->string, payload) < 0) {
error = GIT_EUSER; error = GIT_EUSER;
goto cleanup; goto cleanup;
} }
...@@ -1288,8 +1269,8 @@ static int rename_fetch_refspecs( ...@@ -1288,8 +1269,8 @@ static int rename_fetch_refspecs(
} }
/* Does the dst part of the refspec follow the extected standard format? */ /* Does the dst part of the refspec follow the extected standard format? */
if (strcmp(git_buf_cstr(&base), refspec)) { if (strcmp(git_buf_cstr(&base), spec->string)) {
if (callback(refspec, payload) < 0) { if (callback(spec->string, payload) < 0) {
error = GIT_EUSER; error = GIT_EUSER;
goto cleanup; goto cleanup;
} }
...@@ -1453,18 +1434,13 @@ git_refspec *git_remote__matching_dst_refspec(git_remote *remote, const char *re ...@@ -1453,18 +1434,13 @@ git_refspec *git_remote__matching_dst_refspec(git_remote *remote, const char *re
void git_remote_clear_refspecs(git_remote *remote) void git_remote_clear_refspecs(git_remote *remote)
{ {
git_refspec *spec; git_refspec *spec;
char *str;
size_t i; size_t i;
git_vector_foreach(&remote->refspecs, i, spec) { git_vector_foreach(&remote->refspecs, i, spec) {
git_refspec__free(spec); git_refspec__free(spec);
git__free(spec);
} }
git_vector_clear(&remote->refspecs); git_vector_clear(&remote->refspecs);
git_vector_foreach(&remote->refspec_strings, i, str) {
git__free(str);
}
git_vector_clear(&remote->refspec_strings);
} }
int git_remote_add_fetch(git_remote *remote, const char *refspec) int git_remote_add_fetch(git_remote *remote, const char *refspec)
...@@ -1491,10 +1467,8 @@ static int copy_refspecs(git_strarray *array, git_remote *remote, int push) ...@@ -1491,10 +1467,8 @@ static int copy_refspecs(git_strarray *array, git_remote *remote, int push)
if (spec->push != push) if (spec->push != push)
continue; continue;
dup = git__strdup(git_vector_get(&remote->refspec_strings, i)); if ((dup = git__strdup(spec->string)) == NULL)
if (!dup) {
goto on_error; goto on_error;
}
if (git_vector_insert(&refspecs, dup) < 0) { if (git_vector_insert(&refspecs, dup) < 0) {
git__free(dup); git__free(dup);
......
...@@ -21,7 +21,6 @@ struct git_remote { ...@@ -21,7 +21,6 @@ struct git_remote {
char *pushurl; char *pushurl;
git_vector refs; git_vector refs;
git_vector refspecs; git_vector refspecs;
git_vector refspec_strings;
git_cred_acquire_cb cred_acquire_cb; git_cred_acquire_cb cred_acquire_cb;
void *cred_acquire_payload; void *cred_acquire_payload;
git_transport *transport; git_transport *transport;
......
...@@ -114,17 +114,17 @@ void test_network_remote_remotes__add_fetchspec(void) ...@@ -114,17 +114,17 @@ void test_network_remote_remotes__add_fetchspec(void)
size_t size; size_t size;
size = _remote->refspecs.length; size = _remote->refspecs.length;
cl_assert_equal_i(size, _remote->refspec_strings.length); cl_assert_equal_i(size, _remote->refspecs.length);
cl_git_pass(git_remote_add_fetch(_remote, "refs/*:refs/*")); cl_git_pass(git_remote_add_fetch(_remote, "refs/*:refs/*"));
size++; size++;
cl_assert_equal_i(size, _remote->refspec_strings.length);
cl_assert_equal_i(size, _remote->refspecs.length); cl_assert_equal_i(size, _remote->refspecs.length);
_refspec = git_vector_get(&_remote->refspecs, size-1); _refspec = git_vector_get(&_remote->refspecs, size-1);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
cl_assert_equal_i(_refspec->push, false); cl_assert_equal_i(_refspec->push, false);
} }
...@@ -136,12 +136,12 @@ void test_network_remote_remotes__add_pushspec(void) ...@@ -136,12 +136,12 @@ void test_network_remote_remotes__add_pushspec(void)
cl_git_pass(git_remote_add_push(_remote, "refs/*:refs/*")); cl_git_pass(git_remote_add_push(_remote, "refs/*:refs/*"));
size++; size++;
cl_assert_equal_i(size, _remote->refspec_strings.length);
cl_assert_equal_i(size, _remote->refspecs.length); cl_assert_equal_i(size, _remote->refspecs.length);
_refspec = git_vector_get(&_remote->refspecs, size-1); _refspec = git_vector_get(&_remote->refspecs, size-1);
cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
cl_assert_equal_i(_refspec->push, true); cl_assert_equal_i(_refspec->push, true);
} }
......
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