Add update_cached_repo().

Create lockfile, pull or clone.
This commit is contained in:
tastytea 2020-10-14 02:12:56 +02:00
parent 2c58894c92
commit 4a88652435
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 39 additions and 0 deletions

View File

@ -22,11 +22,13 @@
#include <git2.h>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#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

View File

@ -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