GitWidget.cpp 5.7 KB

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