Browse Source

Minor git improvements

Tim Jones 1 month ago
parent
commit
6cb0cea2e1

+ 10 - 3
RackPeek.Domain/Git/LibGit2GitRepository.cs

@@ -181,15 +181,22 @@ public sealed class LibGit2GitRepository(
         }
     }
 
-    public void Pull() {
+    public void Pull()
+    {
         using Repository repo = OpenRepo();
 
         Commands.Pull(
             repo,
             GetSignature(repo),
-            new PullOptions {
-                FetchOptions = new FetchOptions {
+            new PullOptions
+            {
+                FetchOptions = new FetchOptions
+                {
                     CredentialsProvider = _credentials
+                },
+                MergeOptions = new MergeOptions
+                {
+                    FailOnConflict = false
                 }
             });
     }

+ 22 - 6
RackPeek.Domain/Git/UseCases/AddRemoteUseCase.cs

@@ -1,24 +1,40 @@
 namespace RackPeek.Domain.Git.UseCases;
 
-public class AddRemoteUseCase(IGitRepository repo) : IUseCase {
-    public Task<string?> ExecuteAsync(string url) {
+public class AddRemoteUseCase(IGitRepository repo) : IUseCase
+{
+    public Task<string?> ExecuteAsync(string url)
+    {
         if (!repo.IsAvailable)
             return Task.FromResult<string?>("Git is not available.");
 
         if (string.IsNullOrWhiteSpace(url))
             return Task.FromResult<string?>("URL is required.");
 
-        if (!url.Trim().StartsWith("https://", StringComparison.OrdinalIgnoreCase))
+        url = url.Trim();
+
+        if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
             return Task.FromResult<string?>("Only HTTPS URLs are supported.");
 
         if (repo.HasRemote())
             return Task.FromResult<string?>("Remote already configured.");
 
-        try {
-            repo.AddRemote("origin", url.Trim());
+        try
+        {
+            repo.AddRemote("origin", url);
+
+            // fetch remote state
+            GitSyncStatus sync = repo.FetchAndGetSyncStatus();
+
+            // if remote already has commits, bring them locally
+            if (sync.Behind > 0)
+            {
+                repo.Pull();
+            }
+
             return Task.FromResult<string?>(null);
         }
-        catch (Exception ex) {
+        catch (Exception ex)
+        {
             return Task.FromResult<string?>($"Add remote failed: {ex.Message}");
         }
     }

+ 20 - 7
RackPeek.Domain/Git/UseCases/PushUseCase.cs

@@ -1,19 +1,32 @@
-namespace RackPeek.Domain.Git.UseCases;
+using RackPeek.Domain;
+using RackPeek.Domain.Git;
 
-
-public class PushUseCase(IGitRepository repo) : IUseCase {
-    public Task<string?> ExecuteAsync() {
+public class PushUseCase(IGitRepository repo) : IUseCase
+{
+    public Task<string?> ExecuteAsync()
+    {
         if (!repo.IsAvailable)
             return Task.FromResult<string?>("Git is not available.");
 
         if (!repo.HasRemote())
             return Task.FromResult<string?>("No remote configured.");
 
-        try {
-            repo.Push();
+        try
+        {
+            try
+            {
+                repo.Push();
+            }
+            catch
+            {
+                repo.Pull();
+                repo.Push();
+            }
+
             return Task.FromResult<string?>(null);
         }
-        catch (Exception ex) {
+        catch (Exception ex)
+        {
             return Task.FromResult<string?>($"Push failed: {ex.Message}");
         }
     }

+ 1 - 1
Shared.Rcl/Layout/GitStatusIndicator.razor

@@ -215,7 +215,7 @@ else
                             @(_isPushing ? "Pushing..." : "Push")
                         </button>
                         <button class="w-full text-left px-3 py-2 text-xs text-zinc-300 hover:bg-zinc-700 hover:text-white transition disabled:opacity-50"
-                                disabled="@(_isSyncing || _syncStatus.Behind == 0 || _syncStatus.Error is not null)"
+                                disabled="@(_isSyncing || _syncStatus.Error is not null)"
                                 data-testid="git-pull-button"
                                 @onclick="PullAsync">
                             @(_isPulling ? "Pulling..." : "Pull")