diff --git a/src/Commands/Branch.cs b/src/Commands/Branch.cs index 30e216e4..4ec8da82 100644 --- a/src/Commands/Branch.cs +++ b/src/Commands/Branch.cs @@ -48,31 +48,19 @@ var cmd = new Command(); cmd.WorkingDirectory = repo; cmd.Context = repo; - cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); - cmd.Args = $"push {remote} --delete {name}"; - return cmd.Exec(); - } - public static bool DeleteRemoteTracking(string repo, string name) - { - var cmd = new Command(); - cmd.WorkingDirectory = repo; - cmd.Context = repo; - cmd.Args = $"branch -D -r {name}"; - return cmd.Exec(); - } + bool exists = new Remote(repo).HasBranch(remote, name); + if (exists) + { + cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + cmd.Args = $"push {remote} --delete {name}"; + } + else + { + cmd.Args = $"branch -D -r {remote}/{name}"; + } - public static bool HasRemote(string repo, string remote, string name) - { - var cmd = new Command(); - cmd.WorkingDirectory = repo; - cmd.Context = repo; - cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); - cmd.Args = $"ls-remote {remote} {name}"; - - var rs = cmd.ReadToEnd(); - - return rs.StdOut.Length > 0; + return cmd.Exec(); } } } diff --git a/src/Commands/Remote.cs b/src/Commands/Remote.cs index f2e8d09e..beaf412b 100644 --- a/src/Commands/Remote.cs +++ b/src/Commands/Remote.cs @@ -45,5 +45,14 @@ Args = "remote set-url" + (isPush ? " --push " : " ") + $"{name} {url}"; return Exec(); } + + public bool HasBranch(string remote, string branch) + { + SSHKey = new Config(WorkingDirectory).Get($"remote.{remote}.sshkey"); + Args = $"ls-remote {remote} {branch}"; + + var rs = ReadToEnd(); + return rs.IsSuccess && rs.StdOut.Trim().Length > 0; + } } } diff --git a/src/ViewModels/DeleteBranch.cs b/src/ViewModels/DeleteBranch.cs index 27e60bbd..e7136a0d 100644 --- a/src/ViewModels/DeleteBranch.cs +++ b/src/ViewModels/DeleteBranch.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace SourceGit.ViewModels { @@ -57,34 +56,12 @@ namespace SourceGit.ViewModels if (_alsoDeleteTrackingRemote && TrackingRemoteBranch != null) { - - if (Commands.Branch.HasRemote(_repo.FullPath, TrackingRemoteBranch.Remote, TrackingRemoteBranch.Name)) - { - SetProgressDescription("Deleting remote-tracking branch and remote branch..."); - - Commands.Branch.DeleteRemote(_repo.FullPath, TrackingRemoteBranch.Remote, TrackingRemoteBranch.Name); - } - else - { - SetProgressDescription("Deleting remote-tracking branch..."); - - var remoteTrackingBranch = $"{TrackingRemoteBranch.Remote}/{TrackingRemoteBranch.Name}"; - - Commands.Branch.DeleteRemoteTracking(_repo.FullPath, remoteTrackingBranch); - } - + SetProgressDescription("Deleting remote-tracking branch..."); + Commands.Branch.DeleteRemote(_repo.FullPath, TrackingRemoteBranch.Remote, TrackingRemoteBranch.Name); } } - else if(!Commands.Branch.HasRemote(_repo.FullPath, Target.Remote, Target.Name)) + else { - SetProgressDescription("Remote branch not found. Deleting remote-tracking branch..."); - var remoteTrackingBranch = $"{Target.Remote}/{Target.Name}"; - - Commands.Branch.DeleteRemoteTracking(_repo.FullPath, remoteTrackingBranch); - } - else - { - SetProgressDescription("Deleting remote-tracking branch..."); Commands.Branch.DeleteRemote(_repo.FullPath, Target.Remote, Target.Name); }