Commit 97016f29 by Carlos Martín Nieto

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.
parent a258d8e3
...@@ -305,23 +305,16 @@ cleanup: ...@@ -305,23 +305,16 @@ cleanup:
return error; return error;
} }
int git_branch_remote_name( static int remote_name(git_buf *buf, git_repository *repo, const char *canonical_branch_name)
char *remote_name_out,
size_t buffer_size,
git_repository *repo,
const char *canonical_branch_name)
{ {
git_strarray remote_list = {0}; git_strarray remote_list = {0};
size_t i, remote_name_size; size_t i;
git_remote *remote; git_remote *remote;
const git_refspec *fetchspec; const git_refspec *fetchspec;
int error = 0; int error = 0;
char *remote_name = NULL; char *remote_name = NULL;
assert(repo && canonical_branch_name); assert(buf && repo && canonical_branch_name);
if (remote_name_out && buffer_size)
*remote_name_out = '\0';
/* Verify that this is a remote branch */ /* Verify that this is a remote branch */
if (!git_reference__is_remote(canonical_branch_name)) { if (!git_reference__is_remote(canonical_branch_name)) {
...@@ -362,23 +355,10 @@ int git_branch_remote_name( ...@@ -362,23 +355,10 @@ int git_branch_remote_name(
} }
if (remote_name) { if (remote_name) {
remote_name_size = strlen(remote_name) + 1; git_buf_clear(buf);
error = (int) remote_name_size; error = git_buf_puts(buf, remote_name);
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 { } else {
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
goto cleanup;
} }
cleanup: cleanup:
...@@ -386,6 +366,23 @@ cleanup: ...@@ -386,6 +366,23 @@ cleanup:
return error; 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( int git_branch_upstream_name(
char *tracking_branch_name_out, char *tracking_branch_name_out,
size_t buffer_size, size_t buffer_size,
......
...@@ -42,7 +42,7 @@ void test_refs_branches_remote__insufficient_buffer_returns_error(void) ...@@ -42,7 +42,7 @@ void test_refs_branches_remote__insufficient_buffer_returns_error(void)
cl_git_fail_with(git_branch_remote_name(remotename, cl_git_fail_with(git_branch_remote_name(remotename,
expected_remote_name_length - 1, g_repo, remote_tracking_branch_name), 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) void test_refs_branches_remote__no_matching_remote_returns_error(void)
......
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