diff --git a/.drone.yml b/.drone.yml index 7d2bf7b..f800e61 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 780c940..80ada31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.adoc b/README.adoc index 064a64d..171ed2a 100644 --- a/README.adoc +++ b/README.adoc @@ -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) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4f8958..5be332b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}") diff --git a/src/git.cpp b/src/git.cpp new file mode 100644 index 0000000..857efbf --- /dev/null +++ b/src/git.cpp @@ -0,0 +1,89 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * 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 . + */ + +#include "git.hpp" +#include "files.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +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 diff --git a/src/git.hpp b/src/git.hpp new file mode 100644 index 0000000..97a594b --- /dev/null +++ b/src/git.hpp @@ -0,0 +1,42 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * 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 . + */ + +#ifndef FEDIBLOCK_BACKEND_GIT_HPP +#define FEDIBLOCK_BACKEND_GIT_HPP + +#include + +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 diff --git a/src/main.cpp b/src/main.cpp index 9ed3a55..d5468e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,8 +17,11 @@ #include "cgi.hpp" #include "files.hpp" #include "fs-compat.hpp" +#include "git.hpp" #include "json.hpp" +#include + #include #include @@ -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; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2730a64..2679ace 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,7 @@ target_include_directories(${PROJECT_NAME}_testlib "$") 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)