mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-25 21:07:20 -08:00
feat: delete orphan remote tracking branches (#692)
* feat: remove orphan remote-tracking branches - Allow user remove local remote-tracking branches without remotes (ie. remote was removed on merge request). - Included 'DeleteRemoteTracking' and 'HasRemote' util methods to handle this case. * fix: delete both case (local & remote-tracking) - We have local and remote-tracking but not a remote branch. We need to remove both or only the tracking based on the checkbox and on the 'hasRemote' condition
This commit is contained in:
parent
c16a412aa3
commit
8935bdd4c9
2 changed files with 49 additions and 4 deletions
|
@ -52,5 +52,27 @@
|
||||||
cmd.Args = $"push {remote} --delete {name}";
|
cmd.Args = $"push {remote} --delete {name}";
|
||||||
return cmd.Exec();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SourceGit.ViewModels
|
namespace SourceGit.ViewModels
|
||||||
{
|
{
|
||||||
|
@ -56,12 +57,34 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
if (_alsoDeleteTrackingRemote && TrackingRemoteBranch != null)
|
if (_alsoDeleteTrackingRemote && TrackingRemoteBranch != null)
|
||||||
{
|
{
|
||||||
SetProgressDescription("Deleting tracking remote branch...");
|
|
||||||
Commands.Branch.DeleteRemote(_repo.FullPath, TrackingRemoteBranch.Remote, TrackingRemoteBranch.Name);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if(!Commands.Branch.HasRemote(_repo.FullPath, Target.Remote, Target.Name))
|
||||||
{
|
{
|
||||||
|
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);
|
Commands.Branch.DeleteRemote(_repo.FullPath, Target.Remote, Target.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue