add make_new_branch().

This commit is contained in:
tastytea 2020-07-01 04:38:08 +02:00
parent c219aa80a5
commit afd1e058ed
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 35 additions and 5 deletions

View File

@ -19,7 +19,9 @@
#include <cstdint>
#include <git2/branch.h>
#include <git2/buffer.h>
#include <git2/clone.h>
#include <git2/commit.h>
#include <git2/errors.h>
#include <git2/global.h>
#include <git2/types.h>
@ -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

View File

@ -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();

View File

@ -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)
{