Bläddra i källkod

Sujith :) ->
1. Removed more logs
2. Handled vs code diff logic
3. Handled main thread issues

sujith 1 månad sedan
förälder
incheckning
d450bbb1fc

+ 0 - 1
Assets/Better Git/Arbitrator/Editor/GUI/ArbitratorController.cs

@@ -403,7 +403,6 @@ namespace Terra.Arbitrator.GUI
 
         private void ResetMultipleFiles(List<string> filePaths)
         {
-            // First, check if any of the files to be reset are scripts.
             var hasScripts = filePaths.Any(p => p.EndsWith(".cs", StringComparison.OrdinalIgnoreCase));
 
             if (hasScripts)

+ 16 - 5
Assets/Better Git/Arbitrator/Editor/Services/GitCommand.cs

@@ -28,11 +28,12 @@ namespace Terra.Arbitrator.Services
         /// <summary>
         /// Runs a git command asynchronously.
         /// </summary>
+        /// <param name="execPath">Executable path like git or vs code to perform terminal based command action</param>
         /// <param name="log">A StringBuilder to capture command output for logging.</param>
         /// <param name="progress">A delegate to report real-time standard error lines.</param>
         /// <param name="args">The arguments to pass to the git command.</param>
         /// <param name="acceptableExitCodes">A list of exit codes that should not be treated as errors.</param>
-        public static async Task RunAsync(StringBuilder log, IProgress<string> progress, string[] args, params int[] acceptableExitCodes)
+        public static async Task RunAsync(string execPath, StringBuilder log, IProgress<string> progress, string[] args, params int[] acceptableExitCodes)
         {
             var stdOutBuffer = new StringBuilder();
             var stdErrBuffer = new StringBuilder();
@@ -45,7 +46,7 @@ namespace Terra.Arbitrator.Services
                 progress?.Report(line); // Report progress for each line received.
             });
 
-            var command = Cli.Wrap(FindGitExecutable())
+            var command = Cli.Wrap(execPath)
                              .WithArguments(args)
                              .WithWorkingDirectory(ProjectRoot)
                              .WithValidation(CommandResultValidation.None) // We handle validation manually
@@ -69,9 +70,19 @@ namespace Terra.Arbitrator.Services
             }
         }
         
-        public static Task RunAsync(StringBuilder log, string[] args, params int[] acceptableExitCodes)
+        public static Task RunGitAsync(StringBuilder log, string[] args, params int[] acceptableExitCodes)
         {
-            return RunAsync(log, null, args, acceptableExitCodes);
+            return RunAsync(FindGitExecutable(), log, null, args, acceptableExitCodes);
+        }
+        
+        public static Task RunGitAsync(StringBuilder log, string[] args, IProgress<string> progress, params int[] acceptableExitCodes)
+        {
+            return RunAsync(FindGitExecutable(), log, progress, args, acceptableExitCodes);
+        }
+        
+        public static Task RunVsCodeAsync(StringBuilder log, string[] args, params int[] acceptableExitCodes)
+        {
+            return RunAsync(FindVsCodeExecutable(), log, null, args, acceptableExitCodes);
         }
         
         /// <summary>
@@ -97,7 +108,7 @@ namespace Terra.Arbitrator.Services
             throw new FileNotFoundException($"Could not find executable '{name}'. Please ensure it is installed and in your system's PATH.");
         }
         
-        public static string FindVsCodeExecutable() => FindExecutable("code");
+        private static string FindVsCodeExecutable() => FindExecutable("code");
         private static string FindGitExecutable() => FindExecutable("git");
     }
 }

+ 21 - 25
Assets/Better Git/Arbitrator/Editor/Services/GitExecutors.cs

@@ -5,7 +5,6 @@
 // in the public GitService API.
 
 using System;
-using CliWrap;
 using System.IO;
 using System.Linq;
 using UnityEngine;
@@ -38,7 +37,7 @@ namespace Terra.Arbitrator.Services
     internal static class GitExecutors
     {
         private static string _projectRoot;
-        private static string ProjectRoot => _projectRoot ??= Directory.GetParent(Application.dataPath)?.FullName;
+        private static string ProjectRoot => _projectRoot ??= MainThreadDataCache.ProjectRoot;
         
         public static void GetBranchDataExecutor(Action<BranchData> resolve, Action<Exception> reject)
         {
@@ -48,10 +47,9 @@ namespace Terra.Arbitrator.Services
                 var data = new BranchData
                 {
                     CurrentBranch = repo.Head.FriendlyName,
-                    // Get all local and remote branches, then get their friendly names and remove duplicates.
                     AllBranches = repo.Branches
-                                      .Where(b => !b.FriendlyName.Contains("HEAD")) // Filter out the detached HEAD entry
-                                      .Select(b => b.FriendlyName.Replace("origin/", "")) // Clean up remote names
+                                      .Where(b => !b.FriendlyName.Contains("HEAD"))
+                                      .Select(b => b.FriendlyName.Replace("origin/", ""))
                                       .Distinct()
                                       .OrderBy(name => name)
                                       .ToList()
@@ -69,7 +67,7 @@ namespace Terra.Arbitrator.Services
             try
             {
                 var log = new StringBuilder();
-                await GitCommand.RunAsync(log, new[] { "checkout", branchName });
+                await GitCommand.RunGitAsync(log, new[] { "checkout", branchName });
                 resolve($"Successfully switched to branch '{branchName}'.");
             }
             catch (Exception ex)
@@ -83,9 +81,9 @@ namespace Terra.Arbitrator.Services
             try
             {
                 var log = new StringBuilder();
-                await GitCommand.RunAsync(log, new[] { "reset", "--hard", "HEAD" });
-                await GitCommand.RunAsync(log, new[] { "clean", "-fd" });
-                await GitCommand.RunAsync(log, new[] { "checkout", branchName });
+                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)
@@ -131,7 +129,7 @@ namespace Terra.Arbitrator.Services
             try
             {
                 var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
-                await GitCommand.RunAsync(new StringBuilder(), progressReporter, new[] { "fetch", "--progress" });
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch", "--progress" }, progressReporter);
 
                 using var repo = new Repository(ProjectRoot);
                 resolve(repo.Head.TrackingDetails.BehindBy);
@@ -259,7 +257,7 @@ namespace Terra.Arbitrator.Services
                 }
 
                 var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
-                await GitCommand.RunAsync(new StringBuilder(), progressReporter, new[] { "push", "--progress" }, 0, 141);
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "push", "--progress" }, progressReporter, 0, 141);
 
                 resolve("Successfully committed and pushed changes!");
             }
@@ -377,9 +375,7 @@ namespace Terra.Arbitrator.Services
                         break;
                 }
                 
-                await Cli.Wrap(GitCommand.FindVsCodeExecutable())
-                    .WithArguments(args => args.Add("--diff").Add(fileAPath).Add(fileBPath))
-                    .ExecuteAsync();
+                await GitCommand.RunVsCodeAsync(new StringBuilder(), new[] { "--diff", fileAPath, fileBPath });
 
                 resolve("Launched external diff tool.");
             }
@@ -466,17 +462,17 @@ namespace Terra.Arbitrator.Services
                 {
                     if (repo.RetrieveStatus().IsDirty)
                     {
-                        await GitCommand.RunAsync(log, new[] { "stash", "push", "-u", "-m", "BetterGit-WIP-Pull" }, 0, 141);
+                        await GitCommand.RunGitAsync(log, new[] { "stash", "push", "-u", "-m", "BetterGit-WIP-Pull" }, 0, 141);
                         hasStashed = true;
                     }
                 }
                 
-                await GitCommand.RunAsync(log, new[] { "pull", "--no-rebase" }, 0, 1, 141);
+                await GitCommand.RunGitAsync(log, new[] { "pull", "--no-rebase" }, 0, 1, 141);
 
                 if (hasStashed)
                 {
-                    await GitCommand.RunAsync(log, new[] { "stash", "pop" }, 0, 1, 141);
-                    await GitCommand.RunAsync(log, new[] { "stash", "drop" }, 0, 141);
+                    await GitCommand.RunGitAsync(log, new[] { "stash", "pop" }, 0, 1, 141);
+                    await GitCommand.RunGitAsync(log, new[] { "stash", "drop" }, 0, 141);
                 }
                 
                 resolve(log.ToString());
@@ -487,7 +483,7 @@ namespace Terra.Arbitrator.Services
                 {
                     try
                     {
-                        await GitCommand.RunAsync(new StringBuilder(), new[] { "stash", "pop" }, 0, 1, 141);
+                        await GitCommand.RunGitAsync(new StringBuilder(), new[] { "stash", "pop" }, 0, 1, 141);
                     }
                     catch (Exception exception)
                     {
@@ -516,7 +512,7 @@ namespace Terra.Arbitrator.Services
                     return;
                 }
                 
-                await GitCommand.RunAsync(new StringBuilder(), new[] { GitCommand.FindVsCodeExecutable(), "--wait", change.FilePath }, 0, 141);
+                await GitCommand.RunVsCodeAsync(new StringBuilder(), new[] { "--wait", change.FilePath }, 0, 141);
                 
                 var fullPath = Path.Combine(ProjectRoot, change.FilePath);
                 var fileContent = await File.ReadAllTextAsync(fullPath);
@@ -527,8 +523,8 @@ namespace Terra.Arbitrator.Services
                     return;
                 }
 
-                await GitCommand.RunAsync(new StringBuilder(), new[] { "add", change.FilePath });
-                await GitCommand.RunAsync(new StringBuilder(), new[] { "reset", "HEAD", change.FilePath });
+                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.");
             }
@@ -567,7 +563,7 @@ namespace Terra.Arbitrator.Services
                     return;
                 }
                 
-                await GitCommand.RunAsync(new StringBuilder(), new[] { "reset" });
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "reset" });
                 resolve(true);
             }
             catch (Exception ex)
@@ -581,8 +577,8 @@ namespace Terra.Arbitrator.Services
             try
             {
                 var log = new StringBuilder();
-                await GitCommand.RunAsync(log, new[] { "reset", "--hard", "HEAD" });
-                await GitCommand.RunAsync(log, new[] { "clean", "-fd" });
+                await GitCommand.RunGitAsync(log, new[] { "reset", "--hard", "HEAD" });
+                await GitCommand.RunGitAsync(log, new[] { "clean", "-fd" });
 
                 resolve("Successfully discarded all local changes.");
             }

+ 27 - 0
Assets/Better Git/Arbitrator/Editor/Services/MainThreadDataCache.cs

@@ -0,0 +1,27 @@
+// Copyright (c) 2025 TerraByte Inc.
+//
+// A new utility class that uses [InitializeOnLoad] to safely cache data
+// from Unity's main thread that is needed by background threads.
+
+using System.IO;
+using UnityEngine;
+using UnityEditor;
+using UnityEngine.Scripting;
+
+namespace Terra.Arbitrator.Services
+{
+    [Preserve]
+    [InitializeOnLoad]
+    public static class MainThreadDataCache
+    {
+        /// <summary>
+        /// A thread-safe, cached path to the project's root directory.
+        /// </summary>
+        public static string ProjectRoot { get; private set; }
+
+        static MainThreadDataCache()
+        {
+            ProjectRoot = Directory.GetParent(Application.dataPath)?.FullName;
+        }
+    }
+}

+ 3 - 0
Assets/Better Git/Arbitrator/Editor/Services/MainThreadDataCache.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: cb4dec98d8954b82b29700625b3f8a54
+timeCreated: 1750832173

+ 4 - 1
Terra.Arbitrator.csproj.DotSettings

@@ -1,3 +1,6 @@
 <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
 	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Carbitrator/@EntryIndexedValue">True</s:Boolean>
-	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Carbitrator_005Ceditor/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
+	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Carbitrator_005Ceditor/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cbetter_0020git/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cbetter_0020git_005Carbitrator/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cbetter_0020git_005Carbitrator_005Ceditor/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>