GitRepo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(const LexicalPath& 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(const LexicalPath& repository_root)
  20. {
  21. auto res = command_wrapper({ "init" }, repository_root);
  22. if (res.is_null())
  23. return {};
  24. VERIFY(git_repo_exists(repository_root));
  25. return adopt_ref(*new GitRepo(repository_root));
  26. }
  27. Vector<LexicalPath> 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<LexicalPath> GitRepo::staged_files() const
  36. {
  37. auto raw_result = command({ "diff", "--cached", "--name-only" });
  38. if (raw_result.is_null())
  39. return {};
  40. return parse_files_list(raw_result);
  41. }
  42. Vector<LexicalPath> GitRepo::modified_files() const
  43. {
  44. auto raw_result = command({ "ls-files", "--modified", "--exclude-standard" });
  45. if (raw_result.is_null())
  46. return {};
  47. return parse_files_list(raw_result);
  48. }
  49. Vector<LexicalPath> GitRepo::untracked_files() const
  50. {
  51. auto raw_result = command({ "ls-files", "--others", "--exclude-standard" });
  52. if (raw_result.is_null())
  53. return {};
  54. return parse_files_list(raw_result);
  55. }
  56. Vector<LexicalPath> GitRepo::parse_files_list(const String& raw_result)
  57. {
  58. auto lines = raw_result.split('\n');
  59. Vector<LexicalPath> files;
  60. for (const auto& line : lines) {
  61. files.empend(line);
  62. }
  63. return files;
  64. }
  65. String GitRepo::command(const Vector<String>& command_parts) const
  66. {
  67. return command_wrapper(command_parts, m_repository_root);
  68. }
  69. String GitRepo::command_wrapper(const Vector<String>& command_parts, const LexicalPath& chdir)
  70. {
  71. return Core::command("git", command_parts, chdir);
  72. }
  73. bool GitRepo::git_is_installed()
  74. {
  75. return !command_wrapper({ "--help" }, LexicalPath("/")).is_null();
  76. }
  77. bool GitRepo::git_repo_exists(const LexicalPath& repo_root)
  78. {
  79. return !command_wrapper({ "status" }, repo_root).is_null();
  80. }
  81. bool GitRepo::stage(const LexicalPath& file)
  82. {
  83. return !command({ "add", file.string() }).is_null();
  84. }
  85. bool GitRepo::unstage(const LexicalPath& file)
  86. {
  87. return !command({ "reset", "HEAD", "--", file.string() }).is_null();
  88. }
  89. bool GitRepo::commit(const String& message)
  90. {
  91. return !command({ "commit", "-m", message }).is_null();
  92. }
  93. Optional<String> GitRepo::original_file_content(const LexicalPath& file) const
  94. {
  95. return command({ "show", String::formatted("HEAD:{}", file) });
  96. }
  97. Optional<String> GitRepo::unstaged_diff(const LexicalPath& file) const
  98. {
  99. return command({ "diff", file.string().characters() });
  100. }
  101. bool GitRepo::is_tracked(const LexicalPath& file) const
  102. {
  103. auto res = command({ "ls-files", file.string() });
  104. if (res.is_null())
  105. return false;
  106. return !res.is_empty();
  107. }
  108. }