diff --git a/src/git.cpp b/src/git.cpp index ee130f2..0bf1110 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -16,8 +16,11 @@ #include "git.hpp" #include "files.hpp" +#include "fs-compat.hpp" +#include "json.hpp" #include +#include #include #include @@ -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 diff --git a/src/git.hpp b/src/git.hpp index 4474892..e658b58 100644 --- a/src/git.hpp +++ b/src/git.hpp @@ -17,6 +17,8 @@ #ifndef FEDIBLOCK_BACKEND_GIT_HPP #define FEDIBLOCK_BACKEND_GIT_HPP +#include "cgi.hpp" + #include 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 diff --git a/src/main.cpp b/src/main.cpp index fffcaa2..35009ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 {