Kaynağa Gözat

Minor git changes

Tim Jones 3 hafta önce
ebeveyn
işleme
fd11e6b2ab

+ 20 - 4
RackPeek.Domain/Git/LibGit2GitRepository.cs

@@ -165,11 +165,24 @@ public sealed class LibGit2GitRepository(
         using Repository repo = OpenRepo();
 
         Remote remote = GetRemote(repo);
+        var refSpec = $"refs/heads/{repo.Head.FriendlyName}";
 
-        repo.Network.Push(
-            remote,
-            $"refs/heads/{repo.Head.FriendlyName}",
-            new PushOptions { CredentialsProvider = _credentials });
+        try {
+            repo.Network.Push(
+                remote,
+                refSpec,
+                new PushOptions { CredentialsProvider = _credentials });
+        }
+        catch (LibGit2Sharp.NonFastForwardException) {
+            // remote has commits we don't have
+            Pull();
+
+            // retry push
+            repo.Network.Push(
+                remote,
+                refSpec,
+                new PushOptions { CredentialsProvider = _credentials });
+        }
 
         if (repo.Head.TrackedBranch is null) {
             Branch remoteBranch = repo.Branches[$"{remote.Name}/{repo.Head.FriendlyName}"];
@@ -190,6 +203,9 @@ public sealed class LibGit2GitRepository(
             new PullOptions {
                 FetchOptions = new FetchOptions {
                     CredentialsProvider = _credentials
+                },
+                MergeOptions = new MergeOptions {
+                    FailOnConflict = false
                 }
             });
     }

+ 13 - 2
RackPeek.Domain/Git/UseCases/AddRemoteUseCase.cs

@@ -8,14 +8,25 @@ public class AddRemoteUseCase(IGitRepository repo) : IUseCase {
         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());
+            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) {

+ 2 - 1
RackPeek.Domain/Git/UseCases/PullUseCase.cs

@@ -1,4 +1,5 @@
-namespace RackPeek.Domain.Git.UseCases;
+using RackPeek.Domain;
+using RackPeek.Domain.Git;
 
 public class PullUseCase(IGitRepository repo) : IUseCase {
     public Task<string?> ExecuteAsync() {

+ 10 - 3
RackPeek.Domain/Git/UseCases/PushUseCase.cs

@@ -1,5 +1,5 @@
-namespace RackPeek.Domain.Git.UseCases;
-
+using RackPeek.Domain;
+using RackPeek.Domain.Git;
 
 public class PushUseCase(IGitRepository repo) : IUseCase {
     public Task<string?> ExecuteAsync() {
@@ -10,7 +10,14 @@ public class PushUseCase(IGitRepository repo) : IUseCase {
             return Task.FromResult<string?>("No remote configured.");
 
         try {
-            repo.Push();
+            try {
+                repo.Push();
+            }
+            catch {
+                repo.Pull();
+                repo.Push();
+            }
+
             return Task.FromResult<string?>(null);
         }
         catch (Exception ex) {

+ 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")