Add checkout local changes handling

This commit adds the local changes handling behavior for branch checkout.
One of three can be selected: stash and reapply after checkout, discard changes or leave them as is (previous behaviour)
This commit is contained in:
Alexander Bogomolets 2024-04-28 16:43:53 +03:00 committed by leo
parent 5e6059ba36
commit 927a1cab24
7 changed files with 101 additions and 5 deletions

View file

@ -9,27 +9,98 @@ namespace SourceGit.ViewModels
get;
private set;
}
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);
}
public Checkout(Repository repo, string branch)
{
_repo = repo;
Branch = branch;
View = new Views.Checkout() { DataContext = this };
StashLocalChanges = true;
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Checkout '{Branch}' ...";
var hasLocalChanges = HasLocalChanges;
return Task.Run(() =>
{
var succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
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}");
}
}
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});
}
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);
}
}
private readonly Repository _repo;
private bool _leaveLocalChanges;
private bool _discardLocalChanges;
private bool _stashLocalChanges;
}
}