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