123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- // Copyright (c) 2025 TerraByte Inc.
- //
- // This script serves as the clean, public-facing API for all Git operations.
- // It creates promises and delegates the actual implementation work to the
- // internal GitExecutors class.
- using System;
- using UnityEngine.Scripting;
- using Terra.Arbitrator.Promises;
- using Terra.Arbitrator.Settings;
- using System.Collections.Generic;
- namespace Terra.Arbitrator.Services
- {
- /// <summary>
- /// The public static API for interacting with the Git repository.
- /// </summary>
- [Preserve]
- public static class GitService
- {
- /// <summary>
- /// Gets the current branch and a list of all available branches.
- /// </summary>
- public static IPromise<BranchData> GetBranchData()
- {
- return new Promise<BranchData>(GitExecutors.GetBranchDataExecutor);
- }
- /// <summary>
- /// Performs a safe checkout to the specified branch.
- /// </summary>
- public static IPromise<string> SwitchBranch(string branchName)
- {
- return new Promise<string>((resolve, reject) => GitExecutors.SwitchBranchExecutor(resolve, reject, branchName));
- }
- /// <summary>
- /// Discards all local changes and forcibly checks out the specified branch.
- /// </summary>
- public static IPromise<string> ResetAndSwitchBranch(string branchName)
- {
- return new Promise<string>((resolve, reject) => GitExecutors.ResetAndSwitchBranchExecutor(resolve, reject, branchName));
- }
-
- /// <summary>
- /// Synchronously gets the Git status for a single file.
- /// Required by UI elements that cannot easily use promises.
- /// </summary>
- public static GitChange GetChangeForFile(string filePath)
- {
- return GitExecutors.GetChangeForFile(filePath);
- }
-
- /// <summary>
- /// Gets the number of incoming commits from the tracked remote branch.
- /// </summary>
- public static IPromise<int?> GetUpstreamAheadBy(Action<float, string> onProgress = null)
- {
- return new Promise<int?>((resolve, reject) => GitExecutors.GetUpstreamAheadByExecutor(resolve, reject, onProgress));
- }
-
- /// <summary>
- /// Compares the local repository state to the tracked remote branch.
- /// </summary>
- public static IPromise<List<GitChange>> CompareLocalToRemote()
- {
- return new Promise<List<GitChange>>(GitExecutors.GetLocalStatusExecutor);
- }
-
- /// <summary>
- /// Commits and pushes the selected files to the remote repository.
- /// </summary>
- public static IPromise<string> CommitAndPush(List<GitChange> changesToCommit, string commitMessage, string username, string email, Action<float, string> onProgress = null)
- {
- return new Promise<string>((resolve, reject) =>
- GitExecutors.CommitAndPushExecutor(resolve, reject, changesToCommit, commitMessage, username, email, onProgress));
- }
-
- /// <summary>
- /// Reverts all local changes for a single file.
- /// </summary>
- public static IPromise<string> ResetFileChanges(GitChange changeToReset)
- {
- return new Promise<string>((resolve, reject) =>
- GitExecutors.ResetFileExecutor(resolve, reject, changeToReset));
- }
- /// <summary>
- /// Launches an external diff tool to compare file versions.
- /// </summary>
- public static IPromise<string> LaunchExternalDiff(GitChange change)
- {
- return new Promise<string>((resolve, reject) => GitExecutors.LaunchExternalDiffExecutor(resolve, reject, change));
- }
-
- /// <summary>
- /// Analyzes if a pull operation results in conflicts.
- /// </summary>
- public static IPromise<PullAnalysisResult> AnalyzePullConflicts()
- {
- return new Promise<PullAnalysisResult>(GitExecutors.FileLevelConflictCheckExecutor);
- }
- /// <summary>
- /// Performs a "safe" pull, assuming no conflicts will occur.
- /// </summary>
- public static IPromise<string> PerformSafePull()
- {
- return new Promise<string>(GitExecutors.SafePullExecutor);
- }
- /// <summary>
- /// Performs a pull operation, intelligently handling conflicts based on user choices.
- /// </summary>
- public static IPromise<string> PullAndOverwrite(List<GitChange> resolutions)
- {
- return new Promise<string>((resolve, reject) => GitExecutors.PullAndOverwriteExecutor(resolve, reject, resolutions));
- }
-
- /// <summary>
- /// Launches an external merge tool for a conflicted file.
- /// </summary>
- public static IPromise<string> LaunchMergeTool(GitChange change)
- {
- return new Promise<string>((resolve, reject) => GitExecutors.LaunchMergeToolExecutor(resolve, reject, change));
- }
- /// <summary>
- /// Unstages all files if the repository is in a clean, non-conflicted state.
- /// </summary>
- public static IPromise<bool> UnstageAllFilesIfSafe()
- {
- return new Promise<bool>(GitExecutors.UnstageAllFilesIfSafeExecutor);
- }
-
- /// <summary>
- /// Checks whether there is an existing stash with the specified message in the repository.
- /// </summary>
- public static IPromise<bool> HasStash()
- {
- return new Promise<bool>(GitExecutors.HasStashExecutor);
- }
- /// <summary>
- /// Creates or overwrites a stash with the given message in the repository.
- /// If a stash with the same message already exists, it is dropped before the new one is created.
- /// </summary>
- public static IPromise<string> CreateOrOverwriteStash(List<GitChange> changes)
- {
- return new Promise<string>((res, rej) => GitExecutors.CreateOrOverwriteStashExecutor(res, rej, changes));
- }
- /// <summary>
- /// Drops a stash with the specified message in the repository.
- /// </summary>
- public static IPromise<string> DropStash()
- {
- return new Promise<string>(GitExecutors.DropStashExecutor);
- }
- /// <summary>
- /// Gets the list of files in the stashes with the specified message in the repository.
- /// </summary>
- public static IPromise<List<GitChange>> GetStashedFiles()
- {
- return new Promise<List<GitChange>>(GitExecutors.GetStashedFilesExecutor);
- }
- /// <summary>
- /// Launches an external diff tool to compare the stashed and local versions of a file.
- /// </summary>
- public static IPromise<string> DiffStashedFile(GitChange change)
- {
- return new Promise<string>((res, rej) => GitExecutors.DiffStashedFileExecutor(res, rej, change));
- }
-
- /// <summary>
- /// Analyzes if a stash application results in conflicts.
- /// </summary>
- public static IPromise<PullAnalysisResult> AnalyzeStashConflicts()
- {
- return new Promise<PullAnalysisResult>(GitExecutors.AnalyzeStashConflictsExecutor);
- }
- /// <summary>
- /// Applies the stashed changes and overwrites the existing ones in the working directory.
- /// Handles conflicts based on the provided resolutions.
- /// </summary>
- public static IPromise<string> ApplyStashAndOverwrite(List<GitChange> resolutions)
- {
- return new Promise<string>((res, rej) =>
- GitExecutors.ApplyStashAndOverwriteExecutor(res, rej, resolutions));
- }
- }
- }
|