123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- // Copyright (c) 2025 TerraByte Inc.
- //
- // A new internal class that contains the concrete implementation logic for
- // all Git operations. This separates the "how" from the "what" defined
- // in the public GitService API.
- using System;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- using LibGit2Sharp;
- using System.Text;
- using System.Globalization;
- using System.ComponentModel;
- using UnityEngine.Scripting;
- using System.Threading.Tasks;
- using Terra.Arbitrator.Settings;
- using System.Collections.Generic;
- namespace Terra.Arbitrator.Services
- {
- /// <summary>
- /// A simple data container for branch information.
- /// </summary>
- [Preserve]
- public class BranchData
- {
- public string CurrentBranch { get; set; }
- public List<string> AllBranches { get; set; }
- }
-
- /// <summary>
- /// Contains the promise executor methods for all Git operations.
- /// This is an internal implementation detail and is not exposed publicly.
- /// </summary>
- [Preserve]
- internal static class GitExecutors
- {
- private static string _projectRoot;
- private static string ProjectRoot => _projectRoot ??= MainThreadDataCache.ProjectRoot;
-
- public static void GetBranchDataExecutor(Action<BranchData> resolve, Action<Exception> reject)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
- var data = new BranchData
- {
- CurrentBranch = repo.Head.FriendlyName,
- AllBranches = repo.Branches
- .Where(b => !b.FriendlyName.Contains("HEAD"))
- .Select(b => b.FriendlyName.Replace("origin/", ""))
- .Distinct()
- .OrderBy(name => name)
- .ToList()
- };
- resolve(data);
- }
- catch(Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void SwitchBranchExecutor(Action<string> resolve, Action<Exception> reject, string branchName)
- {
- try
- {
- var log = new StringBuilder();
- await GitCommand.RunGitAsync(log, new[] { "checkout", branchName });
- resolve($"Successfully switched to branch '{branchName}'.");
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void ResetAndSwitchBranchExecutor(Action<string> resolve, Action<Exception> reject, string branchName)
- {
- try
- {
- var log = new StringBuilder();
- await GitCommand.RunGitAsync(log, new[] { "reset", "--hard", "HEAD" });
- await GitCommand.RunGitAsync(log, new[] { "clean", "-fd" });
- await GitCommand.RunGitAsync(log, new[] { "checkout", branchName });
- resolve($"Discarded local changes and switched to branch '{branchName}'.");
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- /// <summary>
- /// Synchronous helper to get a GitChange object for a single file.
- /// This is public so it can be called by the GitService wrapper.
- /// </summary>
- public static GitChange GetChangeForFile(string filePath)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
-
- if (repo.Index.Conflicts.Any(c => c.Ours.Path == filePath))
- {
- return new GitChange(filePath, null, ChangeKind.Conflicted);
- }
- var statusEntry = repo.RetrieveStatus(filePath);
- return statusEntry switch
- {
- FileStatus.NewInWorkdir or FileStatus.NewInIndex => new GitChange(filePath, null, ChangeKind.Added),
- FileStatus.ModifiedInWorkdir or FileStatus.ModifiedInIndex => new GitChange(filePath, null, ChangeKind.Modified),
- FileStatus.DeletedFromWorkdir or FileStatus.DeletedFromIndex => new GitChange(filePath, null, ChangeKind.Deleted),
- FileStatus.RenamedInWorkdir or FileStatus.RenamedInIndex =>
- new GitChange(filePath, null, ChangeKind.Renamed),
- _ => null
- };
- }
- catch { return null; } // Suppress errors if repo is in a unique state
- }
-
- // --- Promise Executor Implementations ---
-
- public static async void GetUpstreamAheadByExecutor(Action<int?> resolve, Action<Exception> reject, Action<float, string> onProgress)
- {
- try
- {
- var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch", "--progress" }, progressReporter);
- using var repo = new Repository(ProjectRoot);
- resolve(repo.Head.TrackingDetails.BehindBy);
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("is not tracking a remote branch"))
- {
- resolve(null);
- }
- else
- {
- reject(ex);
- }
- }
- }
- public static void GetLocalStatusExecutor(Action<List<GitChange>> resolve, Action<Exception> reject)
- {
- try
- {
- var changes = new List<GitChange>();
- using var repo = new Repository(ProjectRoot);
-
- var conflictedPaths = new HashSet<string>(repo.Index.Conflicts.Select(c => c.Ours.Path));
- var statusOptions = new StatusOptions
- {
- IncludeUntracked = true,
- RecurseUntrackedDirs = true,
- DetectRenamesInIndex = true,
- DetectRenamesInWorkDir = true
- };
- foreach (var entry in repo.RetrieveStatus(statusOptions))
- {
- if (conflictedPaths.Contains(entry.FilePath))
- {
- if (changes.All(c => c.FilePath != entry.FilePath))
- {
- changes.Add(new GitChange(entry.FilePath, null, ChangeKind.Conflicted));
- }
- continue;
- }
- switch(entry.State)
- {
- case FileStatus.NewInWorkdir:
- case FileStatus.NewInIndex:
- changes.Add(new GitChange(entry.FilePath, null, ChangeKind.Added));
- break;
-
- case FileStatus.ModifiedInWorkdir:
- case FileStatus.ModifiedInIndex:
- changes.Add(new GitChange(entry.FilePath, null, ChangeKind.Modified));
- break;
- case FileStatus.DeletedFromWorkdir:
- case FileStatus.DeletedFromIndex:
- changes.Add(new GitChange(entry.FilePath, null, ChangeKind.Deleted));
- break;
- case FileStatus.RenamedInWorkdir:
- case FileStatus.RenamedInIndex:
- var renameDetails = entry.HeadToIndexRenameDetails ?? entry.IndexToWorkDirRenameDetails;
- changes.Add(renameDetails != null ? new GitChange(renameDetails.NewFilePath, renameDetails.OldFilePath, ChangeKind.Renamed)
- : new GitChange(entry.FilePath, "Unknown", ChangeKind.Renamed));
- break;
- }
- }
-
- resolve(changes);
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void CommitAndPushExecutor(Action<string> resolve, Action<Exception> reject, List<GitChange> changesToCommit, string commitMessage, string username, string email, Action<float, string> onProgress)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(email))
- {
- throw new Exception("Author email is missing. Please set your email address in Project Settings > Better Git.");
- }
-
- using (var repo = new Repository(ProjectRoot))
- {
- var remote = repo.Network.Remotes["origin"];
- if (remote == null) throw new Exception("No remote named 'origin' found.");
-
- var fetchOptions = new FetchOptions { CertificateCheck = (_, _, _) => true };
- Commands.Fetch(repo, remote.Name, Array.Empty<string>(), fetchOptions, "Arbitrator pre-push fetch");
- var trackingDetails = repo.Head.TrackingDetails;
- if (trackingDetails.BehindBy > 0)
- {
- throw new Exception($"Push aborted. There are {trackingDetails.BehindBy.Value} incoming changes on the remote. Please pull first.");
- }
- var pathsToStage = new List<string>();
- foreach (var change in changesToCommit)
- {
- switch (change.Status)
- {
- case ChangeKind.Deleted:
- Commands.Remove(repo, change.FilePath);
- break;
- case ChangeKind.Renamed:
- Commands.Remove(repo, change.OldFilePath);
- pathsToStage.Add(change.FilePath);
- break;
- default:
- pathsToStage.Add(change.FilePath);
- break;
- }
- }
- if (pathsToStage.Any()) Commands.Stage(repo, pathsToStage);
- var status = repo.RetrieveStatus();
- if (!status.IsDirty) throw new Exception("No effective changes were staged to commit.");
-
- var author = new Signature(username, email, DateTimeOffset.Now);
- repo.Commit(commitMessage, author, author);
- }
- var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "push", "--progress" }, progressReporter, 0, 141);
- resolve("Successfully committed and pushed changes!");
- }
- catch (Exception ex)
- {
- var errorMessage = ex.InnerException?.Message ?? ex.Message;
- reject(new Exception(errorMessage));
- }
- }
-
- private static void ParseProgress(string line, Action<float, string> onProgress)
- {
- if (onProgress == null || string.IsNullOrWhiteSpace(line)) return;
- line = line.Trim();
- var parts = line.Split(new[] { ':' }, 2);
- if (parts.Length < 2) return;
- var action = parts[0];
- var progressPart = parts[1];
-
- var percentIndex = progressPart.IndexOf('%');
- if (percentIndex == -1) return;
- var percentString = progressPart[..percentIndex].Trim();
- if (!float.TryParse(percentString, NumberStyles.Any, CultureInfo.InvariantCulture, out var percentage)) return;
- var progressValue = percentage / 100.0f;
- onProgress(progressValue, $"{action}...");
- }
- public static void ResetFileExecutor(Action<string> resolve, Action<Exception> reject, GitChange changeToReset)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
- switch (changeToReset.Status)
- {
- case ChangeKind.Added:
- {
- Commands.Unstage(repo, changeToReset.FilePath);
-
- var fullPath = Path.Combine(ProjectRoot, changeToReset.FilePath);
- if (File.Exists(fullPath))
- {
- File.Delete(fullPath);
- }
- break;
- }
- case ChangeKind.Renamed:
- {
- Commands.Unstage(repo, changeToReset.FilePath);
- var newFullPath = Path.Combine(ProjectRoot, changeToReset.FilePath);
- if (File.Exists(newFullPath)) File.Delete(newFullPath);
-
- repo.CheckoutPaths(repo.Head.Tip.Sha, new[] { changeToReset.OldFilePath }, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
- break;
- }
- default:
- repo.CheckoutPaths(repo.Head.Tip.Sha, new[] { changeToReset.FilePath }, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
- break;
- }
- resolve($"Successfully reset changes for '{changeToReset.FilePath}'");
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void LaunchExternalDiffExecutor(Action<string> resolve, Action<Exception> reject, GitChange change)
- {
- string fileAPath = null; // Before
- string fileBPath = null; // After
-
- try
- {
- using var repo = new Repository(ProjectRoot);
-
- string GetFileContentFromHead(string path)
- {
- var blob = repo.Head.Tip[path]?.Target as Blob;
- return blob?.GetContentText() ?? "";
- }
- string CreateTempFile(string originalPath, string content)
- {
- var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(originalPath));
- File.WriteAllText(tempPath, content);
- return tempPath;
- }
- switch (change.Status)
- {
- case ChangeKind.Added:
- fileAPath = CreateTempFile(change.FilePath, "");
- fileBPath = Path.Combine(ProjectRoot, change.FilePath);
- break;
-
- case ChangeKind.Deleted:
- fileAPath = CreateTempFile(change.FilePath, GetFileContentFromHead(change.FilePath));
- fileBPath = CreateTempFile(change.FilePath, "");
- break;
- case ChangeKind.Renamed:
- fileAPath = CreateTempFile(change.OldFilePath, GetFileContentFromHead(change.OldFilePath));
- fileBPath = Path.Combine(ProjectRoot, change.FilePath);
- break;
-
- default: // Modified
- fileAPath = CreateTempFile(change.FilePath, GetFileContentFromHead(change.FilePath));
- fileBPath = Path.Combine(ProjectRoot, change.FilePath);
- break;
- }
-
- await GitCommand.RunVsCodeAsync(new StringBuilder(), new[] { "--diff", fileAPath, fileBPath });
- resolve("Launched external diff tool.");
- }
- catch(Win32Exception ex)
- {
- reject(new Exception("Could not launch VS Code. Ensure it is installed and the 'code' command is available in your system's PATH.", ex));
- }
- catch(Exception ex)
- {
- reject(ex);
- }
- finally
- {
- try
- {
- if (fileAPath != null && fileAPath.Contains(Path.GetTempPath()) && File.Exists(fileAPath)) File.Delete(fileAPath);
- if (fileBPath != null && fileBPath.Contains(Path.GetTempPath()) && File.Exists(fileBPath)) File.Delete(fileBPath);
- }
- catch(Exception cleanupEx)
- {
- Debug.LogError($"Failed to clean up temporary diff files: {cleanupEx.Message}");
- }
- }
- }
-
- public static void FileLevelConflictCheckExecutor(Action<PullAnalysisResult> resolve, Action<Exception> reject)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
- var result = AnalyzePullConflictsInternal(repo).Result;
- resolve(result);
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
- private static Task<PullAnalysisResult> AnalyzePullConflictsInternal(Repository repo)
- {
- var remote = repo.Network.Remotes["origin"];
- if (remote == null) throw new Exception("No remote named 'origin' was found.");
- Commands.Fetch(repo, remote.Name, Array.Empty<string>(), new FetchOptions { CertificateCheck = (_,_,_) => true }, null);
-
- var localBranch = repo.Head;
- var remoteBranch = repo.Head.TrackedBranch;
- if (remoteBranch == null) throw new Exception("Current branch is not tracking a remote branch.");
-
- var mergeBase = repo.ObjectDatabase.FindMergeBase(localBranch.Tip, remoteBranch.Tip);
- if (mergeBase == null) throw new Exception("Could not find a common ancestor.");
-
- var theirChanges = new HashSet<string>(repo.Diff.Compare<TreeChanges>(mergeBase.Tree, remoteBranch.Tip.Tree).Select(c => c.Path));
- var ourChanges = new HashSet<string>(repo.Diff.Compare<TreeChanges>(mergeBase.Tree, localBranch.Tip.Tree).Select(c => c.Path));
- foreach (var statusEntry in repo.RetrieveStatus()) ourChanges.Add(statusEntry.FilePath);
-
- return Task.FromResult(new PullAnalysisResult(ourChanges.Where(theirChanges.Contains).ToList()));
- }
- public static void SafePullExecutor(Action<string> resolve, Action<Exception> reject)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
- var signature = new Signature("Better Git Tool", "bettergit@letsterra.com", DateTimeOffset.Now);
- var pullOptions = new PullOptions { FetchOptions = new FetchOptions { CertificateCheck = (_,_,_) => true } };
- var mergeResult = Commands.Pull(repo, signature, pullOptions);
- resolve(mergeResult.Status == MergeStatus.UpToDate ? "Already up-to-date." : $"Pull successful. Status: {mergeResult.Status}");
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void ForcePullExecutor(Action<string> resolve, Action<Exception> reject)
- {
- var log = new StringBuilder();
- var hasStashed = false;
- try
- {
- using (var repo = new Repository(ProjectRoot))
- {
- if (repo.RetrieveStatus().IsDirty)
- {
- await GitCommand.RunGitAsync(log, new[] { "stash", "push", "-u", "-m", "BetterGit-WIP-Pull" }, 0, 141);
- hasStashed = true;
- }
- }
-
- await GitCommand.RunGitAsync(log, new[] { "pull", "--no-rebase" }, 0, 1, 141);
- if (hasStashed)
- {
- await GitCommand.RunGitAsync(log, new[] { "stash", "pop" }, 0, 1, 141);
- await GitCommand.RunGitAsync(log, new[] { "stash", "drop" }, 0, 141);
- }
-
- resolve(log.ToString());
- }
- catch (Exception ex)
- {
- if (hasStashed)
- {
- try
- {
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "stash", "pop" }, 0, 1, 141);
- }
- catch (Exception exception)
- {
- log.AppendLine($"Fatal Error trying to pop stash after a failed pull: {exception.Message}");
- }
- }
- log.AppendLine("\n--- PULL FAILED ---");
- log.AppendLine(ex.ToString());
- reject(new Exception(log.ToString()));
- }
- }
-
- public static async void LaunchMergeToolExecutor(Action<string> resolve, Action<Exception> reject, GitChange change)
- {
- try
- {
- if (change.FilePath == null)
- {
- reject(new Exception("Could not find file path."));
- return;
- }
- var fileExtension = Path.GetExtension(change.FilePath).ToLower();
- if (fileExtension is ".prefab" or ".unity")
- {
- reject(new Exception("Cannot auto-resolve conflicts for binary files. Please use an external merge tool."));
- return;
- }
-
- await GitCommand.RunVsCodeAsync(new StringBuilder(), new[] { "--wait", change.FilePath }, 0, 141);
-
- var fullPath = Path.Combine(ProjectRoot, change.FilePath);
- var fileContent = await File.ReadAllTextAsync(fullPath);
- if (fileContent.Contains("<<<<<<<"))
- {
- resolve($"Conflict in '{change.FilePath}' was not resolved. Please try again.");
- return;
- }
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "add", change.FilePath });
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "reset", "HEAD", change.FilePath });
-
- resolve($"Successfully resolved conflict in '{change.FilePath}'. The file is now modified and ready for review.");
- }
- catch (Win32Exception ex)
- {
- reject(new Exception("Could not launch VS Code. Ensure it is installed and the 'code' command is available in your system's PATH.", ex));
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void UnstageAllFilesIfSafeExecutor(Action<bool> resolve, Action<Exception> reject)
- {
- try
- {
- using var repo = new Repository(ProjectRoot);
- if (repo.Index.Conflicts.Any())
- {
- resolve(false);
- return;
- }
- var stagedFiles = repo.RetrieveStatus().Count(s => s.State is
- FileStatus.NewInIndex or
- FileStatus.ModifiedInIndex or
- FileStatus.DeletedFromIndex or
- FileStatus.RenamedInIndex or
- FileStatus.TypeChangeInIndex);
-
- if (stagedFiles == 0)
- {
- resolve(false);
- return;
- }
-
- await GitCommand.RunGitAsync(new StringBuilder(), new[] { "reset" });
- resolve(true);
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
-
- public static async void ResetAllChangesExecutor(Action<string> resolve, Action<Exception> reject)
- {
- try
- {
- var log = new StringBuilder();
- await GitCommand.RunGitAsync(log, new[] { "reset", "--hard", "HEAD" });
- await GitCommand.RunGitAsync(log, new[] { "clean", "-fd" });
- resolve("Successfully discarded all local changes.");
- }
- catch (Exception ex)
- {
- reject(ex);
- }
- }
- }
- }
|