Add make_commit().

This commit is contained in:
tastytea 2020-07-01 08:48:36 +02:00
parent bf3a718986
commit 46921a6e95
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 84 additions and 2 deletions

View File

@ -16,8 +16,11 @@
#include "git.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
#include "json.hpp"
#include <cstdint>
#include <fstream>
#include <git2.h>
#include <stdexcept>
@ -27,10 +30,12 @@
namespace FediBlock
{
using std::ofstream;
using std::runtime_error;
using std::stoull;
using std::string;
using std::string_view;
using std::to_string;
using std::uint64_t;
git_repository *_repo{nullptr};
@ -108,4 +113,78 @@ void make_new_branch()
check_git_error(error);
}
void make_commit(const entry_type &entry)
{
// Write files.
const auto id{get_last_id() + 1};
const string basename{get_tmpdir() / "repo" / ("web-" + to_string(id))};
ofstream file(basename + ".json");
if (!file.good())
{
throw; // FIXME
}
file << to_json(entry);
file.close();
if (!entry.screenshot_filepath.empty())
{
// TODO: Add real file extension.
fs::copy(entry.screenshot_filepath, basename + ".image");
}
// Add files.
int error{0};
git_index *index{nullptr};
error = git_repository_index(&index, _repo);
check_git_error(error);
error = git_index_add_all(index, nullptr, 0, nullptr, nullptr);
check_git_error(error);
error = git_index_write(index);
check_git_error(error);
// Create commit.
git_signature *sig;
git_oid oid_commit;
git_oid oid_tree;
git_oid oid_parent;
git_tree *tree;
git_object *parent{nullptr};
git_reference *ref{nullptr};
error = git_signature_now(&sig, "Web", "Don't @ me");
check_git_error(error);
error = git_revparse_ext(&parent, &ref, _repo, "HEAD");
check_git_error(error);
error = git_repository_index(&index, _repo);
check_git_error(error);
error = git_index_write_tree(&oid_tree, index);
check_git_error(error);
error = git_index_write(index);
check_git_error(error);
error = git_tree_lookup(&tree, _repo, &oid_tree);
check_git_error(error);
error = git_reference_name_to_id(&oid_parent, _repo, "HEAD");
check_git_error(error);
// 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);
git_index_free(index);
git_signature_free(sig);
git_tree_free(tree);
}
} // namespace FediBlock

View File

@ -17,6 +17,8 @@
#ifndef FEDIBLOCK_BACKEND_GIT_HPP
#define FEDIBLOCK_BACKEND_GIT_HPP
#include "cgi.hpp"
#include <cstdint>
namespace FediBlock
@ -37,8 +39,8 @@ void clone_repo();
// Make a new branch for preparing the pull request.
void make_new_branch();
// Make a new pull request.
void make_pull_request();
// Make a new commit.
void make_commit(const entry_type &entry);
} // namespace FediBlock

View File

@ -69,6 +69,7 @@ int main()
clone_repo();
cout << "Next branch ID: " << get_last_id() + 1 << "\r\n";
make_new_branch();
make_commit(entry);
try
{