branch: refactor git_branch_remote_name Return the size we'd need to write to instead of simply an error. Split the function into two to be used later by the upstream configuration functions.
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
diff --git a/src/branch.c b/src/branch.c
index 56c63a8..3b5d1d3 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -305,23 +305,16 @@ cleanup:
return error;
}
-int git_branch_remote_name(
- char *remote_name_out,
- size_t buffer_size,
- git_repository *repo,
- const char *canonical_branch_name)
+static int remote_name(git_buf *buf, git_repository *repo, const char *canonical_branch_name)
{
git_strarray remote_list = {0};
- size_t i, remote_name_size;
+ size_t i;
git_remote *remote;
const git_refspec *fetchspec;
int error = 0;
char *remote_name = NULL;
- assert(repo && canonical_branch_name);
-
- if (remote_name_out && buffer_size)
- *remote_name_out = '\0';
+ assert(buf && repo && canonical_branch_name);
/* Verify that this is a remote branch */
if (!git_reference__is_remote(canonical_branch_name)) {
@@ -362,23 +355,10 @@ int git_branch_remote_name(
}
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);
- }
+ git_buf_clear(buf);
+ error = git_buf_puts(buf, remote_name);
} else {
error = GIT_ENOTFOUND;
- goto cleanup;
}
cleanup:
@@ -386,6 +366,23 @@ cleanup:
return error;
}
+int git_branch_remote_name(char *buffer, size_t buffer_len, git_repository *repo, const char *refname)
+{
+ int ret;
+ git_buf buf = GIT_BUF_INIT;
+
+ if ((ret = remote_name(&buf, repo, refname)) < 0)
+ return ret;
+
+ if (buffer)
+ git_buf_copy_cstr(buffer, buffer_len, &buf);
+
+ ret = git_buf_len(&buf) + 1;
+ git_buf_free(&buf);
+
+ return ret;
+}
+
int git_branch_upstream_name(
char *tracking_branch_name_out,
size_t buffer_size,
diff --git a/tests-clar/refs/branches/remote.c b/tests-clar/refs/branches/remote.c
index 5272d12..2beef37 100644
--- a/tests-clar/refs/branches/remote.c
+++ b/tests-clar/refs/branches/remote.c
@@ -42,7 +42,7 @@ void test_refs_branches_remote__insufficient_buffer_returns_error(void)
cl_git_fail_with(git_branch_remote_name(remotename,
expected_remote_name_length - 1, g_repo, remote_tracking_branch_name),
- GIT_ERROR);
+ expected_remote_name_length);
}
void test_refs_branches_remote__no_matching_remote_returns_error(void)