Set, create and remove repo directory in git::init() and shutdown().

This commit is contained in:
tastytea 2020-10-13 23:49:33 +02:00
parent f363ebacb8
commit c3020b782d
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 50 additions and 25 deletions

View File

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

View File

@ -200,7 +200,7 @@ int main(int argc, char *argv[])
{
const vector<string_view> 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;
}

View File

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

View File

@ -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();

View File

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