GitRepo.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "GitRepo.h"
  27. #include <LibCore/Command.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. namespace HackStudio {
  31. GitRepo::CreateResult GitRepo::try_to_create(const LexicalPath& repository_root)
  32. {
  33. if (!git_is_installed()) {
  34. return { CreateResult::Type::GitProgramNotFound, nullptr };
  35. }
  36. if (!git_repo_exists(repository_root)) {
  37. return { CreateResult::Type::NoGitRepo, nullptr };
  38. }
  39. return { CreateResult::Type::Success, adopt(*new GitRepo(repository_root)) };
  40. }
  41. RefPtr<GitRepo> GitRepo::initialize_repository(const LexicalPath& repository_root)
  42. {
  43. auto res = command_wrapper({ "init" }, repository_root);
  44. if (res.is_null())
  45. return {};
  46. ASSERT(git_repo_exists(repository_root));
  47. return adopt(*new GitRepo(repository_root));
  48. }
  49. Vector<LexicalPath> GitRepo::unstaged_files() const
  50. {
  51. auto modified = modified_files();
  52. auto untracked = untracked_files();
  53. modified.append(move(untracked));
  54. return modified;
  55. }
  56. //
  57. Vector<LexicalPath> GitRepo::staged_files() const
  58. {
  59. auto raw_result = command({ "diff", "--cached", "--name-only" });
  60. if (raw_result.is_null())
  61. return {};
  62. return parse_files_list(raw_result);
  63. }
  64. Vector<LexicalPath> GitRepo::modified_files() const
  65. {
  66. auto raw_result = command({ "ls-files", "--modified", "--exclude-standard" });
  67. if (raw_result.is_null())
  68. return {};
  69. return parse_files_list(raw_result);
  70. }
  71. Vector<LexicalPath> GitRepo::untracked_files() const
  72. {
  73. auto raw_result = command({ "ls-files", "--others", "--exclude-standard" });
  74. if (raw_result.is_null())
  75. return {};
  76. return parse_files_list(raw_result);
  77. }
  78. Vector<LexicalPath> GitRepo::parse_files_list(const String& raw_result)
  79. {
  80. auto lines = raw_result.split('\n');
  81. Vector<LexicalPath> files;
  82. for (const auto& line : lines) {
  83. files.empend(line);
  84. }
  85. return files;
  86. }
  87. String GitRepo::command(const Vector<String>& command_parts) const
  88. {
  89. return command_wrapper(command_parts, m_repository_root);
  90. }
  91. String GitRepo::command_wrapper(const Vector<String>& command_parts, const LexicalPath& chdir)
  92. {
  93. return Core::command("git", command_parts, chdir);
  94. }
  95. bool GitRepo::git_is_installed()
  96. {
  97. return !command_wrapper({ "--help" }, LexicalPath("/")).is_null();
  98. }
  99. bool GitRepo::git_repo_exists(const LexicalPath& repo_root)
  100. {
  101. return !command_wrapper({ "status" }, repo_root).is_null();
  102. }
  103. bool GitRepo::stage(const LexicalPath& file)
  104. {
  105. return !command({ "add", file.string() }).is_null();
  106. }
  107. bool GitRepo::unstage(const LexicalPath& file)
  108. {
  109. return !command({ "reset", "HEAD", "--", file.string() }).is_null();
  110. }
  111. bool GitRepo::commit(const String& message)
  112. {
  113. return !command({ "commit", "-m", message }).is_null();
  114. }
  115. Optional<String> GitRepo::original_file_content(const LexicalPath& file) const
  116. {
  117. return command({ "show", String::formatted("HEAD:{}", file) });
  118. }
  119. Optional<String> GitRepo::unstaged_diff(const LexicalPath& file) const
  120. {
  121. return command({ "diff", file.string().characters() });
  122. }
  123. bool GitRepo::is_tracked(const LexicalPath& file) const
  124. {
  125. auto res = command({ "ls-files", file.string() });
  126. if (res.is_null())
  127. return false;
  128. return !res.is_empty();
  129. }
  130. }