summaryrefslogtreecommitdiff
path: root/src/remote.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-07 17:53:49 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-17 09:49:01 -0400
commitf0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch)
treebe5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/remote.c
parent5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff)
downloadlibgit2-ethomson/gitstr.tar.gz
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by `git_buf`. We require: 1. A general purpose string class that provides a number of utility APIs for manipulating data (eg, concatenating, truncating, etc). 2. A structure that we can use to return strings to callers that they can take ownership of. By using a single class (`git_buf`) for both of these purposes, we have confused the API to the point that refactorings are difficult and reasoning about correctness is also difficult. Move the utility class `git_buf` to be called `git_str`: this represents its general purpose, as an internal string buffer class. The name also is an homage to Junio Hamano ("gitstr"). The public API remains `git_buf`, and has a much smaller footprint. It is generally only used as an "out" param with strict requirements that follow the documentation. (Exceptions exist for some legacy APIs to avoid breaking callers unnecessarily.) Utility functions exist to convert a user-specified `git_buf` to a `git_str` so that we can call internal functions, then converting it back again.
Diffstat (limited to 'src/remote.c')
-rw-r--r--src/remote.c359
1 files changed, 182 insertions, 177 deletions
diff --git a/src/remote.c b/src/remote.c
index 56d7e42db..bde4ce779 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -7,11 +7,8 @@
#include "remote.h"
-#include "git2/config.h"
-#include "git2/types.h"
-#include "git2/oid.h"
-#include "git2/net.h"
-
+#include "buf.h"
+#include "branch.h"
#include "config.h"
#include "repository.h"
#include "fetch.h"
@@ -20,6 +17,11 @@
#include "fetchhead.h"
#include "push.h"
+#include "git2/config.h"
+#include "git2/types.h"
+#include "git2/oid.h"
+#include "git2/net.h"
+
#define CONFIG_URL_FMT "remote.%s.url"
#define CONFIG_PUSHURL_FMT "remote.%s.pushurl"
#define CONFIG_FETCH_FMT "remote.%s.fetch"
@@ -60,14 +62,14 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
static int download_tags_value(git_remote *remote, git_config *cfg)
{
git_config_entry *ce;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
int error;
- if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0)
+ if (git_str_printf(&buf, "remote.%s.tagopt", remote->name) < 0)
return -1;
- error = git_config__lookup_entry(&ce, cfg, git_buf_cstr(&buf), false);
- git_buf_dispose(&buf);
+ error = git_config__lookup_entry(&ce, cfg, git_str_cstr(&buf), false);
+ git_str_dispose(&buf);
if (!error && ce && ce->value) {
if (!strcmp(ce->value, "--no-tags"))
@@ -99,7 +101,7 @@ static int ensure_remote_name_is_valid(const char *name)
static int write_add_refspec(git_repository *repo, const char *name, const char *refspec, bool fetch)
{
git_config *cfg;
- git_buf var = GIT_BUF_INIT;
+ git_str var = GIT_STR_INIT;
git_refspec spec;
const char *fmt;
int error;
@@ -117,7 +119,7 @@ static int write_add_refspec(git_repository *repo, const char *name, const char
git_refspec__dispose(&spec);
- if ((error = git_buf_printf(&var, fmt, name)) < 0)
+ if ((error = git_str_printf(&var, fmt, name)) < 0)
return error;
/*
@@ -130,11 +132,11 @@ static int write_add_refspec(git_repository *repo, const char *name, const char
}
cleanup:
- git_buf_dispose(&var);
+ git_str_dispose(&var);
return 0;
}
-static int canonicalize_url(git_buf *out, const char *in)
+static int canonicalize_url(git_str *out, const char *in)
{
if (in == NULL || strlen(in) == 0) {
git_error_set(GIT_ERROR_INVALID, "cannot set empty URL");
@@ -149,18 +151,18 @@ static int canonicalize_url(git_buf *out, const char *in)
(git__isalpha(in[2]) || git__isdigit(in[2]))) {
const char *c;
for (c = in; *c; c++)
- git_buf_putc(out, *c == '\\' ? '/' : *c);
+ git_str_putc(out, *c == '\\' ? '/' : *c);
- return git_buf_oom(out) ? -1 : 0;
+ return git_str_oom(out) ? -1 : 0;
}
#endif
- return git_buf_puts(out, in);
+ return git_str_puts(out, in);
}
-static int default_fetchspec_for_name(git_buf *buf, const char *name)
+static int default_fetchspec_for_name(git_str *buf, const char *name)
{
- if (git_buf_printf(buf, "+refs/heads/*:refs/remotes/%s/*", name) < 0)
+ if (git_str_printf(buf, "+refs/heads/*:refs/remotes/%s/*", name) < 0)
return -1;
return 0;
@@ -204,9 +206,9 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
{
git_remote *remote = NULL;
git_config *config_ro = NULL, *config_rw;
- git_buf canonical_url = GIT_BUF_INIT;
- git_buf var = GIT_BUF_INIT;
- git_buf specbuf = GIT_BUF_INIT;
+ git_str canonical_url = GIT_STR_INIT;
+ git_str var = GIT_STR_INIT;
+ git_str specbuf = GIT_STR_INIT;
const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
int error = -1;
@@ -254,7 +256,7 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
GIT_ERROR_CHECK_ALLOC(remote->name);
if (opts->repository &&
- ((error = git_buf_printf(&var, CONFIG_URL_FMT, opts->name)) < 0 ||
+ ((error = git_str_printf(&var, CONFIG_URL_FMT, opts->name)) < 0 ||
(error = git_repository_config__weakptr(&config_rw, opts->repository)) < 0 ||
(error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0))
goto on_error;
@@ -269,7 +271,7 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
if ((error = default_fetchspec_for_name(&specbuf, opts->name)) < 0)
goto on_error;
- fetch = git_buf_cstr(&specbuf);
+ fetch = git_str_cstr(&specbuf);
}
if ((error = add_refspec(remote, fetch, true)) < 0)
@@ -293,7 +295,7 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
- git_buf_dispose(&var);
+ git_str_dispose(&var);
*out = remote;
error = 0;
@@ -303,15 +305,15 @@ on_error:
git_remote_free(remote);
git_config_free(config_ro);
- git_buf_dispose(&specbuf);
- git_buf_dispose(&canonical_url);
- git_buf_dispose(&var);
+ git_str_dispose(&specbuf);
+ git_str_dispose(&canonical_url);
+ git_str_dispose(&var);
return error;
}
int git_remote_create(git_remote **out, git_repository *repo, const char *name, const char *url)
{
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
int error;
git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
@@ -322,14 +324,14 @@ int git_remote_create(git_remote **out, git_repository *repo, const char *name,
if (canonicalize_url(&buf, url) < 0)
return GIT_ERROR;
- git_buf_clear(&buf);
+ git_str_clear(&buf);
opts.repository = repo;
opts.name = name;
error = git_remote_create_with_opts(out, url, &opts);
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
return error;
}
@@ -425,13 +427,13 @@ static int refspec_cb(const git_config_entry *entry, void *payload)
}
static int get_optional_config(
- bool *found, git_config *config, git_buf *buf,
+ bool *found, git_config *config, git_str *buf,
git_config_foreach_cb cb, void *payload)
{
int error = 0;
- const char *key = git_buf_cstr(buf);
+ const char *key = git_str_cstr(buf);
- if (git_buf_oom(buf))
+ if (git_str_oom(buf))
return -1;
if (cb != NULL)
@@ -453,7 +455,7 @@ static int get_optional_config(
int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
{
git_remote *remote = NULL;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
const char *val;
int error = 0;
git_config *config;
@@ -484,7 +486,7 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
goto cleanup;
}
- if ((error = git_buf_printf(&buf, "remote.%s.url", name)) < 0)
+ if ((error = git_str_printf(&buf, "remote.%s.url", name)) < 0)
goto cleanup;
if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0)
@@ -501,8 +503,8 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
}
val = NULL;
- git_buf_clear(&buf);
- git_buf_printf(&buf, "remote.%s.pushurl", name);
+ git_str_clear(&buf);
+ git_str_printf(&buf, "remote.%s.pushurl", name);
if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0)
goto cleanup;
@@ -523,15 +525,15 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
data.remote = remote;
data.fetch = true;
- git_buf_clear(&buf);
- git_buf_printf(&buf, "remote.%s.fetch", name);
+ git_str_clear(&buf);
+ git_str_printf(&buf, "remote.%s.fetch", name);
if ((error = get_optional_config(NULL, config, &buf, refspec_cb, &data)) < 0)
goto cleanup;
data.fetch = false;
- git_buf_clear(&buf);
- git_buf_printf(&buf, "remote.%s.push", name);
+ git_str_clear(&buf);
+ git_str_printf(&buf, "remote.%s.push", name);
if ((error = get_optional_config(NULL, config, &buf, refspec_cb, &data)) < 0)
goto cleanup;
@@ -550,7 +552,7 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
cleanup:
git_config_free(config);
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
if (error < 0)
git_remote_free(remote);
@@ -560,12 +562,12 @@ cleanup:
static int lookup_remote_prune_config(git_remote *remote, git_config *config, const char *name)
{
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
int error = 0;
- git_buf_printf(&buf, "remote.%s.prune", name);
+ git_str_printf(&buf, "remote.%s.prune", name);
- if ((error = git_config_get_bool(&remote->prune_refs, config, git_buf_cstr(&buf))) < 0) {
+ if ((error = git_config_get_bool(&remote->prune_refs, config, git_str_cstr(&buf))) < 0) {
if (error == GIT_ENOTFOUND) {
git_error_clear();
@@ -578,7 +580,7 @@ static int lookup_remote_prune_config(git_remote *remote, git_config *config, co
}
}
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
return error;
}
@@ -619,7 +621,7 @@ int git_remote_set_instance_url(git_remote *remote, const char *url)
static int set_url(git_repository *repo, const char *remote, const char *pattern, const char *url)
{
git_config *cfg;
- git_buf buf = GIT_BUF_INIT, canonical_url = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT, canonical_url = GIT_STR_INIT;
int error;
GIT_ASSERT_ARG(repo);
@@ -631,7 +633,7 @@ static int set_url(git_repository *repo, const char *remote, const char *pattern
if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
return error;
- if ((error = git_buf_printf(&buf, pattern, remote)) < 0)
+ if ((error = git_str_printf(&buf, pattern, remote)) < 0)
return error;
if (url) {
@@ -644,8 +646,8 @@ static int set_url(git_repository *repo, const char *remote, const char *pattern
}
cleanup:
- git_buf_dispose(&canonical_url);
- git_buf_dispose(&buf);
+ git_str_dispose(&canonical_url);
+ git_str_dispose(&buf);
return error;
}
@@ -683,7 +685,7 @@ int git_remote_set_pushurl(git_repository *repo, const char *remote, const char
}
static int resolve_url(
- git_buf *resolved_url,
+ git_str *resolved_url,
const char *url,
int direction,
const git_remote_callbacks *callbacks)
@@ -692,27 +694,28 @@ static int resolve_url(
GIT_UNUSED(direction);
GIT_UNUSED(callbacks);
#else
- int status, error;
+ git_buf buf = GIT_BUF_INIT;
+ int error;
if (callbacks && callbacks->resolve_url) {
- git_buf_clear(resolved_url);
- status = callbacks->resolve_url(resolved_url, url, direction, callbacks->payload);
- if (status != GIT_PASSTHROUGH) {
- git_error_set_after_callback_function(status, "git_resolve_url_cb");
+ error = callbacks->resolve_url(&buf, url, direction, callbacks->payload);
- if ((error = git_buf_sanitize(resolved_url)) < 0)
- return error;
+ if (error != GIT_PASSTHROUGH) {
+ git_error_set_after_callback_function(error, "git_resolve_url_cb");
- return status;
+ git_str_set(resolved_url, buf.ptr, buf.size);
+ git_buf_dispose(&buf);
+
+ return error;
}
}
#endif
- return git_buf_sets(resolved_url, url);
+ return git_str_sets(resolved_url, url);
}
int git_remote__urlfordirection(
- git_buf *url_out,
+ git_str *url_out,
struct git_remote *remote,
int direction,
const git_remote_callbacks *callbacks)
@@ -767,7 +770,7 @@ static int set_transport_custom_headers(git_transport *t, const git_strarray *cu
int git_remote__connect(git_remote *remote, git_direction direction, const git_remote_callbacks *callbacks, const git_remote_connection_opts *conn)
{
git_transport *t;
- git_buf url = GIT_BUF_INIT;
+ git_str url = GIT_STR_INIT;
int flags = GIT_TRANSPORTFLAGS_NONE;
int error;
void *payload = NULL;
@@ -811,7 +814,7 @@ int git_remote__connect(git_remote *remote, git_direction direction, const git_r
remote->transport = t;
- git_buf_dispose(&url);
+ git_str_dispose(&url);
return 0;
@@ -819,7 +822,7 @@ on_error:
if (t)
t->free(t);
- git_buf_dispose(&url);
+ git_str_dispose(&url);
if (t == remote->transport)
remote->transport = NULL;
@@ -885,7 +888,7 @@ static void url_config_trim(git_net_url *url)
static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
{
git_config *cfg = NULL;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
git_net_url lookup_url = GIT_NET_URL_INIT;
int error;
@@ -902,19 +905,19 @@ static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
/* remote.<name>.proxy config setting */
if (remote->name && remote->name[0]) {
- git_buf_clear(&buf);
+ git_str_clear(&buf);
- if ((error = git_buf_printf(&buf, "remote.%s.proxy", remote->name)) < 0 ||
+ if ((error = git_str_printf(&buf, "remote.%s.proxy", remote->name)) < 0 ||
(error = lookup_config(out, cfg, buf.ptr)) != GIT_ENOTFOUND)
goto done;
}
while (true) {
- git_buf_clear(&buf);
+ git_str_clear(&buf);
- if ((error = git_buf_puts(&buf, "http.")) < 0 ||
+ if ((error = git_str_puts(&buf, "http.")) < 0 ||
(error = git_net_url_fmt(&buf, &lookup_url)) < 0 ||
- (error = git_buf_puts(&buf, ".proxy")) < 0 ||
+ (error = git_str_puts(&buf, ".proxy")) < 0 ||
(error = lookup_config(out, cfg, buf.ptr)) != GIT_ENOTFOUND)
goto done;
@@ -924,20 +927,20 @@ static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
url_config_trim(&lookup_url);
}
- git_buf_clear(&buf);
+ git_str_clear(&buf);
error = lookup_config(out, cfg, "http.proxy");
done:
git_config_free(cfg);
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
git_net_url_dispose(&lookup_url);
return error;
}
static int http_proxy_env(char **out, git_remote *remote, git_net_url *url)
{
- git_buf proxy_env = GIT_BUF_INIT, no_proxy_env = GIT_BUF_INIT;
+ git_str proxy_env = GIT_STR_INIT, no_proxy_env = GIT_STR_INIT;
bool use_ssl = (strcmp(url->scheme, "https") == 0);
int error;
@@ -963,13 +966,13 @@ static int http_proxy_env(char **out, git_remote *remote, git_net_url *url)
goto done;
if (!git_net_url_matches_pattern_list(url, no_proxy_env.ptr))
- *out = git_buf_detach(&proxy_env);
+ *out = git_str_detach(&proxy_env);
else
error = GIT_ENOTFOUND;
done:
- git_buf_dispose(&proxy_env);
- git_buf_dispose(&no_proxy_env);
+ git_str_dispose(&proxy_env);
+ git_str_dispose(&no_proxy_env);
return error;
}
@@ -1135,7 +1138,7 @@ int git_remote_fetch(
int error, update_fetchhead = 1;
git_remote_autotag_option_t tagopt = remote->download_tags;
bool prune = false;
- git_buf reflog_msg_buf = GIT_BUF_INIT;
+ git_str reflog_msg_buf = GIT_STR_INIT;
const git_remote_callbacks *cbs = NULL;
git_remote_connection_opts conn = GIT_REMOTE_CONNECTION_OPTIONS_INIT;
@@ -1164,15 +1167,15 @@ int git_remote_fetch(
/* Default reflog message */
if (reflog_message)
- git_buf_sets(&reflog_msg_buf, reflog_message);
+ git_str_sets(&reflog_msg_buf, reflog_message);
else {
- git_buf_printf(&reflog_msg_buf, "fetch %s",
+ git_str_printf(&reflog_msg_buf, "fetch %s",
remote->name ? remote->name : remote->url);
}
/* Create "remote/foo" branches for all remote branches */
- error = git_remote_update_tips(remote, cbs, update_fetchhead, tagopt, git_buf_cstr(&reflog_msg_buf));
- git_buf_dispose(&reflog_msg_buf);
+ error = git_remote_update_tips(remote, cbs, update_fetchhead, tagopt, git_str_cstr(&reflog_msg_buf));
+ git_str_dispose(&reflog_msg_buf);
if (error < 0)
return error;
@@ -1211,22 +1214,22 @@ static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *upda
return 0;
}
-static int ref_to_update(int *update, git_buf *remote_name, git_remote *remote, git_refspec *spec, const char *ref_name)
+static int ref_to_update(int *update, git_str *remote_name, git_remote *remote, git_refspec *spec, const char *ref_name)
{
int error = 0;
git_repository *repo;
- git_buf upstream_remote = GIT_BUF_INIT;
- git_buf upstream_name = GIT_BUF_INIT;
+ git_str upstream_remote = GIT_STR_INIT;
+ git_str upstream_name = GIT_STR_INIT;
repo = git_remote_owner(remote);
if ((!git_reference__is_branch(ref_name)) ||
!git_remote_name(remote) ||
- (error = git_branch_upstream_remote(&upstream_remote, repo, ref_name) < 0) ||
- 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_branch__upstream_remote(&upstream_remote, repo, ref_name) < 0) ||
+ git__strcmp(git_remote_name(remote), git_str_cstr(&upstream_remote)) ||
+ (error = git_branch__upstream_name(&upstream_name, repo, ref_name)) < 0 ||
+ !git_refspec_dst_matches(spec, git_str_cstr(&upstream_name)) ||
+ (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();
@@ -1238,15 +1241,15 @@ static int ref_to_update(int *update, git_buf *remote_name, git_remote *remote,
*update = 1;
}
- git_buf_dispose(&upstream_remote);
- git_buf_dispose(&upstream_name);
+ git_str_dispose(&upstream_remote);
+ git_str_dispose(&upstream_name);
return error;
}
static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_refspec *spec, git_vector *update_heads, git_reference *ref)
{
git_reference *resolved_ref = NULL;
- git_buf remote_name = GIT_BUF_INIT;
+ git_str remote_name = GIT_STR_INIT;
git_config *config = NULL;
const char *ref_name;
int error = 0, update;
@@ -1281,10 +1284,10 @@ static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_re
goto cleanup;
if (update)
- error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name));
+ error = remote_head_for_fetchspec_src(out, update_heads, git_str_cstr(&remote_name));
cleanup:
- git_buf_dispose(&remote_name);
+ git_str_dispose(&remote_name);
git_reference_free(resolved_ref);
git_config_free(config);
return error;
@@ -1422,7 +1425,7 @@ int git_remote_prune(git_remote *remote, const git_remote_callbacks *callbacks)
*/
git_vector_foreach(&candidates, i, refname) {
git_vector_foreach(&remote->active_refspecs, j, spec) {
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
size_t pos;
char *src_name;
git_remote_head key = {0};
@@ -1430,12 +1433,12 @@ 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);
+ key.name = (char *) git_str_cstr(&buf);
error = git_vector_bsearch(&pos, &remote_refs, &key);
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
@@ -1508,7 +1511,7 @@ static int update_tips_for_spec(
{
int error = 0, autotag, valid;
unsigned int i = 0;
- git_buf refname = GIT_BUF_INIT;
+ git_str refname = GIT_STR_INIT;
git_oid old;
git_odb *odb;
git_remote_head *head;
@@ -1531,7 +1534,7 @@ static int update_tips_for_spec(
for (; i < refs->length; ++i) {
head = git_vector_get(refs, i);
autotag = 0;
- git_buf_clear(&refname);
+ git_str_clear(&refname);
/* Ignore malformed ref names (which also saves us from tag^{} */
if (git_reference_name_is_valid(&valid, head->name) < 0)
@@ -1547,8 +1550,8 @@ static int update_tips_for_spec(
if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_AUTO)
autotag = 1;
- git_buf_clear(&refname);
- if (git_buf_puts(&refname, head->name) < 0)
+ git_str_clear(&refname);
+ if (git_str_puts(&refname, head->name) < 0)
goto on_error;
}
}
@@ -1556,7 +1559,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 {
/*
@@ -1571,7 +1574,7 @@ static int update_tips_for_spec(
}
/* If we still don't have a refname, we don't want it */
- if (git_buf_len(&refname) == 0) {
+ if (git_str_len(&refname) == 0) {
continue;
}
@@ -1625,13 +1628,13 @@ static int update_tips_for_spec(
git_vector_free(&update_heads);
git_refspec__dispose(&tagspec);
- git_buf_dispose(&refname);
+ git_str_dispose(&refname);
return 0;
on_error:
git_vector_free(&update_heads);
git_refspec__dispose(&tagspec);
- git_buf_dispose(&refname);
+ git_str_dispose(&refname);
return -1;
}
@@ -1704,7 +1707,7 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call
git_refspec *spec;
git_remote_head *head;
git_reference *ref;
- git_buf refname = GIT_BUF_INIT;
+ git_str refname = GIT_STR_INIT;
int error = 0;
i = j = k = 0;
@@ -1719,8 +1722,8 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call
* FETCH_HEAD
*/
- git_buf_clear(&refname);
- if ((error = git_refspec_transform(&refname, spec, head->name)) < 0)
+ git_str_clear(&refname);
+ if ((error = git_refspec__transform(&refname, spec, head->name)) < 0)
goto cleanup;
error = git_reference_name_to_id(&old, remote->repo, refname.ptr);
@@ -1749,20 +1752,20 @@ static int opportunistic_updates(const git_remote *remote, const git_remote_call
error = 0;
cleanup:
- git_buf_dispose(&refname);
+ git_str_dispose(&refname);
return error;
}
static int truncate_fetch_head(const char *gitdir)
{
- git_buf path = GIT_BUF_INIT;
+ git_str path = GIT_STR_INIT;
int error;
- if ((error = git_buf_joinpath(&path, gitdir, GIT_FETCH_HEAD_FILE)) < 0)
+ if ((error = git_str_joinpath(&path, gitdir, GIT_FETCH_HEAD_FILE)) < 0)
return error;
error = git_futils_truncate(path.ptr, GIT_REFS_FILE_MODE);
- git_buf_dispose(&path);
+ git_str_dispose(&path);
return error;
}
@@ -1943,7 +1946,7 @@ git_remote_autotag_option_t git_remote_autotag(const git_remote *remote)
int git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_autotag_option_t value)
{
- git_buf var = GIT_BUF_INIT;
+ git_str var = GIT_STR_INIT;
git_config *config;
int error;
@@ -1955,7 +1958,7 @@ int git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
return error;
- if ((error = git_buf_printf(&var, CONFIG_TAGOPT_FMT, remote)))
+ if ((error = git_str_printf(&var, CONFIG_TAGOPT_FMT, remote)))
return error;
switch (value) {
@@ -1975,7 +1978,7 @@ int git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_
error = -1;
}
- git_buf_dispose(&var);
+ git_str_dispose(&var);
return error;
}
@@ -1989,25 +1992,25 @@ static int rename_remote_config_section(
const char *old_name,
const char *new_name)
{
- git_buf old_section_name = GIT_BUF_INIT,
- new_section_name = GIT_BUF_INIT;
+ git_str old_section_name = GIT_STR_INIT,
+ new_section_name = GIT_STR_INIT;
int error = -1;
- if (git_buf_printf(&old_section_name, "remote.%s", old_name) < 0)
+ if (git_str_printf(&old_section_name, "remote.%s", old_name) < 0)
goto cleanup;
if (new_name &&
- (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0))
+ (git_str_printf(&new_section_name, "remote.%s", new_name) < 0))
goto cleanup;
error = git_config_rename_section(
repo,
- git_buf_cstr(&old_section_name),
- new_name ? git_buf_cstr(&new_section_name) : NULL);
+ git_str_cstr(&old_section_name),
+ new_name ? git_str_cstr(&new_section_name) : NULL);
cleanup:
- git_buf_dispose(&old_section_name);
- git_buf_dispose(&new_section_name);
+ git_str_dispose(&old_section_name);
+ git_str_dispose(&new_section_name);
return error;
}
@@ -2056,27 +2059,27 @@ static int rename_one_remote_reference(
{
int error;
git_reference *ref = NULL, *dummy = NULL;
- git_buf namespace = GIT_BUF_INIT, old_namespace = GIT_BUF_INIT;
- git_buf new_name = GIT_BUF_INIT;
- git_buf log_message = GIT_BUF_INIT;
+ git_str namespace = GIT_STR_INIT, old_namespace = GIT_STR_INIT;
+ git_str new_name = GIT_STR_INIT;
+ git_str log_message = GIT_STR_INIT;
size_t pfx_len;
const char *target;
- if ((error = git_buf_printf(&namespace, GIT_REFS_REMOTES_DIR "%s/", new_remote_name)) < 0)
+ if ((error = git_str_printf(&namespace, GIT_REFS_REMOTES_DIR "%s/", new_remote_name)) < 0)
return error;
pfx_len = strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name) + 1;
- git_buf_puts(&new_name, namespace.ptr);
- if ((error = git_buf_puts(&new_name, git_reference_name(reference_in) + pfx_len)) < 0)
+ git_str_puts(&new_name, namespace.ptr);
+ if ((error = git_str_puts(&new_name, git_reference_name(reference_in) + pfx_len)) < 0)
goto cleanup;
- if ((error = git_buf_printf(&log_message,
+ if ((error = git_str_printf(&log_message,
"renamed remote %s to %s",
old_remote_name, new_remote_name)) < 0)
goto cleanup;
- if ((error = git_reference_rename(&ref, reference_in, git_buf_cstr(&new_name), 1,
- git_buf_cstr(&log_message))) < 0)
+ if ((error = git_reference_rename(&ref, reference_in, git_str_cstr(&new_name), 1,
+ git_str_cstr(&log_message))) < 0)
goto cleanup;
if (git_reference_type(ref) != GIT_REFERENCE_SYMBOLIC)
@@ -2084,29 +2087,29 @@ static int rename_one_remote_reference(
/* Handle refs like origin/HEAD -> origin/master */
target = git_reference_symbolic_target(ref);
- if ((error = git_buf_printf(&old_namespace, GIT_REFS_REMOTES_DIR "%s/", old_remote_name)) < 0)
+ if ((error = git_str_printf(&old_namespace, GIT_REFS_REMOTES_DIR "%s/", old_remote_name)) < 0)
goto cleanup;
if (git__prefixcmp(target, old_namespace.ptr))
goto cleanup;
- git_buf_clear(&new_name);
- git_buf_puts(&new_name, namespace.ptr);
- if ((error = git_buf_puts(&new_name, target + pfx_len)) < 0)
+ git_str_clear(&new_name);
+ git_str_puts(&new_name, namespace.ptr);
+ if ((error = git_str_puts(&new_name, target + pfx_len)) < 0)
goto cleanup;
- error = git_reference_symbolic_set_target(&dummy, ref, git_buf_cstr(&new_name),
- git_buf_cstr(&log_message));
+ error = git_reference_symbolic_set_target(&dummy, ref, git_str_cstr(&new_name),
+ git_str_cstr(&log_message));
git_reference_free(dummy);
cleanup:
git_reference_free(reference_in);
git_reference_free(ref);
- git_buf_dispose(&namespace);
- git_buf_dispose(&old_namespace);
- git_buf_dispose(&new_name);
- git_buf_dispose(&log_message);
+ git_str_dispose(&namespace);
+ git_str_dispose(&old_namespace);
+ git_str_dispose(&new_name);
+ git_str_dispose(&log_message);
return error;
}
@@ -2116,15 +2119,15 @@ static int rename_remote_references(
const char *new_name)
{
int error;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
git_reference *ref;
git_reference_iterator *iter;
- if ((error = git_buf_printf(&buf, GIT_REFS_REMOTES_DIR "%s/*", old_name)) < 0)
+ if ((error = git_str_printf(&buf, GIT_REFS_REMOTES_DIR "%s/*", old_name)) < 0)
return error;
- error = git_reference_iterator_glob_new(&iter, repo, git_buf_cstr(&buf));
- git_buf_dispose(&buf);
+ error = git_reference_iterator_glob_new(&iter, repo, git_str_cstr(&buf));
+ git_str_dispose(&buf);
if (error < 0)
return error;
@@ -2142,7 +2145,7 @@ static int rename_remote_references(
static int rename_fetch_refspecs(git_vector *problems, git_remote *remote, const char *new_name)
{
git_config *config;
- git_buf base = GIT_BUF_INIT, var = GIT_BUF_INIT, val = GIT_BUF_INIT;
+ git_str base = GIT_STR_INIT, var = GIT_STR_INIT, val = GIT_STR_INIT;
const git_refspec *spec;
size_t i;
int error = 0;
@@ -2161,7 +2164,7 @@ static int rename_fetch_refspecs(git_vector *problems, git_remote *remote, const
continue;
/* Does the dst part of the refspec follow the expected format? */
- if (strcmp(git_buf_cstr(&base), spec->string)) {
+ if (strcmp(git_str_cstr(&base), spec->string)) {
char *dup;
dup = git__strdup(spec->string);
@@ -2175,24 +2178,24 @@ static int rename_fetch_refspecs(git_vector *problems, git_remote *remote, const
/* If we do want to move it to the new section */
- git_buf_clear(&val);
- git_buf_clear(&var);
+ git_str_clear(&val);
+ git_str_clear(&var);
if (default_fetchspec_for_name(&val, new_name) < 0 ||
- git_buf_printf(&var, "remote.%s.fetch", new_name) < 0)
+ git_str_printf(&var, "remote.%s.fetch", new_name) < 0)
{
error = -1;
break;
}
if ((error = git_config_set_string(
- config, git_buf_cstr(&var), git_buf_cstr(&val))) < 0)
+ config, git_str_cstr(&var), git_str_cstr(&val))) < 0)
break;
}
- git_buf_dispose(&base);
- git_buf_dispose(&var);
- git_buf_dispose(&val);
+ git_str_dispose(&base);
+ git_str_dispose(&var);
+ git_str_dispose(&val);
if (error < 0) {
char *str;
@@ -2247,7 +2250,7 @@ cleanup:
int git_remote_name_is_valid(int *valid, const char *remote_name)
{
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
git_refspec refspec = {0};
int error;
@@ -2258,10 +2261,10 @@ int git_remote_name_is_valid(int *valid, const char *remote_name)
if (!remote_name || *remote_name == '\0')
return 0;
- if ((error = git_buf_printf(&buf, "refs/heads/test:refs/remotes/%s/test", remote_name)) < 0)
+ if ((error = git_str_printf(&buf, "refs/heads/test:refs/remotes/%s/test", remote_name)) < 0)
goto done;
- error = git_refspec__parse(&refspec, git_buf_cstr(&buf), true);
+ error = git_refspec__parse(&refspec, git_str_cstr(&buf), true);
if (!error)
*valid = 1;
@@ -2269,7 +2272,7 @@ int git_remote_name_is_valid(int *valid, const char *remote_name)
error = 0;
done:
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
git_refspec__dispose(&refspec);
return error;
@@ -2401,7 +2404,7 @@ static int remove_branch_config_related_entries(
git_config *config;
git_config_entry *entry;
git_config_iterator *iter;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
return error;
@@ -2422,21 +2425,21 @@ static int remove_branch_config_related_entries(
break;
}
- git_buf_clear(&buf);
- if ((error = git_buf_printf(&buf, "branch.%.*s.merge", (int)branch_len, branch)) < 0)
+ git_str_clear(&buf);
+ if ((error = git_str_printf(&buf, "branch.%.*s.merge", (int)branch_len, branch)) < 0)
break;
- if ((error = git_config_delete_entry(config, git_buf_cstr(&buf))) < 0) {
+ if ((error = git_config_delete_entry(config, git_str_cstr(&buf))) < 0) {
if (error != GIT_ENOTFOUND)
break;
git_error_clear();
}
- git_buf_clear(&buf);
- if ((error = git_buf_printf(&buf, "branch.%.*s.remote", (int)branch_len, branch)) < 0)
+ git_str_clear(&buf);
+ if ((error = git_str_printf(&buf, "branch.%.*s.remote", (int)branch_len, branch)) < 0)
break;
- if ((error = git_config_delete_entry(config, git_buf_cstr(&buf))) < 0) {
+ if ((error = git_config_delete_entry(config, git_str_cstr(&buf))) < 0) {
if (error != GIT_ENOTFOUND)
break;
git_error_clear();
@@ -2446,7 +2449,7 @@ static int remove_branch_config_related_entries(
if (error == GIT_ITEROVER)
error = 0;
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
git_config_iterator_free(iter);
return error;
}
@@ -2541,11 +2544,16 @@ int git_remote_delete(git_repository *repo, const char *name)
int git_remote_default_branch(git_buf *out, git_remote *remote)
{
+ GIT_BUF_WRAP_PRIVATE(out, git_remote__default_branch, remote);
+}
+
+int git_remote__default_branch(git_str *out, git_remote *remote)
+{
const git_remote_head **heads;
const git_remote_head *guess = NULL;
const git_oid *head_id;
size_t heads_len, i;
- git_buf local_default = GIT_BUF_INIT;
+ git_str local_default = GIT_STR_INIT;
int error;
GIT_ASSERT_ARG(out);
@@ -2558,12 +2566,9 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
goto done;
}
- if ((error = git_buf_sanitize(out)) < 0)
- return error;
-
/* the first one must be HEAD so if that has the symref info, we're done */
if (heads[0]->symref_target) {
- error = git_buf_puts(out, heads[0]->symref_target);
+ error = git_str_puts(out, heads[0]->symref_target);
goto done;
}
@@ -2601,10 +2606,10 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
goto done;
}
- error = git_buf_puts(out, guess->name);
+ error = git_str_puts(out, guess->name);
done:
- git_buf_dispose(&local_default);
+ git_str_dispose(&local_default);
return error;
}
@@ -2720,7 +2725,7 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
char *replacement = NULL;
const char *regexp;
- git_buf result = GIT_BUF_INIT;
+ git_str result = GIT_STR_INIT;
git_config_entry *entry;
git_config_iterator *iter;
@@ -2768,7 +2773,7 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
if (match_length == 0)
return git__strdup(url);
- git_buf_printf(&result, "%s%s", replacement, url + match_length);
+ git_str_printf(&result, "%s%s", replacement, url + match_length);
git__free(replacement);