GitService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // This script serves as the clean, public-facing API for all Git operations.
  4. // It creates promises and delegates the actual implementation work to the
  5. // internal GitExecutors class.
  6. using System;
  7. using UnityEngine.Scripting;
  8. using Terra.Arbitrator.Promises;
  9. using Terra.Arbitrator.Settings;
  10. using System.Collections.Generic;
  11. namespace Terra.Arbitrator.Services
  12. {
  13. /// <summary>
  14. /// The public static API for interacting with the Git repository.
  15. /// </summary>
  16. [Preserve]
  17. public static class GitService
  18. {
  19. /// <summary>
  20. /// Gets the current branch and a list of all available branches.
  21. /// </summary>
  22. public static IPromise<BranchData> GetBranchData()
  23. {
  24. return new Promise<BranchData>(GitExecutors.GetBranchDataExecutor);
  25. }
  26. /// <summary>
  27. /// Performs a safe checkout to the specified branch.
  28. /// </summary>
  29. public static IPromise<string> SwitchBranch(string branchName)
  30. {
  31. return new Promise<string>((resolve, reject) => GitExecutors.SwitchBranchExecutor(resolve, reject, branchName));
  32. }
  33. /// <summary>
  34. /// Discards all local changes and forcibly checks out the specified branch.
  35. /// </summary>
  36. public static IPromise<string> ResetAndSwitchBranch(string branchName)
  37. {
  38. return new Promise<string>((resolve, reject) => GitExecutors.ResetAndSwitchBranchExecutor(resolve, reject, branchName));
  39. }
  40. /// <summary>
  41. /// Synchronously gets the Git status for a single file.
  42. /// Required by UI elements that cannot easily use promises.
  43. /// </summary>
  44. public static GitChange GetChangeForFile(string filePath)
  45. {
  46. return GitExecutors.GetChangeForFile(filePath);
  47. }
  48. /// <summary>
  49. /// Gets the number of incoming commits from the tracked remote branch.
  50. /// </summary>
  51. public static IPromise<int?> GetUpstreamAheadBy(Action<float, string> onProgress = null)
  52. {
  53. return new Promise<int?>((resolve, reject) => GitExecutors.GetUpstreamAheadByExecutor(resolve, reject, onProgress));
  54. }
  55. /// <summary>
  56. /// Compares the local repository state to the tracked remote branch.
  57. /// </summary>
  58. public static IPromise<List<GitChange>> CompareLocalToRemote()
  59. {
  60. return new Promise<List<GitChange>>(GitExecutors.GetLocalStatusExecutor);
  61. }
  62. /// <summary>
  63. /// Commits and pushes the selected files to the remote repository.
  64. /// </summary>
  65. public static IPromise<string> CommitAndPush(List<GitChange> changesToCommit, string commitMessage, string username, string email, Action<float, string> onProgress = null)
  66. {
  67. return new Promise<string>((resolve, reject) =>
  68. GitExecutors.CommitAndPushExecutor(resolve, reject, changesToCommit, commitMessage, username, email, onProgress));
  69. }
  70. /// <summary>
  71. /// Reverts all local changes for a single file.
  72. /// </summary>
  73. public static IPromise<string> ResetFileChanges(GitChange changeToReset)
  74. {
  75. return new Promise<string>((resolve, reject) =>
  76. GitExecutors.ResetFileExecutor(resolve, reject, changeToReset));
  77. }
  78. /// <summary>
  79. /// Launches an external diff tool to compare file versions.
  80. /// </summary>
  81. public static IPromise<string> LaunchExternalDiff(GitChange change)
  82. {
  83. return new Promise<string>((resolve, reject) => GitExecutors.LaunchExternalDiffExecutor(resolve, reject, change));
  84. }
  85. /// <summary>
  86. /// Analyzes if a pull operation results in conflicts.
  87. /// </summary>
  88. public static IPromise<PullAnalysisResult> AnalyzePullConflicts()
  89. {
  90. return new Promise<PullAnalysisResult>(GitExecutors.FileLevelConflictCheckExecutor);
  91. }
  92. /// <summary>
  93. /// Performs a "safe" pull, assuming no conflicts will occur.
  94. /// </summary>
  95. public static IPromise<string> PerformSafePull()
  96. {
  97. return new Promise<string>(GitExecutors.SafePullExecutor);
  98. }
  99. /// <summary>
  100. /// Performs a pull operation, intelligently handling conflicts based on user choices.
  101. /// </summary>
  102. public static IPromise<string> PullAndOverwrite(List<GitChange> resolutions)
  103. {
  104. return new Promise<string>((resolve, reject) => GitExecutors.PullAndOverwriteExecutor(resolve, reject, resolutions));
  105. }
  106. /// <summary>
  107. /// Launches an external merge tool for a conflicted file.
  108. /// </summary>
  109. public static IPromise<string> LaunchMergeTool(GitChange change)
  110. {
  111. return new Promise<string>((resolve, reject) => GitExecutors.LaunchMergeToolExecutor(resolve, reject, change));
  112. }
  113. /// <summary>
  114. /// Unstages all files if the repository is in a clean, non-conflicted state.
  115. /// </summary>
  116. public static IPromise<bool> UnstageAllFilesIfSafe()
  117. {
  118. return new Promise<bool>(GitExecutors.UnstageAllFilesIfSafeExecutor);
  119. }
  120. /// <summary>
  121. /// Checks whether there is an existing stash with the specified message in the repository.
  122. /// </summary>
  123. public static IPromise<bool> HasStash()
  124. {
  125. return new Promise<bool>(GitExecutors.HasStashExecutor);
  126. }
  127. /// <summary>
  128. /// Creates or overwrites a stash with the given message in the repository.
  129. /// If a stash with the same message already exists, it is dropped before the new one is created.
  130. /// </summary>
  131. public static IPromise<string> CreateOrOverwriteStash(List<GitChange> changes)
  132. {
  133. return new Promise<string>((res, rej) => GitExecutors.CreateOrOverwriteStashExecutor(res, rej, changes));
  134. }
  135. /// <summary>
  136. /// Drops a stash with the specified message in the repository.
  137. /// </summary>
  138. public static IPromise<string> DropStash()
  139. {
  140. return new Promise<string>(GitExecutors.DropStashExecutor);
  141. }
  142. /// <summary>
  143. /// Gets the list of files in the stashes with the specified message in the repository.
  144. /// </summary>
  145. public static IPromise<List<GitChange>> GetStashedFiles()
  146. {
  147. return new Promise<List<GitChange>>(GitExecutors.GetStashedFilesExecutor);
  148. }
  149. /// <summary>
  150. /// Launches an external diff tool to compare the stashed and local versions of a file.
  151. /// </summary>
  152. public static IPromise<string> DiffStashedFile(GitChange change)
  153. {
  154. return new Promise<string>((res, rej) => GitExecutors.DiffStashedFileExecutor(res, rej, change));
  155. }
  156. /// <summary>
  157. /// Analyzes if a stash application results in conflicts.
  158. /// </summary>
  159. public static IPromise<PullAnalysisResult> AnalyzeStashConflicts()
  160. {
  161. return new Promise<PullAnalysisResult>(GitExecutors.AnalyzeStashConflictsExecutor);
  162. }
  163. /// <summary>
  164. /// Applies the stashed changes and overwrites the existing ones in the working directory.
  165. /// Handles conflicts based on the provided resolutions.
  166. /// </summary>
  167. public static IPromise<string> ApplyStashAndOverwrite(List<GitChange> resolutions)
  168. {
  169. return new Promise<string>((res, rej) =>
  170. GitExecutors.ApplyStashAndOverwriteExecutor(res, rej, resolutions));
  171. }
  172. }
  173. }