static int create_and_configure_origin( git_remote **out, git_repository *repo, const char *url, const git_clone_options *options) { int error; git_remote *origin = NULL; if ((error = git_remote_create(&origin, repo, options->remote_name, url)) < 0) goto on_error; git_remote_set_cred_acquire_cb(origin, options->cred_acquire_cb, options->cred_acquire_payload); git_remote_set_autotag(origin, options->remote_autotag); /* * Don't write FETCH_HEAD, we'll check out the remote tracking * branch ourselves based on the server's default. */ git_remote_set_update_fetchhead(origin, 0); if (options->remote_callbacks && (error = git_remote_set_callbacks(origin, options->remote_callbacks)) < 0) goto on_error; if (options->fetch_spec && (error = git_remote_set_fetchspec(origin, options->fetch_spec)) < 0) goto on_error; if (options->push_spec && (error = git_remote_set_pushspec(origin, options->push_spec)) < 0) goto on_error; if (options->pushurl && (error = git_remote_set_pushurl(origin, options->pushurl)) < 0) goto on_error; if ((error = git_remote_save(origin)) < 0) goto on_error; *out = origin; return 0; on_error: git_remote_free(origin); return error; }
static int handle(KDriver const *driver, KImplementation const *impl, int requested) { char const *url = k_dictionary_get(k_implementation_get_values(impl), "href"); char const *tag = k_dictionary_get(k_implementation_get_values(impl), "tag"); char const *path = k_dictionary_get(k_implementation_get_meta(impl), "name"); git_repository* repo = NULL; git_remote* origin = NULL; if (git_repository_open(&repo, path) == GIT_OK) { if (git_remote_load(&origin, repo, "origin") != GIT_OK) { git_repository_free(repo); return -1; } if (strcmp(url, git_remote_url(origin)) != 0) { giterr_set_str(GITERR_INVALID, "different origin"); git_repository_free(repo); return -1; } } else { if (git_repository_init(&repo, path, 0) != GIT_OK) { return -1; } if (git_remote_create(&origin, repo, "origin", url) != GIT_OK) { git_repository_free(repo); return -1; } } git_remote_set_update_fetchhead(origin, 0); git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); if (git_remote_connect(origin, GIT_DIRECTION_FETCH) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_remote_download(origin, transfer_progress, NULL) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_remote_update_tips(origin) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } git_object* object = NULL; if (git_revparse_single(&object, repo, tag) != GIT_OK) { git_remote_free(origin); git_repository_free(repo); return -1; } if (git_repository_set_head_detached(repo, git_object_id(object)) != GIT_OK) { git_object_free(object); git_remote_free(origin); git_repository_free(repo); return -1; } git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; checkout_opts.version = GIT_CHECKOUT_OPTS_VERSION; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; checkout_opts.progress_cb = checkout_progress; checkout_opts.progress_payload = NULL; if (git_checkout_tree(repo, object, &checkout_opts) != GIT_OK) { git_object_free(object); git_remote_free(origin); git_repository_free(repo); return -1; } git_object_free(object); git_remote_free(origin); git_repository_free(repo); return 0; }
void download(KImplementation const *impl, int requested, KError *error, void *ctx) { KDictionary const *values = k_implementation_get_values(impl); char const *href = k_dictionary_lookup(values, "href"); char const *tag = k_dictionary_lookup(values, "tag"); char const *path = k_implementation_get_name(impl); git_repository *repo = NULL; git_remote *origin = NULL; git_object *object = NULL; struct progress_data progress_data = {{0}}; git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; checkout_opts.version = GIT_CHECKOUT_OPTS_VERSION; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; checkout_opts.progress_cb = checkout_progress; checkout_opts.progress_payload = &progress_data; if (git_repository_open(&repo, path) == GIT_OK) { if (git_remote_load(&origin, repo, "origin") != GIT_OK) { k_error_set(error, giterr_last()->message); goto repository_free; } if (strcmp(git_remote_url(origin), href) != 0) { k_error_set(error, "different origin"); goto remote_free; } } else { if (git_repository_init(&repo, path, 0) != GIT_OK) { k_error_set(error, giterr_last()->message); return; } if (git_remote_create(&origin, repo, "origin", href) != GIT_OK) { k_error_set(error, giterr_last()->message); goto repository_free; } } git_remote_set_update_fetchhead(origin, 0); git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); if (git_remote_connect(origin, GIT_DIRECTION_FETCH) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_free; } if (git_remote_download(origin, fetch_progress, &progress_data) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_remote_update_tips(origin) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_revparse_single(&object, repo, tag) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_repository_set_head_detached(repo, git_object_id(object)) != GIT_OK) { k_error_set(error, giterr_last()->message); goto remote_disconnect; } if (git_checkout_tree(repo, object, &checkout_opts) != GIT_OK) { k_error_set(error, giterr_last()->message); } remote_disconnect: git_remote_disconnect(origin); remote_free: git_remote_free(origin); repository_free: git_repository_free(repo); }