add make_new_branch().
This commit is contained in:
parent
c219aa80a5
commit
afd1e058ed
34
src/git.cpp
34
src/git.cpp
|
@ -19,7 +19,9 @@
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <git2/branch.h>
|
#include <git2/branch.h>
|
||||||
|
#include <git2/buffer.h>
|
||||||
#include <git2/clone.h>
|
#include <git2/clone.h>
|
||||||
|
#include <git2/commit.h>
|
||||||
#include <git2/errors.h>
|
#include <git2/errors.h>
|
||||||
#include <git2/global.h>
|
#include <git2/global.h>
|
||||||
#include <git2/types.h>
|
#include <git2/types.h>
|
||||||
|
@ -39,17 +41,22 @@ using std::uint64_t;
|
||||||
|
|
||||||
git_repository *_repo = nullptr;
|
git_repository *_repo = nullptr;
|
||||||
|
|
||||||
void clone_repo()
|
void check_git_error(int error)
|
||||||
{
|
{
|
||||||
int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git",
|
if (error != 0)
|
||||||
(get_tmpdir() / "repo").c_str(), nullptr);
|
|
||||||
if (error < 0)
|
|
||||||
{
|
{
|
||||||
const git_error *e = git_error_last();
|
const git_error *e = git_error_last();
|
||||||
throw runtime_error{e->message};
|
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()
|
uint64_t get_last_id()
|
||||||
{
|
{
|
||||||
constexpr string_view branch_prefix{"from-web-"};
|
constexpr string_view branch_prefix{"from-web-"};
|
||||||
|
@ -86,4 +93,23 @@ uint64_t get_last_id()
|
||||||
return 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
|
} // namespace FediBlock
|
||||||
|
|
|
@ -24,6 +24,9 @@ namespace FediBlock
|
||||||
|
|
||||||
using std::uint64_t;
|
using std::uint64_t;
|
||||||
|
|
||||||
|
// Throws runtime_error on error.
|
||||||
|
void check_git_error(int error);
|
||||||
|
|
||||||
// Clone the git repo.
|
// Clone the git repo.
|
||||||
void clone_repo();
|
void clone_repo();
|
||||||
|
|
||||||
|
|
|
@ -68,10 +68,11 @@ int main()
|
||||||
|
|
||||||
clone_repo();
|
clone_repo();
|
||||||
cout << "Next branch ID: " << get_last_id() + 1 << "\r\n";
|
cout << "Next branch ID: " << get_last_id() + 1 << "\r\n";
|
||||||
|
make_new_branch();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
remove_tmpdir();
|
// remove_tmpdir();
|
||||||
}
|
}
|
||||||
catch (const exception &e)
|
catch (const exception &e)
|
||||||
{
|
{
|
||||||
|
|
Reference in New Issue