Cloud storage: track the repository type and pass the information around

This is easier than having various parts of the code to the string
comparison on the URL.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-06-10 21:52:33 -07:00
parent 39a0ac965b
commit e81a67f9d3

View file

@ -35,6 +35,8 @@
git_branch_create(out, repo, branch_name, target, force) git_branch_create(out, repo, branch_name, target, force)
#endif #endif
enum remote_type { OTHER, HTTPS, SSH };
static char *get_local_dir(const char *remote, const char *branch) static char *get_local_dir(const char *remote, const char *branch)
{ {
SHA_CTX ctx; SHA_CTX ctx;
@ -99,7 +101,7 @@ static int reset_to_remote(git_repository *repo, git_reference *local, const git
return 0; return 0;
} }
static int update_remote(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote) static int update_remote(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote, enum remote_type rt)
{ {
git_push_options opts = GIT_PUSH_OPTIONS_INIT; git_push_options opts = GIT_PUSH_OPTIONS_INIT;
git_strarray refspec; git_strarray refspec;
@ -116,7 +118,7 @@ static int update_remote(git_repository *repo, git_remote *origin, git_reference
return 0; return 0;
} }
static int try_to_update(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote) static int try_to_update(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote, enum remote_type rt)
{ {
git_oid base; git_oid base;
const git_oid *local_id, *remote_id; const git_oid *local_id, *remote_id;
@ -144,7 +146,7 @@ static int try_to_update(git_repository *repo, git_remote *origin, git_reference
/* Is the local repo the more recent one? See if we can update upstream */ /* Is the local repo the more recent one? See if we can update upstream */
if (git_oid_equal(&base, remote_id)) if (git_oid_equal(&base, remote_id))
return update_remote(repo, origin, local, remote); return update_remote(repo, origin, local, remote, rt);
/* Merging a bare repository always needs user action */ /* Merging a bare repository always needs user action */
if (git_repository_is_bare(repo)) if (git_repository_is_bare(repo))
@ -190,7 +192,7 @@ int credential_https_cb(git_cred **out,
} }
#endif #endif
static int check_remote_status(git_repository *repo, git_remote *origin, const char *branch) static int check_remote_status(git_repository *repo, git_remote *origin, const char *branch, enum remote_type rt)
{ {
git_reference *local_ref, *remote_ref; git_reference *local_ref, *remote_ref;
@ -202,7 +204,7 @@ static int check_remote_status(git_repository *repo, git_remote *origin, const c
return report_error("Git cache branch %s no longer has an upstream branch", branch); return report_error("Git cache branch %s no longer has an upstream branch", branch);
} }
try_to_update(repo, origin, local_ref, remote_ref); try_to_update(repo, origin, local_ref, remote_ref, rt);
git_reference_free(local_ref); git_reference_free(local_ref);
git_reference_free(remote_ref); git_reference_free(remote_ref);
} }
@ -212,6 +214,7 @@ static git_repository *update_local_repo(const char *localdir, const char *remot
int error; int error;
git_repository *repo = NULL; git_repository *repo = NULL;
git_remote *origin; git_remote *origin;
enum remote_type rt;
error = git_repository_open(&repo, localdir); error = git_repository_open(&repo, localdir);
if (error) { if (error) {
@ -231,22 +234,27 @@ static git_repository *update_local_repo(const char *localdir, const char *remot
return repo; return repo;
} }
// NOTE! A fetch error is not fatal, we just report it if (strncmp(remote, "ssh://", 6) == 0)
rt = SSH;
else if (strncmp(remote, "https://", 8) == 0)
rt = HTTPS;
else
rt = OTHER;
#if USE_LIBGIT23_API #if USE_LIBGIT23_API
git_fetch_options opts = GIT_FETCH_OPTIONS_INIT; git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
if (strncmp(remote, "ssh://", 6) == 0) if (rt == SSH)
opts.callbacks.credentials = credential_ssh_cb; opts.callbacks.credentials = credential_ssh_cb;
else if (strncmp(remote, "https://", 8) == 0) else if (rt == HTTPS)
opts.callbacks.credentials = credential_https_cb; opts.callbacks.credentials = credential_https_cb;
error = git_remote_fetch(origin, NULL, &opts, NULL); error = git_remote_fetch(origin, NULL, &opts, NULL);
#else #else
error = git_remote_fetch(origin, NULL, NULL, NULL); error = git_remote_fetch(origin, NULL, NULL, NULL);
#endif #endif
// NOTE! A fetch error is not fatal, we just report it
if (error) if (error)
report_error("Unable to fetch remote '%s'", remote); report_error("Unable to fetch remote '%s'", remote);
else else
check_remote_status(repo, origin, branch); check_remote_status(repo, origin, branch, rt);
git_remote_free(origin); git_remote_free(origin);
return repo; return repo;