GitWidget.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. void GitWidget::refresh()
  86. {
  87. auto result = GitRepo::try_to_create(m_repo_root);
  88. if (result.type == GitRepo::CreateResult::Type::Success) {
  89. m_git_repo = result.repo;
  90. } else if (result.type == GitRepo::CreateResult::Type::GitProgramNotFound) {
  91. GUI::MessageBox::show(window(), "Please install the Git port", "Error", GUI::MessageBox::Type::Error);
  92. return;
  93. } else if (result.type == GitRepo::CreateResult::Type::NoGitRepo) {
  94. auto decision = GUI::MessageBox::show(window(), "Create git repository?", "Git", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  95. if (decision != GUI::Dialog::ExecResult::ExecYes)
  96. return;
  97. m_git_repo = GitRepo::initialize_repository(m_repo_root);
  98. }
  99. ASSERT(!m_git_repo.is_null());
  100. m_unstaged_files->set_model(GitFilesModel::create(m_git_repo->unstaged_files()));
  101. m_staged_files->set_model(GitFilesModel::create(m_git_repo->staged_files()));
  102. }
  103. void GitWidget::stage_file(const LexicalPath& file)
  104. {
  105. dbg() << "staging: " << file.string();
  106. bool rc = m_git_repo->stage(file);
  107. ASSERT(rc);
  108. refresh();
  109. }
  110. void GitWidget::unstage_file(const LexicalPath& file)
  111. {
  112. dbg() << "unstaging: " << file.string();
  113. bool rc = m_git_repo->unstage(file);
  114. ASSERT(rc);
  115. refresh();
  116. }
  117. void GitWidget::commit()
  118. {
  119. String message;
  120. auto res = GUI::InputBox::show(message, window(), "Commit message:", "Commit");
  121. if (res != GUI::InputBox::ExecOK || message.is_empty())
  122. return;
  123. dbg() << "commit message: " << message;
  124. m_git_repo->commit(message);
  125. refresh();
  126. }
  127. void GitWidget::set_view_diff_callback(ViewDiffCallback callback)
  128. {
  129. m_view_diff_callback = move(callback);
  130. }
  131. void GitWidget::show_diff(const LexicalPath& file_path)
  132. {
  133. if (!m_git_repo->is_tracked(file_path)) {
  134. auto file = Core::File::construct(file_path.string());
  135. if (!file->open(Core::IODevice::ReadOnly)) {
  136. perror("open");
  137. ASSERT_NOT_REACHED();
  138. }
  139. auto content = file->read_all();
  140. String content_string((char*)content.data(), content.size());
  141. m_view_diff_callback("", Diff::generate_only_additions(content_string));
  142. return;
  143. }
  144. const auto& original_content = m_git_repo->original_file_content(file_path);
  145. const auto& diff = m_git_repo->unstaged_diff(file_path);
  146. ASSERT(original_content.has_value() && diff.has_value());
  147. m_view_diff_callback(original_content.value(), diff.value());
  148. }
  149. }