GitRepo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GitRepo.h"
  7. #include <LibCore/Command.h>
  8. namespace HackStudio {
  9. GitRepo::CreateResult GitRepo::try_to_create(DeprecatedString const& repository_root)
  10. {
  11. if (!git_is_installed()) {
  12. return { CreateResult::Type::GitProgramNotFound, nullptr };
  13. }
  14. if (!git_repo_exists(repository_root)) {
  15. return { CreateResult::Type::NoGitRepo, nullptr };
  16. }
  17. return { CreateResult::Type::Success, adopt_ref(*new GitRepo(repository_root)) };
  18. }
  19. RefPtr<GitRepo> GitRepo::initialize_repository(DeprecatedString const& repository_root)
  20. {
  21. auto res = command_wrapper({ "init" }, repository_root);
  22. if (!res.has_value())
  23. return {};
  24. VERIFY(git_repo_exists(repository_root));
  25. return adopt_ref(*new GitRepo(repository_root));
  26. }
  27. Vector<DeprecatedString> GitRepo::unstaged_files() const
  28. {
  29. auto modified = modified_files();
  30. auto untracked = untracked_files();
  31. modified.extend(move(untracked));
  32. return modified;
  33. }
  34. //
  35. Vector<DeprecatedString> GitRepo::staged_files() const
  36. {
  37. auto raw_result = command({ "diff", "--cached", "--name-only" });
  38. if (!raw_result.has_value())
  39. return {};
  40. return parse_files_list(*raw_result);
  41. }
  42. Vector<DeprecatedString> GitRepo::modified_files() const
  43. {
  44. auto raw_result = command({ "ls-files", "--modified", "--exclude-standard" });
  45. if (!raw_result.has_value())
  46. return {};
  47. return parse_files_list(*raw_result);
  48. }
  49. Vector<DeprecatedString> GitRepo::untracked_files() const
  50. {
  51. auto raw_result = command({ "ls-files", "--others", "--exclude-standard" });
  52. if (!raw_result.has_value())
  53. return {};
  54. return parse_files_list(*raw_result);
  55. }
  56. Vector<DeprecatedString> GitRepo::parse_files_list(DeprecatedString const& raw_result)
  57. {
  58. auto lines = raw_result.split('\n');
  59. Vector<DeprecatedString> files;
  60. for (auto const& line : lines) {
  61. files.empend(line);
  62. }
  63. return files;
  64. }
  65. Optional<DeprecatedString> GitRepo::command(Vector<DeprecatedString> const& command_parts) const
  66. {
  67. return command_wrapper(command_parts, m_repository_root);
  68. }
  69. Optional<DeprecatedString> GitRepo::command_wrapper(Vector<DeprecatedString> const& command_parts, DeprecatedString const& chdir)
  70. {
  71. auto const result = Core::command("git", command_parts, LexicalPath(chdir));
  72. if (result.is_error() || result.value().exit_code != 0)
  73. return {};
  74. return DeprecatedString(result.value().output.bytes());
  75. }
  76. bool GitRepo::git_is_installed()
  77. {
  78. return command_wrapper({ "--help" }, "/").has_value();
  79. }
  80. bool GitRepo::git_repo_exists(DeprecatedString const& repo_root)
  81. {
  82. return command_wrapper({ "status" }, repo_root).has_value();
  83. }
  84. bool GitRepo::stage(DeprecatedString const& file)
  85. {
  86. return command({ "add", file }).has_value();
  87. }
  88. bool GitRepo::unstage(DeprecatedString const& file)
  89. {
  90. return command({ "reset", "HEAD", "--", file }).has_value();
  91. }
  92. bool GitRepo::commit(DeprecatedString const& message)
  93. {
  94. return command({ "commit", "-m", message }).has_value();
  95. }
  96. Optional<DeprecatedString> GitRepo::original_file_content(DeprecatedString const& file) const
  97. {
  98. return command({ "show", DeprecatedString::formatted("HEAD:{}", file) });
  99. }
  100. Optional<DeprecatedString> GitRepo::unstaged_diff(DeprecatedString const& file) const
  101. {
  102. return command({ "diff", "-U0", file.characters() });
  103. }
  104. bool GitRepo::is_tracked(DeprecatedString const& file) const
  105. {
  106. auto res = command({ "ls-files", file });
  107. if (!res.has_value())
  108. return false;
  109. return !res->is_empty();
  110. }
  111. }