GitService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Terra.Arbitrator.Promises;
  8. using Terra.Arbitrator.Settings;
  9. using System.Collections.Generic;
  10. namespace Terra.Arbitrator.Services
  11. {
  12. /// <summary>
  13. /// The public static API for interacting with the Git repository.
  14. /// </summary>
  15. public static class GitService
  16. {
  17. /// <summary>
  18. /// Gets the current branch and a list of all available branches.
  19. /// </summary>
  20. public static IPromise<BranchData> GetBranchData()
  21. {
  22. return new Promise<BranchData>(GitExecutors.GetBranchDataExecutor);
  23. }
  24. /// <summary>
  25. /// Performs a safe checkout to the specified branch.
  26. /// </summary>
  27. public static IPromise<string> SwitchBranch(string branchName)
  28. {
  29. return new Promise<string>((resolve, reject) => GitExecutors.SwitchBranchExecutor(resolve, reject, branchName));
  30. }
  31. /// <summary>
  32. /// Discards all local changes and forcibly checks out the specified branch.
  33. /// </summary>
  34. public static IPromise<string> ResetAndSwitchBranch(string branchName)
  35. {
  36. return new Promise<string>((resolve, reject) => GitExecutors.ResetAndSwitchBranchExecutor(resolve, reject, branchName));
  37. }
  38. /// <summary>
  39. /// Synchronously gets the Git status for a single file.
  40. /// Required by UI elements that cannot easily use promises.
  41. /// </summary>
  42. public static GitChange GetChangeForFile(string filePath)
  43. {
  44. return GitExecutors.GetChangeForFile(filePath);
  45. }
  46. /// <summary>
  47. /// Gets the number of incoming commits from the tracked remote branch.
  48. /// </summary>
  49. /// <returns>A promise that resolves with the number of commits, or null if not tracked.</returns>
  50. public static IPromise<int?> GetUpstreamAheadBy()
  51. {
  52. return new Promise<int?>(GitExecutors.GetUpstreamAheadByExecutor);
  53. }
  54. /// <summary>
  55. /// Compares the local repository state to the tracked remote branch.
  56. /// </summary>
  57. public static IPromise<List<GitChange>> CompareLocalToRemote()
  58. {
  59. return new Promise<List<GitChange>>(GitExecutors.GetLocalStatusExecutor);
  60. }
  61. /// <summary>
  62. /// Commits and pushes the selected files to the remote repository.
  63. /// </summary>
  64. public static IPromise<string> CommitAndPush(List<GitChange> changesToCommit, string commitMessage, string username, string email, Action<float, string> onProgress = null)
  65. {
  66. return new Promise<string>((resolve, reject) =>
  67. GitExecutors.CommitAndPushExecutor(resolve, reject, changesToCommit, commitMessage, username, email, onProgress));
  68. }
  69. /// <summary>
  70. /// Reverts all local changes for a single file.
  71. /// </summary>
  72. public static IPromise<string> ResetFileChanges(GitChange changeToReset)
  73. {
  74. return new Promise<string>((resolve, reject) =>
  75. GitExecutors.ResetFileExecutor(resolve, reject, changeToReset));
  76. }
  77. /// <summary>
  78. /// Launches an external diff tool to compare file versions.
  79. /// </summary>
  80. public static IPromise<string> LaunchExternalDiff(GitChange change)
  81. {
  82. return new Promise<string>((resolve, reject) => GitExecutors.LaunchExternalDiffExecutor(resolve, reject, change));
  83. }
  84. /// <summary>
  85. /// Analyzes if a pull operation results in conflicts.
  86. /// </summary>
  87. public static IPromise<PullAnalysisResult> AnalyzePullConflicts()
  88. {
  89. return new Promise<PullAnalysisResult>(GitExecutors.FileLevelConflictCheckExecutor);
  90. }
  91. /// <summary>
  92. /// Performs a "safe" pull, assuming no conflicts will occur.
  93. /// </summary>
  94. public static IPromise<string> PerformSafePull()
  95. {
  96. return new Promise<string>(GitExecutors.SafePullExecutor);
  97. }
  98. /// <summary>
  99. /// Performs a pull that may result in merge conflicts.
  100. /// </summary>
  101. public static IPromise<string> ForcePull()
  102. {
  103. return new Promise<string>(GitExecutors.ForcePullExecutor);
  104. }
  105. /// <summary>
  106. /// Launches an external merge tool for a conflicted file.
  107. /// </summary>
  108. public static IPromise<string> LaunchMergeTool(GitChange change)
  109. {
  110. return new Promise<string>((resolve, reject) => GitExecutors.LaunchMergeToolExecutor(resolve, reject, change));
  111. }
  112. /// <summary>
  113. /// Unstages all files if the repository is in a clean, non-conflicted state.
  114. /// </summary>
  115. public static IPromise<bool> UnstageAllFilesIfSafe()
  116. {
  117. return new Promise<bool>(GitExecutors.UnstageAllFilesIfSafeExecutor);
  118. }
  119. }
  120. }