diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-05-27 23:10:12 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-05-29 11:33:58 +0100 |
commit | 067bb2add64c9fd7a9f0696072e19821693d9219 (patch) | |
tree | 9db4539cddb7a78e493d75b6c06a9b4e0ab3c930 | |
parent | 52c57d373f48f0a4171b9dc0c66ed1a315a1ac3b (diff) | |
download | libgit2-067bb2add64c9fd7a9f0696072e19821693d9219.tar.gz |
refspec: user-facing functions write to a userbuf
-rw-r--r-- | include/git2/refspec.h | 6 | ||||
-rw-r--r-- | src/branch.c | 4 | ||||
-rw-r--r-- | src/clone.c | 2 | ||||
-rw-r--r-- | src/push.c | 2 | ||||
-rw-r--r-- | src/refspec.c | 19 | ||||
-rw-r--r-- | src/refspec.h | 4 | ||||
-rw-r--r-- | src/remote.c | 8 | ||||
-rw-r--r-- | src/transports/smart.c | 2 | ||||
-rw-r--r-- | tests/network/refspecs.c | 12 | ||||
-rw-r--r-- | tests/network/remote/remotes.c | 8 | ||||
-rw-r--r-- | tests/online/push.c | 16 |
11 files changed, 49 insertions, 34 deletions
diff --git a/include/git2/refspec.h b/include/git2/refspec.h index eaf774746..5676c1ab9 100644 --- a/include/git2/refspec.h +++ b/include/git2/refspec.h @@ -79,7 +79,7 @@ GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec); GIT_EXTERN(git_direction) git_refspec_direction(const git_refspec *spec); /** - * Check if a refspec's source descriptor matches a reference + * Check if a refspec's source descriptor matches a reference * * @param refspec the refspec * @param refname the name of the reference to check @@ -104,7 +104,7 @@ GIT_EXTERN(int) git_refspec_dst_matches(const git_refspec *refspec, const char * * @param name the name of the reference to transform * @return 0, GIT_EBUFS or another error */ -GIT_EXTERN(int) git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name); +GIT_EXTERN(int) git_refspec_transform(git_userbuf *out, const git_refspec *spec, const char *name); /** * Transform a target reference to its source reference following the refspec's rules @@ -114,7 +114,7 @@ GIT_EXTERN(int) git_refspec_transform(git_buf *out, const git_refspec *spec, con * @param name the name of the reference to transform * @return 0, GIT_EBUFS or another error */ -GIT_EXTERN(int) git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name); +GIT_EXTERN(int) git_refspec_rtransform(git_userbuf *out, const git_refspec *spec, const char *name); GIT_END_DECL diff --git a/src/branch.c b/src/branch.c index 805729d61..657aaeb6a 100644 --- a/src/branch.c +++ b/src/branch.c @@ -441,7 +441,7 @@ int git_branch__upstream_name( goto cleanup; } - if (git_refspec_transform(&buf, refspec, git_buf_cstr(&merge_name)) < 0) + if (git_refspec__transform(&buf, refspec, git_buf_cstr(&merge_name)) < 0) goto cleanup; } else if (git_buf_set(&buf, git_buf_cstr(&merge_name), git_buf_len(&merge_name)) < 0) @@ -681,7 +681,7 @@ int git_branch_set_upstream(git_reference *branch, const char *branch_name) goto on_error; fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream)); - if (!fetchspec || git_refspec_rtransform(&merge_refspec, fetchspec, git_reference_name(upstream)) < 0) + if (!fetchspec || git_refspec__rtransform(&merge_refspec, fetchspec, git_reference_name(upstream)) < 0) goto on_error; git_remote_free(remote); diff --git a/src/clone.c b/src/clone.c index d08185e4d..c52539242 100644 --- a/src/clone.c +++ b/src/clone.c @@ -180,7 +180,7 @@ static int update_head_to_remote( } /* Determine the remote tracking reference name from the local master */ - if ((error = git_refspec_transform( + if ((error = git_refspec__transform( &remote_master_name, refspec, branch.ptr)) < 0) diff --git a/src/push.c b/src/push.c index 34867c2e4..34fefaf1d 100644 --- a/src/push.c +++ b/src/push.c @@ -182,7 +182,7 @@ int git_push_update_tips(git_push *push, const git_remote_callbacks *callbacks) /* Clear the buffer which can be dirty from previous iteration */ git_buf_clear(&remote_ref_name); - if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0) + if ((error = git_refspec__transform(&remote_ref_name, fetch_spec, status->ref)) < 0) goto on_error; /* Find matching push ref spec */ diff --git a/src/refspec.c b/src/refspec.c index 854240a84..dcb0c6078 100644 --- a/src/refspec.c +++ b/src/refspec.c @@ -13,6 +13,7 @@ #include "util.h" #include "vector.h" #include "wildmatch.h" +#include "userbuf.h" int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) { @@ -260,10 +261,9 @@ static int refspec_transform( return git_buf_puts(out, to_star + 1); } -int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name) +int git_refspec__transform(git_buf *out, const git_refspec *spec, const char *name) { assert(out && spec && name); - git_buf_sanitize(out); if (!git_refspec_src_matches(spec, name)) { git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name); @@ -276,10 +276,15 @@ int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *nam return refspec_transform(out, spec->src, spec->dst, name); } -int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name) +int git_refspec_transform(git_userbuf *out, const git_refspec *spec, const char *name) +{ + git_userbuf_sanitize(out); + return git_refspec__transform((git_buf *)out, spec, name); +} + +int git_refspec__rtransform(git_buf *out, const git_refspec *spec, const char *name) { assert(out && spec && name); - git_buf_sanitize(out); if (!git_refspec_dst_matches(spec, name)) { git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name); @@ -292,6 +297,12 @@ int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *na return refspec_transform(out, spec->dst, spec->src, name); } +int git_refspec_rtransform(git_userbuf *out, const git_refspec *spec, const char *name) +{ + git_userbuf_sanitize(out); + return git_refspec__rtransform((git_buf *)out, spec, name); +} + int git_refspec__serialize(git_buf *out, const git_refspec *refspec) { if (refspec->force) diff --git a/src/refspec.h b/src/refspec.h index 2b4111f04..b2378baf7 100644 --- a/src/refspec.h +++ b/src/refspec.h @@ -48,4 +48,8 @@ int git_refspec_is_wildcard(const git_refspec *spec); */ int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs); +/* Internal transformation functions that manipulate a git_buf */ +extern int git_refspec__transform(git_buf *out, const git_refspec *spec, const char *name); +extern int git_refspec__rtransform(git_buf *out, const git_refspec *spec, const char *name); + #endif diff --git a/src/remote.c b/src/remote.c index bf4b912ee..6d231320a 100644 --- a/src/remote.c +++ b/src/remote.c @@ -1098,7 +1098,7 @@ static int ref_to_update(int *update, git_buf *remote_name, git_remote *remote, git__strcmp(git_remote_name(remote), git_buf_cstr(&upstream_remote)) || (error = git_branch__upstream_name(&upstream_name, repo, ref_name)) < 0 || !git_refspec_dst_matches(spec, git_buf_cstr(&upstream_name)) || - (error = git_refspec_rtransform(remote_name, spec, upstream_name.ptr)) < 0) { + (error = git_refspec__rtransform(remote_name, spec, upstream_name.ptr)) < 0) { /* Not an error if there is no upstream */ if (error == GIT_ENOTFOUND) { git_error_clear(); @@ -1290,7 +1290,7 @@ int git_remote_prune(git_remote *remote, const git_remote_callbacks *callbacks) if (!git_refspec_dst_matches(spec, refname)) continue; - if ((error = git_refspec_rtransform(&buf, spec, refname)) < 0) + if ((error = git_refspec__rtransform(&buf, spec, refname)) < 0) goto cleanup; key.name = (char *) git_buf_cstr(&buf); @@ -1413,7 +1413,7 @@ static int update_tips_for_spec( /* If we didn't want to auto-follow the tag, check if the refspec matches */ if (!autotag && git_refspec_src_matches(spec, head->name)) { if (spec->dst) { - if (git_refspec_transform(&refname, spec, head->name) < 0) + if (git_refspec__transform(&refname, spec, head->name) < 0) goto on_error; } else { /* @@ -1568,7 +1568,7 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call */ git_buf_clear(&refname); - if ((error = git_refspec_transform(&refname, spec, head->name)) < 0) + if ((error = git_refspec__transform(&refname, spec, head->name)) < 0) goto cleanup; error = git_reference_name_to_id(&old, remote->repo, refname.ptr); diff --git a/src/transports/smart.c b/src/transports/smart.c index bb4d2a228..53fe8babb 100644 --- a/src/transports/smart.c +++ b/src/transports/smart.c @@ -171,7 +171,7 @@ int git_smart__update_heads(transport_smart *t, git_vector *symrefs) git_vector_foreach(symrefs, j, spec) { git_buf_clear(&buf); if (git_refspec_src_matches(spec, ref->head.name) && - !(error = git_refspec_transform(&buf, spec, ref->head.name))) { + !(error = git_refspec__transform(&buf, spec, ref->head.name))) { git__free(ref->head.symref_target); ref->head.symref_target = git_buf_detach(&buf); } diff --git a/tests/network/refspecs.c b/tests/network/refspecs.c index 5c8eb1502..fcd0d4329 100644 --- a/tests/network/refspecs.c +++ b/tests/network/refspecs.c @@ -94,13 +94,13 @@ void test_network_refspecs__parsing(void) static void assert_valid_transform(const char *refspec, const char *name, const char *result) { git_refspec spec; - git_buf buf = GIT_BUF_INIT; + git_userbuf buf = GIT_USERBUF_INIT; cl_git_pass(git_refspec__parse(&spec, refspec, true)); cl_git_pass(git_refspec_transform(&buf, &spec, name)); cl_assert_equal_s(result, buf.ptr); - git_buf_dispose(&buf); + git_userbuf_dispose(&buf); git_refspec__dispose(&spec); } @@ -133,12 +133,12 @@ void test_network_refspecs__no_dst(void) static void assert_invalid_transform(const char *refspec, const char *name) { git_refspec spec; - git_buf buf = GIT_BUF_INIT; + git_userbuf buf = GIT_USERBUF_INIT; git_refspec__parse(&spec, refspec, true); cl_git_fail(git_refspec_transform(&buf, &spec, name)); - git_buf_dispose(&buf); + git_userbuf_dispose(&buf); git_refspec__dispose(&spec); } @@ -151,12 +151,12 @@ void test_network_refspecs__invalid(void) static void assert_invalid_rtransform(const char *refspec, const char *name) { git_refspec spec; - git_buf buf = GIT_BUF_INIT; + git_userbuf buf = GIT_USERBUF_INIT; cl_git_pass(git_refspec__parse(&spec, refspec, true)); cl_git_fail(git_refspec_rtransform(&buf, &spec, name)); - git_buf_dispose(&buf); + git_userbuf_dispose(&buf); git_refspec__dispose(&spec); } diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c index b5e8fdade..eb41f0dc6 100644 --- a/tests/network/remote/remotes.c +++ b/tests/network/remote/remotes.c @@ -256,20 +256,20 @@ void test_network_remote_remotes__fnmatch(void) void test_network_remote_remotes__transform(void) { - git_buf ref = GIT_BUF_INIT; + git_userbuf ref = GIT_USERBUF_INIT; cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master")); cl_assert_equal_s(ref.ptr, "refs/remotes/test/master"); - git_buf_dispose(&ref); + git_userbuf_dispose(&ref); } void test_network_remote_remotes__transform_destination_to_source(void) { - git_buf ref = GIT_BUF_INIT; + git_userbuf ref = GIT_USERBUF_INIT; cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master")); cl_assert_equal_s(ref.ptr, "refs/heads/master"); - git_buf_dispose(&ref); + git_userbuf_dispose(&ref); } void test_network_remote_remotes__missing_refspecs(void) diff --git a/tests/online/push.c b/tests/online/push.c index c82b606cb..b2b559a5d 100644 --- a/tests/online/push.c +++ b/tests/online/push.c @@ -193,7 +193,7 @@ static void verify_tracking_branches(git_remote *remote, expected_ref expected_r git_refspec *fetch_spec; size_t i, j; git_buf msg = GIT_BUF_INIT; - git_buf ref_name = GIT_BUF_INIT; + git_userbuf ref_name = GIT_USERBUF_INIT; git_vector actual_refs = GIT_VECTOR_INIT; git_branch_iterator *iter; char *actual_ref; @@ -230,12 +230,12 @@ static void verify_tracking_branches(git_remote *remote, expected_ref expected_r /* Find matching remote branch */ git_vector_foreach(&actual_refs, j, actual_ref) { - if (!strcmp(git_buf_cstr(&ref_name), actual_ref)) + if (!strcmp(ref_name.ptr, actual_ref)) break; } if (j == actual_refs.length) { - git_buf_printf(&msg, "Did not find expected tracking branch '%s'.", git_buf_cstr(&ref_name)); + git_buf_printf(&msg, "Did not find expected tracking branch '%s'.", ref_name.ptr); failed = 1; goto failed; } @@ -269,14 +269,14 @@ failed: git_vector_free(&actual_refs); git_buf_dispose(&msg); - git_buf_dispose(&ref_name); + git_userbuf_dispose(&ref_name); } static void verify_update_tips_callback(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len) { git_refspec *fetch_spec; git_buf msg = GIT_BUF_INIT; - git_buf ref_name = GIT_BUF_INIT; + git_userbuf ref_name = GIT_USERBUF_INIT; updated_tip *tip = NULL; size_t i, j; int failed = 0; @@ -293,12 +293,12 @@ static void verify_update_tips_callback(git_remote *remote, expected_ref expecte /* Find matching update_tip entry */ git_vector_foreach(&_record_cbs_data.updated_tips, j, tip) { - if (!strcmp(git_buf_cstr(&ref_name), tip->name)) + if (!strcmp(ref_name.ptr, tip->name)) break; } if (j == _record_cbs_data.updated_tips.length) { - git_buf_printf(&msg, "Did not find expected updated tip entry for branch '%s'.", git_buf_cstr(&ref_name)); + git_buf_printf(&msg, "Did not find expected updated tip entry for branch '%s'.", ref_name.ptr); failed = 1; goto failed; } @@ -314,7 +314,7 @@ failed: if (failed) cl_fail(git_buf_cstr(&msg)); - git_buf_dispose(&ref_name); + git_userbuf_dispose(&ref_name); git_buf_dispose(&msg); } |