Merge pull request #1323 from jamill/resolve_remote Resolve a remote branch's remote
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 54a1ab1..3c7fb44 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -210,6 +210,31 @@ GIT_EXTERN(int) git_branch_tracking_name(
GIT_EXTERN(int) git_branch_is_head(
git_reference *branch);
+/**
+ * Return the name of remote that the remote tracking branch belongs to.
+ *
+ * @param remote_name_out The user-allocated buffer which will be
+ * filled with the name of the remote. Pass NULL if you just want to
+ * get the needed size of the name of the remote as the output value.
+ *
+ * @param buffer_size Size of the `out` buffer in bytes.
+ *
+ * @param repo The repository where the branch lives.
+ *
+ * @param branch The reference to the remote tracking branch.
+ *
+ * @return Number of characters in the reference name
+ * including the trailing NUL byte; GIT_ENOTFOUND
+ * when no remote matching remote was gound,
+ * GIT_EAMBIGUOUS when the branch maps to several remotes,
+ * otherwise an error code.
+ */
+GIT_EXTERN(int) git_branch_remote_name(
+ char *remote_name_out,
+ size_t buffer_size,
+ git_repository *repo,
+ git_reference *branch);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/include/git2/refspec.h b/include/git2/refspec.h
index ee06f8e..ec7830b 100644
--- a/include/git2/refspec.h
+++ b/include/git2/refspec.h
@@ -53,6 +53,15 @@ GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec);
GIT_EXTERN(int) git_refspec_src_matches(const git_refspec *refspec, const char *refname);
/**
+ * Check if a refspec's destination descriptor matches a reference
+ *
+ * @param refspec the refspec
+ * @param refname the name of the reference to check
+ * @return 1 if the refspec matches, 0 otherwise
+ */
+GIT_EXTERN(int) git_refspec_dst_matches(const git_refspec *refspec, const char *refname);
+
+/**
* Transform a reference to its target following the refspec's rules
*
* @param out where to store the target name
@@ -63,6 +72,17 @@ GIT_EXTERN(int) git_refspec_src_matches(const git_refspec *refspec, const char *
*/
GIT_EXTERN(int) git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name);
+/**
+ * Transform a target reference to its source reference following the refspec's rules
+ *
+ * @param out where to store the source reference name
+ * @param outlen the size of the `out` buffer
+ * @param spec the refspec
+ * @param name the name of the reference to transform
+ * @return 0, GIT_EBUFS or another error
+ */
+GIT_EXTERN(int) git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name);
+
GIT_END_DECL
#endif
diff --git a/src/branch.c b/src/branch.c
index 3959409..936947a 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -319,6 +319,87 @@ cleanup:
return error;
}
+int git_branch_remote_name(
+ char *remote_name_out,
+ size_t buffer_size,
+ git_repository *repo,
+ git_reference *branch)
+{
+ git_strarray remote_list = {0};
+ size_t i, remote_name_size;
+ git_remote *remote;
+ const git_refspec *fetchspec;
+ int error = 0;
+ char *remote_name = NULL;
+
+ assert(branch);
+
+ if (remote_name_out && buffer_size)
+ *remote_name_out = '\0';
+
+ /* Verify that this is a remote branch */
+ if (!git_reference_is_remote(branch)) {
+ giterr_set(GITERR_INVALID,
+ "Reference '%s' is not a remote branch.", branch->name);
+ error = GIT_ERROR;
+ goto cleanup;
+ }
+
+ /* Get the remotes */
+ if ((error = git_remote_list(&remote_list, repo)) < 0)
+ goto cleanup;
+
+ /* Find matching remotes */
+ for (i = 0; i < remote_list.count; i++) {
+ if ((error = git_remote_load(&remote, repo, remote_list.strings[i])) < 0)
+ goto cleanup;
+
+ fetchspec = git_remote_fetchspec(remote);
+
+ /* Defensivly check that we have a fetchspec */
+ if (fetchspec &&
+ git_refspec_dst_matches(fetchspec, branch->name)) {
+ /* If we have not already set out yet, then set
+ * it to the matching remote name. Otherwise
+ * multiple remotes match this reference, and it
+ * is ambiguous. */
+ if (!remote_name) {
+ remote_name = remote_list.strings[i];
+ } else {
+ git_remote_free(remote);
+ error = GIT_EAMBIGUOUS;
+ goto cleanup;
+ }
+ }
+
+ git_remote_free(remote);
+ }
+
+ if (remote_name) {
+ remote_name_size = strlen(remote_name) + 1;
+ error = (int) remote_name_size;
+
+ if (remote_name_out) {
+ if(remote_name_size > buffer_size) {
+ giterr_set(
+ GITERR_INVALID,
+ "Buffer too short to hold the remote name.");
+ error = GIT_ERROR;
+ goto cleanup;
+ }
+
+ memcpy(remote_name_out, remote_name, remote_name_size);
+ }
+ } else {
+ error = GIT_ENOTFOUND;
+ goto cleanup;
+ }
+
+cleanup:
+ git_strarray_free(&remote_list);
+ return error;
+}
+
int git_branch_tracking_name(
char *tracking_branch_name_out,
size_t buffer_size,
diff --git a/src/refspec.c b/src/refspec.c
index bd69f58..a51b0cf 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -159,11 +159,19 @@ int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
return (p_fnmatch(refspec->src, refname, 0) == 0);
}
-int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name)
+int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
+{
+ if (refspec == NULL || refspec->dst == NULL)
+ return false;
+
+ return (p_fnmatch(refspec->dst, refname, 0) == 0);
+}
+
+static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name)
{
size_t baselen, namelen;
- baselen = strlen(spec->dst);
+ baselen = strlen(to);
if (outlen <= baselen) {
giterr_set(GITERR_INVALID, "Reference name too long");
return GIT_EBUFS;
@@ -173,8 +181,8 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con
* No '*' at the end means that it's mapped to one specific local
* branch, so no actual transformation is needed.
*/
- if (spec->dst[baselen - 1] != '*') {
- memcpy(out, spec->dst, baselen + 1); /* include '\0' */
+ if (to[baselen - 1] != '*') {
+ memcpy(out, to, baselen + 1); /* include '\0' */
return 0;
}
@@ -182,7 +190,7 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con
baselen--;
/* skip the prefix, -1 is for the '*' */
- name += strlen(spec->src) - 1;
+ name += strlen(from) - 1;
namelen = strlen(name);
@@ -191,12 +199,22 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con
return GIT_EBUFS;
}
- memcpy(out, spec->dst, baselen);
+ memcpy(out, to, baselen);
memcpy(out + baselen, name, namelen + 1);
return 0;
}
+int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name)
+{
+ return refspec_transform_internal(out, outlen, spec->src, spec->dst, name);
+}
+
+int git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name)
+{
+ return refspec_transform_internal(out, outlen, spec->dst, spec->src, name);
+}
+
static int refspec_transform(git_buf *out, const char *from, const char *to, const char *name)
{
if (git_buf_sets(out, to) < 0)
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index b138d8c..51d6c94 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -173,13 +173,20 @@ void test_network_remotes__fnmatch(void)
void test_network_remotes__transform(void)
{
- char ref[1024];
+ char ref[1024] = {0};
- memset(ref, 0x0, sizeof(ref));
cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
cl_assert_equal_s(ref, "refs/remotes/test/master");
}
+void test_network_remotes__transform_destination_to_source(void)
+{
+ char ref[1024] = {0};
+
+ cl_git_pass(git_refspec_rtransform(ref, sizeof(ref), _refspec, "refs/remotes/test/master"));
+ cl_assert_equal_s(ref, "refs/heads/master");
+}
+
void test_network_remotes__transform_r(void)
{
git_buf buf = GIT_BUF_INIT;
diff --git a/tests-clar/refs/branches/remote.c b/tests-clar/refs/branches/remote.c
new file mode 100644
index 0000000..be355af
--- /dev/null
+++ b/tests-clar/refs/branches/remote.c
@@ -0,0 +1,119 @@
+#include "clar_libgit2.h"
+#include "branch.h"
+#include "remote.h"
+
+static git_repository *g_repo;
+
+static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
+
+void test_refs_branches_remote__initialize(void)
+{
+ git_oid id;
+
+ g_repo = cl_git_sandbox_init("testrepo");
+ git_oid_fromstr(&id, current_master_tip);
+
+ /* Create test/master */
+ git_reference_create(NULL, g_repo, "refs/remotes/test/master", &id, 1);
+}
+
+void test_refs_branches_remote__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_refs_branches_remote__can_get_remote_for_branch(void)
+{
+ git_reference *ref;
+ const char *name;
+ char *expectedRemoteName = "test";
+ int expectedRemoteNameLength = strlen(expectedRemoteName) + 1;
+ char remotename[1024] = {0};
+
+ cl_git_pass(git_branch_lookup(&ref, g_repo, "test/master", GIT_BRANCH_REMOTE));
+ cl_git_pass(git_branch_name(&name, ref));
+ cl_assert_equal_s("test/master", name);
+
+ cl_assert_equal_i(expectedRemoteNameLength,
+ git_branch_remote_name(NULL, 0, g_repo, ref));
+ cl_assert_equal_i(expectedRemoteNameLength,
+ git_branch_remote_name(remotename, expectedRemoteNameLength, g_repo, ref));
+ cl_assert_equal_s("test", remotename);
+
+ git_reference_free(ref);
+}
+
+void test_refs_branches_remote__insufficient_buffer_returns_error(void)
+{
+ git_reference *ref;
+ const char *name;
+ char *expectedRemoteName = "test";
+ int expectedRemoteNameLength = strlen(expectedRemoteName) + 1;
+ char remotename[1024] = {0};
+
+ cl_git_pass(git_branch_lookup(&ref, g_repo, "test/master", GIT_BRANCH_REMOTE));
+ cl_git_pass(git_branch_name(&name, ref));
+ cl_assert_equal_s("test/master", name);
+
+ cl_assert_equal_i(expectedRemoteNameLength,
+ git_branch_remote_name(NULL, 0, g_repo, ref));
+ cl_git_fail_with(GIT_ERROR,
+ git_branch_remote_name(remotename, expectedRemoteNameLength - 1, g_repo, ref));
+
+ git_reference_free(ref);
+}
+
+void test_refs_branches_remote__no_matching_remote_returns_error(void)
+{
+ git_reference *ref;
+ const char *name;
+ git_oid id;
+
+ git_oid_fromstr(&id, current_master_tip);
+
+ /* Create nonexistent/master */
+ git_reference_create(NULL, g_repo, "refs/remotes/nonexistent/master", &id, 1);
+
+ cl_git_pass(git_branch_lookup(&ref, g_repo,"nonexistent/master", GIT_BRANCH_REMOTE));
+ cl_git_pass(git_branch_name(&name, ref));
+ cl_assert_equal_s("nonexistent/master", name);
+
+ cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo, ref), GIT_ENOTFOUND);
+ git_reference_free(ref);
+}
+
+void test_refs_branches_remote__local_remote_returns_error(void)
+{
+ git_reference *ref;
+ const char *name;
+
+ cl_git_pass(git_branch_lookup(&ref,g_repo, "master", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_name(&name, ref));
+ cl_assert_equal_s("master",name);
+
+ cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo, ref), GIT_ERROR);
+ git_reference_free(ref);
+}
+
+void test_refs_branches_remote__ambiguous_remote_returns_error(void)
+{
+ git_reference *ref;
+ const char *name;
+ git_remote *remote;
+
+ /* Create the remote */
+ cl_git_pass(git_remote_create(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2"));
+
+ /* Update the remote fetch spec */
+ cl_git_pass(git_remote_set_fetchspec(remote, "refs/heads/*:refs/remotes/test/*"));
+ cl_git_pass(git_remote_save(remote));
+
+ git_remote_free(remote);
+
+ cl_git_pass(git_branch_lookup(&ref,g_repo, "test/master", GIT_BRANCH_REMOTE));
+ cl_git_pass(git_branch_name(&name, ref));
+ cl_assert_equal_s("test/master", name);
+
+ cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo, ref), GIT_EAMBIGUOUS);
+ git_reference_free(ref);
+}