core: turn saved_git_id into a std::string

Simplifies memory management. Think about unglobalizing this,
once everything is in C++ so that we can put an std::string
into struct divelog.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-02-28 08:49:42 +01:00 committed by Michael Keller
parent c05be40bfd
commit a1826f77da
4 changed files with 15 additions and 14 deletions

View file

@ -266,20 +266,18 @@ static int parse_file_buffer(const char *filename, struct memblock *mem, struct
extern "C" bool remote_repo_uptodate(const char *filename, struct git_info *info)
{
char *current_sha = copy_string(saved_git_id);
std::string current_sha = saved_git_id;
if (is_git_repository(filename, info) && open_git_repository(info)) {
const char *sha = get_sha(info->repo, info->branch);
if (!empty_string(sha) && same_string(sha, current_sha)) {
if (!sha.empty() && current_sha == sha) {
fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha);
free(current_sha);
return true;
}
}
// Either the repository couldn't be opened, or the SHA couldn't
// be found.
free(current_sha);
return false;
}

View file

@ -45,7 +45,6 @@ extern int git_load_dives(struct git_info *, struct divelog *log);
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;
extern void clear_git_id(void);
@ -58,6 +57,10 @@ int get_authorship(git_repository *repo, git_signature **authorp);
#ifdef __cplusplus
}
#include <string>
extern std::string saved_git_id;
#endif
#endif // GITACCESS_H

View file

@ -10,6 +10,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <git2.h>
#include <memory>
#include <libdivecomputer/parser.h>
#include "gettext.h"
@ -32,7 +33,8 @@
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
const char *saved_git_id = NULL;
// TODO: Should probably be moved to struct divelog to allow for multi-document
std::string saved_git_id;
struct git_parser_state {
git_repository *repo;
@ -1878,8 +1880,7 @@ static int load_dives_from_tree(git_repository *repo, git_tree *tree, struct git
extern "C" void clear_git_id(void)
{
free((void *)saved_git_id);
saved_git_id = NULL;
saved_git_id.clear();
}
extern "C" void set_git_id(const struct git_oid *id)
@ -1887,8 +1888,7 @@ extern "C" void set_git_id(const struct git_oid *id)
char git_id_buffer[GIT_OID_HEXSZ + 1];
git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), id);
free((void *)saved_git_id);
saved_git_id = strdup(git_id_buffer);
saved_git_id = git_id_buffer;
}
static int find_commit(git_repository *repo, const char *branch, git_commit **commit_p)

View file

@ -1193,19 +1193,19 @@ static int create_new_commit(struct git_info *info, git_oid *tree_id, bool creat
return report_error("Invalid branch name '%s'", info->branch);
case GIT_ENOTFOUND: /* We'll happily create it */
ref = NULL;
parent = try_to_find_parent(saved_git_id, info->repo);
parent = try_to_find_parent(saved_git_id.c_str(), info->repo);
break;
case 0:
if (git_reference_peel(&parent, ref, GIT_OBJ_COMMIT))
return report_error("Unable to look up parent in branch '%s'", info->branch);
if (saved_git_id) {
if (!saved_git_id.empty()) {
if (existing_filename && verbose)
SSRF_INFO("existing filename %s\n", existing_filename);
const git_oid *id = git_commit_id((const git_commit *) parent);
/* if we are saving to the same git tree we got this from, let's make
* sure there is no confusion */
if (same_string(existing_filename, info->url) && git_oid_strcmp(id, saved_git_id))
if (same_string(existing_filename, info->url) && git_oid_strcmp(id, saved_git_id.c_str()))
return report_error("The git branch does not match the git parent of the source");
}
@ -1321,7 +1321,7 @@ extern "C" int do_git_save(struct git_info *info, bool select_only, bool create_
* Check if we can do the cached writes - we need to
* have the original git commit we loaded in the repo
*/
cached_ok = try_to_find_parent(saved_git_id, info->repo);
cached_ok = try_to_find_parent(saved_git_id.c_str(), info->repo);
/* Start with an empty tree: no subdirectories, no files */
if (git_treebuilder_new(&tree.files, info->repo, NULL))