GitService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /// <returns>A promise that resolves with the number of commits, or null if not tracked.</returns>
  52. public static IPromise<int?> GetUpstreamAheadBy(Action<float, string> onProgress = null)
  53. {
  54. return new Promise<int?>((resolve, reject) => GitExecutors.GetUpstreamAheadByExecutor(resolve, reject, onProgress));
  55. }
  56. /// <summary>
  57. /// Compares the local repository state to the tracked remote branch.
  58. /// </summary>
  59. public static IPromise<List<GitChange>> CompareLocalToRemote()
  60. {
  61. return new Promise<List<GitChange>>(GitExecutors.GetLocalStatusExecutor);
  62. }
  63. /// <summary>
  64. /// Commits and pushes the selected files to the remote repository.
  65. /// </summary>
  66. public static IPromise<string> CommitAndPush(List<GitChange> changesToCommit, string commitMessage, string username, string email, Action<float, string> onProgress = null)
  67. {
  68. return new Promise<string>((resolve, reject) =>
  69. GitExecutors.CommitAndPushExecutor(resolve, reject, changesToCommit, commitMessage, username, email, onProgress));
  70. }
  71. /// <summary>
  72. /// Reverts all local changes for a single file.
  73. /// </summary>
  74. public static IPromise<string> ResetFileChanges(GitChange changeToReset)
  75. {
  76. return new Promise<string>((resolve, reject) =>
  77. GitExecutors.ResetFileExecutor(resolve, reject, changeToReset));
  78. }
  79. /// <summary>
  80. /// Launches an external diff tool to compare file versions.
  81. /// </summary>
  82. public static IPromise<string> LaunchExternalDiff(GitChange change)
  83. {
  84. return new Promise<string>((resolve, reject) => GitExecutors.LaunchExternalDiffExecutor(resolve, reject, change));
  85. }
  86. /// <summary>
  87. /// Analyzes if a pull operation results in conflicts.
  88. /// </summary>
  89. public static IPromise<PullAnalysisResult> AnalyzePullConflicts()
  90. {
  91. return new Promise<PullAnalysisResult>(GitExecutors.FileLevelConflictCheckExecutor);
  92. }
  93. /// <summary>
  94. /// Performs a "safe" pull, assuming no conflicts will occur.
  95. /// </summary>
  96. public static IPromise<string> PerformSafePull()
  97. {
  98. return new Promise<string>(GitExecutors.SafePullExecutor);
  99. }
  100. /// <summary>
  101. /// Performs a pull operation, intelligently handling conflicts based on user choices.
  102. /// </summary>
  103. public static IPromise<string> PullAndOverwrite(List<GitChange> resolutions)
  104. {
  105. return new Promise<string>((resolve, reject) => GitExecutors.PullAndOverwriteExecutor(resolve, reject, resolutions));
  106. }
  107. /// <summary>
  108. /// Launches an external merge tool for a conflicted file.
  109. /// </summary>
  110. public static IPromise<string> LaunchMergeTool(GitChange change)
  111. {
  112. return new Promise<string>((resolve, reject) => GitExecutors.LaunchMergeToolExecutor(resolve, reject, change));
  113. }
  114. /// <summary>
  115. /// Unstages all files if the repository is in a clean, non-conflicted state.
  116. /// </summary>
  117. public static IPromise<bool> UnstageAllFilesIfSafe()
  118. {
  119. return new Promise<bool>(GitExecutors.UnstageAllFilesIfSafeExecutor);
  120. }
  121. }
  122. }