GitService.cs 4.4 KB

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