1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:07:35 +00:00

DevTools: Move to Userland/DevTools/

This commit is contained in:
Andreas Kling 2021-01-12 12:18:55 +01:00
parent 13d7c09125
commit 4055b03291
125 changed files with 2 additions and 2 deletions

View file

@ -0,0 +1,253 @@
/*
* 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 "DiffViewer.h"
#include <LibDiff/Hunks.h>
#include <LibGUI/AbstractView.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGfx/Color.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
// #define DEBUG_DIFF
namespace HackStudio {
DiffViewer::~DiffViewer()
{
}
void DiffViewer::paint_event(GUI::PaintEvent& event)
{
GUI::Painter painter(*this);
painter.add_clip_rect(widget_inner_rect());
painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), palette().color(background_role()));
painter.translate(frame_thickness(), frame_thickness());
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
// Why we need to translate here again? We've already translated the painter.
// Anyways, it paints correctly so I'll leave it like this.
painter.fill_rect_with_dither_pattern(
separator_rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()),
Gfx::Color::LightGray,
Gfx::Color::White);
size_t y_offset = 10;
size_t current_original_line_index = 0;
for (const auto& hunk : m_hunks) {
for (size_t i = current_original_line_index; i < hunk.original_start_line; ++i) {
draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
y_offset += line_height();
}
current_original_line_index = hunk.original_start_line + hunk.removed_lines.size();
size_t left_y_offset = y_offset;
for (const auto& removed_line : hunk.removed_lines) {
draw_line(painter, removed_line, left_y_offset, LinePosition::Left, LineType::Diff);
left_y_offset += line_height();
}
for (int i = 0; i < (int)hunk.added_lines.size() - (int)hunk.removed_lines.size(); ++i) {
draw_line(painter, "", left_y_offset, LinePosition::Left, LineType::Missing);
left_y_offset += line_height();
}
size_t right_y_offset = y_offset;
for (const auto& added_line : hunk.added_lines) {
draw_line(painter, added_line, right_y_offset, LinePosition::Right, LineType::Diff);
right_y_offset += line_height();
}
for (int i = 0; i < (int)hunk.removed_lines.size() - (int)hunk.added_lines.size(); ++i) {
draw_line(painter, "", right_y_offset, LinePosition::Right, LineType::Missing);
right_y_offset += line_height();
}
ASSERT(left_y_offset == right_y_offset);
y_offset = left_y_offset;
}
for (size_t i = current_original_line_index; i < m_original_lines.size(); ++i) {
draw_line(painter, m_original_lines[i], y_offset, LinePosition::Both, LineType::Normal);
y_offset += line_height();
}
}
void DiffViewer::draw_line(GUI::Painter& painter, const String& line, size_t y_offset, LinePosition line_position, LineType line_type)
{
size_t line_width = font().width(line);
constexpr size_t padding = 10;
size_t left_side_x_offset = padding;
size_t right_side_x_offset = separator_rect().x() + padding;
// FIXME: Long lines will overflow out of their side of the diff view
Gfx::IntRect left_line_rect { (int)left_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
Gfx::IntRect right_line_rect { (int)right_side_x_offset, (int)y_offset, (int)line_width, (int)line_height() };
auto color = palette().color(foreground_role());
if (line_position == LinePosition::Left || line_position == LinePosition::Both) {
painter.draw_text(left_line_rect, line, Gfx::TextAlignment::TopLeft, color);
if (line_type != LineType::Normal) {
Gfx::IntRect outline = { (int)left_side_x_offset, ((int)y_offset) - 2, separator_rect().x() - (int)(padding * 2), (int)line_height() };
if (line_type == LineType::Diff) {
painter.fill_rect(
outline,
red_background());
}
if (line_type == LineType::Missing) {
painter.fill_rect(
outline,
gray_background());
}
}
}
if (line_position == LinePosition::Right || line_position == LinePosition::Both) {
painter.draw_text(right_line_rect, line, Gfx::TextAlignment::TopLeft, color);
if (line_type != LineType::Normal) {
Gfx::IntRect outline = { (int)right_side_x_offset, ((int)y_offset) - 2, frame_inner_rect().width() - separator_rect().x() - (int)(padding * 2) - 10, (int)line_height() };
if (line_type == LineType::Diff) {
painter.fill_rect(
outline,
green_background());
}
if (line_type == LineType::Missing) {
painter.fill_rect(
outline,
gray_background());
}
}
}
}
size_t DiffViewer::line_height() const
{
return font().glyph_height() + 4;
}
Gfx::IntRect DiffViewer::separator_rect() const
{
return Gfx::IntRect { frame_inner_rect().width() / 2 - 2,
0,
4,
frame_inner_rect().height() };
}
void DiffViewer::set_content(const String& original, const String& diff)
{
m_original_lines = split_to_lines(original);
m_hunks = Diff::parse_hunks(diff);
#ifdef DEBUG_DIFF
for (size_t i = 0; i < m_original_lines.size(); ++i)
dbgln("{}:{}", i, m_original_lines[i]);
#endif
}
DiffViewer::DiffViewer()
{
setup_properties();
}
DiffViewer::DiffViewer(const String& original, const String& diff)
: m_original_lines(split_to_lines(original))
, m_hunks(Diff::parse_hunks(diff))
{
setup_properties();
}
void DiffViewer::setup_properties()
{
set_font(Gfx::FontDatabase::default_fixed_width_font());
set_background_role(ColorRole::Base);
set_foreground_role(ColorRole::BaseText);
}
Vector<String> DiffViewer::split_to_lines(const String& text)
{
// NOTE: This is slightly different than text.split('\n')
Vector<String> lines;
size_t next_line_start_index = 0;
for (size_t i = 0; i < text.length(); ++i) {
if (text[i] == '\n') {
auto line_text = text.substring(next_line_start_index, i - next_line_start_index);
lines.append(move(line_text));
next_line_start_index = i + 1;
}
}
lines.append(text.substring(next_line_start_index, text.length() - next_line_start_index));
return lines;
}
Gfx::Color DiffViewer::red_background()
{
static Gfx::Color color = Gfx::Color::from_rgba(0x88ff0000);
return color;
}
Gfx::Color DiffViewer::green_background()
{
static Gfx::Color color = Gfx::Color::from_rgba(0x8800ff00);
return color;
}
Gfx::Color DiffViewer::gray_background()
{
static Gfx::Color color = Gfx::Color::from_rgba(0x88888888);
return color;
}
void DiffViewer::update_content_size()
{
if (m_hunks.is_empty()) {
set_content_size({ 0, 0 });
return;
}
size_t num_lines = 0;
size_t current_original_line_index = 0;
for (const auto& hunk : m_hunks) {
num_lines += ((int)hunk.original_start_line - (int)current_original_line_index);
num_lines += hunk.removed_lines.size();
if (hunk.added_lines.size() > hunk.removed_lines.size()) {
num_lines += ((int)hunk.added_lines.size() - (int)hunk.removed_lines.size());
}
current_original_line_index = hunk.original_start_line + hunk.removed_lines.size();
}
num_lines += ((int)m_original_lines.size() - (int)current_original_line_index);
// TODO: Support Horizontal scrolling
set_content_size({ 0, (int)(num_lines * line_height()) });
}
void DiffViewer::resize_event(GUI::ResizeEvent& event)
{
ScrollableWidget::resize_event(event);
update_content_size();
}
}

View file

@ -0,0 +1,80 @@
/*
* 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/String.h>
#include <AK/Vector.h>
#include <LibDiff/Hunks.h>
#include <LibGUI/ScrollableWidget.h>
namespace HackStudio {
class DiffViewer final : public GUI::ScrollableWidget {
C_OBJECT(DiffViewer)
public:
virtual ~DiffViewer() override;
void set_content(const String& original, const String& diff);
private:
DiffViewer(const String& original, const String& diff);
DiffViewer();
void setup_properties();
virtual void paint_event(GUI::PaintEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override;
void update_content_size();
enum class LinePosition {
Left,
Right,
Both,
};
enum class LineType {
Normal,
Diff,
Missing,
};
void draw_line(GUI::Painter&, const String& line, size_t y_offset, LinePosition, LineType);
static Vector<String> split_to_lines(const String& text);
static Gfx::Color red_background();
static Gfx::Color green_background();
static Gfx::Color gray_background();
size_t line_height() const;
Gfx::IntRect separator_rect() const;
Vector<String> m_original_lines;
Vector<Diff::Hunk> m_hunks;
};
}

View file

@ -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));
}
};

View file

@ -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;
};
}

View file

@ -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()));
}
};

View file

@ -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;
};
}

View file

@ -0,0 +1,151 @@
/*
* 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 Vector<String>& command_parts) const
{
return command_wrapper(command_parts, m_repository_root);
}
String GitRepo::command_wrapper(const Vector<String>& command_parts, const LexicalPath& chdir)
{
return Core::command("git", command_parts, 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)
{
return !command({ "add", file.string() }).is_null();
}
bool GitRepo::unstage(const LexicalPath& file)
{
return !command({ "reset", "HEAD", "--", file.string() }).is_null();
}
bool GitRepo::commit(const String& message)
{
return !command({ "commit", "-m", message }).is_null();
}
Optional<String> GitRepo::original_file_content(const LexicalPath& file) const
{
return command({ "show", String::formatted("HEAD:{}", file) });
}
Optional<String> GitRepo::unstaged_diff(const LexicalPath& file) const
{
return command({ "diff", file.string().characters() });
}
bool GitRepo::is_tracked(const LexicalPath& file) const
{
auto res = command({ "ls-files", file.string() });
if (res.is_null())
return false;
return !res.is_empty();
}
}

View file

@ -0,0 +1,81 @@
/*
* 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);
bool commit(const String& message);
Optional<String> original_file_content(const LexicalPath& file) const;
Optional<String> unstaged_diff(const LexicalPath& file) const;
bool is_tracked(const LexicalPath& file) const;
private:
static String command_wrapper(const Vector<String>& command_parts, 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 Vector<String>& command_parts) const;
LexicalPath m_repository_root;
};
}

View file

@ -0,0 +1,188 @@
/*
* 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 <LibCore/File.h>
#include <LibDiff/Format.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Model.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <stdio.h>
namespace HackStudio {
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_header = unstaged.add<GUI::Widget>();
unstaged_header.set_layout<GUI::HorizontalBoxLayout>();
auto& refresh_button = unstaged_header.add<GUI::Button>();
refresh_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"));
refresh_button.set_fixed_size(16, 16);
refresh_button.set_tooltip("refresh");
refresh_button.on_click = [this](int) { refresh(); };
auto& unstaged_label = unstaged_header.add<GUI::Label>();
unstaged_label.set_text("Unstaged");
unstaged_header.set_fixed_height(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());
m_unstaged_files->on_selection = [this](const GUI::ModelIndex& index) {
const auto& selected = index.data().as_string();
show_diff(LexicalPath(selected));
};
auto& staged = add<GUI::Widget>();
staged.set_layout<GUI::VerticalBoxLayout>();
auto& staged_header = staged.add<GUI::Widget>();
staged_header.set_layout<GUI::HorizontalBoxLayout>();
auto& commit_button = staged_header.add<GUI::Button>();
commit_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/commit.png"));
commit_button.set_fixed_size(16, 16);
commit_button.set_tooltip("commit");
commit_button.on_click = [this](int) { commit(); };
auto& staged_label = staged_header.add<GUI::Label>();
staged_label.set_text("Staged");
staged_header.set_fixed_height(20);
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());
}
bool GitWidget::initialize()
{
auto result = GitRepo::try_to_create(m_repo_root);
switch (result.type) {
case GitRepo::CreateResult::Type::Success:
m_git_repo = result.repo;
return true;
case GitRepo::CreateResult::Type::GitProgramNotFound:
GUI::MessageBox::show(window(), "Please install the Git port", "Error", GUI::MessageBox::Type::Error);
return false;
case 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 false;
m_git_repo = GitRepo::initialize_repository(m_repo_root);
return true;
}
default:
ASSERT_NOT_REACHED();
}
}
bool GitWidget::initialize_if_needed()
{
if (initialized())
return true;
return initialize();
}
void GitWidget::refresh()
{
if (!initialize_if_needed()) {
dbgln("GitWidget initialization failed");
return;
}
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()));
}
void GitWidget::stage_file(const LexicalPath& file)
{
dbgln("staging: {}", file);
bool rc = m_git_repo->stage(file);
ASSERT(rc);
refresh();
}
void GitWidget::unstage_file(const LexicalPath& file)
{
dbgln("unstaging: {}", file);
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;
dbgln("commit message: {}", message);
m_git_repo->commit(message);
refresh();
}
void GitWidget::set_view_diff_callback(ViewDiffCallback callback)
{
m_view_diff_callback = move(callback);
}
void GitWidget::show_diff(const LexicalPath& file_path)
{
if (!m_git_repo->is_tracked(file_path)) {
auto file = Core::File::construct(file_path.string());
if (!file->open(Core::IODevice::ReadOnly)) {
perror("open");
ASSERT_NOT_REACHED();
}
auto content = file->read_all();
String content_string((char*)content.data(), content.size());
m_view_diff_callback("", Diff::generate_only_additions(content_string));
return;
}
const auto& original_content = m_git_repo->original_file_content(file_path);
const auto& diff = m_git_repo->unstaged_diff(file_path);
ASSERT(original_content.has_value() && diff.has_value());
m_view_diff_callback(original_content.value(), diff.value());
}
}

View file

@ -0,0 +1,65 @@
/*
* 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 <AK/Function.h>
#include <LibGUI/Forward.h>
#include <LibGUI/Widget.h>
namespace HackStudio {
typedef Function<void(const String& original_content, const String& diff)> ViewDiffCallback;
class GitWidget final : public GUI::Widget {
C_OBJECT(GitWidget)
public:
virtual ~GitWidget() override { }
void refresh();
void set_view_diff_callback(ViewDiffCallback callback);
bool initialized() const { return !m_git_repo.is_null(); };
private:
explicit GitWidget(const LexicalPath& repo_root);
bool initialize();
bool initialize_if_needed();
void stage_file(const LexicalPath&);
void unstage_file(const LexicalPath&);
void commit();
void show_diff(const LexicalPath&);
LexicalPath m_repo_root;
RefPtr<GitFilesView> m_unstaged_files;
RefPtr<GitFilesView> m_staged_files;
RefPtr<GitRepo> m_git_repo;
ViewDiffCallback m_view_diff_callback;
};
}