diff options
| author | Vicent Marti <tanoku@gmail.com> | 2014-06-20 14:42:16 +0200 |
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2014-06-20 14:42:16 +0200 |
| commit | 28f087c8642ff9c8dd6964e101e6d8539db6281a (patch) | |
| tree | 3518d1bf420e92c964bed03074575d8a1db88654 /src/remote.c | |
| parent | 4b0a36e881506a02b43a4ae3c19c93c919b36eeb (diff) | |
| parent | 1589aa0c4d48fb130d8a5db28c45cd3d173cde6d (diff) | |
| download | libgit2-28f087c8642ff9c8dd6964e101e6d8539db6281a.tar.gz | |
libgit2 v0.21.0v0.21.0
Diffstat (limited to 'src/remote.c')
| -rw-r--r-- | src/remote.c | 851 |
1 files changed, 570 insertions, 281 deletions
diff --git a/src/remote.c b/src/remote.c index 3d890a5f1..47b61b1b1 100644 --- a/src/remote.c +++ b/src/remote.c @@ -45,7 +45,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch) static int download_tags_value(git_remote *remote, git_config *cfg) { - const char *val; + const git_config_entry *ce; git_buf buf = GIT_BUF_INIT; int error; @@ -53,16 +53,14 @@ static int download_tags_value(git_remote *remote, git_config *cfg) if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0) return -1; - error = git_config_get_string(&val, cfg, git_buf_cstr(&buf)); + error = git_config__lookup_entry(&ce, cfg, git_buf_cstr(&buf), false); git_buf_free(&buf); - if (!error && !strcmp(val, "--no-tags")) - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; - else if (!error && !strcmp(val, "--tags")) - remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; - if (error == GIT_ENOTFOUND) { - giterr_clear(); - error = 0; + if (!error && ce && ce->value) { + if (!strcmp(ce->value, "--no-tags")) + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_NONE; + else if (!strcmp(ce->value, "--tags")) + remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; } return error; @@ -75,7 +73,7 @@ static int ensure_remote_name_is_valid(const char *name) if (!git_remote_is_valid_name(name)) { giterr_set( GITERR_CONFIG, - "'%s' is not a valid remote name.", name); + "'%s' is not a valid remote name.", name ? name : "(null)"); error = GIT_EINVALIDSPEC; } @@ -104,12 +102,7 @@ static int get_check_cert(int *out, git_repository *repo) if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) return error; - if ((error = git_config_get_bool(out, cfg, "http.sslVerify")) == 0) - return 0; - else if (error != GIT_ENOTFOUND) - return error; - - giterr_clear(); + *out = git_config__get_bool_force(cfg, "http.sslverify", 1); return 0; } @@ -243,7 +236,7 @@ on_error: return -1; } -int git_remote_create_inmemory(git_remote **out, git_repository *repo, const char *fetch, const char *url) +int git_remote_create_anonymous(git_remote **out, git_repository *repo, const char *url, const char *fetch) { int error; git_remote *remote; @@ -255,6 +248,62 @@ int git_remote_create_inmemory(git_remote **out, git_repository *repo, const cha return 0; } +int git_remote_dup(git_remote **dest, git_remote *source) +{ + int error = 0; + git_strarray refspecs = { 0 }; + git_remote *remote = git__calloc(1, sizeof(git_remote)); + GITERR_CHECK_ALLOC(remote); + + if (source->name != NULL) { + remote->name = git__strdup(source->name); + GITERR_CHECK_ALLOC(remote->name); + } + + if (source->url != NULL) { + remote->url = git__strdup(source->url); + GITERR_CHECK_ALLOC(remote->url); + } + + if (source->pushurl != NULL) { + remote->pushurl = git__strdup(source->pushurl); + GITERR_CHECK_ALLOC(remote->pushurl); + } + + remote->repo = source->repo; + remote->download_tags = source->download_tags; + remote->check_cert = source->check_cert; + remote->update_fetchhead = source->update_fetchhead; + + if (git_vector_init(&remote->refs, 32, NULL) < 0 || + git_vector_init(&remote->refspecs, 2, NULL) < 0 || + git_vector_init(&remote->active_refspecs, 2, NULL) < 0) { + error = -1; + goto cleanup; + } + + if ((error = git_remote_get_fetch_refspecs(&refspecs, source)) < 0 || + (error = git_remote_set_fetch_refspecs(remote, &refspecs)) < 0) + goto cleanup; + + git_strarray_free(&refspecs); + + if ((error = git_remote_get_push_refspecs(&refspecs, source)) < 0 || + (error = git_remote_set_push_refspecs(remote, &refspecs)) < 0) + goto cleanup; + + *dest = remote; + +cleanup: + + git_strarray_free(&refspecs); + + if (error < 0) + git__free(remote); + + return error; +} + struct refspec_cb_data { git_remote *remote; int fetch; @@ -262,8 +311,7 @@ struct refspec_cb_data { static int refspec_cb(const git_config_entry *entry, void *payload) { - const struct refspec_cb_data *data = (struct refspec_cb_data *)payload; - + struct refspec_cb_data *data = (struct refspec_cb_data *)payload; return add_refspec(data->remote, entry->value, data->fetch); } @@ -290,9 +338,6 @@ static int get_optional_config( error = 0; } - if (error < 0) - error = -1; - return error; } @@ -303,7 +348,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) const char *val; int error = 0; git_config *config; - struct refspec_cb_data data; + struct refspec_cb_data data = { NULL }; bool optional_setting_found = false, found; assert(out && repo && name); @@ -311,8 +356,8 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) if ((error = ensure_remote_name_is_valid(name)) < 0) return error; - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; + if ((error = git_repository_config_snapshot(&config, repo)) < 0) + return error; remote = git__malloc(sizeof(git_remote)); GITERR_CHECK_ALLOC(remote); @@ -325,17 +370,15 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) if ((error = get_check_cert(&remote->check_cert, repo)) < 0) goto cleanup; - if ((git_vector_init(&remote->refs, 32, NULL) < 0) || - (git_vector_init(&remote->refspecs, 2, NULL) < 0) || - (git_vector_init(&remote->active_refspecs, 2, NULL) < 0)) { + if (git_vector_init(&remote->refs, 32, NULL) < 0 || + git_vector_init(&remote->refspecs, 2, NULL) < 0 || + git_vector_init(&remote->active_refspecs, 2, NULL) < 0) { error = -1; goto cleanup; } - if (git_buf_printf(&buf, "remote.%s.url", name) < 0) { - error = -1; + if ((error = git_buf_printf(&buf, "remote.%s.url", name)) < 0) goto cleanup; - } if ((error = get_optional_config(&found, config, &buf, NULL, (void *)&val)) < 0) goto cleanup; @@ -360,6 +403,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) if (!optional_setting_found) { error = GIT_ENOTFOUND; + giterr_set(GITERR_CONFIG, "Remote '%s' does not exist.", name); goto cleanup; } @@ -370,6 +414,7 @@ int git_remote_load(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); @@ -393,6 +438,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) *out = remote; cleanup: + git_config_free(config); git_buf_free(&buf); if (error < 0) @@ -451,57 +497,46 @@ cleanup: int git_remote_save(const git_remote *remote) { int error; - git_config *config; + git_config *cfg; const char *tagopt = NULL; git_buf buf = GIT_BUF_INIT; + const git_config_entry *existing; assert(remote); if (!remote->name) { - giterr_set(GITERR_INVALID, "Can't save an in-memory remote."); + giterr_set(GITERR_INVALID, "Can't save an anonymous remote."); return GIT_EINVALIDSPEC; } if ((error = ensure_remote_name_is_valid(remote->name)) < 0) return error; - if (git_repository_config__weakptr(&config, remote->repo) < 0) - return -1; + if ((error = git_repository_config__weakptr(&cfg, remote->repo)) < 0) + return error; - if (git_buf_printf(&buf, "remote.%s.url", remote->name) < 0) - return -1; + if ((error = git_buf_printf(&buf, "remote.%s.url", remote->name)) < 0) + return error; - if (git_config_set_string(config, git_buf_cstr(&buf), remote->url) < 0) { - git_buf_free(&buf); - return -1; - } + /* after this point, buffer is allocated so end with cleanup */ + + if ((error = git_config_set_string( + cfg, git_buf_cstr(&buf), remote->url)) < 0) + goto cleanup; git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.pushurl", remote->name) < 0) - return -1; + if ((error = git_buf_printf(&buf, "remote.%s.pushurl", remote->name)) < 0) + goto cleanup; - if (remote->pushurl) { - if (git_config_set_string(config, git_buf_cstr(&buf), remote->pushurl) < 0) { - git_buf_free(&buf); - return -1; - } - } else { - int error = git_config_delete_entry(config, git_buf_cstr(&buf)); - if (error == GIT_ENOTFOUND) { - error = 0; - giterr_clear(); - } - if (error < 0) { - git_buf_free(&buf); - return -1; - } - } + if ((error = git_config__update_entry( + cfg, git_buf_cstr(&buf), remote->pushurl, true, false)) < 0) + goto cleanup; - if (update_config_refspec(remote, config, GIT_DIRECTION_FETCH) < 0) - goto on_error; + if ((error = update_config_refspec(remote, cfg, GIT_DIRECTION_FETCH)) < 0) + goto cleanup; - if (update_config_refspec(remote, config, GIT_DIRECTION_PUSH) < 0) - goto on_error; + if ((error = update_config_refspec(remote, cfg, GIT_DIRECTION_PUSH)) < 0) + goto cleanup; /* * What action to take depends on the old and new values. This @@ -517,31 +552,26 @@ int git_remote_save(const git_remote *remote) */ git_buf_clear(&buf); - if (git_buf_printf(&buf, "remote.%s.tagopt", remote->name) < 0) - goto on_error; - - error = git_config_get_string(&tagopt, config, git_buf_cstr(&buf)); - if (error < 0 && error != GIT_ENOTFOUND) - goto on_error; + if ((error = git_buf_printf(&buf, "remote.%s.tagopt", remote->name)) < 0) + goto cleanup; - if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { - if (git_config_set_string(config, git_buf_cstr(&buf), "--tags") < 0) - goto on_error; - } else if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_NONE) { - if (git_config_set_string(config, git_buf_cstr(&buf), "--no-tags") < 0) - goto on_error; - } else if (tagopt) { - if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0) - goto on_error; - } + if ((error = git_config__lookup_entry( + &existing, cfg, git_buf_cstr(&buf), false)) < 0) + goto cleanup; - git_buf_free(&buf); + if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) + tagopt = "--tags"; + else if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_NONE) + tagopt = "--no-tags"; + else if (existing != NULL) + tagopt = NULL; - return 0; + error = git_config__update_entry( + cfg, git_buf_cstr(&buf), tagopt, true, false); -on_error: +cleanup: git_buf_free(&buf); - return -1; + return error; } const char *git_remote_name(const git_remote *remote) @@ -635,13 +665,13 @@ int git_remote_connect(git_remote *remote, git_direction direction) return error; if (t->set_callbacks && - (error = t->set_callbacks(t, remote->callbacks.progress, NULL, remote->callbacks.payload)) < 0) + (error = t->set_callbacks(t, remote->callbacks.sideband_progress, NULL, remote->callbacks.payload)) < 0) goto on_error; if (!remote->check_cert) flags |= GIT_TRANSPORTFLAGS_NO_CHECK_CERT; - if ((error = t->connect(t, url, remote->callbacks.credentials, remote->callbacks.payload, direction, flags)) < 0) + if ((error = t->connect(t, url, remote->callbacks.credentials, remote->callbacks.payload, direction, flags)) != 0) goto on_error; remote->transport = t; @@ -667,7 +697,8 @@ int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url) { git_config *cfg; - const char *val; + const git_config_entry *ce; + const char *val = NULL; int error; assert(remote); @@ -684,44 +715,39 @@ int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_ur * to least specific. */ /* remote.<name>.proxy config setting */ - if (remote->name && 0 != *(remote->name)) { + if (remote->name && remote->name[0]) { git_buf buf = GIT_BUF_INIT; if ((error = git_buf_printf(&buf, "remote.%s.proxy", remote->name)) < 0) return error; - if ((error = git_config_get_string(&val, cfg, git_buf_cstr(&buf))) == 0 && - val && ('\0' != *val)) { - git_buf_free(&buf); + error = git_config__lookup_entry(&ce, cfg, git_buf_cstr(&buf), false); + git_buf_free(&buf); - *proxy_url = git__strdup(val); - GITERR_CHECK_ALLOC(*proxy_url); - return 0; - } else if (error != GIT_ENOTFOUND) + if (error < 0) return error; - giterr_clear(); - git_buf_free(&buf); + if (ce && ce->value) { + val = ce->value; + goto found; + } } /* http.proxy config setting */ - if ((error = git_config_get_string(&val, cfg, "http.proxy")) == 0 && - val && ('\0' != *val)) { - *proxy_url = git__strdup(val); - GITERR_CHECK_ALLOC(*proxy_url); - return 0; - } else if (error != GIT_ENOTFOUND) + if ((error = git_config__lookup_entry(&ce, cfg, "http.proxy", false)) < 0) return error; - - giterr_clear(); + if (ce && ce->value) { + val = ce->value; + goto found; + } /* HTTP_PROXY / HTTPS_PROXY environment variables */ val = use_ssl ? getenv("HTTPS_PROXY") : getenv("HTTP_PROXY"); - if (val && ('\0' != *val)) { +found: + if (val && val[0]) { *proxy_url = git__strdup(val); GITERR_CHECK_ALLOC(*proxy_url); - return 0; } return 0; @@ -797,7 +823,7 @@ int git_remote_download(git_remote *remote) git_vector_free(&refs); if (error < 0) - return -1; + return error; if ((error = git_fetch_negotiate(remote)) < 0) return error; @@ -805,22 +831,39 @@ int git_remote_download(git_remote *remote) return git_fetch_download_pack(remote); } -int git_remote_fetch(git_remote *remote) +int git_remote_fetch( + git_remote *remote, + const git_signature *signature, + const char *reflog_message) { int error; + git_buf reflog_msg_buf = GIT_BUF_INIT; /* Connect and download everything */ - if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) < 0) + if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0) return error; - if ((error = git_remote_download(remote)) < 0) - return error; + error = git_remote_download(remote); /* We don't need to be connected anymore */ git_remote_disconnect(remote); + /* If the download failed, return the error */ + if (error != 0) + return error; + + /* Default reflog message */ + if (reflog_message) + git_buf_sets(&reflog_msg_buf, reflog_message); + else { + git_buf_printf(&reflog_msg_buf, "fetch %s", + remote->name ? remote->name : remote->url); + } + /* Create "remote/foo" branches for all remote branches */ - return git_remote_update_tips(remote); + error = git_remote_update_tips(remote, signature, git_buf_cstr(&reflog_msg_buf)); + git_buf_free(&reflog_msg_buf); + return error; } static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src) @@ -845,19 +888,32 @@ static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *upda static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vector *update_heads, git_reference *ref) { git_reference *resolved_ref = NULL; - git_reference *tracking_ref = NULL; git_buf remote_name = GIT_BUF_INIT; + git_buf upstream_name = GIT_BUF_INIT; + git_repository *repo; + const char *ref_name; int error = 0; assert(out && spec && ref); *out = NULL; - if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 || - (!git_reference_is_branch(resolved_ref)) || - (error = git_branch_upstream(&tracking_ref, resolved_ref)) < 0 || - (error = git_refspec_transform_l(&remote_name, spec, git_reference_name(tracking_ref))) < 0) { - /* Not an error if HEAD is unborn or no tracking branch */ + repo = git_reference_owner(ref); + + error = git_reference_resolve(&resolved_ref, ref); + + /* If we're in an unborn branch, let's pretend nothing happened */ + if (error == GIT_ENOTFOUND && git_reference_type(ref) == GIT_REF_SYMBOLIC) { + ref_name = git_reference_symbolic_target(ref); + error = 0; + } else { + ref_name = git_reference_name(resolved_ref); + } + + if ((!git_reference__is_branch(ref_name)) || + (error = git_branch_upstream_name(&upstream_name, repo, ref_name)) < 0 || + (error = git_refspec_rtransform(&remote_name, spec, upstream_name.ptr)) < 0) { + /* Not an error if there is no upstream */ if (error == GIT_ENOTFOUND) error = 0; @@ -867,9 +923,9 @@ static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vec error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name)); cleanup: - git_reference_free(tracking_ref); git_reference_free(resolved_ref); git_buf_free(&remote_name); + git_buf_free(&upstream_name); return error; } @@ -938,7 +994,12 @@ cleanup: return error; } -static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vector *refs) +static int update_tips_for_spec( + git_remote *remote, + git_refspec *spec, + git_vector *refs, + const git_signature *signature, + const char *log_message) { int error = 0, autotag; unsigned int i = 0; @@ -971,7 +1032,7 @@ static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vecto continue; if (git_refspec_src_matches(spec, head->name) && spec->dst) { - if (git_refspec_transform_r(&refname, spec, head->name) < 0) + if (git_refspec_transform(&refname, spec, head->name) < 0) goto on_error; } else if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_NONE) { @@ -1005,7 +1066,8 @@ static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vecto continue; /* In autotag mode, don't overwrite any locally-existing tags */ - error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag); + error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag, + signature, log_message); if (error < 0 && error != GIT_EEXISTS) goto on_error; @@ -1034,14 +1096,16 @@ on_error: } -int git_remote_update_tips(git_remote *remote) +int git_remote_update_tips( + git_remote *remote, + const git_signature *signature, + const char *reflog_message) { git_refspec *spec, tagspec; git_vector refs; int error; size_t i; - if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) return -1; @@ -1050,7 +1114,7 @@ int git_remote_update_tips(git_remote *remote) goto out; if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { - error = update_tips_for_spec(remote, &tagspec, &refs); + error = update_tips_for_spec(remote, &tagspec, &refs, signature, reflog_message); goto out; } @@ -1058,7 +1122,7 @@ int git_remote_update_tips(git_remote *remote) if (spec->push) continue; - if ((error = update_tips_for_spec(remote, spec, &refs)) < 0) + if ((error = update_tips_for_spec(remote, spec, &refs, signature, reflog_message)) < 0) goto out; } @@ -1068,7 +1132,7 @@ out: return error; } -int git_remote_connected(git_remote *remote) +int git_remote_connected(const git_remote *remote) { assert(remote); @@ -1141,40 +1205,28 @@ static int remote_list_cb(const git_config_entry *entry, void *payload) int git_remote_list(git_strarray *remotes_list, git_repository *repo) { - git_config *cfg; - git_vector list; int error; + git_config *cfg; + git_vector list = GIT_VECTOR_INIT; - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; - if (git_vector_init(&list, 4, git__strcmp_cb) < 0) - return -1; + if ((error = git_vector_init(&list, 4, git__strcmp_cb)) < 0) + return error; error = git_config_foreach_match( cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &list); if (error < 0) { - size_t i; - char *elem; - - git_vector_foreach(&list, i, elem) { - git__free(elem); - } - - git_vector_free(&list); - - /* cb error is converted to GIT_EUSER by git_config_foreach */ - if (error == GIT_EUSER) - error = -1; - + git_vector_free_deep(&list); return error; } git_vector_uniq(&list, git__free); - remotes_list->strings = (char **)list.contents; - remotes_list->count = list.length; + remotes_list->strings = + (char **)git_vector_detach(&remotes_list->count, NULL, &list); return 0; } @@ -1196,13 +1248,20 @@ int git_remote_set_callbacks(git_remote *remote, const git_remote_callbacks *cal if (remote->transport && remote->transport->set_callbacks) return remote->transport->set_callbacks(remote->transport, - remote->callbacks.progress, + remote->callbacks.sideband_progress, NULL, remote->callbacks.payload); return 0; } +const git_remote_callbacks *git_remote_get_callbacks(git_remote *remote) +{ + assert(remote); + + return &remote->callbacks; +} + int git_remote_set_transport(git_remote *remote, git_transport *transport) { assert(remote && transport); @@ -1224,7 +1283,7 @@ const git_transfer_progress* git_remote_stats(git_remote *remote) return &remote->stats; } -git_remote_autotag_option_t git_remote_autotag(git_remote *remote) +git_remote_autotag_option_t git_remote_autotag(const git_remote *remote) { return remote->download_tags; } @@ -1246,13 +1305,14 @@ static int rename_remote_config_section( if (git_buf_printf(&old_section_name, "remote.%s", old_name) < 0) goto cleanup; - if (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0) - goto cleanup; + if (new_name && + (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0)) + goto cleanup; error = git_config_rename_section( repo, git_buf_cstr(&old_section_name), - git_buf_cstr(&new_section_name)); + new_name ? git_buf_cstr(&new_section_name) : NULL); cleanup: git_buf_free(&old_section_name); @@ -1261,8 +1321,7 @@ cleanup: return error; } -struct update_data -{ +struct update_data { git_config *config; const char *old_remote_name; const char *new_remote_name; @@ -1278,9 +1337,7 @@ static int update_config_entries_cb( return 0; return git_config_set_string( - data->config, - entry->name, - data->new_remote_name); + data->config, entry->name, data->new_remote_name); } static int update_branch_remote_config_entry( @@ -1288,41 +1345,77 @@ static int update_branch_remote_config_entry( const char *old_name, const char *new_name) { - git_config *config; - struct update_data data; + int error; + struct update_data data = { NULL }; - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; + if ((error = git_repository_config__weakptr(&data.config, repo)) < 0) + return error; - data.config = config; data.old_remote_name = old_name; data.new_remote_name = new_name; return git_config_foreach_match( - config, - "branch\\..+\\.remote", - update_config_entries_cb, &data); + data.config, "branch\\..+\\.remote", update_config_entries_cb, &data); } static int rename_one_remote_reference( - git_reference *reference, + git_reference *reference_in, const char *old_remote_name, const char *new_remote_name) { - int error = -1; + 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; + size_t pfx_len; + const char *target; - if (git_buf_printf( - &new_name, - GIT_REFS_REMOTES_DIR "%s%s", - new_remote_name, - reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0) - return -1; + if ((error = git_buf_printf(&namespace, GIT_REFS_REMOTES_DIR "%s/", new_remote_name)) < 0) + return error; - error = git_reference_rename(NULL, reference, git_buf_cstr(&new_name), 0); - git_reference_free(reference); + 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) + goto cleanup; + + if ((error = git_buf_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, + NULL, git_buf_cstr(&log_message))) < 0) + goto cleanup; + + if (git_reference_type(ref) != GIT_REF_SYMBOLIC) + goto cleanup; + + /* 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) + 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) + goto cleanup; + + error = git_reference_symbolic_set_target(&dummy, ref, git_buf_cstr(&new_name), + NULL, git_buf_cstr(&log_message)); + + git_reference_free(dummy); + +cleanup: + git_reference_free(reference_in); + git_reference_free(ref); + git_buf_free(&namespace); + git_buf_free(&old_namespace); git_buf_free(&new_name); + git_buf_free(&log_message); return error; } @@ -1331,159 +1424,138 @@ static int rename_remote_references( const char *old_name, const char *new_name) { - int error = -1; + int error; + git_buf buf = GIT_BUF_INIT; git_reference *ref; git_reference_iterator *iter; - if (git_reference_iterator_new(&iter, repo) < 0) - return -1; + if ((error = git_buf_printf(&buf, GIT_REFS_REMOTES_DIR "%s/*", old_name)) < 0) + return error; - while ((error = git_reference_next(&ref, iter)) == 0) { - if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) { - git_reference_free(ref); - continue; - } + error = git_reference_iterator_glob_new(&iter, repo, git_buf_cstr(&buf)); + git_buf_free(&buf); - if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) { - git_reference_iterator_free(iter); - return error; - } + if (error < 0) + return error; + + while ((error = git_reference_next(&ref, iter)) == 0) { + if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) + break; } git_reference_iterator_free(iter); - if (error == GIT_ITEROVER) - return 0; - - return error; + return (error == GIT_ITEROVER) ? 0 : error; } -static int rename_fetch_refspecs( - git_remote *remote, - const char *new_name, - int (*callback)(const char *problematic_refspec, void *payload), - void *payload) +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; const git_refspec *spec; size_t i; - int error = -1; + int error = 0; - if (git_buf_printf(&base, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0) - goto cleanup; + if ((error = git_repository_config__weakptr(&config, remote->repo)) < 0) + return error; + + if ((error = git_vector_init(problems, 1, NULL)) < 0) + return error; + + if ((error = git_buf_printf( + &base, "+refs/heads/*:refs/remotes/%s/*", remote->name)) < 0) + return error; git_vector_foreach(&remote->refspecs, i, spec) { if (spec->push) continue; - /* Every refspec is a problem refspec for an in-memory remote */ - if (!remote->name) { - if (callback(spec->string, payload) < 0) { - error = GIT_EUSER; - goto cleanup; - } + /* Does the dst part of the refspec follow the expected format? */ + if (strcmp(git_buf_cstr(&base), spec->string)) { + char *dup; - continue; - } + dup = git__strdup(spec->string); + GITERR_CHECK_ALLOC(dup); - /* Does the dst part of the refspec follow the extected standard format? */ - if (strcmp(git_buf_cstr(&base), spec->string)) { - if (callback(spec->string, payload) < 0) { - error = GIT_EUSER; - goto cleanup; - } + if ((error = git_vector_insert(problems, dup)) < 0) + break; continue; } /* If we do want to move it to the new section */ - if (git_buf_printf(&val, "+refs/heads/*:refs/remotes/%s/*", new_name) < 0) - goto cleanup; - if (git_buf_printf(&var, "remote.%s.fetch", new_name) < 0) - goto cleanup; + git_buf_clear(&val); + git_buf_clear(&var); - if (git_repository_config__weakptr(&config, remote->repo) < 0) - goto cleanup; + if (git_buf_printf( + &val, "+refs/heads/*:refs/remotes/%s/*", new_name) < 0 || + git_buf_printf(&var, "remote.%s.fetch", new_name) < 0) + { + error = -1; + break; + } - if (git_config_set_string(config, git_buf_cstr(&var), git_buf_cstr(&val)) < 0) - goto cleanup; + if ((error = git_config_set_string( + config, git_buf_cstr(&var), git_buf_cstr(&val))) < 0) + break; } - error = 0; - -cleanup: git_buf_free(&base); git_buf_free(&var); git_buf_free(&val); + + if (error < 0) { + char *str; + git_vector_foreach(problems, i, str) + git__free(str); + + git_vector_free(problems); + } + return error; } -int git_remote_rename( - git_remote *remote, - const char *new_name, - git_remote_rename_problem_cb callback, - void *payload) +int git_remote_rename(git_strarray *out, git_remote *remote, const char *new_name) { int error; + git_vector problem_refspecs; + char *tmp, *dup; - assert(remote && new_name); + assert(out && remote && new_name); if (!remote->name) { - giterr_set(GITERR_INVALID, "Can't rename an in-memory remote."); + giterr_set(GITERR_INVALID, "Can't rename an anonymous remote."); return GIT_EINVALIDSPEC; } if ((error = ensure_remote_name_is_valid(new_name)) < 0) return error; - if (remote->repo) { - if ((error = ensure_remote_doesnot_exist(remote->repo, new_name)) < 0) - return error; + if ((error = ensure_remote_doesnot_exist(remote->repo, new_name)) < 0) + return error; - if (!remote->name) { - if ((error = rename_fetch_refspecs( - remote, - new_name, - callback, - payload)) < 0) - return error; + if ((error = rename_remote_config_section(remote->repo, remote->name, new_name)) < 0) + return error; - remote->name = git__strdup(new_name); + if ((error = update_branch_remote_config_entry(remote->repo, remote->name, new_name)) < 0) + return error; - if (!remote->name) return 0; - return git_remote_save(remote); - } + if ((error = rename_remote_references(remote->repo, remote->name, new_name)) < 0) + return error; - if ((error = rename_remote_config_section( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = update_branch_remote_config_entry( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = rename_remote_references( - remote->repo, - remote->name, - new_name)) < 0) - return error; - - if ((error = rename_fetch_refspecs( - remote, - new_name, - callback, - payload)) < 0) - return error; - } + if ((error = rename_fetch_refspecs(&problem_refspecs, remote, new_name)) < 0) + return error; - git__free(remote->name); - remote->name = git__strdup(new_name); + out->count = problem_refspecs.length; + out->strings = (char **) problem_refspecs.contents; + + dup = git__strdup(new_name); + GITERR_CHECK_ALLOC(dup); + + tmp = remote->name; + remote->name = dup; + git__free(tmp); return 0; } @@ -1626,7 +1698,7 @@ int git_remote_set_push_refspecs(git_remote *remote, git_strarray *array) return set_refspecs(remote, array, true); } -static int copy_refspecs(git_strarray *array, git_remote *remote, unsigned int push) +static int copy_refspecs(git_strarray *array, const git_remote *remote, unsigned int push) { size_t i; git_vector refspecs; @@ -1655,29 +1727,246 @@ static int copy_refspecs(git_strarray *array, git_remote *remote, unsigned int p return 0; on_error: - git_vector_foreach(&refspecs, i, dup) - git__free(dup); - git_vector_free(&refspecs); + git_vector_free_deep(&refspecs); return -1; } -int git_remote_get_fetch_refspecs(git_strarray *array, git_remote *remote) +int git_remote_get_fetch_refspecs(git_strarray *array, const git_remote *remote) { return copy_refspecs(array, remote, false); } -int git_remote_get_push_refspecs(git_strarray *array, git_remote *remote) +int git_remote_get_push_refspecs(git_strarray *array, const git_remote *remote) { return copy_refspecs(array, remote, true); } -size_t git_remote_refspec_count(git_remote *remote) +size_t git_remote_refspec_count(const git_remote *remote) { return remote->refspecs.length; } -const git_refspec *git_remote_get_refspec(git_remote *remote, size_t n) +const git_refspec *git_remote_get_refspec(const git_remote *remote, size_t n) { return git_vector_get(&remote->refspecs, n); } + +int git_remote_init_callbacks(git_remote_callbacks *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_remote_callbacks, GIT_REMOTE_CALLBACKS_INIT); + return 0; +} + +/* asserts a branch.<foo>.remote format */ +static const char *name_offset(size_t *len_out, const char *name) +{ + size_t prefix_len; + const char *dot; + + prefix_len = strlen("remote."); + dot = strchr(name + prefix_len, '.'); + + assert(dot); + + *len_out = dot - name - prefix_len; + return name + prefix_len; +} + +static int remove_branch_config_related_entries( + git_repository *repo, + const char *remote_name) +{ + int error; + git_config *config; + git_config_entry *entry; + git_config_iterator *iter; + git_buf buf = GIT_BUF_INIT; + + if ((error = git_repository_config__weakptr(&config, repo)) < 0) + return error; + + if ((error = git_config_iterator_glob_new(&iter, config, "branch\\..+\\.remote")) < 0) + return error; + + /* find any branches with us as upstream and remove that config */ + while ((error = git_config_next(&entry, iter)) == 0) { + const char *branch; + size_t branch_len; + + if (strcmp(remote_name, entry->value)) + continue; + + branch = name_offset(&branch_len, entry->name); + + git_buf_clear(&buf); + if (git_buf_printf(&buf, "branch.%.*s.merge", (int)branch_len, branch) < 0) + break; + + if ((error = git_config_delete_entry(config, git_buf_cstr(&buf))) < 0) + break; + + git_buf_clear(&buf); + if (git_buf_printf(&buf, "branch.%.*s.remote", (int)branch_len, branch) < 0) + break; + + if ((error = git_config_delete_entry(config, git_buf_cstr(&buf))) < 0) + break; + } + + if (error == GIT_ITEROVER) + error = 0; + + git_buf_free(&buf); + git_config_iterator_free(iter); + return error; +} + +static int remove_refs(git_repository *repo, const git_refspec *spec) +{ + git_reference_iterator *iter = NULL; + git_vector refs; + const char *name; + char *dup; + int error; + size_t i; + + if ((error = git_vector_init(&refs, 8, NULL)) < 0) + return error; + + if ((error = git_reference_iterator_new(&iter, repo)) < 0) + goto cleanup; + + while ((error = git_reference_next_name(&name, iter)) == 0) { + if (!git_refspec_dst_matches(spec, name)) + continue; + + dup = git__strdup(name); + if (!dup) { + error = -1; + goto cleanup; + } + + if ((error = git_vector_insert(&refs, dup)) < 0) + goto cleanup; + } + if (error == GIT_ITEROVER) + error = 0; + if (error < 0) + goto cleanup; + + git_vector_foreach(&refs, i, name) { + if ((error = git_reference_remove(repo, name)) < 0) + break; + } + +cleanup: + git_reference_iterator_free(iter); + git_vector_foreach(&refs, i, dup) { + git__free(dup); + } + git_vector_free(&refs); + return error; +} + +static int remove_remote_tracking(git_repository *repo, const char *remote_name) +{ + git_remote *remote; + int error; + size_t i, count; + + /* we want to use what's on the config, regardless of changes to the instance in memory */ + if ((error = git_remote_load(&remote, repo, remote_name)) < 0) + return error; + + count = git_remote_refspec_count(remote); + for (i = 0; i < count; i++) { + const git_refspec *refspec = git_remote_get_refspec(remote, i); + + /* shouldn't ever actually happen */ + if (refspec == NULL) + continue; + + if ((error = remove_refs(repo, refspec)) < 0) + break; + } + + git_remote_free(remote); + return error; +} + +int git_remote_delete(git_remote *remote) +{ + int error; + git_repository *repo; + + assert(remote); + + if (!remote->name) { + giterr_set(GITERR_INVALID, "Can't delete an anonymous remote."); + return -1; + } + + repo = git_remote_owner(remote); + + if ((error = remove_branch_config_related_entries(repo, + git_remote_name(remote))) < 0) + return error; + + if ((error = remove_remote_tracking(repo, git_remote_name(remote))) < 0) + return error; + + if ((error = rename_remote_config_section( + repo, git_remote_name(remote), NULL)) < 0) + return error; + + return 0; +} + +int git_remote_default_branch(git_buf *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; + int error; + + if ((error = git_remote_ls(&heads, &heads_len, remote)) < 0) + return error; + + if (heads_len == 0) + return GIT_ENOTFOUND; + + git_buf_sanitize(out); + /* the first one must be HEAD so if that has the symref info, we're done */ + if (heads[0]->symref_target) + return git_buf_puts(out, heads[0]->symref_target); + + /* + * If there's no symref information, we have to look over them + * and guess. We return the first match unless the master + * branch is a candidate. Then we return the master branch. + */ + head_id = &heads[0]->oid; + + for (i = 1; i < heads_len; i++) { + if (git_oid_cmp(head_id, &heads[i]->oid)) + continue; + + if (!guess) { + guess = heads[i]; + continue; + } + + if (!git__strcmp(GIT_REFS_HEADS_MASTER_FILE, heads[i]->name)) { + guess = heads[i]; + break; + } + } + + if (!guess) + return GIT_ENOTFOUND; + + return git_buf_puts(out, guess->name); +} |
