From af099af4d02d028a382b1cce94ced391d019f56d Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 3 Oct 2024 18:28:01 +0800 Subject: [PATCH] refactor: stash selected changes in staged group will apply `--staged` paramter for `git stash push` (#535) --- src/Commands/Stash.cs | 49 +++++++++++++++++++++------------- src/ViewModels/StashChanges.cs | 9 ++++--- src/ViewModels/WorkingCopy.cs | 30 +++++++++------------ 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/Commands/Stash.cs b/src/Commands/Stash.cs index cf7d84d1..1eb7d03d 100644 --- a/src/Commands/Stash.cs +++ b/src/Commands/Stash.cs @@ -17,32 +17,45 @@ namespace SourceGit.Commands return Exec(); } - public bool Push(List changes, string message) + public bool Push(List changes, string message, bool onlyStaged) { var pathsBuilder = new StringBuilder(); - var needAdd = new List(); - foreach (var c in changes) - { - pathsBuilder.Append($"\"{c.Path}\" "); - if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked) + if (onlyStaged) + { + foreach (var c in changes) + pathsBuilder.Append($"\"{c.Path}\" "); + + var paths = pathsBuilder.ToString(); + Args = $"stash push --staged -m \"{message}\" -- {paths}"; + } + else + { + var needAdd = new List(); + foreach (var c in changes) { - needAdd.Add(c); - if (needAdd.Count > 10) + pathsBuilder.Append($"\"{c.Path}\" "); + + if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked) { - new Add(WorkingDirectory, needAdd).Exec(); - needAdd.Clear(); + needAdd.Add(c); + if (needAdd.Count > 10) + { + new Add(WorkingDirectory, needAdd).Exec(); + needAdd.Clear(); + } } } + if (needAdd.Count > 0) + { + new Add(WorkingDirectory, needAdd).Exec(); + needAdd.Clear(); + } + + var paths = pathsBuilder.ToString(); + Args = $"stash push -m \"{message}\" -- {paths}"; } - if (needAdd.Count > 0) - { - new Add(WorkingDirectory, needAdd).Exec(); - needAdd.Clear(); - } - - var paths = pathsBuilder.ToString(); - Args = $"stash push -m \"{message}\" -- {paths}"; + return Exec(); } diff --git a/src/ViewModels/StashChanges.cs b/src/ViewModels/StashChanges.cs index 9a06ff9a..abc45492 100644 --- a/src/ViewModels/StashChanges.cs +++ b/src/ViewModels/StashChanges.cs @@ -14,7 +14,6 @@ namespace SourceGit.ViewModels public bool CanIgnoreUntracked { get; - private set; } public bool IncludeUntracked @@ -23,10 +22,11 @@ namespace SourceGit.ViewModels set; } - public StashChanges(Repository repo, List changes, bool canIgnoreUntracked) + public StashChanges(Repository repo, List changes, bool onlyStaged, bool canIgnoreUntracked) { _repo = repo; _changes = changes; + _onlyStaged = onlyStaged; CanIgnoreUntracked = canIgnoreUntracked; IncludeUntracked = true; @@ -56,17 +56,18 @@ namespace SourceGit.ViewModels return Task.Run(() => { - new Commands.Stash(_repo.FullPath).Push(jobs, Message); + var succ = new Commands.Stash(_repo.FullPath).Push(jobs, Message, _onlyStaged); CallUIThread(() => { _repo.MarkWorkingCopyDirtyManually(); _repo.SetWatcherEnabled(true); }); - return true; + return succ; }); } private readonly Repository _repo = null; private readonly List _changes = null; + private readonly bool _onlyStaged = false; } } diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index 74ff3e0e..4bfb10bc 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -318,9 +318,9 @@ namespace SourceGit.ViewModels return; if (autoStart) - PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, true)); + PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, false, true)); else - PopupHost.ShowPopup(new StashChanges(_repo, _cached, true)); + PopupHost.ShowPopup(new StashChanges(_repo, _cached, false, true)); } public void StageSelected(Models.Change next) @@ -524,7 +524,7 @@ namespace SourceGit.ViewModels { if (PopupHost.CanCreatePopup()) { - PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false)); + PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false)); } e.Handled = true; }; @@ -843,7 +843,7 @@ namespace SourceGit.ViewModels stash.Click += (_, e) => { if (PopupHost.CanCreatePopup()) - PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false)); + PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false)); e.Handled = true; }; @@ -928,7 +928,7 @@ namespace SourceGit.ViewModels stash.Click += (_, e) => { if (PopupHost.CanCreatePopup()) - PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false)); + PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false)); e.Handled = true; }; @@ -1097,7 +1097,7 @@ namespace SourceGit.ViewModels stash.Click += (_, e) => { if (PopupHost.CanCreatePopup()) - PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false)); + PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false)); e.Handled = true; }; @@ -1202,20 +1202,16 @@ namespace SourceGit.ViewModels private List GetStagedChanges() { if (_useAmend) - { return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).Result(); - } - else + + var rs = new List(); + foreach (var c in _cached) { - var rs = new List(); - foreach (var c in _cached) - { - if (c.Index != Models.ChangeState.None && - c.Index != Models.ChangeState.Untracked) - rs.Add(c); - } - return rs; + if (c.Index != Models.ChangeState.None && + c.Index != Models.ChangeState.Untracked) + rs.Add(c); } + return rs; } private void SetDetail(Models.Change change, bool isUnstaged)