GitService.cs 4.4 KB

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