QML UI: don't fetch the remote twice when loading

We first check the sha to see if we want to load at all. But at that
point we already have the repository and the branch and we have synced
with the remote. So when we decide that we need to reload from storage,
we don't need to repeat those steps, instead we can go directly to the
git load.

For that to work we need to pass the repository pointer and the branch
name back to the caller so that we can directly call git_load_dives().

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-04-05 22:51:09 -07:00
parent 8b670c3f3f
commit 3555cadb77
4 changed files with 18 additions and 6 deletions

View file

@ -675,7 +675,6 @@ extern int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buf
extern int parse_divinglog_buffer(sqlite3 *handle, const char *url, const char *buf, int size, struct dive_table *table);
extern int parse_dlf_buffer(unsigned char *buffer, size_t size);
extern int check_git_sha(const char *filename);
extern int parse_file(const char *filename);
extern int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate);
extern int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate);

View file

@ -435,13 +435,17 @@ static int parse_file_buffer(const char *filename, struct memblock *mem)
return parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL);
}
int check_git_sha(const char *filename)
int check_git_sha(const char *filename, struct git_repository **git_p, const char **branch_p)
{
struct git_repository *git;
const char *branch = NULL;
char *current_sha = strdup(saved_git_id);
git = is_git_repository(filename, &branch, NULL, false);
if (git_p)
*git_p = git;
if (branch_p)
*branch_p = branch;
if (prefs.cloud_git_url &&
strstr(filename, prefs.cloud_git_url)
&& git == dummy_git_repository) {

View file

@ -15,6 +15,7 @@ struct git_oid;
struct git_repository;
#define dummy_git_repository ((git_repository *)3ul) /* Random bogus pointer, not NULL */
extern struct git_repository *is_git_repository(const char *filename, const char **branchp, const char **remote, bool dry_run);
extern int check_git_sha(const char *filename, git_repository **git_p, const char **branch_p);
extern int sync_with_remote(struct git_repository *repo, const char *remote, const char *branch, enum remote_transport rt);
extern int git_save_dives(struct git_repository *, const char *, const char *remote, bool select_only);
extern int git_load_dives(struct git_repository *, const char *);

View file

@ -352,7 +352,10 @@ void QMLManager::loadDivesWithValidCredentials()
return;
}
QByteArray fileNamePrt = QFile::encodeName(url);
if (check_git_sha(fileNamePrt.data()) == 0) {
git_repository *git;
const char *branch;
int error;
if (check_git_sha(fileNamePrt.data(), &git, &branch) == 0) {
qDebug() << "local cache was current, no need to modify dive list";
appendTextToLog("Cloud sync shows local cache was current");
setLoadFromCloud(true);
@ -360,8 +363,14 @@ void QMLManager::loadDivesWithValidCredentials()
return;
}
appendTextToLog("Cloud sync brought newer data, reloading the dive list");
int error = parse_file(fileNamePrt.data());
clear_dive_file_data();
if (git != dummy_git_repository) {
appendTextToLog(QString("have repository and branch %1").arg(branch));
error = git_load_dives(git, branch);
} else {
appendTextToLog(QString("didn't receive valid git repo, try again"));
error = parse_file(fileNamePrt.data());
}
setAccessingCloud(-1);
if (!error) {
report_error("filename is now %s", fileNamePrt.data());
@ -379,7 +388,6 @@ void QMLManager::loadDivesWithValidCredentials()
if (informational_prefs.unit_system == IMPERIAL)
informational_prefs.units = IMPERIAL_units;
prefs.units = informational_prefs.units;
clear_dive_file_data();
DiveListModel::instance()->clear();
process_dives(false, false);
DiveListModel::instance()->addAllDives();