Skip to content

Commit

Permalink
rebase.cmd processing using boost asio
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Jan 28, 2025
1 parent 3f48e0c commit f65a1b6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ endif()
set(_BOOST_USED_COMPONENTS
${BOOST_USED_COMPONENTS}
${BOOST_ADDITIONAL_COMPONENTS}
asio
chrono
date_time
filesystem
Expand Down
7 changes: 2 additions & 5 deletions cmake/gitect.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,15 @@ 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

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 $<TARGET_FILE:append-each-line> rebase.cmd "${CMD_LINE_SUFFIX}"
COMMAND cmd /c rebase.cmd
COMMAND $<TARGET_FILE:process-cmd-rebase-or-abort>

COMMENT "Rebasing all branches onto origin/main using cmd with improved branch handling."
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand Down
1 change: 1 addition & 0 deletions cmake/process-cmd-rebase-or-abort/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exe()
94 changes: 94 additions & 0 deletions cmake/process-cmd-rebase-or-abort/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <fstream>
#include <iostream>
#include <string>
#include <boost/process.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <system_error>
#include <utility>


// 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] << " <input_file> <suffix>" << std::endl;
std::cout << "Defaults used: <input_file>=" INPUT_FILE_DEFAULT " <suffix>=" 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;
}
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "OpenMind",
"dependencies": [
"boost",
"boost-asio",
"boost-chrono",
"boost-compute",
"boost-date-time",
Expand Down

0 comments on commit f65a1b6

Please sign in to comment.