Cloud storage: check the top commit without loading dives

This way we can check if the local cache is in sync with the remote without
always triggering a load of the dives from git.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-12-27 09:56:27 -08:00
parent 739d7d74e7
commit bc200c3089
2 changed files with 27 additions and 4 deletions

View file

@ -18,6 +18,7 @@ extern struct git_repository *is_git_repository(const char *filename, const char
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 *);
extern const char *get_sha(git_repository *repo, const char *branch);
extern int do_git_save(git_repository *repo, const char *branch, const char *remote, bool select_only, bool create_empty);
extern const char *saved_git_id;
extern void clear_git_id(void);

View file

@ -1595,6 +1595,19 @@ void set_git_id(const struct git_oid * id)
saved_git_id = git_id_buffer;
}
static int find_commit(git_repository *repo, const char *branch, git_commit **commit_p)
{
int ret;
git_object *object;
git_tree *tree;
if (git_revparse_single(&object, repo, branch))
return report_error("Unable to look up revision '%s'", branch);
if (git_object_peel((git_object **)commit_p, object, GIT_OBJ_COMMIT))
return report_error("Revision '%s' is not a valid commit", branch);
return 0;
}
static int do_git_load(git_repository *repo, const char *branch)
{
int ret;
@ -1602,10 +1615,9 @@ static int do_git_load(git_repository *repo, const char *branch)
git_commit *commit;
git_tree *tree;
if (git_revparse_single(&object, repo, branch))
return report_error("Unable to look up revision '%s'", branch);
if (git_object_peel((git_object **)&commit, object, GIT_OBJ_COMMIT))
return report_error("Revision '%s' is not a valid commit", branch);
ret = find_commit(repo, branch, &commit);
if (ret)
return ret;
if (git_commit_tree(&tree, commit))
return report_error("Could not look up tree of commit in branch '%s'", branch);
ret = load_dives_from_tree(repo, tree);
@ -1615,6 +1627,16 @@ static int do_git_load(git_repository *repo, const char *branch)
return ret;
}
const char *get_sha(git_repository *repo, const char *branch)
{
static char git_id_buffer[GIT_OID_HEXSZ+1];
git_commit *commit;
if (find_commit(repo, branch, &commit))
return NULL;
git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), (const git_oid *)commit);
return git_id_buffer;
}
/*
* Like git_save_dives(), this silently returns a negative
* value if it's not a git repository at all (so that you