Skip to content

Commit

Permalink
Remove dependency on procxx.
Browse files Browse the repository at this point in the history
Goal is to support Windows:
See #2
  • Loading branch information
ArthurSonzogni committed Oct 12, 2021
1 parent 933a4e7 commit 30d675d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 815 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_executable(git-tui
src/scroller.cpp
src/scroller.hpp
src/process.hpp
src/process.cpp
)

target_include_directories(git-tui
Expand Down
10 changes: 3 additions & 7 deletions src/diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,11 @@ int main(int argc, const char** argv) {
files.clear();
file_menu_entries.clear();

procxx::process git("git");
git.add_argument("diff");
git.add_argument("-U" + std::to_string(hunk_size));
std::string command = "git diff -U" + std::to_string(hunk_size);
for (int i = 0; i < argc; ++i)
git.add_argument(argv[i]);
git.exec();
command += std::string(" ") + argv[i];

std::string diff(std::istreambuf_iterator<char>(git.output()),
std::istreambuf_iterator<char>());
std::string diff = ExecuteProcess(command);
files = Parse(diff);
for (const auto& file : files)
file_menu_entries.push_back(file.right_file);
Expand Down
34 changes: 11 additions & 23 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <map> // for map, map<>::mapped_type
#include <memory> // for allocator, shared_ptr, __shared_ptr_access, unique_ptr, make_unique
#include <queue> // for queue
#include <sstream>
#include <string> // for wstring, basic_string, string, getline, operator+, operator<, to_string
#include <utility> // for move
#include <vector> // for vector
Expand All @@ -28,14 +29,7 @@ using namespace ftxui;
namespace gittui::log {

std::string ResolveHead() {
procxx::process git("git");
git.add_argument("rev-list");
git.add_argument("HEAD");
git.add_argument("-1");
git.exec();
std::string line;
std::getline(git.output(), line);
return line;
return ExecuteProcess("git rev-list HEAD -1");
}

struct Commit {
Expand All @@ -57,14 +51,12 @@ Commit* GetCommit(std::wstring hash) {
Commit* commit = g_commit[hash].get();
commit->hash = hash;

procxx::process git("git");
git.add_argument("cat-file");
git.add_argument("commit");
git.add_argument(to_string(hash));
git.exec();
std::string data = ExecuteProcess("git cat-file commit " + to_string(hash));
std::stringstream ss;
ss << data;

std::string line;
while (std::getline(git.output(), line)) {
while (std::getline(ss, line)) {
if (line.find("tree ", 0) == 0) {
commit->tree = to_wstring(line.substr(5));
continue;
Expand All @@ -87,7 +79,7 @@ Commit* GetCommit(std::wstring hash) {

if (line.empty()) {
int index = -1;
while (std::getline(git.output(), line)) {
while (std::getline(ss, line)) {
++index;
if (index == 0)
commit->title = to_wstring(line);
Expand Down Expand Up @@ -137,15 +129,11 @@ int main(int argc, const char** argv) {
files.clear();
menu_files_entries.clear();

procxx::process git("git");
git.add_argument("diff");
git.add_argument("-U" + std::to_string(hunk_size));
git.add_argument(to_string(commit->hash) + "~..." +
to_string(commit->hash));
git.exec();
std::string diff =
ExecuteProcess("git diff -U" + std::to_string(hunk_size) + " " + //
to_string(commit->hash) + "~..." + //
to_string(commit->hash));

std::string diff(std::istreambuf_iterator<char>(git.output()),
std::istreambuf_iterator<char>());
files = diff::Parse(diff);
menu_files_entries.push_back(L"description");
for (const auto& file : files)
Expand Down
20 changes: 20 additions & 0 deletions src/process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <array>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>

std::string ExecuteProcess(std::string cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"),
pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
Loading

0 comments on commit 30d675d

Please sign in to comment.