90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
/* 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>
|
|
#include <git2/clone.h>
|
|
#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;
|
|
|
|
void clone_repo()
|
|
{
|
|
int error = git_clone(&_repo, "https://schlomp.space/FediBlock/data.git",
|
|
(get_tmpdir() / "repo").c_str(), nullptr);
|
|
if (error < 0)
|
|
{
|
|
const git_error *e = git_error_last();
|
|
throw runtime_error{e->message};
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} // namespace FediBlock
|