From 3b1a54dffddc3a72f3a67935ff80e57292f38298 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 21 Oct 2024 16:20:34 +0800 Subject: [PATCH] refactor: use `git update-ref $LOCAL_BRANCH $REMOTE_BRANCH` instead of `git fetch $REMOTE $LOCAL_BRANCH $REMOTE_BRANCH` to fast-forward local branch without checkout it first. --- src/Commands/Fetch.cs | 10 --------- src/Commands/UpdateRef.cs | 23 ++++++++++++++++++++ src/ViewModels/FastForwardWithoutCheckout.cs | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/Commands/UpdateRef.cs diff --git a/src/Commands/Fetch.cs b/src/Commands/Fetch.cs index 94b7fde9..17f08609 100644 --- a/src/Commands/Fetch.cs +++ b/src/Commands/Fetch.cs @@ -24,16 +24,6 @@ namespace SourceGit.Commands Args += remote; } - public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action outputHandler) - { - _outputHandler = outputHandler; - WorkingDirectory = repo; - Context = repo; - TraitErrorAsOutput = true; - SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); - Args = $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}"; - } - protected override void OnReadline(string line) { _outputHandler?.Invoke(line); diff --git a/src/Commands/UpdateRef.cs b/src/Commands/UpdateRef.cs new file mode 100644 index 00000000..ba1b3d2f --- /dev/null +++ b/src/Commands/UpdateRef.cs @@ -0,0 +1,23 @@ +using System; + +namespace SourceGit.Commands +{ + public class UpdateRef : Command + { + public UpdateRef(string repo, string refName, string toRevision, Action outputHandler) + { + _outputHandler = outputHandler; + + WorkingDirectory = repo; + Context = repo; + Args = $"update-ref {refName} {toRevision}"; + } + + protected override void OnReadline(string line) + { + _outputHandler?.Invoke(line); + } + + private Action _outputHandler; + } +} diff --git a/src/ViewModels/FastForwardWithoutCheckout.cs b/src/ViewModels/FastForwardWithoutCheckout.cs index 3861046d..df4b4d73 100644 --- a/src/ViewModels/FastForwardWithoutCheckout.cs +++ b/src/ViewModels/FastForwardWithoutCheckout.cs @@ -31,7 +31,7 @@ namespace SourceGit.ViewModels return Task.Run(() => { - new Commands.Fetch(_repo.FullPath, To.Remote, Local.Name, To.Name, SetProgressDescription).Exec(); + new Commands.UpdateRef(_repo.FullPath, Local.FullName, To.FullName, SetProgressDescription).Exec(); CallUIThread(() => _repo.SetWatcherEnabled(true)); return true; });