git clone + brittle ID calculation.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2020-07-01 03:51:00 +02:00
parent 9231159fdf
commit c219aa80a5
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
8 changed files with 148 additions and 4 deletions

View File

@ -26,7 +26,7 @@ steps:
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
- apt-get update -q
- apt-get install -qq build-essential cmake g++-10 clang pkg-config
- apt-get install -qq catch libcgicc-dev nlohmann-json3-dev
- apt-get install -qq catch libcgicc-dev nlohmann-json3-dev libgit2-dev
- rm -rf build && mkdir -p build && cd build
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
- make VERBOSE=1
@ -62,7 +62,7 @@ steps:
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
- apt-get update -q
- apt-get install -qq build-essential cmake clang pkg-config
- apt-get install -qq catch libcgicc-dev nlohmann-json-dev
- apt-get install -qq catch libcgicc-dev nlohmann-json-dev libgit2-dev
- rm -rf build && mkdir -p build && cd build
- cmake -G "Unix Makefiles" -DWITH_TESTS=YES ..
- make VERBOSE=1

View File

@ -39,6 +39,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(cgicc REQUIRED IMPORTED_TARGET cgicc)
find_package(nlohmann_json REQUIRED CONFIG)
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
pkg_check_modules(libgit2 REQUIRED IMPORTED_TARGET libgit2)
add_subdirectory(src)

View File

@ -14,6 +14,7 @@
:uri-clang-tidy: https://clang.llvm.org/extra/clang-tidy/
:uri-cgicc: https://www.gnu.org/software/cgicc/
:uri-nlohmann-json: https://nlohmann.github.io/json/
:uri-libgit2: https://libgit2.org/
*{project}* turns form data into JSON and opens a pull request on
link:https://schlomp.space/FediBlock/data[FediBlock/data].
@ -30,6 +31,7 @@ link:https://schlomp.space/FediBlock/data[FediBlock/data].
* link:{uri-cmake}[CMake] (at least: 3.9)
* link:{uri-cgicc}[cgicc] (tested: 3.2)
* link:{uri-nlohmann-json}[nlohmann-json] (tested: 3.7 / 2.1)
* link:{uri-libgit2}[libgit2] (tested: 1.0 / 0.27)
* Optional
** Tests: link:{uri-catch}[Catch] (tested: 2.5 / 1.10)
** DEB package: link:{uri-dpkg}[dpkg] (tested: 1.19)

View File

@ -15,7 +15,7 @@ target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${PROJECT_NAME}
PRIVATE PkgConfig::cgicc nlohmann_json std::filesystem)
PRIVATE PkgConfig::cgicc nlohmann_json std::filesystem PkgConfig::libgit2)
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")

89
src/git.cpp Normal file
View File

@ -0,0 +1,89 @@
/* 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

42
src/git.hpp Normal file
View File

@ -0,0 +1,42 @@
/* 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/>.
*/
#ifndef FEDIBLOCK_BACKEND_GIT_HPP
#define FEDIBLOCK_BACKEND_GIT_HPP
#include <cstdint>
namespace FediBlock
{
using std::uint64_t;
// Clone the git repo.
void clone_repo();
// FIXME: This will collapse when branches are deleted, use pull request IDs?
// Get highest branch-ID in use.
[[nodiscard]] uint64_t get_last_id();
// Make a new branch for preparing the pull request.
void make_new_branch();
// Make a new pull request.
void make_pull_request();
} // namespace FediBlock
#endif // FEDIBLOCK_BACKEND_GIT_HPP

View File

@ -17,8 +17,11 @@
#include "cgi.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
#include "git.hpp"
#include "json.hpp"
#include <git2/global.h>
#include <exception>
#include <iostream>
@ -61,6 +64,11 @@ int main()
print_debug(entry);
git_libgit2_init();
clone_repo();
cout << "Next branch ID: " << get_last_id() + 1 << "\r\n";
try
{
remove_tmpdir();
@ -70,5 +78,7 @@ int main()
cerr << e.what() << '\n';
}
git_libgit2_shutdown();
return 0;
}

View File

@ -9,7 +9,7 @@ target_include_directories(${PROJECT_NAME}_testlib
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>")
target_link_libraries(${PROJECT_NAME}_testlib
PUBLIC PkgConfig::cgicc nlohmann_json std::filesystem)
PUBLIC PkgConfig::cgicc nlohmann_json std::filesystem PkgConfig::libgit2)
file(GLOB sources_tests test_*.cpp)