-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rebase.cmd processing using boost asio
- Loading branch information
Showing
5 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters