From 4a886524356c043bb403f5a8c3bb4cc1337993f5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 14 Oct 2020 02:12:56 +0200 Subject: [PATCH] Add update_cached_repo(). Create lockfile, pull or clone. --- src/git.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/git.hpp | 3 +++ 2 files changed, 39 insertions(+) diff --git a/src/git.cpp b/src/git.cpp index c46840f..d25e15d 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -22,11 +22,13 @@ #include +#include #include #include #include #include #include +#include #if LIBGIT2_VER_MAJOR == 0 # if LIBGIT2_VER_MINOR < 28 @@ -47,6 +49,10 @@ using std::string; using std::string_view; using std::to_string; using std::uint64_t; +using std::uint8_t; +using std::this_thread::sleep_for; + +using namespace std::chrono_literals; git_repository *_repo{nullptr}; constexpr string_view _clone_url{"git@schlomp.space:FediBlock/data.git"}; @@ -212,4 +218,34 @@ string get_branch_name() return "web-" + to_string(id); } +void update_cached_repo() +{ + for (uint8_t counter{0}; counter < 20; ++counter) + { + if (files::create_lockfile() == files::lock_result::created) + { + break; + } + sleep_for(2s); + } + + if (!fs::exists(_repo_dir / ".git")) + { + clone(); + return; + } + + git_remote *remote{nullptr}; + check(git_repository_open(&_repo, _repo_dir.c_str())); + check(git_remote_lookup(&remote, _repo, "origin")); + + check(git_remote_fetch(remote, nullptr, nullptr, nullptr)); + + git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT; + options.checkout_strategy = GIT_CHECKOUT_FORCE; + check(git_checkout_head(_repo, options)); + + git_remote_free(remote); +} + } // namespace FediBlock::git diff --git a/src/git.hpp b/src/git.hpp index 7004c35..081ef87 100644 --- a/src/git.hpp +++ b/src/git.hpp @@ -75,6 +75,9 @@ void push(); // Get the name of the branch we use. string get_branch_name(); +// If cached repo exist, pull. If not, clone. +void update_cached_repo(); + } // namespace FediBlock::git #endif // FEDIBLOCK_BACKEND_GIT_HPP