2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class Checkout : Popup
|
|
|
|
|
{
|
|
|
|
|
public string Branch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
2024-04-28 06:43:53 -07:00
|
|
|
|
|
|
|
|
|
public bool HasLocalChanges
|
|
|
|
|
{
|
|
|
|
|
get => _repo.WorkingCopyChangesCount > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool LeaveLocalChanges
|
|
|
|
|
{
|
|
|
|
|
get => _leaveLocalChanges;
|
|
|
|
|
set => SetProperty(ref _leaveLocalChanges, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DiscardLocalChanges
|
|
|
|
|
{
|
|
|
|
|
get => _discardLocalChanges;
|
|
|
|
|
set => SetProperty(ref _discardLocalChanges, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool StashLocalChanges
|
|
|
|
|
{
|
|
|
|
|
get => _stashLocalChanges;
|
|
|
|
|
set => SetProperty(ref _stashLocalChanges, value);
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Checkout(Repository repo, string branch)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
Branch = branch;
|
|
|
|
|
View = new Views.Checkout() { DataContext = this };
|
2024-04-28 06:43:53 -07:00
|
|
|
|
|
|
|
|
|
StashLocalChanges = true;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-02-25 19:29:57 -08:00
|
|
|
|
ProgressDescription = $"Checkout '{Branch}' ...";
|
2024-04-28 06:43:53 -07:00
|
|
|
|
var hasLocalChanges = HasLocalChanges;
|
2024-02-25 19:29:57 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-04-28 06:43:53 -07:00
|
|
|
|
var succ = false;
|
|
|
|
|
if (hasLocalChanges)
|
|
|
|
|
{
|
|
|
|
|
if (DiscardLocalChanges)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Discard local changes...");
|
|
|
|
|
Commands.Discard.All(_repo.FullPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StashLocalChanges)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Stash local changes...");
|
|
|
|
|
succ = new Commands.Add(_repo.FullPath).Exec();
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetProgressDescription("Checkout branch ...");
|
|
|
|
|
succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
|
|
|
|
|
|
|
|
|
|
if(hasLocalChanges && StashLocalChanges)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Re-apply local changes...");
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Apply("stash@{0}");
|
|
|
|
|
if (succ)
|
|
|
|
|
{
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Drop("stash@{0}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return succ;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-28 06:43:53 -07:00
|
|
|
|
|
|
|
|
|
public static void ShowPopup(Repository repo, string branch)
|
|
|
|
|
{
|
|
|
|
|
var checkout = new Checkout(repo, branch);
|
|
|
|
|
if (repo.WorkingCopyChangesCount > 0)
|
|
|
|
|
{
|
|
|
|
|
PopupHost.ShowPopup(checkout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PopupHost.ShowAndStartPopup(checkout);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo;
|
2024-04-28 06:43:53 -07:00
|
|
|
|
private bool _leaveLocalChanges;
|
|
|
|
|
private bool _discardLocalChanges;
|
|
|
|
|
private bool _stashLocalChanges;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|