2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-03-11 20:08:31 +00:00
|
|
|
#ifdef __clang__
|
2016-03-09 18:17:47 +00:00
|
|
|
// Clang has a bug on zero-initialization of C structs.
|
|
|
|
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
|
2017-03-11 20:08:31 +00:00
|
|
|
#endif
|
2016-03-09 18:17:47 +00:00
|
|
|
|
2015-01-23 03:40:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <git2.h>
|
|
|
|
|
|
|
|
#include "dive.h"
|
2015-02-08 18:35:25 +00:00
|
|
|
#include "membuffer.h"
|
2015-06-01 06:12:30 +00:00
|
|
|
#include "strndup.h"
|
2015-06-12 13:45:56 +00:00
|
|
|
#include "qthelperfromc.h"
|
2015-06-13 15:01:06 +00:00
|
|
|
#include "git-access.h"
|
2015-06-13 16:12:16 +00:00
|
|
|
#include "gettext.h"
|
2015-02-08 18:35:25 +00:00
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
bool is_subsurface_cloud = false;
|
|
|
|
|
2017-06-18 06:22:37 +00:00
|
|
|
int (*update_progress_cb)(const char *) = NULL;
|
2015-09-09 20:02:39 +00:00
|
|
|
|
2017-06-18 06:22:37 +00:00
|
|
|
void set_git_update_cb(int(*cb)(const char *))
|
2015-09-09 20:02:39 +00:00
|
|
|
{
|
|
|
|
update_progress_cb = cb;
|
|
|
|
}
|
|
|
|
|
2016-04-04 00:26:05 +00:00
|
|
|
// total overkill, but this allows us to get good timing in various scenarios;
|
|
|
|
// the various parts of interacting with the local and remote git repositories send
|
|
|
|
// us updates which indicate progress (and no, this is not smooth and definitely not
|
|
|
|
// proportional - some parts are based on compute performance, some on network speed)
|
|
|
|
// they also provide information where in the process we are so we can analyze the log
|
|
|
|
// to understand which parts of the process take how much time.
|
2017-06-18 06:22:37 +00:00
|
|
|
int git_storage_update_progress(const char *text)
|
2015-09-09 20:02:39 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if (update_progress_cb)
|
2017-06-18 06:22:37 +00:00
|
|
|
ret = (*update_progress_cb)(text);
|
2015-09-09 20:02:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the checkout_progress_cb doesn't allow canceling of the operation
|
2016-04-06 05:54:54 +00:00
|
|
|
// map the git progress to 20% of overall progress
|
2015-09-09 20:02:39 +00:00
|
|
|
static void progress_cb(const char *path, size_t completed_steps, size_t total_steps, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) path;
|
|
|
|
(void) payload;
|
2017-06-18 06:22:37 +00:00
|
|
|
char buf[80];
|
|
|
|
snprintf(buf, sizeof(buf), translate("gettextFromC", "Checkout from storage (%lu/%lu)"), completed_steps, total_steps);
|
|
|
|
(void)git_storage_update_progress(buf);
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this randomly assumes that 80% of the time is spent on the objects and 20% on the deltas
|
2016-04-06 05:54:54 +00:00
|
|
|
// map the git progress to 20% of overall progress
|
2015-09-09 20:02:39 +00:00
|
|
|
// if the user cancels the dialog this is passed back to libgit2
|
|
|
|
static int transfer_progress_cb(const git_transfer_progress *stats, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) payload;
|
|
|
|
|
2017-06-18 06:22:37 +00:00
|
|
|
static int last_done = -1;
|
|
|
|
char buf[80];
|
|
|
|
int done = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
if (stats->total_objects) {
|
|
|
|
total = 60;
|
|
|
|
done = 60 * stats->received_objects / stats->total_objects;
|
|
|
|
}
|
|
|
|
if (stats->total_deltas) {
|
|
|
|
total += 20;
|
|
|
|
done += 20 * stats->indexed_deltas / stats->total_deltas;
|
|
|
|
}
|
2016-04-04 00:26:05 +00:00
|
|
|
/* for debugging this is useful
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, 100, "transfer cb rec_obj %d tot_obj %d idx_delta %d total_delta %d local obj %d", stats->received_objects, stats->total_objects, stats->indexed_deltas, stats->total_deltas, stats->local_objects);
|
2017-06-18 06:22:37 +00:00
|
|
|
return git_storage_update_progress(buf);
|
2016-04-04 00:26:05 +00:00
|
|
|
*/
|
2017-06-18 06:22:37 +00:00
|
|
|
if (done > last_done) {
|
|
|
|
last_done = done;
|
|
|
|
snprintf(buf, sizeof(buf), translate("gettextFromC", "Transfer from storage (%d/%d)"), done, total);
|
|
|
|
return git_storage_update_progress(buf);
|
2016-04-06 05:54:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 05:54:54 +00:00
|
|
|
// the initial push to sync the repos is mapped to 10% of overall progress
|
2015-09-10 16:41:12 +00:00
|
|
|
static int push_transfer_progress_cb(unsigned int current, unsigned int total, size_t bytes, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) bytes;
|
|
|
|
(void) payload;
|
2017-06-18 06:22:37 +00:00
|
|
|
char buf[80];
|
|
|
|
snprintf(buf, sizeof(buf), translate("gettextFromC", "Transfer to storage (%d/%d)"), current, total);
|
2017-06-18 06:50:22 +00:00
|
|
|
return git_storage_update_progress(buf);
|
2015-09-10 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 20:59:20 +00:00
|
|
|
char *get_local_dir(const char *remote, const char *branch)
|
2015-02-08 18:35:25 +00:00
|
|
|
{
|
|
|
|
SHA_CTX ctx;
|
|
|
|
unsigned char hash[20];
|
|
|
|
|
|
|
|
// That zero-byte update is so that we don't get hash
|
|
|
|
// collisions for "repo1 branch" vs "repo 1branch".
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
SHA1_Update(&ctx, remote, strlen(remote));
|
|
|
|
SHA1_Update(&ctx, "", 1);
|
|
|
|
SHA1_Update(&ctx, branch, strlen(branch));
|
|
|
|
SHA1_Final(hash, &ctx);
|
|
|
|
|
2015-10-06 10:10:20 +00:00
|
|
|
return format_string("%s/cloudstorage/%02x%02x%02x%02x%02x%02x%02x%02x",
|
2015-02-15 19:55:52 +00:00
|
|
|
system_default_directory(),
|
2015-02-08 18:35:25 +00:00
|
|
|
hash[0], hash[1], hash[2], hash[3],
|
|
|
|
hash[4], hash[5], hash[6], hash[7]);
|
|
|
|
}
|
|
|
|
|
2015-09-23 12:14:46 +00:00
|
|
|
static char *move_local_cache(const char *remote, const char *branch)
|
|
|
|
{
|
|
|
|
char *old_path = get_local_dir(remote, branch);
|
|
|
|
return move_away(old_path);
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
static int check_clean(const char *path, unsigned int status, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) payload;
|
2015-02-08 18:35:25 +00:00
|
|
|
status &= ~GIT_STATUS_CURRENT | GIT_STATUS_IGNORED;
|
|
|
|
if (!status)
|
|
|
|
return 0;
|
2015-09-18 18:51:50 +00:00
|
|
|
if (is_subsurface_cloud)
|
|
|
|
report_error(translate("gettextFromC", "Local cache directory %s corrupted - can't sync with Subsurface cloud storage"), path);
|
|
|
|
else
|
|
|
|
report_error("WARNING: Git cache directory modified (path %s) status %0x", path, status);
|
2015-02-08 18:35:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-10 20:37:22 +00:00
|
|
|
/*
|
|
|
|
* The remote is strictly newer than the local branch.
|
|
|
|
*/
|
|
|
|
static int reset_to_remote(git_repository *repo, git_reference *local, const git_oid *new_id)
|
2015-02-08 18:35:25 +00:00
|
|
|
{
|
2015-06-10 20:37:22 +00:00
|
|
|
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
|
2015-09-09 20:02:39 +00:00
|
|
|
opts.progress_cb = &progress_cb;
|
2015-06-10 20:37:22 +00:00
|
|
|
git_object *target;
|
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: reset to remote\n");
|
|
|
|
|
2015-06-10 20:37:22 +00:00
|
|
|
// If it's not checked out (bare or not HEAD), just update the reference */
|
|
|
|
if (git_repository_is_bare(repo) || git_branch_is_head(local) != 1) {
|
|
|
|
git_reference *out;
|
|
|
|
|
|
|
|
if (git_reference_set_target(&out, local, new_id, "Update to remote"))
|
2015-09-18 18:51:50 +00:00
|
|
|
return report_error(translate("gettextFromC", "Could not update local cache to newer remote data"));
|
2015-06-10 20:37:22 +00:00
|
|
|
|
|
|
|
git_reference_free(out);
|
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
#ifdef DEBUG
|
2015-06-10 20:37:22 +00:00
|
|
|
// Not really an error, just informational
|
|
|
|
report_error("Updated local branch from remote");
|
2015-09-18 18:51:50 +00:00
|
|
|
#endif
|
2015-06-10 20:37:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_object_lookup(&target, repo, new_id, GIT_OBJ_COMMIT)) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
return report_error(translate("gettextFromC", "Subsurface cloud storage corrupted"));
|
|
|
|
else
|
|
|
|
return report_error("Could not look up remote commit");
|
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_reset(repo, target, GIT_RESET_HARD, &opts)) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
return report_error(translate("gettextFromC", "Could not update local cache to newer remote data"));
|
|
|
|
else
|
|
|
|
return report_error("Local head checkout failed after update");
|
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
// Not really an error, just informational
|
2015-09-18 18:51:50 +00:00
|
|
|
#ifdef DEBUG
|
2015-06-10 20:37:22 +00:00
|
|
|
report_error("Updated local information from remote");
|
2015-09-18 18:51:50 +00:00
|
|
|
#endif
|
2015-06-10 20:37:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:39:40 +00:00
|
|
|
static int auth_attempt = 0;
|
|
|
|
|
2015-06-11 04:54:42 +00:00
|
|
|
int credential_ssh_cb(git_cred **out,
|
|
|
|
const char *url,
|
|
|
|
const char *username_from_url,
|
|
|
|
unsigned int allowed_types,
|
|
|
|
void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) url;
|
|
|
|
(void) allowed_types;
|
|
|
|
(void) payload;
|
|
|
|
|
2015-06-11 04:54:42 +00:00
|
|
|
const char *priv_key = format_string("%s/%s", system_default_directory(), "ssrf_remote.key");
|
|
|
|
const char *passphrase = prefs.cloud_storage_password ? strdup(prefs.cloud_storage_password) : strdup("");
|
2016-04-25 16:56:16 +00:00
|
|
|
|
|
|
|
/* Bail out from libgit authentication loop when credentials are
|
|
|
|
* incorrect */
|
|
|
|
|
2016-08-14 16:39:40 +00:00
|
|
|
if (auth_attempt++ > 2) {
|
2016-04-25 16:56:16 +00:00
|
|
|
report_error("Authentication to cloud storage failed.");
|
|
|
|
return GIT_EUSER;
|
|
|
|
}
|
|
|
|
|
2015-06-11 04:54:42 +00:00
|
|
|
return git_cred_ssh_key_new(out, username_from_url, NULL, priv_key, passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
int credential_https_cb(git_cred **out,
|
|
|
|
const char *url,
|
|
|
|
const char *username_from_url,
|
|
|
|
unsigned int allowed_types,
|
|
|
|
void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) url;
|
|
|
|
(void) username_from_url;
|
|
|
|
(void) payload;
|
2016-03-07 19:18:05 +00:00
|
|
|
(void) allowed_types;
|
2015-06-11 04:54:42 +00:00
|
|
|
const char *username = prefs.cloud_storage_email_encoded;
|
|
|
|
const char *password = prefs.cloud_storage_password ? strdup(prefs.cloud_storage_password) : strdup("");
|
2016-04-25 16:56:16 +00:00
|
|
|
|
|
|
|
/* Bail out from libgit authentication loop when credentials are
|
|
|
|
* incorrect */
|
|
|
|
|
2016-08-14 16:39:40 +00:00
|
|
|
if (auth_attempt++ > 2) {
|
2016-04-25 16:56:16 +00:00
|
|
|
report_error("Authentication to cloud storage failed.");
|
|
|
|
return GIT_EUSER;
|
|
|
|
}
|
2015-06-11 04:54:42 +00:00
|
|
|
return git_cred_userpass_plaintext_new(out, username, password);
|
|
|
|
}
|
2015-07-13 18:20:18 +00:00
|
|
|
|
|
|
|
#define KNOWN_CERT "\xfd\xb8\xf7\x73\x76\xe2\x75\x53\x93\x37\xdc\xfe\x1e\x55\x43\x3d\xf2\x2c\x18\x2c"
|
|
|
|
int certificate_check_cb(git_cert *cert, int valid, const char *host, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) payload;
|
2015-07-13 18:20:18 +00:00
|
|
|
if (same_string(host, "cloud.subsurface-divelog.org") && cert->cert_type == GIT_CERT_X509) {
|
|
|
|
SHA_CTX ctx;
|
|
|
|
unsigned char hash[21];
|
|
|
|
git_cert_x509 *cert509 = (git_cert_x509 *)cert;
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
SHA1_Update(&ctx, cert509->data, cert509->len);
|
|
|
|
SHA1_Final(hash, &ctx);
|
|
|
|
hash[20] = 0;
|
2015-08-24 20:55:50 +00:00
|
|
|
if (verbose > 1)
|
|
|
|
if (same_string((char *)hash, KNOWN_CERT)) {
|
|
|
|
fprintf(stderr, "cloud certificate considered %s, forcing it valid\n",
|
|
|
|
valid ? "valid" : "not valid");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-07-13 18:20:18 +00:00
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:01:06 +00:00
|
|
|
static int update_remote(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote, enum remote_transport rt)
|
2015-06-11 00:18:44 +00:00
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) repo;
|
|
|
|
(void) remote;
|
|
|
|
|
git storage: actually update the remote repository if the local cache is more recent
Again, note that this currently only happens when you initially open the repository.
So if you do
subsurface https://.../repo[myubranch]
it will start up by fetching the remote information, and updating the
local cache. If you then download new dives, and do a save-and-exit, it
will save to the local cache, but it doesn't do the fetch at this point,
so the remote is now begind.
The *next* time you start subsurface, and load that git branch again, it
will fetch the remote, and now notice that the local cache is ahead of
it (because you downloaded new dives and saved them locally), and *then*
it will try to update the remote with the new information.
This is obviously bogus, but we will need to decide exactly how we want
to sync with the remote repository. But now the core functionality is
there, it's just that we need some interface to say "sync now".
Especially in the face of spotty (or non-working) internet, you want a
GUI etc for this whole remote sync, rather than doing it unconditionally
and silently whenever you load the local cache initially.
With that caveat:
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2015-06-11 01:03:37 +00:00
|
|
|
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
|
|
|
|
git_strarray refspec;
|
|
|
|
const char *name = git_reference_name(local);
|
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: update remote\n");
|
|
|
|
|
git storage: actually update the remote repository if the local cache is more recent
Again, note that this currently only happens when you initially open the repository.
So if you do
subsurface https://.../repo[myubranch]
it will start up by fetching the remote information, and updating the
local cache. If you then download new dives, and do a save-and-exit, it
will save to the local cache, but it doesn't do the fetch at this point,
so the remote is now begind.
The *next* time you start subsurface, and load that git branch again, it
will fetch the remote, and now notice that the local cache is ahead of
it (because you downloaded new dives and saved them locally), and *then*
it will try to update the remote with the new information.
This is obviously bogus, but we will need to decide exactly how we want
to sync with the remote repository. But now the core functionality is
there, it's just that we need some interface to say "sync now".
Especially in the face of spotty (or non-working) internet, you want a
GUI etc for this whole remote sync, rather than doing it unconditionally
and silently whenever you load the local cache initially.
With that caveat:
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2015-06-11 01:03:37 +00:00
|
|
|
refspec.count = 1;
|
|
|
|
refspec.strings = (char **)&name;
|
|
|
|
|
2016-08-14 16:39:40 +00:00
|
|
|
auth_attempt = 0;
|
2015-09-10 22:13:54 +00:00
|
|
|
opts.callbacks.push_transfer_progress = &push_transfer_progress_cb;
|
2015-06-13 15:01:06 +00:00
|
|
|
if (rt == RT_SSH)
|
2015-06-11 04:55:19 +00:00
|
|
|
opts.callbacks.credentials = credential_ssh_cb;
|
2015-06-13 15:01:06 +00:00
|
|
|
else if (rt == RT_HTTPS)
|
2015-06-11 04:55:19 +00:00
|
|
|
opts.callbacks.credentials = credential_https_cb;
|
2015-07-13 18:20:18 +00:00
|
|
|
opts.callbacks.certificate_check = certificate_check_cb;
|
2015-12-15 06:34:15 +00:00
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_remote_push(origin, &refspec, &opts)) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
return report_error(translate("gettextFromC", "Could not update Subsurface cloud storage, try again later"));
|
|
|
|
else
|
|
|
|
return report_error("Unable to update remote with current local cache state (%s)", giterr_last()->message);
|
|
|
|
}
|
2015-06-11 00:18:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-23 18:32:14 +00:00
|
|
|
extern int update_git_checkout(git_repository *repo, git_object *parent, git_tree *tree);
|
|
|
|
|
2016-04-03 23:48:19 +00:00
|
|
|
static int try_to_git_merge(git_repository *repo, git_reference **local_p, git_reference *remote, git_oid *base, const git_oid *local_id, const git_oid *remote_id)
|
2015-08-23 18:32:14 +00:00
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) remote;
|
2015-08-23 18:32:14 +00:00
|
|
|
git_tree *local_tree, *remote_tree, *base_tree;
|
|
|
|
git_commit *local_commit, *remote_commit, *base_commit;
|
|
|
|
git_index *merged_index;
|
|
|
|
git_merge_options merge_options;
|
2015-08-24 01:19:36 +00:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
char outlocal[41], outremote[41];
|
|
|
|
outlocal[40] = outremote[40] = 0;
|
|
|
|
git_oid_fmt(outlocal, local_id);
|
|
|
|
git_oid_fmt(outremote, remote_id);
|
|
|
|
fprintf(stderr, "trying to merge local SHA %s remote SHA %s\n", outlocal, outremote);
|
|
|
|
}
|
|
|
|
|
2015-08-23 18:32:14 +00:00
|
|
|
git_merge_init_options(&merge_options, GIT_MERGE_OPTIONS_VERSION);
|
2016-06-23 20:56:35 +00:00
|
|
|
#if !LIBGIT2_VER_MAJOR && LIBGIT2_VER_MINOR > 23
|
|
|
|
merge_options.flags = GIT_MERGE_FIND_RENAMES;
|
|
|
|
#else
|
2015-08-23 18:32:14 +00:00
|
|
|
merge_options.tree_flags = GIT_MERGE_TREE_FIND_RENAMES;
|
2016-06-23 20:56:35 +00:00
|
|
|
#endif
|
2015-08-23 18:32:14 +00:00
|
|
|
merge_options.file_favor = GIT_MERGE_FILE_FAVOR_UNION;
|
2015-08-24 01:15:33 +00:00
|
|
|
merge_options.rename_threshold = 100;
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_commit_lookup(&local_commit, repo, local_id)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: can't get commit (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_commit_tree(&local_tree, local_commit)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: failed local tree lookup (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_commit_lookup(&remote_commit, repo, remote_id)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: can't get commit (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_commit_tree(&remote_tree, remote_commit)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: failed local tree lookup (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_commit_lookup(&base_commit, repo, base)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: can't get commit (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_commit_tree(&base_tree, base_commit)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: failed base tree lookup (%s)", giterr_last()->message);
|
|
|
|
goto diverged_error;
|
|
|
|
}
|
|
|
|
if (git_merge_trees(&merged_index, repo, base_tree, local_tree, remote_tree, &merge_options)) {
|
|
|
|
fprintf(stderr, "Remote storage and local data diverged. Error: merge failed (%s)", giterr_last()->message);
|
|
|
|
// this is the one where I want to report more detail to the user - can't quite explain why
|
2015-08-23 18:32:14 +00:00
|
|
|
return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: merge failed (%s)"), giterr_last()->message);
|
2015-09-18 18:51:50 +00:00
|
|
|
}
|
2015-08-23 18:32:14 +00:00
|
|
|
if (git_index_has_conflicts(merged_index)) {
|
2015-08-24 01:16:59 +00:00
|
|
|
int error;
|
|
|
|
const git_index_entry *ancestor = NULL,
|
|
|
|
*ours = NULL,
|
|
|
|
*theirs = NULL;
|
|
|
|
git_index_conflict_iterator *iter = NULL;
|
|
|
|
error = git_index_conflict_iterator_new(&iter, merged_index);
|
|
|
|
while (git_index_conflict_next(&ancestor, &ours, &theirs, iter)
|
|
|
|
!= GIT_ITEROVER) {
|
|
|
|
/* Mark this conflict as resolved */
|
2015-08-24 23:21:53 +00:00
|
|
|
fprintf(stderr, "conflict in %s / %s / %s -- ",
|
2015-08-24 01:16:59 +00:00
|
|
|
ours ? ours->path : "-",
|
|
|
|
theirs ? theirs->path : "-",
|
|
|
|
ancestor ? ancestor->path : "-");
|
2015-08-24 23:21:53 +00:00
|
|
|
if ((!ours && theirs && ancestor) ||
|
|
|
|
(ours && !theirs && ancestor)) {
|
|
|
|
// the file was removed on one side or the other - just remove it
|
|
|
|
fprintf(stderr, "looks like a delete on one side; removing the file from the index\n");
|
|
|
|
error = git_index_remove(merged_index, ours ? ours->path : theirs->path, GIT_INDEX_STAGE_ANY);
|
2015-10-02 01:37:32 +00:00
|
|
|
} else if (ancestor) {
|
2015-08-24 23:21:53 +00:00
|
|
|
error = git_index_conflict_remove(merged_index, ours ? ours->path : theirs ? theirs->path : ancestor->path);
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
fprintf(stderr, "error at conflict resplution (%s)", giterr_last()->message);
|
|
|
|
}
|
2015-08-23 18:33:15 +00:00
|
|
|
}
|
2015-08-24 23:21:53 +00:00
|
|
|
git_index_conflict_cleanup(merged_index);
|
2015-08-24 01:16:59 +00:00
|
|
|
git_index_conflict_iterator_free(iter);
|
2015-09-18 18:51:50 +00:00
|
|
|
report_error(translate("gettextFromC", "Remote storage and local data diverged. Cannot combine local and remote changes"));
|
2015-08-23 18:32:14 +00:00
|
|
|
}
|
2015-08-24 01:16:59 +00:00
|
|
|
git_oid merge_oid, commit_oid;
|
|
|
|
git_tree *merged_tree;
|
|
|
|
git_signature *author;
|
|
|
|
git_commit *commit;
|
|
|
|
|
|
|
|
if (git_index_write_tree_to(&merge_oid, merged_index, repo))
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
if (git_tree_lookup(&merged_tree, repo, &merge_oid))
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
if (git_signature_default(&author, repo) < 0)
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_signature_now(&author, "Subsurface", "noemail@given") < 0)
|
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
if (git_commit_create_v(&commit_oid, repo, NULL, author, author, NULL, "automatic merge", merged_tree, 2, local_commit, remote_commit))
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
if (git_commit_lookup(&commit, repo, &commit_oid))
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2016-04-03 23:48:19 +00:00
|
|
|
if (git_branch_is_head(*local_p) && !git_repository_is_bare(repo)) {
|
2015-08-24 01:16:59 +00:00
|
|
|
git_object *parent;
|
2016-04-03 23:48:19 +00:00
|
|
|
git_reference_peel(&parent, *local_p, GIT_OBJ_COMMIT);
|
2015-08-24 01:16:59 +00:00
|
|
|
if (update_git_checkout(repo, parent, merged_tree)) {
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 23:48:19 +00:00
|
|
|
if (git_reference_set_target(local_p, *local_p, &commit_oid, "Subsurface merge event"))
|
2015-09-18 18:51:50 +00:00
|
|
|
goto write_error;
|
2015-08-24 01:16:59 +00:00
|
|
|
set_git_id(&commit_oid);
|
|
|
|
git_signature_free(author);
|
2016-04-03 23:48:19 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "Successfully merged repositories");
|
2015-08-23 18:32:14 +00:00
|
|
|
return 0;
|
2015-09-18 18:51:50 +00:00
|
|
|
|
|
|
|
diverged_error:
|
|
|
|
return report_error(translate("gettextFromC", "Remote storage and local data diverged"));
|
|
|
|
|
|
|
|
write_error:
|
|
|
|
return report_error(translate("gettextFromC", "Remote storage and local data diverged. Error: writing the data failed (%s)"), giterr_last()->message);
|
2015-08-23 18:32:14 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 12:14:46 +00:00
|
|
|
// if accessing the local cache of Subsurface cloud storage fails, we simplify things
|
|
|
|
// for the user and simply move the cache away (in case they want to try and extract data)
|
|
|
|
// and ask them to retry the operation (which will then refresh the data from the cloud server)
|
|
|
|
static int cleanup_local_cache(const char *remote_url, const char *branch)
|
|
|
|
{
|
|
|
|
char *backup_path = move_local_cache(remote_url, branch);
|
|
|
|
report_error(translate("gettextFromC", "Problems with local cache of Subsurface cloud data"));
|
|
|
|
report_error(translate("gettextFromC", "Moved cache data to %s. Please try the operation again."), backup_path);
|
|
|
|
free(backup_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
static int try_to_update(git_repository *repo, git_remote *origin, git_reference *local, git_reference *remote,
|
|
|
|
const char *remote_url, const char *branch, enum remote_transport rt)
|
2015-06-10 20:37:22 +00:00
|
|
|
{
|
|
|
|
git_oid base;
|
|
|
|
const git_oid *local_id, *remote_id;
|
2016-04-03 23:48:19 +00:00
|
|
|
int ret = 0;
|
2015-06-10 20:37:22 +00:00
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: try to update\n");
|
2017-06-18 06:50:22 +00:00
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
if (!git_reference_cmp(local, remote))
|
|
|
|
return 0;
|
2015-06-10 20:37:22 +00:00
|
|
|
|
2015-06-11 00:18:44 +00:00
|
|
|
// Dirty modified state in the working tree? We're not going
|
|
|
|
// to update either way
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_status_foreach(repo, check_clean, NULL)) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
goto cloud_data_error;
|
|
|
|
else
|
|
|
|
return report_error("local cached copy is dirty, skipping update");
|
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
local_id = git_reference_target(local);
|
|
|
|
remote_id = git_reference_target(remote);
|
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
if (!local_id || !remote_id) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
goto cloud_data_error;
|
|
|
|
else
|
|
|
|
return report_error("Unable to get local or remote SHA1");
|
|
|
|
}
|
|
|
|
if (git_merge_base(&base, repo, local_id, remote_id)) {
|
2016-04-22 14:00:29 +00:00
|
|
|
// TODO:
|
|
|
|
// if they have no merge base, they actually are different repos
|
|
|
|
// so instead merge this as merging a commit into a repo - git_merge() appears to do that
|
|
|
|
// but needs testing and cleanup afterwards
|
|
|
|
//
|
2015-09-18 18:51:50 +00:00
|
|
|
if (is_subsurface_cloud)
|
|
|
|
goto cloud_data_error;
|
|
|
|
else
|
|
|
|
return report_error("Unable to find common commit of local and remote branches");
|
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
/* Is the remote strictly newer? Use it */
|
2016-04-06 04:13:27 +00:00
|
|
|
if (git_oid_equal(&base, local_id)) {
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Update local storage to match cloud storage"));
|
2015-06-10 20:37:22 +00:00
|
|
|
return reset_to_remote(repo, local, remote_id);
|
2016-04-06 04:13:27 +00:00
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
|
2015-06-11 00:18:44 +00:00
|
|
|
/* Is the local repo the more recent one? See if we can update upstream */
|
2016-04-03 23:48:19 +00:00
|
|
|
if (git_oid_equal(&base, remote_id)) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "local is newer than remote, update remote\n");
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Push local changes to cloud storage"));
|
2015-06-11 04:52:33 +00:00
|
|
|
return update_remote(repo, origin, local, remote, rt);
|
2016-04-03 23:48:19 +00:00
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
/* Merging a bare repository always needs user action */
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_repository_is_bare(repo)) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
goto cloud_data_error;
|
|
|
|
else
|
|
|
|
return report_error("Local and remote have diverged, merge of bare branch needed");
|
|
|
|
}
|
2015-06-10 20:37:22 +00:00
|
|
|
/* Merging will definitely need the head branch too */
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_branch_is_head(local) != 1) {
|
|
|
|
if (is_subsurface_cloud)
|
|
|
|
goto cloud_data_error;
|
|
|
|
else
|
|
|
|
return report_error("Local and remote do not match, local branch not HEAD - cannot update");
|
|
|
|
}
|
2015-08-21 05:23:12 +00:00
|
|
|
/* Ok, let's try to merge these */
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Try to merge local changes into cloud storage"));
|
2016-04-03 23:48:19 +00:00
|
|
|
ret = try_to_git_merge(repo, &local, remote, &base, local_id, remote_id);
|
|
|
|
if (ret == 0)
|
|
|
|
return update_remote(repo, origin, local, remote, rt);
|
|
|
|
else
|
|
|
|
return ret;
|
2015-09-18 18:51:50 +00:00
|
|
|
|
|
|
|
cloud_data_error:
|
2015-09-23 12:14:46 +00:00
|
|
|
// since we are working with Subsurface cloud storage we want to make the user interaction
|
|
|
|
// as painless as possible. So if something went wrong with the local cache, tell the user
|
|
|
|
// about it an move it away
|
|
|
|
return cleanup_local_cache(remote_url, branch);
|
2015-02-08 18:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 12:14:46 +00:00
|
|
|
static int check_remote_status(git_repository *repo, git_remote *origin, const char *remote, const char *branch, enum remote_transport rt)
|
2015-06-11 00:18:44 +00:00
|
|
|
{
|
2015-06-14 03:03:20 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2015-06-11 00:18:44 +00:00
|
|
|
git_reference *local_ref, *remote_ref;
|
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: check remote status\n");
|
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
if (git_branch_lookup(&local_ref, repo, branch, GIT_BRANCH_LOCAL)) {
|
|
|
|
if (is_subsurface_cloud)
|
2015-09-23 12:14:46 +00:00
|
|
|
return cleanup_local_cache(remote, branch);
|
2015-09-18 18:51:50 +00:00
|
|
|
else
|
|
|
|
return report_error("Git cache branch %s no longer exists", branch);
|
|
|
|
}
|
2015-06-11 00:18:44 +00:00
|
|
|
if (git_branch_upstream(&remote_ref, local_ref)) {
|
2015-06-14 01:31:27 +00:00
|
|
|
/* so there is no upstream branch for our branch; that's a problem.
|
2015-06-22 03:02:01 +00:00
|
|
|
* let's push our branch */
|
2015-06-14 01:31:27 +00:00
|
|
|
git_strarray refspec;
|
|
|
|
git_reference_list(&refspec, repo);
|
|
|
|
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
|
2015-09-09 20:02:39 +00:00
|
|
|
opts.callbacks.transfer_progress = &transfer_progress_cb;
|
2016-08-14 16:39:40 +00:00
|
|
|
auth_attempt = 0;
|
2015-06-14 01:31:27 +00:00
|
|
|
if (rt == RT_SSH)
|
|
|
|
opts.callbacks.credentials = credential_ssh_cb;
|
|
|
|
else if (rt == RT_HTTPS)
|
|
|
|
opts.callbacks.credentials = credential_https_cb;
|
2015-07-13 18:20:18 +00:00
|
|
|
opts.callbacks.certificate_check = certificate_check_cb;
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Store data into cloud storage"));
|
2015-06-14 03:03:20 +00:00
|
|
|
error = git_remote_push(origin, &refspec, &opts);
|
2015-06-14 01:31:27 +00:00
|
|
|
} else {
|
2015-09-23 12:14:46 +00:00
|
|
|
error = try_to_update(repo, origin, local_ref, remote_ref, remote, branch, rt);
|
2015-06-14 01:31:27 +00:00
|
|
|
git_reference_free(remote_ref);
|
2015-06-11 00:18:44 +00:00
|
|
|
}
|
|
|
|
git_reference_free(local_ref);
|
2015-06-14 03:03:20 +00:00
|
|
|
return error;
|
2015-06-11 00:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 15:01:06 +00:00
|
|
|
int sync_with_remote(git_repository *repo, const char *remote, const char *branch, enum remote_transport rt)
|
2015-02-08 18:35:25 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
git_remote *origin;
|
2015-06-12 13:24:48 +00:00
|
|
|
char *proxy_string;
|
|
|
|
git_config *conf;
|
2015-02-08 18:35:25 +00:00
|
|
|
|
2015-12-27 16:31:08 +00:00
|
|
|
if (prefs.git_local_only) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "don't sync with remote - read from cache only\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-24 20:55:50 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "sync with remote %s[%s]\n", remote, branch);
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Sync with cloud storage"));
|
2015-06-12 13:29:22 +00:00
|
|
|
git_repository_config(&conf, repo);
|
2015-06-13 15:01:06 +00:00
|
|
|
if (rt == RT_HTTPS && getProxyString(&proxy_string)) {
|
2015-09-26 16:19:17 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "set proxy to \"%s\"\n", proxy_string);
|
2015-06-12 13:24:48 +00:00
|
|
|
git_config_set_string(conf, "http.proxy", proxy_string);
|
|
|
|
free(proxy_string);
|
2015-06-12 13:29:22 +00:00
|
|
|
} else {
|
2015-09-26 16:19:17 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "delete proxy setting\n");
|
|
|
|
git_config_delete_entry(conf, "http.proxy");
|
2015-06-12 13:24:48 +00:00
|
|
|
}
|
2015-06-12 13:29:22 +00:00
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
/*
|
|
|
|
* NOTE! Remote errors are reported, but are nonfatal:
|
|
|
|
* we still successfully return the local repository.
|
|
|
|
*/
|
|
|
|
error = git_remote_lookup(&origin, repo, "origin");
|
|
|
|
if (error) {
|
2015-09-18 18:51:50 +00:00
|
|
|
if (!is_subsurface_cloud)
|
|
|
|
report_error("Repository '%s' origin lookup failed (%s)", remote, giterr_last()->message);
|
2015-06-12 19:32:12 +00:00
|
|
|
return 0;
|
2015-02-08 18:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 19:42:17 +00:00
|
|
|
if (rt == RT_HTTPS && !canReachCloudServer()) {
|
|
|
|
// this is not an error, just a warning message, so return 0
|
|
|
|
report_error("Cannot connect to cloud server, working with local copy");
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Can't reach cloud server, working with local data"));
|
2015-06-12 19:32:12 +00:00
|
|
|
return 0;
|
2015-06-16 19:42:17 +00:00
|
|
|
}
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: fetch remote\n");
|
2015-05-29 04:43:32 +00:00
|
|
|
git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
|
2015-09-09 20:02:39 +00:00
|
|
|
opts.callbacks.transfer_progress = &transfer_progress_cb;
|
2016-08-14 16:39:40 +00:00
|
|
|
auth_attempt = 0;
|
2015-06-13 15:01:06 +00:00
|
|
|
if (rt == RT_SSH)
|
2015-06-01 00:15:58 +00:00
|
|
|
opts.callbacks.credentials = credential_ssh_cb;
|
2015-06-13 15:01:06 +00:00
|
|
|
else if (rt == RT_HTTPS)
|
2015-06-01 00:15:58 +00:00
|
|
|
opts.callbacks.credentials = credential_https_cb;
|
2015-07-13 18:20:18 +00:00
|
|
|
opts.callbacks.certificate_check = certificate_check_cb;
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Successful cloud connection, fetch remote"));
|
2015-05-28 19:03:51 +00:00
|
|
|
error = git_remote_fetch(origin, NULL, &opts, NULL);
|
2015-06-11 04:52:33 +00:00
|
|
|
// NOTE! A fetch error is not fatal, we just report it
|
2015-06-14 03:03:20 +00:00
|
|
|
if (error) {
|
2015-09-25 17:44:15 +00:00
|
|
|
if (is_subsurface_cloud)
|
|
|
|
report_error("Cannot sync with cloud server, working with offline copy");
|
|
|
|
else
|
2015-09-18 18:51:50 +00:00
|
|
|
report_error("Unable to fetch remote '%s'", remote);
|
2015-09-25 17:44:15 +00:00
|
|
|
if (verbose)
|
2015-09-26 16:19:17 +00:00
|
|
|
fprintf(stderr, "remote fetch failed (%s)\n", giterr_last()->message);
|
2015-06-14 03:03:20 +00:00
|
|
|
error = 0;
|
|
|
|
} else {
|
2015-09-23 12:14:46 +00:00
|
|
|
error = check_remote_status(repo, origin, remote, branch, rt);
|
2015-06-14 03:03:20 +00:00
|
|
|
}
|
2015-06-11 00:18:44 +00:00
|
|
|
git_remote_free(origin);
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Done syncing with cloud storage"));
|
2015-06-14 03:03:20 +00:00
|
|
|
return error;
|
2015-06-12 19:32:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 15:01:06 +00:00
|
|
|
static git_repository *update_local_repo(const char *localdir, const char *remote, const char *branch, enum remote_transport rt)
|
2015-06-12 19:32:12 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
git_repository *repo = NULL;
|
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: update local repo\n");
|
|
|
|
|
2015-06-12 19:32:12 +00:00
|
|
|
error = git_repository_open(&repo, localdir);
|
|
|
|
if (error) {
|
2015-09-18 18:51:50 +00:00
|
|
|
if (is_subsurface_cloud)
|
2015-09-23 12:14:46 +00:00
|
|
|
(void)cleanup_local_cache(remote, branch);
|
2015-09-18 18:51:50 +00:00
|
|
|
else
|
|
|
|
report_error("Unable to open git cache repository at %s: %s", localdir, giterr_last()->message);
|
2015-06-12 19:32:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-06-18 06:50:22 +00:00
|
|
|
if (!prefs.git_local_only)
|
2016-04-08 19:28:43 +00:00
|
|
|
sync_with_remote(repo, remote, branch, rt);
|
2017-06-18 06:50:22 +00:00
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
2015-06-13 16:13:08 +00:00
|
|
|
static int repository_create_cb(git_repository **out, const char *path, int bare, void *payload)
|
|
|
|
{
|
2016-03-07 00:39:19 +00:00
|
|
|
(void) payload;
|
2015-06-13 16:13:08 +00:00
|
|
|
char *proxy_string;
|
|
|
|
git_config *conf;
|
|
|
|
|
|
|
|
int ret = git_repository_init(out, path, bare);
|
2017-03-28 16:34:05 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "Initializing git repository failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2015-06-13 16:13:08 +00:00
|
|
|
|
2015-09-26 16:19:17 +00:00
|
|
|
git_repository_config(&conf, *out);
|
2015-06-13 16:13:08 +00:00
|
|
|
if (getProxyString(&proxy_string)) {
|
2015-09-26 16:19:17 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "set proxy to \"%s\"\n", proxy_string);
|
2015-06-13 16:13:08 +00:00
|
|
|
git_config_set_string(conf, "http.proxy", proxy_string);
|
|
|
|
free(proxy_string);
|
2015-09-26 16:19:17 +00:00
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "delete proxy setting\n");
|
|
|
|
git_config_delete_entry(conf, "http.proxy");
|
2015-06-13 16:13:08 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:33:12 +00:00
|
|
|
/* 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;
|
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: create and push remote\n");
|
|
|
|
|
2015-06-14 01:33:12 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:01:06 +00:00
|
|
|
static git_repository *create_local_repo(const char *localdir, const char *remote, const char *branch, enum remote_transport rt)
|
2015-02-08 18:35:25 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
git_repository *cloned_repo = NULL;
|
|
|
|
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
|
2015-09-20 17:11:09 +00:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "git storage: create_local_repo\n");
|
|
|
|
|
2016-08-14 16:39:40 +00:00
|
|
|
auth_attempt = 0;
|
2015-09-10 22:13:54 +00:00
|
|
|
opts.fetch_opts.callbacks.transfer_progress = &transfer_progress_cb;
|
2015-06-19 14:31:15 +00:00
|
|
|
if (rt == RT_SSH)
|
2015-06-01 00:15:58 +00:00
|
|
|
opts.fetch_opts.callbacks.credentials = credential_ssh_cb;
|
2015-06-19 14:31:15 +00:00
|
|
|
else if (rt == RT_HTTPS)
|
2015-06-01 00:15:58 +00:00
|
|
|
opts.fetch_opts.callbacks.credentials = credential_https_cb;
|
2015-06-13 16:13:08 +00:00
|
|
|
opts.repository_cb = repository_create_cb;
|
2015-07-13 18:20:18 +00:00
|
|
|
opts.fetch_opts.callbacks.certificate_check = certificate_check_cb;
|
2015-12-15 06:34:15 +00:00
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
opts.checkout_branch = branch;
|
2015-06-13 15:04:20 +00:00
|
|
|
if (rt == RT_HTTPS && !canReachCloudServer())
|
|
|
|
return 0;
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose > 1)
|
|
|
|
fprintf(stderr, "git storage: calling git_clone()\n");
|
2015-02-08 18:35:25 +00:00
|
|
|
error = git_clone(&cloned_repo, remote, localdir, &opts);
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose > 1)
|
|
|
|
fprintf(stderr, "git storage: returned from git_clone() with error %d\n", error);
|
2015-02-08 18:35:25 +00:00
|
|
|
if (error) {
|
2015-06-13 16:12:16 +00:00
|
|
|
char *msg = giterr_last()->message;
|
2015-06-22 03:02:01 +00:00
|
|
|
int len = sizeof("Reference 'refs/remotes/origin/' not found") + strlen(branch);
|
2015-06-13 16:12:16 +00:00
|
|
|
char *pattern = malloc(len);
|
|
|
|
snprintf(pattern, len, "Reference 'refs/remotes/origin/%s' not found", branch);
|
2015-06-15 13:05:00 +00:00
|
|
|
if (strstr(remote, prefs.cloud_git_url) && strstr(msg, pattern)) {
|
2015-06-14 01:33:12 +00:00
|
|
|
/* 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);
|
2016-03-14 01:22:54 +00:00
|
|
|
#if !defined(DEBUG) && !defined(SUBSURFACE_MOBILE)
|
2015-09-18 18:51:50 +00:00
|
|
|
} else if (is_subsurface_cloud) {
|
2015-06-13 16:12:16 +00:00
|
|
|
report_error(translate("gettextFromC", "Error connecting to Subsurface cloud storage"));
|
2015-07-13 14:11:55 +00:00
|
|
|
#endif
|
2015-06-13 16:12:16 +00:00
|
|
|
} else {
|
|
|
|
report_error(translate("gettextFromC", "git clone of %s failed (%s)"), remote, msg);
|
|
|
|
}
|
|
|
|
free(pattern);
|
2015-02-08 18:35:25 +00:00
|
|
|
}
|
|
|
|
return cloned_repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct git_repository *get_remote_repo(const char *localdir, const char *remote, const char *branch)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2015-06-13 15:01:06 +00:00
|
|
|
enum remote_transport rt;
|
|
|
|
|
|
|
|
/* figure out the remote transport */
|
|
|
|
if (strncmp(remote, "ssh://", 6) == 0)
|
|
|
|
rt = RT_SSH;
|
|
|
|
else if (strncmp(remote, "https://", 8) == 0)
|
|
|
|
rt = RT_HTTPS;
|
|
|
|
else
|
|
|
|
rt = RT_OTHER;
|
2015-02-08 18:35:25 +00:00
|
|
|
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose > 1) {
|
2016-04-04 00:26:05 +00:00
|
|
|
fprintf(stderr, "git_remote_repo: accessing %s\n", remote);
|
2015-09-20 17:11:09 +00:00
|
|
|
}
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Synchronising data file"));
|
2015-02-08 18:35:25 +00:00
|
|
|
/* Do we already have a local cache? */
|
2017-02-24 07:06:48 +00:00
|
|
|
if (!subsurface_stat(localdir, &st)) {
|
2015-02-08 18:35:25 +00:00
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2015-09-18 18:51:50 +00:00
|
|
|
if (is_subsurface_cloud)
|
2015-09-23 12:14:46 +00:00
|
|
|
(void)cleanup_local_cache(remote, branch);
|
2015-09-18 18:51:50 +00:00
|
|
|
else
|
|
|
|
report_error("local git cache at '%s' is corrupt");
|
2015-02-08 18:35:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-13 15:01:06 +00:00
|
|
|
return update_local_repo(localdir, remote, branch, rt);
|
2015-02-08 18:35:25 +00:00
|
|
|
}
|
2016-02-13 06:01:20 +00:00
|
|
|
if (!prefs.git_local_only)
|
|
|
|
return create_local_repo(localdir, remote, branch, rt);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This turns a remote repository into a local one if possible.
|
|
|
|
*
|
|
|
|
* The recognized formats are
|
|
|
|
* git://host/repo[branch]
|
|
|
|
* ssh://host/repo[branch]
|
|
|
|
* http://host/repo[branch]
|
|
|
|
* https://host/repo[branch]
|
|
|
|
* file://repo[branch]
|
|
|
|
*/
|
2015-06-01 05:11:27 +00:00
|
|
|
static struct git_repository *is_remote_git_repository(char *remote, const char *branch)
|
2015-02-08 18:35:25 +00:00
|
|
|
{
|
|
|
|
char c, *localdir;
|
|
|
|
const char *p = remote;
|
|
|
|
|
|
|
|
while ((c = *p++) >= 'a' && c <= 'z')
|
|
|
|
/* nothing */;
|
|
|
|
if (c != ':')
|
|
|
|
return NULL;
|
|
|
|
if (*p++ != '/' || *p++ != '/')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Special-case "file://", since it's already local */
|
|
|
|
if (!strncmp(remote, "file://", 7))
|
|
|
|
remote += 7;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we found "[a-z]*://", we've simplified the
|
|
|
|
* local repo case (because libgit2 is insanely slow
|
|
|
|
* for that), and we think we have a real "remote
|
|
|
|
* git" format.
|
|
|
|
*
|
|
|
|
* We now create the SHA1 hash of the whole thing,
|
|
|
|
* including the branch name. That will be our unique
|
|
|
|
* unique local repository name.
|
|
|
|
*
|
|
|
|
* NOTE! We will create a local repository per branch,
|
|
|
|
* because
|
|
|
|
*
|
|
|
|
* (a) libgit2 remote tracking branch support seems to
|
|
|
|
* be a bit lacking
|
|
|
|
* (b) we'll actually check the branch out so that we
|
|
|
|
* can do merges etc too.
|
|
|
|
*
|
|
|
|
* so even if you have a single remote git repo with
|
|
|
|
* multiple branches for different people, the local
|
|
|
|
* caches will sadly force that to split into multiple
|
|
|
|
* individual repositories.
|
|
|
|
*/
|
2015-06-01 05:11:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* next we need to make sure that any encoded username
|
|
|
|
* has been extracted from an https:// based URL
|
|
|
|
*/
|
|
|
|
if (!strncmp(remote, "https://", 8)) {
|
|
|
|
char *at = strchr(remote, '@');
|
|
|
|
if (at) {
|
2015-06-10 03:54:01 +00:00
|
|
|
/* was this the @ that denotes an account? that means it was before the
|
|
|
|
* first '/' after the https:// - so let's find a '/' after that and compare */
|
|
|
|
char *slash = strchr(remote + 8, '/');
|
|
|
|
if (slash && slash > at) {
|
|
|
|
/* grab the part between "https://" and "@" as encoded email address
|
|
|
|
* (that's our username) and move the rest of the URL forward, remembering
|
|
|
|
* to copy the closing NUL as well */
|
|
|
|
prefs.cloud_storage_email_encoded = strndup(remote + 8, at - remote - 8);
|
|
|
|
memmove(remote + 8, at + 1, strlen(at + 1) + 1);
|
|
|
|
}
|
2015-06-01 05:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 18:35:25 +00:00
|
|
|
localdir = get_local_dir(remote, branch);
|
|
|
|
if (!localdir)
|
|
|
|
return NULL;
|
|
|
|
|
2015-09-18 18:51:50 +00:00
|
|
|
/* remember if the current git storage we are working on is our cloud storage
|
|
|
|
* this is used to create more user friendly error message and warnings */
|
|
|
|
is_subsurface_cloud = strstr(remote, prefs.cloud_git_url) != NULL;
|
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
return get_remote_repo(localdir, remote, branch);
|
|
|
|
}
|
2015-01-23 03:40:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's not a git repo, return NULL. Be very conservative.
|
|
|
|
*/
|
2015-09-23 09:13:07 +00:00
|
|
|
struct git_repository *is_git_repository(const char *filename, const char **branchp, const char **remote, bool dry_run)
|
2015-01-23 03:40:01 +00:00
|
|
|
{
|
|
|
|
int flen, blen, ret;
|
2015-05-28 15:14:16 +00:00
|
|
|
int offset = 1;
|
2015-01-23 03:40:01 +00:00
|
|
|
struct stat st;
|
|
|
|
git_repository *repo;
|
|
|
|
char *loc, *branch;
|
|
|
|
|
|
|
|
flen = strlen(filename);
|
|
|
|
if (!flen || filename[--flen] != ']')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Find the matching '[' */
|
|
|
|
blen = 0;
|
|
|
|
while (flen && filename[--flen] != '[')
|
|
|
|
blen++;
|
|
|
|
|
2015-02-08 18:35:25 +00:00
|
|
|
/* Ignore slashes at the end of the repo name */
|
2015-05-28 15:14:16 +00:00
|
|
|
while (flen && filename[flen-1] == '/') {
|
2015-02-08 18:35:25 +00:00
|
|
|
flen--;
|
2015-05-28 15:14:16 +00:00
|
|
|
offset++;
|
|
|
|
}
|
2015-02-08 18:35:25 +00:00
|
|
|
|
2015-01-23 03:40:01 +00:00
|
|
|
if (!flen)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the "point of no return": the name matches
|
|
|
|
* the git repository name rules, and we will no longer
|
|
|
|
* return NULL.
|
|
|
|
*
|
|
|
|
* We will either return "dummy_git_repository" and the
|
|
|
|
* branch pointer will have the _whole_ filename in it,
|
|
|
|
* or we will return a real git repository with the
|
|
|
|
* branch pointer being filled in with just the branch
|
|
|
|
* name.
|
|
|
|
*
|
|
|
|
* The actual git reading/writing routines can use this
|
|
|
|
* to generate proper error messages.
|
|
|
|
*/
|
|
|
|
*branchp = filename;
|
2015-01-24 00:56:34 +00:00
|
|
|
loc = format_string("%.*s", flen, filename);
|
2015-01-23 03:40:01 +00:00
|
|
|
if (!loc)
|
|
|
|
return dummy_git_repository;
|
|
|
|
|
2015-05-28 15:14:16 +00:00
|
|
|
branch = format_string("%.*s", blen, filename + flen + offset);
|
2015-01-23 03:40:01 +00:00
|
|
|
if (!branch) {
|
|
|
|
free(loc);
|
|
|
|
return dummy_git_repository;
|
|
|
|
}
|
|
|
|
|
2015-10-05 21:06:25 +00:00
|
|
|
if (dry_run) {
|
|
|
|
*branchp = branch;
|
|
|
|
*remote = loc;
|
2015-09-23 09:13:07 +00:00
|
|
|
return dummy_git_repository;
|
2015-10-05 21:06:25 +00:00
|
|
|
}
|
2015-02-08 18:35:25 +00:00
|
|
|
repo = is_remote_git_repository(loc, branch);
|
|
|
|
if (repo) {
|
2015-06-12 19:32:12 +00:00
|
|
|
if (remote)
|
|
|
|
*remote = loc;
|
|
|
|
else
|
|
|
|
free(loc);
|
2015-02-08 18:35:25 +00:00
|
|
|
*branchp = branch;
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
2017-02-24 07:06:48 +00:00
|
|
|
if (subsurface_stat(loc, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
2015-01-23 03:40:01 +00:00
|
|
|
free(loc);
|
|
|
|
free(branch);
|
|
|
|
return dummy_git_repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = git_repository_open(&repo, loc);
|
|
|
|
free(loc);
|
|
|
|
if (ret < 0) {
|
|
|
|
free(branch);
|
|
|
|
return dummy_git_repository;
|
|
|
|
}
|
2015-06-17 03:37:15 +00:00
|
|
|
if (remote)
|
|
|
|
*remote = NULL;
|
2015-01-23 03:40:01 +00:00
|
|
|
*branchp = branch;
|
|
|
|
return repo;
|
|
|
|
}
|
2016-04-22 14:00:29 +00:00
|
|
|
|
|
|
|
int git_create_local_repo(const char *filename)
|
|
|
|
{
|
|
|
|
git_repository *repo;
|
2016-04-30 13:51:26 +00:00
|
|
|
char *path = strdup(filename);
|
|
|
|
char *branch = strchr(path, '[');
|
|
|
|
if (branch)
|
|
|
|
*branch = '\0';
|
|
|
|
int ret = git_repository_init(&repo, path, false);
|
|
|
|
free(path);
|
2016-04-22 14:00:29 +00:00
|
|
|
if (ret != 0)
|
|
|
|
(void)report_error("Create local repo failed with error code %d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|