mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-21 13:20:30 +08:00
71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using System.Threading.Tasks;
|
|
|
|
namespace SourceGit.ViewModels
|
|
{
|
|
public class CheckoutCommit : Popup
|
|
{
|
|
public Models.Commit Commit
|
|
{
|
|
get;
|
|
}
|
|
|
|
public bool DiscardLocalChanges
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public CheckoutCommit(Repository repo, Models.Commit commit)
|
|
{
|
|
_repo = repo;
|
|
Commit = commit;
|
|
DiscardLocalChanges = false;
|
|
View = new Views.CheckoutCommit() { DataContext = this };
|
|
}
|
|
|
|
public override Task<bool> Sure()
|
|
{
|
|
_repo.SetWatcherEnabled(false);
|
|
ProgressDescription = $"Checkout Commit '{Commit.SHA}' ...";
|
|
|
|
var log = _repo.CreateLog("Checkout Commit");
|
|
Use(log);
|
|
|
|
return Task.Run(() =>
|
|
{
|
|
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
|
|
var needPopStash = false;
|
|
if (changes > 0)
|
|
{
|
|
if (DiscardLocalChanges)
|
|
{
|
|
Commands.Discard.All(_repo.FullPath, false, log);
|
|
}
|
|
else
|
|
{
|
|
var succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CHECKOUT_AUTO_STASH");
|
|
if (!succ)
|
|
{
|
|
log.Complete();
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
return false;
|
|
}
|
|
|
|
needPopStash = true;
|
|
}
|
|
}
|
|
|
|
var rs = new Commands.Checkout(_repo.FullPath).Use(log).Commit(Commit.SHA);
|
|
if (needPopStash)
|
|
rs = new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
|
|
|
|
log.Complete();
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
return rs;
|
|
});
|
|
}
|
|
|
|
private readonly Repository _repo = null;
|
|
}
|
|
}
|