using System.Threading.Tasks; namespace SourceGit.ViewModels { public class Checkout : Popup { public string Branch { get; } public bool DiscardLocalChanges { get; set; } public bool IsRecurseSubmoduleVisible { get => _repo.Submodules.Count > 0; } public bool RecurseSubmodules { get => _repo.Settings.UpdateSubmodulesOnCheckoutBranch; set => _repo.Settings.UpdateSubmodulesOnCheckoutBranch = value; } public Checkout(Repository repo, string branch) { _repo = repo; Branch = branch; DiscardLocalChanges = false; } public override async Task Sure() { _repo.SetWatcherEnabled(false); ProgressDescription = $"Checkout '{Branch}' ..."; var log = _repo.CreateLog($"Checkout '{Branch}'"); Use(log); var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules; var succ = false; var needPopStash = false; var confirmed = await _repo.ConfirmCheckoutBranchAsync().ConfigureAwait(false); if (!confirmed) { _repo.SetWatcherEnabled(true); return true; } if (DiscardLocalChanges) { succ = await new Commands.Checkout(_repo.FullPath) .Use(log) .BranchAsync(Branch, true) .ConfigureAwait(false); } else { var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath) .GetResultAsync() .ConfigureAwait(false); if (changes > 0) { succ = await new Commands.Stash(_repo.FullPath) .Use(log) .PushAsync("CHECKOUT_AUTO_STASH") .ConfigureAwait(false); if (!succ) { log.Complete(); _repo.SetWatcherEnabled(true); return false; } needPopStash = true; } succ = await new Commands.Checkout(_repo.FullPath) .Use(log) .BranchAsync(Branch, false) .ConfigureAwait(false); } if (succ) { if (updateSubmodules) { var submodules = await new Commands.QueryUpdatableSubmodules(_repo.FullPath) .GetResultAsync() .ConfigureAwait(false); if (submodules.Count > 0) await new Commands.Submodule(_repo.FullPath) .Use(log) .UpdateAsync(submodules, true, true) .ConfigureAwait(false); } if (needPopStash) await new Commands.Stash(_repo.FullPath) .Use(log) .PopAsync("stash@{0}") .ConfigureAwait(false); } log.Complete(); var b = _repo.Branches.Find(x => x.IsLocal && x.Name == Branch); if (b != null && _repo.HistoriesFilterMode == Models.FilterMode.Included) _repo.SetBranchFilterMode(b, Models.FilterMode.Included, true, false); _repo.MarkBranchesDirtyManually(); _repo.SetWatcherEnabled(true); ProgressDescription = "Waiting for branch updated..."; await Task.Delay(400); return succ; } private readonly Repository _repo = null; } }