GitWidget.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "GitWidget.h"
  27. #include "GitFilesModel.h"
  28. #include <AK/LogStream.h>
  29. #include <LibCore/File.h>
  30. #include <LibDiff/Format.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/InputBox.h>
  35. #include <LibGUI/Label.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Model.h>
  38. #include <LibGUI/Painter.h>
  39. #include <LibGfx/Bitmap.h>
  40. #include <stdio.h>
  41. namespace HackStudio {
  42. GitWidget::GitWidget(const LexicalPath& repo_root)
  43. : m_repo_root(repo_root)
  44. {
  45. set_layout<GUI::HorizontalBoxLayout>();
  46. auto& unstaged = add<GUI::Widget>();
  47. unstaged.set_layout<GUI::VerticalBoxLayout>();
  48. auto& unstaged_header = unstaged.add<GUI::Widget>();
  49. unstaged_header.set_layout<GUI::HorizontalBoxLayout>();
  50. auto& refresh_button = unstaged_header.add<GUI::Button>();
  51. refresh_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"));
  52. refresh_button.set_preferred_size({ 16, 16 });
  53. refresh_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  54. refresh_button.set_tooltip("refresh");
  55. refresh_button.on_click = [this](int) { refresh(); };
  56. auto& unstaged_label = unstaged_header.add<GUI::Label>();
  57. unstaged_label.set_text("Unstaged");
  58. unstaged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  59. unstaged_header.set_preferred_size(0, 20);
  60. m_unstaged_files = unstaged.add<GitFilesView>(
  61. [this](const auto& file) { stage_file(file); },
  62. Gfx::Bitmap::load_from_file("/res/icons/16x16/plus.png").release_nonnull());
  63. m_unstaged_files->on_selection = [this](const GUI::ModelIndex& index) {
  64. const auto& selected = index.data().as_string();
  65. show_diff(LexicalPath(selected));
  66. };
  67. auto& staged = add<GUI::Widget>();
  68. staged.set_layout<GUI::VerticalBoxLayout>();
  69. auto& staged_header = staged.add<GUI::Widget>();
  70. staged_header.set_layout<GUI::HorizontalBoxLayout>();
  71. auto& commit_button = staged_header.add<GUI::Button>();
  72. commit_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/commit.png"));
  73. commit_button.set_preferred_size({ 16, 16 });
  74. commit_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  75. commit_button.set_tooltip("commit");
  76. commit_button.on_click = [this](int) { commit(); };
  77. auto& staged_label = staged_header.add<GUI::Label>();
  78. staged_label.set_text("Staged");
  79. staged_header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  80. staged_header.set_preferred_size(0, 20);
  81. m_staged_files = staged.add<GitFilesView>(
  82. [this](const auto& file) { unstage_file(file); },
  83. Gfx::Bitmap::load_from_file("/res/icons/16x16/minus.png").release_nonnull());
  84. }
  85. bool GitWidget::initialize()
  86. {
  87. auto result = GitRepo::try_to_create(m_repo_root);
  88. switch (result.type) {
  89. case GitRepo::CreateResult::Type::Success:
  90. m_git_repo = result.repo;
  91. return true;
  92. case GitRepo::CreateResult::Type::GitProgramNotFound:
  93. GUI::MessageBox::show(window(), "Please install the Git port", "Error", GUI::MessageBox::Type::Error);
  94. return false;
  95. case GitRepo::CreateResult::Type::NoGitRepo: {
  96. auto decision = GUI::MessageBox::show(window(), "Create git repository?", "Git", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  97. if (decision != GUI::Dialog::ExecResult::ExecYes)
  98. return false;
  99. m_git_repo = GitRepo::initialize_repository(m_repo_root);
  100. return true;
  101. }
  102. default:
  103. ASSERT_NOT_REACHED();
  104. }
  105. }
  106. bool GitWidget::initialize_if_needed()
  107. {
  108. if (initialized())
  109. return true;
  110. return initialize();
  111. }
  112. void GitWidget::refresh()
  113. {
  114. if (!initialize_if_needed()) {
  115. dbg() << "GitWidget initialization failed";
  116. return;
  117. }
  118. ASSERT(!m_git_repo.is_null());
  119. m_unstaged_files->set_model(GitFilesModel::create(m_git_repo->unstaged_files()));
  120. m_staged_files->set_model(GitFilesModel::create(m_git_repo->staged_files()));
  121. }
  122. void GitWidget::stage_file(const LexicalPath& file)
  123. {
  124. dbg() << "staging: " << file.string();
  125. bool rc = m_git_repo->stage(file);
  126. ASSERT(rc);
  127. refresh();
  128. }
  129. void GitWidget::unstage_file(const LexicalPath& file)
  130. {
  131. dbg() << "unstaging: " << file.string();
  132. bool rc = m_git_repo->unstage(file);
  133. ASSERT(rc);
  134. refresh();
  135. }
  136. void GitWidget::commit()
  137. {
  138. String message;
  139. auto res = GUI::InputBox::show(message, window(), "Commit message:", "Commit");
  140. if (res != GUI::InputBox::ExecOK || message.is_empty())
  141. return;
  142. dbg() << "commit message: " << message;
  143. m_git_repo->commit(message);
  144. refresh();
  145. }
  146. void GitWidget::set_view_diff_callback(ViewDiffCallback callback)
  147. {
  148. m_view_diff_callback = move(callback);
  149. }
  150. void GitWidget::show_diff(const LexicalPath& file_path)
  151. {
  152. if (!m_git_repo->is_tracked(file_path)) {
  153. auto file = Core::File::construct(file_path.string());
  154. if (!file->open(Core::IODevice::ReadOnly)) {
  155. perror("open");
  156. ASSERT_NOT_REACHED();
  157. }
  158. auto content = file->read_all();
  159. String content_string((char*)content.data(), content.size());
  160. m_view_diff_callback("", Diff::generate_only_additions(content_string));
  161. return;
  162. }
  163. const auto& original_content = m_git_repo->original_file_content(file_path);
  164. const auto& diff = m_git_repo->unstaged_diff(file_path);
  165. ASSERT(original_content.has_value() && diff.has_value());
  166. m_view_diff_callback(original_content.value(), diff.value());
  167. }
  168. }