Browse Source

Sujith :) ->
1. Pushing git changes

sujith 1 month ago
parent
commit
f28c5a0a4a

+ 13 - 6
Assets/Better Git/Arbitrator/Editor/GUI/BetterGitSettingsProvider.cs

@@ -7,12 +7,9 @@ namespace Terra.Arbitrator.GUI
     [Preserve]
     internal class BetterGitSettingsProvider : SettingsProvider
     {
-        // The path in the Project Settings window.
         private const string SettingsPath = "Project/Better Git";
 
-        // Constructor
-        private BetterGitSettingsProvider(string path, SettingsScope scope = SettingsScope.User)
-            : base(path, scope) {}
+        private BetterGitSettingsProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) {}
 
         /// <summary>
         /// This static method is what registers our settings provider with Unity.
@@ -29,9 +26,10 @@ namespace Terra.Arbitrator.GUI
         /// </summary>
         public override void OnGUI(string searchContext)
         {
-            // Load the current settings directly within OnGUI.
             var username = BetterGitSettings.Username;
             var email = BetterGitSettings.Email;
+            var authUsername = BetterGitSettings.AuthUsername;
+            var authPassword = BetterGitSettings.AuthPassword;
             
             EditorGUILayout.Space();
 
@@ -39,15 +37,24 @@ namespace Terra.Arbitrator.GUI
             EditorGUILayout.HelpBox("These credentials will be used for Git operations like Push. They are saved per-user and shared across your projects. Changes are saved automatically.", MessageType.Info);
             
             EditorGUILayout.Space();
-
             
             EditorGUI.BeginChangeCheck();
             username = EditorGUILayout.TextField("Username", username);
             email = EditorGUILayout.TextField("Email Address", email);
+            
+            EditorGUILayout.Space(20);
+            EditorGUILayout.LabelField("HTTPS Authentication", EditorStyles.boldLabel);
+            EditorGUILayout.HelpBox("Use these fields to provide credentials for private servers like Gogs or other systems that require a username and password/token for HTTPS operations.", MessageType.Info);
+
+            authUsername = EditorGUILayout.TextField("Auth Username", authUsername);
+            authPassword = EditorGUILayout.PasswordField("Auth Password / Token", authPassword);
+            
             if (!EditorGUI.EndChangeCheck()) return;
             
             BetterGitSettings.Username = username;
             BetterGitSettings.Email = email;
+            BetterGitSettings.AuthUsername = authUsername;
+            BetterGitSettings.AuthPassword = authPassword;
         }
     }
 }

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

@@ -27,7 +27,7 @@ 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="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>

+ 32 - 20
Assets/Better Git/Arbitrator/Editor/Services/GitExecutors.cs

@@ -8,14 +8,15 @@ using System;
 using System.IO;
 using System.Linq;
 using UnityEngine;
-using LibGit2Sharp;
 using System.Text;
+using LibGit2Sharp;
 using System.Globalization;
 using System.ComponentModel;
 using UnityEngine.Scripting;
 using System.Threading.Tasks;
 using Terra.Arbitrator.Settings;
 using System.Collections.Generic;
+using System.Text.RegularExpressions;
 
 namespace Terra.Arbitrator.Services
 {
@@ -39,6 +40,29 @@ namespace Terra.Arbitrator.Services
         private static string _projectRoot;
         private static string ProjectRoot => _projectRoot ??= MainThreadDataCache.ProjectRoot;
         
+        private static string GetAuthenticatedRemoteUrl()
+        {
+            var authUsername = BetterGitSettings.AuthUsername;
+            var authPassword = BetterGitSettings.AuthPassword;
+
+            if (string.IsNullOrEmpty(authUsername) || string.IsNullOrEmpty(authPassword))
+            {
+                return "origin";
+            }
+            
+            using var repo = new Repository(ProjectRoot);
+            var remote = repo.Network.Remotes["origin"];
+            if (remote == null) throw new Exception("No remote named 'origin' found.");
+            
+            var originalUrl = remote.Url;
+
+            var authenticatedUrl = Regex.Replace(originalUrl, 
+                @"://", 
+                $"://{Uri.EscapeDataString(authUsername)}:{Uri.EscapeDataString(authPassword)}@");
+
+            return authenticatedUrl;
+        }
+        
         public static void GetBranchDataExecutor(Action<BranchData> resolve, Action<Exception> reject)
         {
             try
@@ -128,8 +152,9 @@ namespace Terra.Arbitrator.Services
         {
             try
             {
+                var authenticatedUrl = GetAuthenticatedRemoteUrl();
                 var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
-                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch", "--progress" }, progressReporter);
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch", authenticatedUrl, "--progress" }, progressReporter);
 
                 using var repo = new Repository(ProjectRoot);
                 resolve(repo.Head.TrackingDetails.BehindBy);
@@ -216,7 +241,8 @@ namespace Terra.Arbitrator.Services
                     throw new Exception("Author email is missing. Please set your email address in Project Settings > Better Git.");
                 }
                 
-                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch" });
+                var authenticatedUrl = GetAuthenticatedRemoteUrl();
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "fetch", authenticatedUrl });
                 
                 using (var repo = new Repository(ProjectRoot))
                 {
@@ -259,7 +285,9 @@ namespace Terra.Arbitrator.Services
                 }
 
                 var progressReporter = new Progress<string>(line => ParseProgress(line, onProgress));
-                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "push", "--progress" }, progressReporter, 0, 141);
+                using var tempRepo = new Repository(ProjectRoot);
+                var currentBranch = tempRepo.Head.FriendlyName;
+                await GitCommand.RunGitAsync(new StringBuilder(), new[] { "push", "--progress", authenticatedUrl, $"HEAD:{currentBranch}" }, progressReporter, 0, 141);
 
                 resolve("Successfully committed and pushed changes!");
             }
@@ -573,21 +601,5 @@ namespace Terra.Arbitrator.Services
                 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);
-            }
-        }
     }
 }

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

@@ -7,6 +7,7 @@ using System.IO;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.Scripting;
+using Terra.Arbitrator.Settings;
 
 namespace Terra.Arbitrator.Services
 {
@@ -18,10 +19,24 @@ namespace Terra.Arbitrator.Services
         /// A thread-safe, cached path to the project's root directory.
         /// </summary>
         public static string ProjectRoot { get; private set; }
+        public static string UserName { get; private set; }
+        public static string EmailAddress { get; private set; }
+        public static string AuthUsername { get; private set; }
+        public static string AuthPassword { get; private set; }
 
         static MainThreadDataCache()
         {
             ProjectRoot = Directory.GetParent(Application.dataPath)?.FullName;
+            EditorApplication.update += UpdatePrefValues;
+            UpdatePrefValues();
+        }
+
+        private static void UpdatePrefValues()
+        {
+            UserName = EditorPrefs.GetString(BetterGitSettings.UsernameKey, string.Empty);
+            EmailAddress = EditorPrefs.GetString(BetterGitSettings.EmailKey, string.Empty);
+            AuthUsername = EditorPrefs.GetString(BetterGitSettings.AuthUsernameKey, string.Empty);
+            AuthPassword = EditorPrefs.GetString(BetterGitSettings.AuthPasswordKey, string.Empty);
         }
     }
 }

+ 19 - 5
Assets/Better Git/Arbitrator/Editor/Settings/BetterGitSettings.cs

@@ -1,25 +1,39 @@
 using UnityEditor;
 using UnityEngine.Scripting;
+using Terra.Arbitrator.Services;
 
 namespace Terra.Arbitrator.Settings
 {
     [Preserve]
     public static class BetterGitSettings
     {
-        // Define unique keys to avoid conflicts in EditorPrefs.
-        private const string UsernameKey = "Terra.Arbitrator.Username";
-        private const string EmailKey = "Terra.Arbitrator.Email";
+        internal const string UsernameKey = "Terra.Arbitrator.Username";
+        internal const string EmailKey = "Terra.Arbitrator.Email";
+        internal const string AuthUsernameKey = "Terra.Arbitrator.AuthUsername";
+        internal const string AuthPasswordKey = "Terra.Arbitrator.AuthPassword";
 
         public static string Username
         {
-            get => EditorPrefs.GetString(UsernameKey, "");
+            get => MainThreadDataCache.UserName;
             set => EditorPrefs.SetString(UsernameKey, value);
         }
         
         public static string Email
         {
-            get => EditorPrefs.GetString(EmailKey, "");
+            get => MainThreadDataCache.EmailAddress;
             set => EditorPrefs.SetString(EmailKey, value);
         }
+        
+        public static string AuthUsername
+        {
+            get => MainThreadDataCache.AuthUsername;
+            set => EditorPrefs.SetString(AuthUsernameKey, value);
+        }
+
+        public static string AuthPassword
+        {
+            get => MainThreadDataCache.AuthPassword;
+            set => EditorPrefs.SetString(AuthPasswordKey, value);
+        }
     }
 }