GitService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /// Compares the local repository state to the tracked remote branch.
  26. /// </summary>
  27. public static IPromise<List<GitChange>> CompareLocalToRemote()
  28. {
  29. return new Promise<List<GitChange>>(GitExecutors.GetLocalStatusExecutor);
  30. }
  31. /// <summary>
  32. /// Commits and pushes the selected files to the remote repository.
  33. /// </summary>
  34. public static IPromise<string> CommitAndPush(List<GitChange> changesToCommit, string commitMessage, string username, string email)
  35. {
  36. return new Promise<string>((resolve, reject) =>
  37. GitExecutors.CommitAndPushExecutor(resolve, reject, changesToCommit, commitMessage, username, email));
  38. }
  39. /// <summary>
  40. /// Reverts all local changes for a single file.
  41. /// </summary>
  42. public static IPromise<string> ResetFileChanges(GitChange changeToReset)
  43. {
  44. return new Promise<string>((resolve, reject) =>
  45. GitExecutors.ResetFileExecutor(resolve, reject, changeToReset));
  46. }
  47. /// <summary>
  48. /// Launches an external diff tool to compare file versions.
  49. /// </summary>
  50. public static IPromise<string> LaunchExternalDiff(GitChange change)
  51. {
  52. return new Promise<string>((resolve, reject) => GitExecutors.LaunchExternalDiffExecutor(resolve, reject, change));
  53. }
  54. /// <summary>
  55. /// Analyzes if a pull operation results in conflicts.
  56. /// </summary>
  57. public static IPromise<PullAnalysisResult> AnalyzePullConflicts()
  58. {
  59. return new Promise<PullAnalysisResult>(GitExecutors.FileLevelConflictCheckExecutor);
  60. }
  61. /// <summary>
  62. /// Performs a "safe" pull, assuming no conflicts will occur.
  63. /// </summary>
  64. public static IPromise<string> PerformSafePull()
  65. {
  66. return new Promise<string>(GitExecutors.SafePullExecutor);
  67. }
  68. /// <summary>
  69. /// Performs a pull that may result in merge conflicts.
  70. /// </summary>
  71. public static IPromise<string> ForcePull()
  72. {
  73. return new Promise<string>(GitExecutors.ForcePullExecutor);
  74. }
  75. /// <summary>
  76. /// Launches an external merge tool for a conflicted file.
  77. /// </summary>
  78. public static IPromise<string> LaunchMergeTool(GitChange change)
  79. {
  80. return new Promise<string>((resolve, reject) => GitExecutors.LaunchMergeToolExecutor(resolve, reject, change));
  81. }
  82. /// <summary>
  83. /// Unstages all files if the repository is in a clean, non-conflicted state.
  84. /// </summary>
  85. public static IPromise<bool> UnstageAllFilesIfSafe()
  86. {
  87. return new Promise<bool>(GitExecutors.UnstageAllFilesIfSafeExecutor);
  88. }
  89. /// <summary>
  90. /// Discards all local changes (tracked and untracked) in the repository.
  91. /// </summary>
  92. public static IPromise<string> ResetAllChanges()
  93. {
  94. return new Promise<string>(GitExecutors.ResetAllChangesExecutor);
  95. }
  96. }
  97. }