From 5f5ea77ae5ca6b6033fb3e1e3e90a30dd2cfbc42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Lasersk=C3=B6ld?= Date: Wed, 22 May 2024 20:00:45 +0200 Subject: [PATCH] Implement git add and git blame --- src/plugin/git.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++---- todo.md | 3 ++- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/plugin/git.cpp b/src/plugin/git.cpp index 9cb988b..71797a8 100644 --- a/src/plugin/git.cpp +++ b/src/plugin/git.cpp @@ -11,11 +11,11 @@ #include #include #include -#include namespace { -FString universalGitFormat(std::istream &stream) { +FString universalGitFormat( + std::istream &stream, std::function extraLineFormat = {}) { auto ret = std::vector{}; for (std::string line; std::getline(stream, line);) { auto fstr = FString{line}; @@ -39,6 +39,9 @@ FString universalGitFormat(std::istream &stream) { if (str.starts_with("Author: ") || str.starts_with("Date: ")) { fstr.format(Palette::comment); } + if (extraLineFormat) { + extraLineFormat(fstr); + } ret.push_back(std::move(fstr)); } @@ -56,18 +59,68 @@ void gitPush(std::shared_ptr env) { console.run(env); } -void viewResultAsInteraction(std::shared_ptr env, - const std::string &command) { +void viewResultAsInteraction( + std::shared_ptr env, + const std::string &command, + std::function extraLineFormat = {}) { auto stream = POpenStream{command}; auto interaction = Interaction{ - .text = universalGitFormat(stream), + .text = universalGitFormat(stream, extraLineFormat), .title = command + "...", }; + if (stream.returnCode()) { + interaction.text += ("\n command failed: " + command); + } + interaction.begin(env->interactions(), {}); } +void gitBlame(std::shared_ptr env) { + auto file = env->editor().file(); + if (!file) { + std::cerr << "could not diff file for editor: editor does not have a " + "file associated\n"; + return; + } + + auto command = "git blame \"" + file->path().string() + "\""; + + auto formatF = [](FString &line) { + auto f = line.find(' '); + + if (f == FString::npos) { + return; + } + line.format(Palette::comment, 0, f); + + auto f2 = line.find(')', f); + if (f2 == FString::npos) { + return; + } + line.format(Palette::string, f + 1, f2 + 1); + }; + + viewResultAsInteraction(env, command, formatF); +} + +void gitAdd(std::shared_ptr env) { + auto file = env->editor().file(); + if (!file) { + std::cerr << "could not add file for editor: editor does not have a " + "file associated\n"; + return; + } + + std::cout << "adding current file\n"; + auto command = "git add \"" + file->path().string() + "\""; + std::cout << command << std::endl; + + auto stream = POpenStream{command}; + std::cout << stream.rdbuf(); +} + } // namespace void registerGitCommands(StandardCommands &standardCommands) { @@ -88,4 +141,6 @@ void registerGitCommands(StandardCommands &standardCommands) { standardCommands.namedCommands["git_log"] = wrapInteraction(git + " log"); standardCommands.namedCommands["git_commit"] = beginGitCommit; standardCommands.namedCommands["git_commit_amend"] = beginGitCommitAmmend; + standardCommands.namedCommands["git_blame"] = gitBlame; + standardCommands.namedCommands["git_add"] = gitAdd; } diff --git a/todo.md b/todo.md index b065208..56ee614 100644 --- a/todo.md +++ b/todo.md @@ -71,7 +71,8 @@ Todo: - [x] Push - [ ] Diff - [x] Status - - [ ] Blame + - [x] Blame + - [x] Add - [ ] Commit (interaction where you select which files and write message) - [x] GDB debugger integration - [x] Basic operations, run, set breakpoints step