Skip to content

Commit

Permalink
HackStudio: Add basic Git integration
Browse files Browse the repository at this point in the history
This adds a "Git" tab to Hackstudio.
Currently has support for staging and unstaging files.
  • Loading branch information
itamar8910 authored and awesomekling committed Sep 15, 2020
1 parent 7b66469 commit 435c6c6
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 42 deletions.
Binary file added Base/res/icons/16x16/minus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Base/res/icons/16x16/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions DevTools/HackStudio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
56 changes: 56 additions & 0 deletions DevTools/HackStudio/Git/GitFilesModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Itamar S. <[email protected]>
* 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));
}

};
57 changes: 57 additions & 0 deletions DevTools/HackStudio/Git/GitFilesModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2020, Itamar S. <[email protected]>
* 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;
};
}
82 changes: 82 additions & 0 deletions DevTools/HackStudio/Git/GitFilesView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2020, Itamar S. <[email protected]>
* 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()));
}

};
56 changes: 56 additions & 0 deletions DevTools/HackStudio/Git/GitFilesView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Itamar S. <[email protected]>
* 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;
};

}
131 changes: 131 additions & 0 deletions DevTools/HackStudio/Git/GitRepo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2020, Itamar S. <[email protected]>
* 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();
}

}
Loading

0 comments on commit 435c6c6

Please sign in to comment.