Commit f0f3a18a by Carlos Martín Nieto

Move git_remote_load() to git_buf

parent 89e5ed98
...@@ -96,9 +96,9 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *url, cons ...@@ -96,9 +96,9 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *url, cons
int git_remote_load(git_remote **out, git_repository *repo, const char *name) int git_remote_load(git_remote **out, git_repository *repo, const char *name)
{ {
git_remote *remote; git_remote *remote;
char *buf = NULL; git_buf buf = GIT_BUF_INIT;
const char *val; const char *val;
int ret, error, buf_len; int error;
git_config *config; git_config *config;
assert(out && repo && name); assert(out && repo && name);
...@@ -123,21 +123,13 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -123,21 +123,13 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
goto cleanup; goto cleanup;
} }
/* "fetch" is the longest var name we're interested in */ git_buf_printf(&buf, "remote.%s.url", name);
buf_len = strlen("remote.") + strlen(".fetch") + strlen(name) + 1; if (git_buf_oom(&buf)) {
buf = git__malloc(buf_len);
if (buf == NULL) {
error = GIT_ENOMEM; error = GIT_ENOMEM;
goto cleanup; goto cleanup;
} }
ret = p_snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "url"); error = git_config_get_string(config, git_buf_cstr(&buf), &val);
if (ret < 0) {
error = git__throw(GIT_EOSERR, "Failed to build config var name");
goto cleanup;
}
error = git_config_get_string(config, buf, &val);
if (error < GIT_SUCCESS) { if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Remote's url doesn't exist"); error = git__rethrow(error, "Remote's url doesn't exist");
goto cleanup; goto cleanup;
...@@ -150,25 +142,27 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -150,25 +142,27 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
goto cleanup; goto cleanup;
} }
ret = p_snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "fetch"); git_buf_clear(&buf);
if (ret < 0) { git_buf_printf(&buf, "remote.%s.fetch", name);
error = git__throw(GIT_EOSERR, "Failed to build config var name"); if (git_buf_oom(&buf)) {
error = GIT_ENOMEM;
goto cleanup; goto cleanup;
} }
error = parse_remote_refspec(config, &remote->fetch, buf); error = parse_remote_refspec(config, &remote->fetch, git_buf_cstr(&buf));
if (error < GIT_SUCCESS) { if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Failed to get fetch refspec"); error = git__rethrow(error, "Failed to get fetch refspec");
goto cleanup; goto cleanup;
} }
ret = p_snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "push"); git_buf_clear(&buf);
if (ret < 0) { git_buf_printf(&buf, "remote.%s.push", name);
error = git__throw(GIT_EOSERR, "Failed to build config var name"); if (git_buf_oom(&buf)) {
error = GIT_ENOMEM;
goto cleanup; goto cleanup;
} }
error = parse_remote_refspec(config, &remote->push, buf); error = parse_remote_refspec(config, &remote->push, git_buf_cstr(&buf));
/* Not finding push is fine */ /* Not finding push is fine */
if (error == GIT_ENOTFOUND) if (error == GIT_ENOTFOUND)
error = GIT_SUCCESS; error = GIT_SUCCESS;
...@@ -179,7 +173,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -179,7 +173,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
*out = remote; *out = remote;
cleanup: cleanup:
git__free(buf); git_buf_free(&buf);
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
git_remote_free(remote); git_remote_free(remote);
......
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