This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/git.cpp

116 lines
3.0 KiB
C++
Raw Normal View History

2020-07-01 03:51:00 +02:00
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "git.hpp"
#include "files.hpp"
#include <cstdint>
#include <git2/branch.h>
2020-07-01 04:38:08 +02:00
#include <git2/buffer.h>
2020-07-01 03:51:00 +02:00
#include <git2/clone.h>
2020-07-01 04:38:08 +02:00
#include <git2/commit.h>
2020-07-01 03:51:00 +02:00
#include <git2/errors.h>
#include <git2/global.h>
#include <git2/types.h>
#include <stdexcept>
#include <string>
#include <string_view>
namespace FediBlock
{
using std::runtime_error;
using std::stoull;
using std::string;
using std::string_view;
using std::uint64_t;
git_repository *_repo = nullptr;
2020-07-01 04:38:08 +02:00
void check_git_error(int error)
2020-07-01 03:51:00 +02:00
{
2020-07-01 04:38:08 +02:00
if (error != 0)
2020-07-01 03:51:00 +02:00
{
const git_error *e = git_error_last();
throw runtime_error{e->message};
}
}
2020-07-01 04:38:08 +02:00
void clone_repo()
{
int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git",
(get_tmpdir() / "repo").c_str(), nullptr);
check_git_error(error);
}
2020-07-01 03:51:00 +02:00
uint64_t get_last_id()
{
constexpr string_view branch_prefix{"from-web-"};
uint64_t id{0};
git_branch_iterator *it;
if (git_branch_iterator_new(&it, _repo, GIT_BRANCH_ALL) == 0)
{
git_reference *ref;
git_branch_t type;
while (git_branch_next(&ref, &type, it) == 0)
{
const char *out{nullptr};
git_branch_name(&out, ref);
if (type == GIT_BRANCH_REMOTE)
{
const string branch_name{out};
size_t pos{branch_name.find(branch_prefix)};
if (pos != string::npos)
{
pos += branch_prefix.size();
uint64_t newid{stoull(branch_name.substr(pos))};
if (newid > id)
{
id = newid;
}
}
}
git_reference_free(ref);
}
git_branch_iterator_free(it);
}
return id;
}
2020-07-01 04:38:08 +02:00
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);
}
2020-07-01 03:51:00 +02:00
} // namespace FediBlock