diff options
author | Itamar <itamar8910@gmail.com> | 2020-09-12 22:43:35 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-15 21:43:29 +0200 |
commit | 435c6c6f969ea765c437508ccdd67580835b6e49 (patch) | |
tree | 9acce27353ccbed0959afbf31ca446aeaadd8e32 | |
parent | 7b66469ab33ccee6f3bb6a9aee2d08012dc21ab1 (diff) | |
download | serenity-435c6c6f969ea765c437508ccdd67580835b6e49.zip |
HackStudio: Add basic Git integration
This adds a "Git" tab to Hackstudio.
Currently has support for staging and unstaging files.
-rw-r--r-- | Base/res/icons/16x16/minus.png | bin | 0 -> 202 bytes | |||
-rw-r--r-- | Base/res/icons/16x16/plus.png | bin | 0 -> 238 bytes | |||
-rw-r--r-- | DevTools/HackStudio/CMakeLists.txt | 4 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitFilesModel.cpp | 56 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitFilesModel.h | 57 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitFilesView.cpp | 82 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitFilesView.h | 56 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitRepo.cpp | 131 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitRepo.h | 77 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitWidget.cpp | 103 | ||||
-rw-r--r-- | DevTools/HackStudio/Git/GitWidget.h | 55 | ||||
-rw-r--r-- | DevTools/HackStudio/Project.h | 2 | ||||
-rw-r--r-- | DevTools/HackStudio/main.cpp | 25 | ||||
-rw-r--r-- | Libraries/LibGUI/ListView.cpp | 82 | ||||
-rw-r--r-- | Libraries/LibGUI/ListView.h | 4 |
15 files changed, 692 insertions, 42 deletions
diff --git a/Base/res/icons/16x16/minus.png b/Base/res/icons/16x16/minus.png Binary files differnew file mode 100644 index 0000000000..4288ca17d2 --- /dev/null +++ b/Base/res/icons/16x16/minus.png diff --git a/Base/res/icons/16x16/plus.png b/Base/res/icons/16x16/plus.png Binary files differnew file mode 100644 index 0000000000..c3583756fc --- /dev/null +++ b/Base/res/icons/16x16/plus.png diff --git a/DevTools/HackStudio/CMakeLists.txt b/DevTools/HackStudio/CMakeLists.txt index 8105917661..2f392cabff 100644 --- a/DevTools/HackStudio/CMakeLists.txt +++ b/DevTools/HackStudio/CMakeLists.txt @@ -1,5 +1,9 @@ set(SOURCES CursorTool.cpp + Git/GitWidget.cpp + Git/GitFilesModel.cpp + Git/GitRepo.cpp + Git/GitFilesView.cpp Debugger/BacktraceModel.cpp Debugger/Debugger.cpp Debugger/DebugInfoWidget.cpp diff --git a/DevTools/HackStudio/Git/GitFilesModel.cpp b/DevTools/HackStudio/Git/GitFilesModel.cpp new file mode 100644 index 0000000000..720198586a --- /dev/null +++ b/DevTools/HackStudio/Git/GitFilesModel.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "GitFilesModel.h" + +namespace HackStudio { + +NonnullRefPtr<GitFilesModel> GitFilesModel::create(Vector<LexicalPath>&& files) +{ + return adopt(*new GitFilesModel(move(files))); +} + +GitFilesModel::GitFilesModel(Vector<LexicalPath>&& files) + : m_files(move(files)) +{ +} + +GUI::Variant GitFilesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const +{ + if (role == GUI::ModelRole::Display) { + return m_files.at(index.row()).string(); + } + return {}; +} + +GUI::ModelIndex GitFilesModel::index(int row, int column, const GUI::ModelIndex&) const +{ + if (row < 0 || row >= static_cast<int>(m_files.size())) + return {}; + return create_index(row, column, &m_files.at(row)); +} + +}; diff --git a/DevTools/HackStudio/Git/GitFilesModel.h b/DevTools/HackStudio/Git/GitFilesModel.h new file mode 100644 index 0000000000..988be1c472 --- /dev/null +++ b/DevTools/HackStudio/Git/GitFilesModel.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GitRepo.h" +#include <AK/LexicalPath.h> +#include <AK/NonnullRefPtr.h> +#include <LibGUI/Model.h> + +namespace HackStudio { + +class GitFilesModel final : public GUI::Model { +public: + static NonnullRefPtr<GitFilesModel> create(Vector<LexicalPath>&& files); + + virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_files.size(); } + virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 1; } + + virtual String column_name(int) const override + { + return ""; + } + + virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override; + + virtual void update() override {} + virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex&) const override; + +private: + explicit GitFilesModel(Vector<LexicalPath>&& files); + Vector<LexicalPath> m_files; +}; +} diff --git a/DevTools/HackStudio/Git/GitFilesView.cpp b/DevTools/HackStudio/Git/GitFilesView.cpp new file mode 100644 index 0000000000..76aa3abda8 --- /dev/null +++ b/DevTools/HackStudio/Git/GitFilesView.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "GitFilesView.h" +#include <LibGUI/Model.h> +#include <LibGUI/Painter.h> +#include <LibGUI/ScrollBar.h> +#include <LibGfx/Palette.h> + +namespace HackStudio { +GitFilesView::~GitFilesView() +{ +} + +void GitFilesView::paint_list_item(GUI::Painter& painter, int row_index, int painted_item_index) +{ + ListView::paint_list_item(painter, row_index, painted_item_index); + + painter.blit(action_icon_rect((size_t)painted_item_index).top_left(), *m_action_icon, m_action_icon->rect()); +} + +Gfx::IntRect GitFilesView::action_icon_rect(size_t painted_item_index) +{ + int y = painted_item_index * item_height(); + return { content_width() - 20, y, m_action_icon->rect().width(), m_action_icon->rect().height() }; +} + +GitFilesView::GitFilesView(GitFileActionCallback callback, NonnullRefPtr<Gfx::Bitmap> action_icon) + : m_action_callback(move(callback)) + , m_action_icon(action_icon) +{ + set_alternating_row_colors(false); +} + +void GitFilesView::mousedown_event(GUI::MouseEvent& event) +{ + if (event.button() != GUI::MouseButton::Left) { + ListView::mousedown_event(event); + return; + } + + if (event.x() < action_icon_rect(0).x() || event.x() > action_icon_rect(0).top_right().x()) { + ListView::mousedown_event(event); + return; + } + + size_t item_index = (event.y() + vertical_scrollbar().value()) / item_height(); + if (model()->row_count() == 0 || item_index > (size_t)model()->row_count()) { + ListView::mousedown_event(event); + return; + } + + auto data = model()->index(item_index, model_column()).data(); + + ASSERT(data.is_string()); + m_action_callback(LexicalPath(data.to_string())); +} + +}; diff --git a/DevTools/HackStudio/Git/GitFilesView.h b/DevTools/HackStudio/Git/GitFilesView.h new file mode 100644 index 0000000000..8eb636304c --- /dev/null +++ b/DevTools/HackStudio/Git/GitFilesView.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/LexicalPath.h> +#include <LibGUI/ListView.h> +#include <LibGfx/Bitmap.h> + +namespace HackStudio { + +// A "GitFileAction" is either the staging or the unstaging of a file. +typedef Function<void(const LexicalPath& file)> GitFileActionCallback; + +class GitFilesView : public GUI::ListView { + C_OBJECT(GitFilesView) +public: + virtual ~GitFilesView() override; + +protected: + GitFilesView(GitFileActionCallback, NonnullRefPtr<Gfx::Bitmap> action_icon); + +private: + virtual void paint_list_item(GUI::Painter& painter, int row_index, int painted_item_index); + + virtual void mousedown_event(GUI::MouseEvent&) override; + virtual Gfx::IntRect action_icon_rect(size_t painted_item_index); + + GitFileActionCallback m_action_callback; + NonnullRefPtr<Gfx::Bitmap> m_action_icon; +}; + +} diff --git a/DevTools/HackStudio/Git/GitRepo.cpp b/DevTools/HackStudio/Git/GitRepo.cpp new file mode 100644 index 0000000000..1dbd0570ff --- /dev/null +++ b/DevTools/HackStudio/Git/GitRepo.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "GitRepo.h" +#include <LibCore/Command.h> +#include <stdio.h> +#include <stdlib.h> + +namespace HackStudio { + +GitRepo::CreateResult GitRepo::try_to_create(const LexicalPath& repository_root) +{ + if (!git_is_installed()) { + return { CreateResult::Type::GitProgramNotFound, nullptr }; + } + if (!git_repo_exists(repository_root)) { + return { CreateResult::Type::NoGitRepo, nullptr }; + } + + return { CreateResult::Type::Success, adopt(*new GitRepo(repository_root)) }; +} + +RefPtr<GitRepo> GitRepo::initialize_repository(const LexicalPath& repository_root) +{ + auto res = command_wrapper("init", repository_root); + if (res.is_null()) + return {}; + + ASSERT(git_repo_exists(repository_root)); + return adopt(*new GitRepo(repository_root)); +} + +Vector<LexicalPath> GitRepo::unstaged_files() const +{ + auto modified = modified_files(); + auto untracked = untracked_files(); + modified.append(move(untracked)); + return modified; +} + +Vector<LexicalPath> GitRepo::staged_files() const +{ + auto raw_result = command("diff --cached --name-only"); + if (raw_result.is_null()) + return {}; + return parse_files_list(raw_result); +} + +Vector<LexicalPath> GitRepo::modified_files() const +{ + auto raw_result = command("ls-files --modified --exclude-standard"); + if (raw_result.is_null()) + return {}; + return parse_files_list(raw_result); +} + +Vector<LexicalPath> GitRepo::untracked_files() const +{ + auto raw_result = command("ls-files --others --exclude-standard"); + if (raw_result.is_null()) + return {}; + return parse_files_list(raw_result); +} + +Vector<LexicalPath> GitRepo::parse_files_list(const String& raw_result) +{ + auto lines = raw_result.split('\n'); + Vector<LexicalPath> files; + for (const auto& line : lines) { + files.empend(line); + } + return files; +} + +String GitRepo::command(const String& git_command) const +{ + return command_wrapper(git_command, m_repository_root); +} + +String GitRepo::command_wrapper(const String& git_command, const LexicalPath& chdir) +{ + return Core::command(String::format("git %s", git_command.characters()), chdir); +} + +bool GitRepo::git_is_installed() +{ + return !command_wrapper("--help", LexicalPath("/")).is_null(); +} + +bool GitRepo::git_repo_exists(const LexicalPath& repo_root) +{ + 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(); +} + +bool GitRepo::unstage(const LexicalPath& file) +{ + auto cmd = String::format("reset HEAD -- %s", file.string().characters()); + return !command(cmd).is_null(); +} + +} diff --git a/DevTools/HackStudio/Git/GitRepo.h b/DevTools/HackStudio/Git/GitRepo.h new file mode 100644 index 0000000000..e80edcf39b --- /dev/null +++ b/DevTools/HackStudio/Git/GitRepo.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/LexicalPath.h> +#include <AK/Optional.h> +#include <AK/RefCounted.h> +#include <AK/RefPtr.h> +#include <AK/String.h> +#include <AK/Vector.h> + +namespace HackStudio { + +class GitRepo final : public RefCounted<GitRepo> { +public: + struct CreateResult { + enum class Type { + Success, + NoGitRepo, + GitProgramNotFound, + }; + Type type; + RefPtr<GitRepo> repo; + }; + + static CreateResult try_to_create(const LexicalPath& repository_root); + static RefPtr<GitRepo> initialize_repository(const LexicalPath& repository_root); + + Vector<LexicalPath> unstaged_files() const; + Vector<LexicalPath> staged_files() const; + bool stage(const LexicalPath& file); + bool unstage(const LexicalPath& file); + +private: + static String command_wrapper(const String& git_command, const LexicalPath& chdir); + static bool git_is_installed(); + static bool git_repo_exists(const LexicalPath& repo_root); + static Vector<LexicalPath> parse_files_list(const String&); + + explicit GitRepo(const LexicalPath& repository_root) + : m_repository_root(repository_root) + { + } + + Vector<LexicalPath> modified_files() const; + Vector<LexicalPath> untracked_files() const; + + String command(const String& git_command) const; + + LexicalPath m_repository_root; +}; + +} diff --git a/DevTools/HackStudio/Git/GitWidget.cpp b/DevTools/HackStudio/Git/GitWidget.cpp new file mode 100644 index 0000000000..b7dc132b43 --- /dev/null +++ b/DevTools/HackStudio/Git/GitWidget.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "GitWidget.h" +#include "GitFilesModel.h" +#include <AK/LogStream.h> +#include <LibGUI/Application.h> +#include <LibGUI/BoxLayout.h> +#include <LibGUI/Label.h> +#include <LibGUI/MessageBox.h> +#include <LibGUI/Model.h> +#include <LibGUI/Painter.h> +#include <LibGfx/Bitmap.h> + +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) +{ + set_layout<GUI::HorizontalBoxLayout>(); + + auto& unstaged = add<GUI::Widget>(); + unstaged.set_layout<GUI::VerticalBoxLayout>(); + auto& unstaged_label = unstaged.add<GUI::Label>(); + unstaged_label.set_text("Unstaged"); + unstaged_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + unstaged_label.set_preferred_size(0, 20); + m_unstaged_files = unstaged.add<GitFilesView>( + [this](const auto& file) { stage_file(file); }, + Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png").release_nonnull()); + + auto& staged = add<GUI::Widget>(); + staged.set_layout<GUI::VerticalBoxLayout>(); + auto& staged_label = staged.add<GUI::Label>(); + staged_label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + staged_label.set_preferred_size(0, 20); + staged_label.set_text("Staged"); + m_staged_files = staged.add<GitFilesView>( + [this](const auto& file) { unstage_file(file); }, + Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png").release_nonnull()); +} + +void GitWidget::refresh() +{ + auto result = GitRepo::try_to_create(m_repo_root); + if (result.type == GitRepo::CreateResult::Type::Success) { + m_git_repo = result.repo; + } else if (result.type == GitRepo::CreateResult::Type::GitProgramNotFound) { + GUI::MessageBox::show(window(), "Please install the Git port", "Error", GUI::MessageBox::Type::Error); + return; + } else if (result.type == GitRepo::CreateResult::Type::NoGitRepo) { + auto decision = GUI::MessageBox::show(window(), "Create git repository?", "Git", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); + if (decision != GUI::Dialog::ExecResult::ExecYes) + return; + m_git_repo = GitRepo::initialize_repository(m_repo_root); + } + + ASSERT(!m_git_repo.is_null()); + + m_unstaged_files->set_model(GitFilesModel::create(m_git_repo->unstaged_files())); + m_staged_files->set_model(GitFilesModel::create(m_git_repo->staged_files())); +} + +}; diff --git a/DevTools/HackStudio/Git/GitWidget.h b/DevTools/HackStudio/Git/GitWidget.h new file mode 100644 index 0000000000..1bff585c33 --- /dev/null +++ b/DevTools/HackStudio/Git/GitWidget.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GitFilesView.h" +#include "GitRepo.h" +#include <LibGUI/Forward.h> +#include <LibGUI/Widget.h> + +namespace HackStudio { + +class GitWidget final : public GUI::Widget { + C_OBJECT(GitWidget) +public: + virtual ~GitWidget() override {} + + void refresh(); + +private: + explicit GitWidget(const LexicalPath& repo_root); + + void stage_file(const LexicalPath&); + void unstage_file(const LexicalPath&); + + LexicalPath m_repo_root; + RefPtr<GitFilesView> m_unstaged_files; + RefPtr<GitFilesView> m_staged_files; + RefPtr<GitRepo> m_git_repo; +}; + +} diff --git a/DevTools/HackStudio/Project.h b/DevTools/HackStudio/Project.h index a7c90fd2b5..d1a3c31ac8 100644 --- a/DevTools/HackStudio/Project.h +++ b/DevTools/HackStudio/Project.h @@ -27,6 +27,7 @@ #pragma once #include "ProjectFile.h" +#include <AK/LexicalPath.h> #include <AK/Noncopyable.h> #include <AK/NonnullRefPtrVector.h> #include <AK/OwnPtr.h> @@ -61,6 +62,7 @@ public: String default_file() const; String name() const { return m_name; } String path() const { return m_path; } + String root_directory() const { return LexicalPath(m_path).dirname(); } template<typename Callback> void for_each_text_file(Callback callback) const diff --git a/DevTools/HackStudio/main.cpp b/DevTools/HackStudio/main.cpp index c07504e197..78d4764542 100644 --- a/DevTools/HackStudio/main.cpp +++ b/DevTools/HackStudio/main.cpp @@ -33,6 +33,7 @@ #include "FindInFilesWidget.h" #include "FormEditorWidget.h" #include "FormWidget.h" +#include "Git/GitWidget.h" #include "HackStudio.h" #include "Locator.h" #include "Project.h" @@ -192,6 +193,7 @@ static int main_impl(int argc, char** argv) } Function<void()> update_actions; + Function<void()> on_action_tab_change; g_window = GUI::Window::construct(); g_window->resize(840, 600); @@ -501,7 +503,14 @@ static int main_impl(int argc, char** argv) s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); s_action_tab_widget->set_preferred_size(0, 24); - s_action_tab_widget->on_change = [&](auto&) { update_actions(); }; + s_action_tab_widget->on_change = [&](auto&) { + on_action_tab_change(); + + static bool first_time = true; + if (!first_time) + s_action_tab_widget->set_preferred_size(0, 200); + first_time = false; + }; auto reveal_action_tab = [&](auto& widget) { if (s_action_tab_widget->preferred_size().height() < 200) @@ -551,6 +560,8 @@ static int main_impl(int argc, char** argv) auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false); auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug"); auto& disassembly_widget = s_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly"); + auto& git_widget = s_action_tab_widget->add_tab<GitWidget>("Git", LexicalPath(g_project->root_directory())); + (void)git_widget; auto& locator = widget.add<Locator>(); @@ -700,7 +711,7 @@ static int main_impl(int argc, char** argv) auto widget = s_action_tab_widget->active_widget(); if (!widget) return false; - if (strcmp(widget->class_name(), "TerminalWrapper") != 0) + if (StringView { "TerminalWrapper" } != widget->class_name()) return false; if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned()) return false; @@ -711,6 +722,16 @@ static int main_impl(int argc, char** argv) remove_current_terminal_action->set_enabled(is_remove_terminal_enabled()); }; + on_action_tab_change = [&]() { + update_actions(); + auto git_widget = s_action_tab_widget->active_widget(); + if (!git_widget) + return; + if (StringView { "GitWidget" } != git_widget->class_name()) + return; + reinterpret_cast<GitWidget*>(git_widget)->refresh(); + }; + g_open_file = open_file; if (!argument_absolute_path.is_empty() && !argument_absolute_path.ends_with(".hsp")) diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp index f6a15031a2..255b63d8fd 100644 --- a/Libraries/LibGUI/ListView.cpp +++ b/Libraries/LibGUI/ListView.cpp @@ -110,6 +110,48 @@ Gfx::IntPoint ListView::adjusted_position(const Gfx::IntPoint& position) const return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); } +void ListView::paint_list_item(Painter& painter, int row_index, int painted_item_index) +{ + bool is_selected_row = selection().contains_row(row_index); + + int y = painted_item_index * item_height(); + + Color background_color; + if (is_selected_row) { + background_color = is_focused() ? palette().selection() : palette().inactive_selection(); + } else { + Color row_fill_color = palette().color(background_role()); + if (alternating_row_colors() && (painted_item_index % 2)) { + background_color = row_fill_color.darkened(0.8f); + } else { + background_color = row_fill_color; + } + } + + Gfx::IntRect row_rect(0, y, content_width(), item_height()); + painter.fill_rect(row_rect, background_color); + auto index = model()->index(row_index, m_model_column); + auto data = index.data(); + auto font = font_for_index(index); + if (data.is_bitmap()) { + painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); + } else if (data.is_icon()) { + if (auto bitmap = data.as_icon().bitmap_for_size(16)) + painter.blit(row_rect.location(), *bitmap, bitmap->rect()); + } else { + Color text_color; + if (is_selected_row) + text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text(); + else + text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role())); + auto text_rect = row_rect; + text_rect.move_by(horizontal_padding(), 0); + text_rect.set_width(text_rect.width() - horizontal_padding() * 2); + auto text_alignment = index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft); + painter.draw_text(text_rect, data.to_string(), font, text_alignment, text_color); + } +} + void ListView::paint_event(PaintEvent& event) { Frame::paint_event(event); @@ -127,45 +169,7 @@ void ListView::paint_event(PaintEvent& event) int painted_item_index = 0; for (int row_index = 0; row_index < model()->row_count(); ++row_index) { - bool is_selected_row = selection().contains_row(row_index); - - int y = painted_item_index * item_height(); - - Color background_color; - if (is_selected_row) { - background_color = is_focused() ? palette().selection() : palette().inactive_selection(); - } else { - Color row_fill_color = palette().color(background_role()); - if (alternating_row_colors() && (painted_item_index % 2)) { - background_color = row_fill_color.darkened(0.8f); - } else { - background_color = row_fill_color; - } - } - - Gfx::IntRect row_rect(0, y, content_width(), item_height()); - painter.fill_rect(row_rect, background_color); - auto index = model()->index(row_index, m_model_column); - auto data = index.data(); - auto font = font_for_index(index); - if (data.is_bitmap()) { - painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); - } else if (data.is_icon()) { - if (auto bitmap = data.as_icon().bitmap_for_size(16)) - painter.blit(row_rect.location(), *bitmap, bitmap->rect()); - } else { - Color text_color; - if (is_selected_row) - text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text(); - else - text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role())); - auto text_rect = row_rect; - text_rect.move_by(horizontal_padding(), 0); - text_rect.set_width(text_rect.width() - horizontal_padding() * 2); - auto text_alignment = index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft); - painter.draw_text(text_rect, data.to_string(), font, text_alignment, text_color); - } - + paint_list_item(painter, row_index, painted_item_index); ++painted_item_index; }; diff --git a/Libraries/LibGUI/ListView.h b/Libraries/LibGUI/ListView.h index 4f81364a51..f1accd47ae 100644 --- a/Libraries/LibGUI/ListView.h +++ b/Libraries/LibGUI/ListView.h @@ -62,9 +62,11 @@ public: virtual void move_cursor(CursorMovement, SelectionUpdate) override; void move_cursor_relative(int steps, SelectionUpdate); -private: +protected: ListView(); + virtual void paint_list_item(Painter&, int row_index, int painted_item_index); +private: virtual void did_update_model(unsigned flags) override; virtual void paint_event(PaintEvent&) override; virtual void keydown_event(KeyEvent&) override; |