Rename git functions, error checking with fewer steps.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2020-07-01 08:55:13 +02:00
parent 542d1a865c
commit 74e1471e49
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 29 additions and 46 deletions

View File

@ -40,7 +40,7 @@ using std::uint64_t;
git_repository *_repo{nullptr};
void check_git_error(int error)
void check(int error)
{
if (error != 0)
{
@ -49,11 +49,10 @@ void check_git_error(int error)
}
}
void clone_repo()
void clone()
{
int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git",
(get_tmpdir() / "repo").c_str(), nullptr);
check_git_error(error);
check(git_clone(&_repo, "https://schlomp.space/FediBlock/data.git",
(get_tmpdir() / "repo").c_str(), nullptr));
}
uint64_t get_last_id()
@ -92,7 +91,7 @@ uint64_t get_last_id()
return id;
}
void make_new_branch()
void create_branch()
{
const auto id{get_last_id() + 1};
const string branch_name = "web-" + std::to_string(id);
@ -100,20 +99,17 @@ void make_new_branch()
git_commit *commit;
// Get SHA1 of HEAD.
int error{git_reference_name_to_id(&oid_parent, _repo, "HEAD")};
check_git_error(error);
check(git_reference_name_to_id(&oid_parent, _repo, "HEAD"));
// Translate SHA-1 to git_commit.
error = git_commit_lookup(&commit, _repo, &oid_parent);
check_git_error(error);
check(git_commit_lookup(&commit, _repo, &oid_parent));
git_reference *branch;
// Create new branch.
error = git_branch_create(&branch, _repo, branch_name.c_str(), commit, 0);
check_git_error(error);
check(git_branch_create(&branch, _repo, branch_name.c_str(), commit, 0));
}
void make_commit(const entry_type &entry)
void commit(const entry_type &entry)
{
// Write files.
const auto id{get_last_id() + 1};
@ -134,17 +130,12 @@ void make_commit(const entry_type &entry)
}
// Add files.
int error{0};
git_index *index{nullptr};
error = git_repository_index(&index, _repo);
check_git_error(error);
check(git_repository_index(&index, _repo));
error = git_index_add_all(index, nullptr, 0, nullptr, nullptr);
check_git_error(error);
check(git_index_add_all(index, nullptr, 0, nullptr, nullptr));
error = git_index_write(index);
check_git_error(error);
check(git_index_write(index));
// Create commit.
git_signature *sig;
@ -155,32 +146,24 @@ void make_commit(const entry_type &entry)
git_object *parent{nullptr};
git_reference *ref{nullptr};
error = git_signature_now(&sig, "Web", "Don't @ me");
check_git_error(error);
check(git_signature_now(&sig, "Web", "Don't @ me"));
error = git_revparse_ext(&parent, &ref, _repo, "HEAD");
check_git_error(error);
check(git_revparse_ext(&parent, &ref, _repo, "HEAD"));
error = git_repository_index(&index, _repo);
check_git_error(error);
check(git_repository_index(&index, _repo));
error = git_index_write_tree(&oid_tree, index);
check_git_error(error);
check(git_index_write_tree(&oid_tree, index));
error = git_index_write(index);
check_git_error(error);
check(git_index_write(index));
error = git_tree_lookup(&tree, _repo, &oid_tree);
check_git_error(error);
check(git_tree_lookup(&tree, _repo, &oid_tree));
error = git_reference_name_to_id(&oid_parent, _repo, "HEAD");
check_git_error(error);
check(git_reference_name_to_id(&oid_parent, _repo, "HEAD"));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
error = git_commit_create_v(&oid_commit, _repo, "HEAD", sig, sig, nullptr,
("New entry: " + entry.instance).c_str(), tree,
parent != nullptr ? 1 : 0, parent);
check_git_error(error);
check(git_commit_create_v(&oid_commit, _repo, "HEAD", sig, sig, nullptr,
("New entry: " + entry.instance).c_str(), tree,
parent != nullptr ? 1 : 0, parent));
git_index_free(index);
git_signature_free(sig);

View File

@ -27,20 +27,20 @@ namespace FediBlock::git
using std::uint64_t;
// Throws runtime_error on error.
void check_git_error(int error);
void check(int error);
// Clone the git repo.
void clone_repo();
void clone();
// FIXME: This will collapse when branches are deleted, use pull request IDs?
// Get highest branch-ID in use.
[[nodiscard]] uint64_t get_last_id();
// Make a new branch for preparing the pull request.
void make_new_branch();
void create_branch();
// Make a new commit.
void make_commit(const entry_type &entry);
void commit(const entry_type &entry);
} // namespace FediBlock::git

View File

@ -66,10 +66,10 @@ int main()
git_libgit2_init();
git::clone_repo();
git::clone();
cout << "Next branch ID: " << git::get_last_id() + 1 << "\r\n";
git::make_new_branch();
git::make_commit(entry);
git::create_branch();
git::commit(entry);
try
{