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-04-29 02:22:22 -07:00
|
|
|
|
public bool AutoStash
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
get => _autoStash;
|
|
|
|
|
set => SetProperty(ref _autoStash, 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-04-29 02:22:22 -07:00
|
|
|
|
var hasLocalChanges = _repo.WorkingCopyChangesCount > 0;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
var needPopStash = false;
|
2024-04-28 06:43:53 -07:00
|
|
|
|
if (hasLocalChanges)
|
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
if (AutoStash)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
SetProgressDescription("Adding untracked changes ...");
|
|
|
|
|
var succ = new Commands.Add(_repo.FullPath).Exec();
|
|
|
|
|
if (succ)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Stash local changes ...");
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
else
|
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-04-29 02:22:22 -07:00
|
|
|
|
rs = new Commands.Stash(_repo.FullPath).Apply("stash@{0}");
|
|
|
|
|
if (rs)
|
2024-04-28 06:43:53 -07:00
|
|
|
|
{
|
2024-04-29 02:22:22 -07:00
|
|
|
|
rs = new Commands.Stash(_repo.FullPath).Drop("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;
|
|
|
|
|
private bool _autoStash = true;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|