Set, create and remove repo directory in git::init() and shutdown().
This commit is contained in:
parent
f363ebacb8
commit
c3020b782d
|
@ -125,7 +125,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
const string_view target_dir{args[1]};
|
const string_view target_dir{args[1]};
|
||||||
|
|
||||||
git_libgit2_init();
|
git::init();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -139,8 +139,7 @@ int main(int argc, char *argv[])
|
||||||
cerr << "Error: " << e.what() << '\n';
|
cerr << "Error: " << e.what() << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
files::remove_tmpdir();
|
git::cleanup();
|
||||||
git_libgit2_shutdown();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,7 +200,7 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const vector<string_view> args(argv, argv + argc);
|
const vector<string_view> args(argv, argv + argc);
|
||||||
|
|
||||||
git_libgit2_init();
|
git::init();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -214,8 +214,7 @@ int main(int argc, char *argv[])
|
||||||
cerr << "Error: " << e.what() << '\n';
|
cerr << "Error: " << e.what() << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
files::remove_tmpdir();
|
git::cleanup();
|
||||||
git_libgit2_shutdown();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
39
src/git.cpp
39
src/git.cpp
|
@ -50,6 +50,28 @@ using std::uint64_t;
|
||||||
|
|
||||||
git_repository *_repo{nullptr};
|
git_repository *_repo{nullptr};
|
||||||
constexpr string_view _clone_url{"git@schlomp.space:FediBlock/data.git"};
|
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)
|
void check(int error)
|
||||||
{
|
{
|
||||||
|
@ -71,22 +93,11 @@ int cred_acquire(git_cred **cred, const char * /*url*/,
|
||||||
(datadir / "ssh_id").c_str(), nullptr);
|
(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;
|
git_clone_options options = GIT_CLONE_OPTIONS_INIT;
|
||||||
options.fetch_opts.callbacks.credentials = cred_acquire;
|
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()
|
void create_branch()
|
||||||
|
@ -112,7 +123,7 @@ void create_branch()
|
||||||
void commit(const entry_type &entry)
|
void commit(const entry_type &entry)
|
||||||
{
|
{
|
||||||
// Write files.
|
// Write files.
|
||||||
const string basename{files::get_tmpdir() / "repo" / get_branch_name()};
|
const string basename{_repo_dir / get_branch_name()};
|
||||||
|
|
||||||
ofstream file(basename + ".json");
|
ofstream file(basename + ".json");
|
||||||
if (!file.good())
|
if (!file.good())
|
||||||
|
|
22
src/git.hpp
22
src/git.hpp
|
@ -30,6 +30,24 @@ namespace FediBlock::git
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::uint64_t;
|
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.
|
// Throw runtime_error on error.
|
||||||
void check(int error);
|
void check(int error);
|
||||||
|
|
||||||
|
@ -41,11 +59,9 @@ int cred_acquire(git_cred **cred, const char *url,
|
||||||
/*!
|
/*!
|
||||||
* @brief Clone the git repo.
|
* @brief Clone the git repo.
|
||||||
*
|
*
|
||||||
* @param cache Clone to cache if true, to temporary dir if false.
|
|
||||||
*
|
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
void clone(bool cache = false);
|
void clone();
|
||||||
|
|
||||||
// Make a new branch for preparing the pull request.
|
// Make a new branch for preparing the pull request.
|
||||||
void create_branch();
|
void create_branch();
|
||||||
|
|
|
@ -37,7 +37,7 @@ int main()
|
||||||
{
|
{
|
||||||
cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
|
cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
|
||||||
|
|
||||||
git_libgit2_init();
|
git::init();
|
||||||
gitea::init();
|
gitea::init();
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -66,7 +66,7 @@ int main()
|
||||||
}
|
}
|
||||||
|
|
||||||
gitea::cleanup();
|
gitea::cleanup();
|
||||||
git_libgit2_shutdown();
|
git::cleanup();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue