From f300b816486c85190b9eaadf69e418ccb572b35f Mon Sep 17 00:00:00 2001 From: Itamar Date: Mon, 14 Sep 2020 20:48:00 +0300 Subject: HackStudio: Add "commit" and "refresh" actions to Git widget --- DevTools/HackStudio/Git/GitRepo.cpp | 33 +++++++------- DevTools/HackStudio/Git/GitRepo.h | 5 ++- DevTools/HackStudio/Git/GitWidget.cpp | 81 +++++++++++++++++++++++++---------- DevTools/HackStudio/Git/GitWidget.h | 1 + 4 files changed, 79 insertions(+), 41 deletions(-) (limited to 'DevTools') diff --git a/DevTools/HackStudio/Git/GitRepo.cpp b/DevTools/HackStudio/Git/GitRepo.cpp index 1dbd0570ff..3461dacbf2 100644 --- a/DevTools/HackStudio/Git/GitRepo.cpp +++ b/DevTools/HackStudio/Git/GitRepo.cpp @@ -45,7 +45,7 @@ GitRepo::CreateResult GitRepo::try_to_create(const LexicalPath& repository_root) RefPtr GitRepo::initialize_repository(const LexicalPath& repository_root) { - auto res = command_wrapper("init", repository_root); + auto res = command_wrapper({ "init" }, repository_root); if (res.is_null()) return {}; @@ -60,10 +60,10 @@ Vector GitRepo::unstaged_files() const modified.append(move(untracked)); return modified; } - +// Vector GitRepo::staged_files() const { - auto raw_result = command("diff --cached --name-only"); + auto raw_result = command({ "diff", "--cached", "--name-only" }); if (raw_result.is_null()) return {}; return parse_files_list(raw_result); @@ -71,7 +71,7 @@ Vector GitRepo::staged_files() const Vector GitRepo::modified_files() const { - auto raw_result = command("ls-files --modified --exclude-standard"); + auto raw_result = command({ "ls-files", "--modified", "--exclude-standard" }); if (raw_result.is_null()) return {}; return parse_files_list(raw_result); @@ -79,7 +79,7 @@ Vector GitRepo::modified_files() const Vector GitRepo::untracked_files() const { - auto raw_result = command("ls-files --others --exclude-standard"); + auto raw_result = command({ "ls-files", "--others", "--exclude-standard" }); if (raw_result.is_null()) return {}; return parse_files_list(raw_result); @@ -95,37 +95,38 @@ Vector GitRepo::parse_files_list(const String& raw_result) return files; } -String GitRepo::command(const String& git_command) const +String GitRepo::command(const Vector& command_parts) const { - return command_wrapper(git_command, m_repository_root); + return command_wrapper(command_parts, m_repository_root); } -String GitRepo::command_wrapper(const String& git_command, const LexicalPath& chdir) +String GitRepo::command_wrapper(const Vector& command_parts, const LexicalPath& chdir) { - return Core::command(String::format("git %s", git_command.characters()), chdir); + return Core::command("git", command_parts, chdir); } bool GitRepo::git_is_installed() { - return !command_wrapper("--help", LexicalPath("/")).is_null(); + return !command_wrapper({ "--help" }, LexicalPath("/")).is_null(); } bool GitRepo::git_repo_exists(const LexicalPath& repo_root) { - return !command_wrapper("status", repo_root).is_null(); + return !command_wrapper({ "status" }, repo_root).is_null(); } bool GitRepo::stage(const LexicalPath& file) { - auto cmd = String::format("add %s", file.string().characters()); - auto res = command(cmd); - return !res.is_null(); + return !command({ "add", file.string() }).is_null(); } bool GitRepo::unstage(const LexicalPath& file) { - auto cmd = String::format("reset HEAD -- %s", file.string().characters()); - return !command(cmd).is_null(); + return !command({ "reset", "HEAD", "--", file.string() }).is_null(); } +bool GitRepo::commit(const String& message) +{ + return !command({ "commit", "-m", message }).is_null(); +} } diff --git a/DevTools/HackStudio/Git/GitRepo.h b/DevTools/HackStudio/Git/GitRepo.h index e80edcf39b..5f17e46dcb 100644 --- a/DevTools/HackStudio/Git/GitRepo.h +++ b/DevTools/HackStudio/Git/GitRepo.h @@ -54,9 +54,10 @@ public: Vector staged_files() const; bool stage(const LexicalPath& file); bool unstage(const LexicalPath& file); + bool commit(const String& message); private: - static String command_wrapper(const String& git_command, const LexicalPath& chdir); + static String command_wrapper(const Vector& command_parts, const LexicalPath& chdir); static bool git_is_installed(); static bool git_repo_exists(const LexicalPath& repo_root); static Vector parse_files_list(const String&); @@ -69,7 +70,7 @@ private: Vector modified_files() const; Vector untracked_files() const; - String command(const String& git_command) const; + String command(const Vector& command_parts) const; LexicalPath m_repository_root; }; diff --git a/DevTools/HackStudio/Git/GitWidget.cpp b/DevTools/HackStudio/Git/GitWidget.cpp index b7dc132b43..e8dfb3b4a4 100644 --- a/DevTools/HackStudio/Git/GitWidget.cpp +++ b/DevTools/HackStudio/Git/GitWidget.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -37,22 +39,6 @@ namespace HackStudio { -void GitWidget::stage_file(const LexicalPath& file) -{ - dbg() << "staging: " << file.string(); - bool rc = m_git_repo->stage(file); - ASSERT(rc); - refresh(); -} - -void GitWidget::unstage_file(const LexicalPath& file) -{ - dbg() << "unstaging: " << file.string(); - bool rc = m_git_repo->unstage(file); - ASSERT(rc); - refresh(); -} - GitWidget::GitWidget(const LexicalPath& repo_root) : m_repo_root(repo_root) { @@ -60,20 +46,43 @@ GitWidget::GitWidget(const LexicalPath& repo_root) auto& unstaged = add(); unstaged.set_layout(); - auto& unstaged_label = unstaged.add(); + auto& unstaged_header = unstaged.add(); + unstaged_header.set_layout(); + + auto& refresh_button = unstaged_header.add(); + refresh_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png")); + refresh_button.set_preferred_size({ 16, 16 }); + refresh_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + refresh_button.set_tooltip("refresh"); + refresh_button.on_click = [this](int) { refresh(); }; + + auto& unstaged_label = unstaged_header.add(); unstaged_label.set_text("Unstaged"); - unstaged_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); - unstaged_label.set_preferred_size(0, 20); + + unstaged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + unstaged_header.set_preferred_size(0, 20); m_unstaged_files = unstaged.add( [this](const auto& file) { stage_file(file); }, Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png").release_nonnull()); auto& staged = add(); staged.set_layout(); - auto& staged_label = staged.add(); - staged_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); - staged_label.set_preferred_size(0, 20); + + auto& staged_header = staged.add(); + staged_header.set_layout(); + + auto& commit_button = staged_header.add(); + commit_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/commit.png")); + commit_button.set_preferred_size({ 16, 16 }); + commit_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + commit_button.set_tooltip("commit"); + commit_button.on_click = [this](int) { commit(); }; + + auto& staged_label = staged_header.add(); staged_label.set_text("Staged"); + + staged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + staged_header.set_preferred_size(0, 20); m_staged_files = staged.add( [this](const auto& file) { unstage_file(file); }, Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png").release_nonnull()); @@ -100,4 +109,30 @@ void GitWidget::refresh() m_staged_files->set_model(GitFilesModel::create(m_git_repo->staged_files())); } -}; +void GitWidget::stage_file(const LexicalPath& file) +{ + dbg() << "staging: " << file.string(); + bool rc = m_git_repo->stage(file); + ASSERT(rc); + refresh(); +} + +void GitWidget::unstage_file(const LexicalPath& file) +{ + dbg() << "unstaging: " << file.string(); + bool rc = m_git_repo->unstage(file); + ASSERT(rc); + refresh(); +} + +void GitWidget::commit() +{ + String message; + auto res = GUI::InputBox::show(message, window(), "Commit message:", "Commit"); + if (res != GUI::InputBox::ExecOK || message.is_empty()) + return; + dbg() << "commit message: " << message; + m_git_repo->commit(message); + refresh(); +} +} diff --git a/DevTools/HackStudio/Git/GitWidget.h b/DevTools/HackStudio/Git/GitWidget.h index 1bff585c33..417af9ebbb 100644 --- a/DevTools/HackStudio/Git/GitWidget.h +++ b/DevTools/HackStudio/Git/GitWidget.h @@ -45,6 +45,7 @@ private: void stage_file(const LexicalPath&); void unstage_file(const LexicalPath&); + void commit(); LexicalPath m_repo_root; RefPtr m_unstaged_files; -- cgit v1.2.3