diff --git a/core/file.c b/core/file.c index ad3181ff7..dbd51a7b9 100644 --- a/core/file.c +++ b/core/file.c @@ -306,11 +306,15 @@ int parse_file(const char *filename, struct dive_table *table, struct trip_table * Opening the cloud storage repository failed for some reason * give up here and don't send errors about git repositories */ - if (info.is_subsurface_cloud) + if (info.is_subsurface_cloud) { + cleanup_git_info(&info); return -1; + } } - return git_load_dives(&info, table, trips, sites, devices, filter_presets); + ret = git_load_dives(&info, table, trips, sites, devices, filter_presets); + cleanup_git_info(&info); + return ret; } if ((ret = readfile(filename, &mem)) < 0) { diff --git a/core/git-access.c b/core/git-access.c index 6f1c19e79..d6e2031bc 100644 --- a/core/git-access.c +++ b/core/git-access.c @@ -1009,6 +1009,17 @@ static void extract_username(struct git_info *info, char *url) prefs.cloud_storage_email_encoded = strdup(info->username); } +void cleanup_git_info(struct git_info *info) +{ + if (info->repo) + git_repository_free(info->repo); + free((void *)info->url); + free((void *)info->branch); + free((void *)info->username); + free((void *)info->localdir); + memset(info, 0, sizeof(*info)); +} + /* * If it's not a git repo, return NULL. Be very conservative. * diff --git a/core/git-access.h b/core/git-access.h index 56885ee00..cad97cf01 100644 --- a/core/git-access.h +++ b/core/git-access.h @@ -48,6 +48,7 @@ extern int git_load_dives(struct git_info *, struct dive_table *table, struct tr struct filter_preset_table *filter_presets); extern const char *get_sha(git_repository *repo, const char *branch); extern int do_git_save(struct git_info *, bool select_only, bool create_empty); +extern void cleanup_git_info(struct git_info *); extern const char *saved_git_id; extern bool git_local_only; extern bool git_remote_sync_successful; diff --git a/core/load-git.c b/core/load-git.c index ed3e9d7fd..ff74746e9 100644 --- a/core/load-git.c +++ b/core/load-git.c @@ -1960,8 +1960,6 @@ int git_load_dives(struct git_info *info, struct dive_table *table, struct trip_ if (!info->repo) return report_error("Unable to open git repository '%s[%s]'", info->url, info->branch); ret = do_git_load(info->repo, info->branch, &state); - git_repository_free(info->repo); - free((void *)info->branch); finish_active_dive(&state); finish_active_trip(&state); return ret; diff --git a/core/save-git.c b/core/save-git.c index ef12cc744..c5d447887 100644 --- a/core/save-git.c +++ b/core/save-git.c @@ -1362,8 +1362,6 @@ int do_git_save(struct git_info *info, bool select_only, bool create_empty) int git_save_dives(struct git_info *info, bool select_only) { - int ret; - /* * FIXME!! This open_git_repository() will * sync with the cloud. That is NOT what @@ -1389,8 +1387,5 @@ int git_save_dives(struct git_info *info, bool select_only) if (!open_git_repository(info)) report_error(translate("gettextFromC", "Failed to save dives to %s[%s] (%s)"), info->url, info->branch, strerror(errno)); - ret = do_git_save(info, select_only, false); - git_repository_free(info->repo); - free((void *)info->branch); - return ret; + return do_git_save(info, select_only, false); } diff --git a/core/save-xml.c b/core/save-xml.c index ba6d9a0ac..a8478fc96 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -826,8 +826,11 @@ int save_dives_logic(const char *filename, const bool select_only, bool anonymiz FILE *f; int error = 0; - if (is_git_repository(filename, &info)) - return git_save_dives(&info, select_only); + if (is_git_repository(filename, &info)) { + error = git_save_dives(&info, select_only); + cleanup_git_info(&info); + return error; + } save_dives_buffer(&buf, select_only, anonymize); diff --git a/core/subsurfacestartup.c b/core/subsurfacestartup.c index ffadf1bfe..9e66ce625 100644 --- a/core/subsurfacestartup.c +++ b/core/subsurfacestartup.c @@ -62,6 +62,7 @@ void print_files() } else { printf("Unable to get local git directory\n"); } + cleanup_git_info(&info); printf("Cloud URL: %s\n", filename); free((void *)filename); char *tmp = hashfile_name_string(); diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index 5e33df0c3..8ff3d2bb5 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -769,6 +769,8 @@ void QMLManager::loadDivesWithValidCredentials() } consumeFinishedLoad(); } + cleanup_git_info(&info); + setLoadFromCloud(true); // if we came from local storage mode, let's merge the local data into the local cache diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index 6eedbc8c5..b1518a17e 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -66,9 +66,7 @@ static void localRemoteCleanup() // and since this will have created a local repo, remove that one, again so the tests start clean QCOMPARE(localCacheDirectory.removeRecursively(), true); - free((void *)info.branch); - free((void *)info.url); - git_repository_free(info.repo); + cleanup_git_info(&info); } void TestGitStorage::initTestCase()