From f65a1b6ab36f4c4f2694ecf8ee76c24761218294 Mon Sep 17 00:00:00 2001 From: Sergii Kryvonos Date: Tue, 28 Jan 2025 23:54:47 +0100 Subject: [PATCH] rebase.cmd processing using boost asio --- CMakeLists.txt | 1 + cmake/gitect.cmake | 7 +- .../CMakeLists.txt | 1 + cmake/process-cmd-rebase-or-abort/main.cpp | 94 +++++++++++++++++++ vcpkg.json | 1 + 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 cmake/process-cmd-rebase-or-abort/CMakeLists.txt create mode 100644 cmake/process-cmd-rebase-or-abort/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 28fd32bd6..ba70823d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ endif() set(_BOOST_USED_COMPONENTS ${BOOST_USED_COMPONENTS} ${BOOST_ADDITIONAL_COMPONENTS} + asio chrono date_time filesystem diff --git a/cmake/gitect.cmake b/cmake/gitect.cmake index 5cbe27a81..37ce7026c 100644 --- a/cmake/gitect.cmake +++ b/cmake/gitect.cmake @@ -137,7 +137,7 @@ if(GIT_EXECUTABLE) set(CMD_ITERATE_BRANCHES "for /f \"usebackq tokens=*\" %b in ( branches.txt ) do ( ${CMD_REBASE_BRANCH} )") set(CMD_LINE_SUFFIX " ^|^| ${GIT_EXE_CMD} rebase --abort") add_custom_target(rebase-all-branches - DEPENDS append-each-line + DEPENDS process-cmd-rebase-or-abort COMMAND ${CMAKE_COMMAND} -E echo "Rebasing all branches onto origin/main" COMMAND ${GIT_EXECUTABLE} fetch --all || echo git fetch error @@ -145,10 +145,7 @@ if(GIT_EXECUTABLE) COMMAND cmd /c ${CMD_LIST_BRANCHES} > branches.txt COMMAND type NUL > rebase.cmd COMMAND cmd /c ( for /f "usebackq tokens=*" %b in ( branches.txt ) do @echo ${CMD_REBASE_BRANCH} ) >> rebase.cmd - COMMAND echo echo rebasing all branches > rebase-or-abort.cmd - #COMMAND cmd /c ( for /f "usebackq tokens=*" %a in ( rebase.cmd ) do echo %a ^|^| ${GIT_EXE_CMD} rebase --abort ) >> rebase-or-abort.cmd - COMMAND $ rebase.cmd "${CMD_LINE_SUFFIX}" - COMMAND cmd /c rebase.cmd + COMMAND $ COMMENT "Rebasing all branches onto origin/main using cmd with improved branch handling." WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} diff --git a/cmake/process-cmd-rebase-or-abort/CMakeLists.txt b/cmake/process-cmd-rebase-or-abort/CMakeLists.txt new file mode 100644 index 000000000..f86342a58 --- /dev/null +++ b/cmake/process-cmd-rebase-or-abort/CMakeLists.txt @@ -0,0 +1 @@ +exe() diff --git a/cmake/process-cmd-rebase-or-abort/main.cpp b/cmake/process-cmd-rebase-or-abort/main.cpp new file mode 100644 index 000000000..2066427f4 --- /dev/null +++ b/cmake/process-cmd-rebase-or-abort/main.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +// defaults used for rebase-all-branches target +#define INPUT_FILE_DEFAULT SRC_DIR "rebase.cmd" +#define GIT_REBASE_ABORT "\"" GIT_EXECUTABLE_PATH "\" rebase --abort" +#define SUFFIX_DEFAULT " || " GIT_REBASE_ABORT + +namespace bp = boost::process; + +char my_buffer[1024]; + +boost::asio::io_service io; +bp::async_pipe in_pipe(io); + +void handle_pipe_read(const boost::system::error_code& ec, std::size_t bytes_transferred); + +void schedule_read() { + in_pipe.async_read_some(boost::asio::buffer(my_buffer), boost::bind(&handle_pipe_read, _1, _2)); +} + +void handle_pipe_read(const boost::system::error_code& ec, std::size_t bytes_transferred) { + if (ec) + return; // app probably exit + + // Do something with buffer and re-schedule + std::cout.write(my_buffer, bytes_transferred); + + if (in_pipe.is_open()) + schedule_read(); +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + std::cout << "Usage: " << argv[0] << " " << std::endl; + std::cout << "Defaults used: =" INPUT_FILE_DEFAULT " =" SUFFIX_DEFAULT << std::endl; + } + + std::string_view input_file = argc == 3 ? argv[1] : INPUT_FILE_DEFAULT; + std::string_view suffix = argc == 3 ? argv[2] : SUFFIX_DEFAULT; + + std::ifstream infile(input_file.data()); + if (!infile.is_open()) { + std::cerr << "Failed to open file: " << input_file << std::endl; + return 1; + } + + + std::string line; + while (std::getline(infile, line)) { + bp::child c(line, bp::std_out > in_pipe, bp::on_exit([](int code, std::error_code ec) { + std::cout << "Child exited (code=" << code << "): " << ec.message() << std::endl; + in_pipe.close(); + + if (code != 0) { + bp::child c( + GIT_REBASE_ABORT, bp::std_out > in_pipe, bp::on_exit([](int code, std::error_code ec) { + std::cout << "Child exited (code=" << code << "): " << ec.message() + << std::endl; + in_pipe.close(); + + if (code != 0) { + std::cerr << "Failed to abort rebase" << std::endl; + std::exit(1); + } + }), + io); + + schedule_read(); + io.run(); + } + }), + io); + + schedule_read(); + io.run(); + + std::cout << "Rebased, exit code: " << c.exit_code() << std::endl; + + + } + + infile.close(); + + return 0; +} diff --git a/vcpkg.json b/vcpkg.json index 2a2ed05f7..1a448da0a 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -6,6 +6,7 @@ "description": "OpenMind", "dependencies": [ "boost", + "boost-asio", "boost-chrono", "boost-compute", "boost-date-time",