sourcegit/src/ViewModels/StashChanges.cs
leo 68519c76ca
refactor: stash local changes (#550)
* when try to stash all the local changes, add a option to only stash the staged changes
* stash changes in selected files will prompt user - both staged and unstaged changes of selected file(s) will be stashed
2024-10-10 11:54:59 +08:00

79 lines
2 KiB
C#

using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class StashChanges : Popup
{
public string Message
{
get;
set;
}
public bool HasSelectedFiles
{
get;
}
public bool IncludeUntracked
{
get;
set;
}
public bool OnlyStaged
{
get;
set;
}
public StashChanges(Repository repo, List<Models.Change> changes, bool hasSelectedFiles)
{
_repo = repo;
_changes = changes;
HasSelectedFiles = hasSelectedFiles;
IncludeUntracked = true;
OnlyStaged = false;
View = new Views.StashChanges() { DataContext = this };
}
public override Task<bool> Sure()
{
var jobs = _changes;
if (!HasSelectedFiles && !IncludeUntracked)
{
jobs = new List<Models.Change>();
foreach (var job in _changes)
{
if (job.WorkTree != Models.ChangeState.Untracked && job.WorkTree != Models.ChangeState.Added)
{
jobs.Add(job);
}
}
}
if (jobs.Count == 0)
return null;
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Stash changes ...";
return Task.Run(() =>
{
var succ = new Commands.Stash(_repo.FullPath).Push(jobs, Message, !HasSelectedFiles && OnlyStaged);
CallUIThread(() =>
{
_repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true);
});
return succ;
});
}
private readonly Repository _repo = null;
private readonly List<Models.Change> _changes = null;
}
}