summaryrefslogtreecommitdiffstats
path: root/git-access.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-13 18:33:12 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-13 18:36:08 -0700
commit0c5ea57f1c69812fb85fac14ed4e380c3af03ca3 (patch)
treebd3687710d22ef32a372cccc2d2b3efd8d35c467 /git-access.c
parente6dce94088ded1174af6e4a1bb498e2944ee0aff (diff)
downloadsubsurface-0c5ea57f1c69812fb85fac14ed4e380c3af03ca3.tar.gz
Cloud storage: initialize local cache and remote on first use
There's no point in telling the user that the remote is empty. We need to instead fix that and create the local cache and set things up for the remote. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'git-access.c')
-rw-r--r--git-access.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/git-access.c b/git-access.c
index cb8a683be..8c834cb83 100644
--- a/git-access.c
+++ b/git-access.c
@@ -303,6 +303,44 @@ static int repository_create_cb(git_repository **out, const char *path, int bare
return ret;
}
+/* this should correctly initialize both the local and remote
+ * repository for the Subsurface cloud storage */
+static git_repository *create_and_push_remote(const char *localdir, const char *remote, const char *branch)
+{
+ git_repository *repo;
+ git_config *conf;
+ int len;
+ char *variable_name, *merge_head;
+
+ /* first make sure the directory for the local cache exists */
+ subsurface_mkdir(localdir);
+
+ /* set up the origin to point to our remote */
+ git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+ init_opts.origin_url = remote;
+
+ /* now initialize the repository with */
+ git_repository_init_ext(&repo, localdir, &init_opts);
+
+ /* create a config so we can set the remote tracking branch */
+ git_repository_config(&conf, repo);
+ len = sizeof("branch..remote") + strlen(branch);
+ variable_name = malloc(len);
+ snprintf(variable_name, len, "branch.%s.remote", branch);
+ git_config_set_string(conf, variable_name, "origin");
+ /* we know this is shorter than the previous one, so we reuse the variable*/
+ snprintf(variable_name, len, "branch.%s.merge", branch);
+ len = sizeof("refs/heads/") + strlen(branch);
+ merge_head = malloc(len);
+ snprintf(merge_head, len, "refs/heads/%s", branch);
+ git_config_set_string(conf, variable_name, merge_head);
+
+ /* finally create an empty commit and push it to the remote */
+ if (do_git_save(repo, branch, remote, false, true))
+ return NULL;
+ return(repo);
+}
+
static git_repository *create_local_repo(const char *localdir, const char *remote, const char *branch, enum remote_transport rt)
{
int error;
@@ -325,14 +363,16 @@ static git_repository *create_local_repo(const char *localdir, const char *remot
char *pattern = malloc(len);
snprintf(pattern, len, "Reference 'refs/remotes/origin/%s' not found", branch);
if (strstr(remote, "https://cloud.subsurface-divelog.org/git") && strstr(msg, pattern)) {
- report_error(translate("gettextFromC", "Subsurface cloud storage is empty"));
+ /* we're trying to open the remote branch that corresponds
+ * to our cloud storage and the branch doesn't exist.
+ * So we need to create the branch and push it to the remote */
+ cloned_repo = create_and_push_remote(localdir, remote, branch);
} else if (strstr(remote, "https://cloud.subsurface-divelog.org/git")) {
report_error(translate("gettextFromC", "Error connecting to Subsurface cloud storage"));
} else {
report_error(translate("gettextFromC", "git clone of %s failed (%s)"), remote, msg);
}
free(pattern);
- return NULL;
}
return cloned_repo;
}