From c3020b782db9e5bbe402f415bf26c2a2db38ab10 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 13 Oct 2020 23:49:33 +0200 Subject: [PATCH] Set, create and remove repo directory in git::init() and shutdown(). --- src/generators/html.cpp | 5 ++--- src/generators/rss.cpp | 5 ++--- src/git.cpp | 39 +++++++++++++++++++++++++-------------- src/git.hpp | 22 +++++++++++++++++++--- src/main.cpp | 4 ++-- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/generators/html.cpp b/src/generators/html.cpp index 2532411..2141e21 100644 --- a/src/generators/html.cpp +++ b/src/generators/html.cpp @@ -125,7 +125,7 @@ int main(int argc, char *argv[]) } const string_view target_dir{args[1]}; - git_libgit2_init(); + git::init(); try { @@ -139,8 +139,7 @@ int main(int argc, char *argv[]) cerr << "Error: " << e.what() << '\n'; } - files::remove_tmpdir(); - git_libgit2_shutdown(); + git::cleanup(); return 0; } diff --git a/src/generators/rss.cpp b/src/generators/rss.cpp index 518223e..056a1b2 100644 --- a/src/generators/rss.cpp +++ b/src/generators/rss.cpp @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) { const vector args(argv, argv + argc); - git_libgit2_init(); + git::init(); try { @@ -214,8 +214,7 @@ int main(int argc, char *argv[]) cerr << "Error: " << e.what() << '\n'; } - files::remove_tmpdir(); - git_libgit2_shutdown(); + git::cleanup(); return 0; } diff --git a/src/git.cpp b/src/git.cpp index a6416fa..c6667bf 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -50,6 +50,28 @@ using std::uint64_t; git_repository *_repo{nullptr}; constexpr string_view _clone_url{"git@schlomp.space:FediBlock/data.git"}; +fs::path _repo_dir{}; + +void init(const bool cache) +{ + if (cache) + { + _repo_dir = files::get_cachedir() / "repo"; + } + _repo_dir = files::get_tmpdir() / "repo"; + + git_libgit2_init(); +} + +void cleanup(const bool cache) +{ + git_libgit2_shutdown(); + + if (cache) + { + files::remove_tmpdir(); + } +} void check(int error) { @@ -71,22 +93,11 @@ int cred_acquire(git_cred **cred, const char * /*url*/, (datadir / "ssh_id").c_str(), nullptr); } -void clone(const bool cache) +void clone() { - // clang-format off - const string destination{[cache] - { - if (cache) - { - return files::get_cachedir() / "repo"; - } - return files::get_tmpdir() / "repo"; - }()}; - // clang-format on - git_clone_options options = GIT_CLONE_OPTIONS_INIT; options.fetch_opts.callbacks.credentials = cred_acquire; - check(git_clone(&_repo, _clone_url.data(), destination.c_str(), &options)); + check(git_clone(&_repo, _clone_url.data(), _repo_dir.c_str(), &options)); } void create_branch() @@ -112,7 +123,7 @@ void create_branch() void commit(const entry_type &entry) { // Write files. - const string basename{files::get_tmpdir() / "repo" / get_branch_name()}; + const string basename{_repo_dir / get_branch_name()}; ofstream file(basename + ".json"); if (!file.good()) diff --git a/src/git.hpp b/src/git.hpp index 3d90529..7004c35 100644 --- a/src/git.hpp +++ b/src/git.hpp @@ -30,6 +30,24 @@ namespace FediBlock::git using std::string; using std::uint64_t; +/*! + * @brief Initialize libgit and _repo_dir. + * + * @param cache Set _repo_dir to cache dir if true, to temporary dir if false. + * + * @since 0.1.0 + */ +void init(bool cache = false); + +/*! + * @brief Clean up libgit's global state. + * + * @param cache remove temporary dir if false. + * + * @since 0.1.0 + */ +void cleanup(bool cache = false); + // Throw runtime_error on error. void check(int error); @@ -41,11 +59,9 @@ int cred_acquire(git_cred **cred, const char *url, /*! * @brief Clone the git repo. * - * @param cache Clone to cache if true, to temporary dir if false. - * * @since 0.1.0 */ -void clone(bool cache = false); +void clone(); // Make a new branch for preparing the pull request. void create_branch(); diff --git a/src/main.cpp b/src/main.cpp index d10cce4..aa03e6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,7 @@ int main() { cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n"; - git_libgit2_init(); + git::init(); gitea::init(); try @@ -66,7 +66,7 @@ int main() } gitea::cleanup(); - git_libgit2_shutdown(); + git::cleanup(); return 0; }