GitWidget.cpp 5.7 KB

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