diff --git a/src/git.cpp b/src/git.cpp index 857efbf..55b7778 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -19,7 +19,9 @@ #include #include +#include #include +#include #include #include #include @@ -39,17 +41,22 @@ using std::uint64_t; git_repository *_repo = nullptr; -void clone_repo() +void check_git_error(int error) { - int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git", - (get_tmpdir() / "repo").c_str(), nullptr); - if (error < 0) + if (error != 0) { const git_error *e = git_error_last(); throw runtime_error{e->message}; } } +void clone_repo() +{ + int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git", + (get_tmpdir() / "repo").c_str(), nullptr); + check_git_error(error); +} + uint64_t get_last_id() { constexpr string_view branch_prefix{"from-web-"}; @@ -86,4 +93,23 @@ uint64_t get_last_id() return id; } +void make_new_branch() +{ + git_oid oid_parent; + git_commit *commit; + + // Get SHA1 of HEAD. + int error{git_reference_name_to_id(&oid_parent, _repo, "HEAD")}; + check_git_error(error); + + // Translate SHA-1 to git_commit. + error = git_commit_lookup(&commit, _repo, &oid_parent); + check_git_error(error); + + git_reference *branch; + // Create new branch. + error = git_branch_create(&branch, _repo, "test", commit, 0); + check_git_error(error); +} + } // namespace FediBlock diff --git a/src/git.hpp b/src/git.hpp index 97a594b..4474892 100644 --- a/src/git.hpp +++ b/src/git.hpp @@ -24,6 +24,9 @@ namespace FediBlock using std::uint64_t; +// Throws runtime_error on error. +void check_git_error(int error); + // Clone the git repo. void clone_repo(); diff --git a/src/main.cpp b/src/main.cpp index d5468e1..fffcaa2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,10 +68,11 @@ int main() clone_repo(); cout << "Next branch ID: " << get_last_id() + 1 << "\r\n"; + make_new_branch(); try { - remove_tmpdir(); + // remove_tmpdir(); } catch (const exception &e) {