Serenity Operating System
at master 175 lines 5.7 kB view raw
1/* 2 * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "GitWidget.h" 8#include "../Dialogs/Git/GitCommitDialog.h" 9#include "GitFilesModel.h" 10#include <LibDiff/Format.h> 11#include <LibGUI/Application.h> 12#include <LibGUI/BoxLayout.h> 13#include <LibGUI/Button.h> 14#include <LibGUI/InputBox.h> 15#include <LibGUI/Label.h> 16#include <LibGUI/MessageBox.h> 17#include <LibGUI/Painter.h> 18#include <LibGfx/Bitmap.h> 19#include <stdio.h> 20 21namespace HackStudio { 22 23GitWidget::GitWidget() 24{ 25 set_layout<GUI::HorizontalBoxLayout>(); 26 27 auto& unstaged = add<GUI::Widget>(); 28 unstaged.set_layout<GUI::VerticalBoxLayout>(); 29 auto& unstaged_header = unstaged.add<GUI::Widget>(); 30 unstaged_header.set_layout<GUI::HorizontalBoxLayout>(); 31 32 auto& refresh_button = unstaged_header.add<GUI::Button>(); 33 refresh_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"sv).release_value_but_fixme_should_propagate_errors()); 34 refresh_button.set_fixed_size(16, 16); 35 refresh_button.set_tooltip("refresh"); 36 refresh_button.on_click = [this](int) { refresh(); }; 37 38 auto& unstaged_label = unstaged_header.add<GUI::Label>(); 39 unstaged_label.set_text("Unstaged"); 40 41 unstaged_header.set_fixed_height(20); 42 m_unstaged_files = unstaged.add<GitFilesView>( 43 [this](auto const& file) { stage_file(file); }, 44 Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png"sv).release_value_but_fixme_should_propagate_errors()); 45 m_unstaged_files->on_selection_change = [this] { 46 const auto& index = m_unstaged_files->selection().first(); 47 if (!index.is_valid()) 48 return; 49 50 const auto& selected = index.data().as_string(); 51 show_diff(selected); 52 }; 53 54 auto& staged = add<GUI::Widget>(); 55 staged.set_layout<GUI::VerticalBoxLayout>(); 56 57 auto& staged_header = staged.add<GUI::Widget>(); 58 staged_header.set_layout<GUI::HorizontalBoxLayout>(); 59 60 auto& commit_button = staged_header.add<GUI::Button>(); 61 commit_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/commit.png"sv).release_value_but_fixme_should_propagate_errors()); 62 commit_button.set_fixed_size(16, 16); 63 commit_button.set_tooltip("commit"); 64 commit_button.on_click = [this](int) { commit(); }; 65 66 auto& staged_label = staged_header.add<GUI::Label>(); 67 staged_label.set_text("Staged"); 68 69 staged_header.set_fixed_height(20); 70 m_staged_files = staged.add<GitFilesView>( 71 [this](auto const& file) { unstage_file(file); }, 72 Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png"sv).release_value_but_fixme_should_propagate_errors()); 73} 74 75bool GitWidget::initialize() 76{ 77 auto result = GitRepo::try_to_create(m_repo_root); 78 switch (result.type) { 79 case GitRepo::CreateResult::Type::Success: 80 m_git_repo = result.repo; 81 return true; 82 case GitRepo::CreateResult::Type::GitProgramNotFound: 83 GUI::MessageBox::show(window(), "Please install the Git port"sv, "Error"sv, GUI::MessageBox::Type::Error); 84 return false; 85 case GitRepo::CreateResult::Type::NoGitRepo: { 86 auto decision = GUI::MessageBox::show(window(), "Create git repository?"sv, "Git"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); 87 if (decision != GUI::Dialog::ExecResult::Yes) 88 return false; 89 m_git_repo = GitRepo::initialize_repository(m_repo_root); 90 return true; 91 } 92 default: 93 VERIFY_NOT_REACHED(); 94 } 95} 96 97bool GitWidget::initialize_if_needed() 98{ 99 if (initialized()) 100 return true; 101 102 return initialize(); 103} 104 105void GitWidget::refresh() 106{ 107 if (!initialize_if_needed()) { 108 dbgln("GitWidget initialization failed"); 109 return; 110 } 111 112 VERIFY(!m_git_repo.is_null()); 113 114 m_unstaged_files->set_model(GitFilesModel::create(m_git_repo->unstaged_files())); 115 m_staged_files->set_model(GitFilesModel::create(m_git_repo->staged_files())); 116} 117 118void GitWidget::stage_file(DeprecatedString const& file) 119{ 120 dbgln("staging: {}", file); 121 bool rc = m_git_repo->stage(file); 122 VERIFY(rc); 123 refresh(); 124} 125 126void GitWidget::unstage_file(DeprecatedString const& file) 127{ 128 dbgln("unstaging: {}", file); 129 bool rc = m_git_repo->unstage(file); 130 VERIFY(rc); 131 refresh(); 132} 133 134void GitWidget::commit() 135{ 136 if (m_git_repo.is_null()) { 137 GUI::MessageBox::show(window(), "There is no git repository to commit to!"sv, "Error"sv, GUI::MessageBox::Type::Error); 138 return; 139 } 140 141 auto dialog = GitCommitDialog::construct(window()); 142 dialog->on_commit = [this](auto& message) { 143 m_git_repo->commit(message); 144 refresh(); 145 }; 146 dialog->exec(); 147} 148 149void GitWidget::set_view_diff_callback(ViewDiffCallback callback) 150{ 151 m_view_diff_callback = move(callback); 152} 153 154void GitWidget::show_diff(DeprecatedString const& file_path) 155{ 156 if (!m_git_repo->is_tracked(file_path)) { 157 auto file = Core::File::open(file_path, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors(); 158 auto content = file->read_until_eof().release_value_but_fixme_should_propagate_errors(); 159 m_view_diff_callback("", Diff::generate_only_additions(content)); 160 return; 161 } 162 auto const& original_content = m_git_repo->original_file_content(file_path); 163 auto const& diff = m_git_repo->unstaged_diff(file_path); 164 VERIFY(original_content.has_value() && diff.has_value()); 165 m_view_diff_callback(original_content.value(), diff.value()); 166} 167 168void GitWidget::change_repo(DeprecatedString const& repo_root) 169{ 170 m_repo_root = repo_root; 171 m_git_repo = nullptr; 172 m_unstaged_files->set_model(nullptr); 173 m_staged_files->set_model(nullptr); 174} 175}