mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
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>
This commit is contained in:
parent
e6dce94088
commit
0c5ea57f1c
1 changed files with 42 additions and 2 deletions
44
git-access.c
44
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue