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
|
|
|
|
|
2024-05-29 01:42:47 -07:00
|
|
|
|
public Models.DealWithLocalChanges PreAction
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.DealWithLocalChangesOnCheckoutBranch;
|
|
|
|
|
set => _repo.Settings.DealWithLocalChangesOnCheckoutBranch = value;
|
2024-04-28 06:43:53 -07:00
|
|
|
|
}
|
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-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-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-08-07 00:52:58 -07:00
|
|
|
|
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
|
2024-04-29 02:22:22 -07:00
|
|
|
|
var needPopStash = false;
|
2024-08-07 00:52:58 -07:00
|
|
|
|
if (changes > 0)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
if (PreAction == Models.DealWithLocalChanges.StashAndReaply)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-08-07 00:20:21 -07:00
|
|
|
|
SetProgressDescription("Stash local changes ...");
|
|
|
|
|
var succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
|
2024-04-29 02:22:22 -07:00
|
|
|
|
if (!succ)
|
|
|
|
|
{
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-04-28 06:43:53 -07:00
|
|
|
|
|
2024-04-29 02:22:22 -07:00
|
|
|
|
needPopStash = true;
|
|
|
|
|
}
|
2024-07-02 23:41:43 -07:00
|
|
|
|
else if (PreAction == Models.DealWithLocalChanges.Discard)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
SetProgressDescription("Discard local changes ...");
|
|
|
|
|
Commands.Discard.All(_repo.FullPath);
|
2024-04-28 06:43:53 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-29 02:55:38 -07:00
|
|
|
|
|
2024-04-28 06:43:53 -07:00
|
|
|
|
SetProgressDescription("Checkout branch ...");
|
2024-04-29 02:22:22 -07:00
|
|
|
|
var rs = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
|
2024-04-29 02:55:38 -07:00
|
|
|
|
|
|
|
|
|
if (needPopStash)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Re-apply local changes...");
|
2024-08-07 00:59:35 -07:00
|
|
|
|
rs = new Commands.Stash(_repo.FullPath).Pop("stash@{0}");
|
2024-04-28 06:43:53 -07:00
|
|
|
|
}
|
2024-04-29 02:55:38 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
2024-04-29 02:22:22 -07:00
|
|
|
|
return rs;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:22:22 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|