refactor: stash selected changes in staged group will apply --staged paramter for git stash push (#535)

This commit is contained in:
leo 2024-10-03 18:28:01 +08:00
parent ad3eec99cf
commit af099af4d0
No known key found for this signature in database
3 changed files with 49 additions and 39 deletions

View file

@ -17,32 +17,45 @@ namespace SourceGit.Commands
return Exec(); return Exec();
} }
public bool Push(List<Models.Change> changes, string message) public bool Push(List<Models.Change> changes, string message, bool onlyStaged)
{ {
var pathsBuilder = new StringBuilder(); var pathsBuilder = new StringBuilder();
var needAdd = new List<Models.Change>();
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<Models.Change>();
foreach (var c in changes)
{ {
needAdd.Add(c); pathsBuilder.Append($"\"{c.Path}\" ");
if (needAdd.Count > 10)
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
{ {
new Add(WorkingDirectory, needAdd).Exec(); needAdd.Add(c);
needAdd.Clear(); if (needAdd.Count > 10)
{
new Add(WorkingDirectory, needAdd).Exec();
needAdd.Clear();
}
} }
} }
} if (needAdd.Count > 0)
if (needAdd.Count > 0) {
{ new Add(WorkingDirectory, needAdd).Exec();
new Add(WorkingDirectory, needAdd).Exec(); needAdd.Clear();
needAdd.Clear(); }
var paths = pathsBuilder.ToString();
Args = $"stash push -m \"{message}\" -- {paths}";
} }
var paths = pathsBuilder.ToString();
Args = $"stash push -m \"{message}\" -- {paths}";
return Exec(); return Exec();
} }

View file

@ -14,7 +14,6 @@ namespace SourceGit.ViewModels
public bool CanIgnoreUntracked public bool CanIgnoreUntracked
{ {
get; get;
private set;
} }
public bool IncludeUntracked public bool IncludeUntracked
@ -23,10 +22,11 @@ namespace SourceGit.ViewModels
set; set;
} }
public StashChanges(Repository repo, List<Models.Change> changes, bool canIgnoreUntracked) public StashChanges(Repository repo, List<Models.Change> changes, bool onlyStaged, bool canIgnoreUntracked)
{ {
_repo = repo; _repo = repo;
_changes = changes; _changes = changes;
_onlyStaged = onlyStaged;
CanIgnoreUntracked = canIgnoreUntracked; CanIgnoreUntracked = canIgnoreUntracked;
IncludeUntracked = true; IncludeUntracked = true;
@ -56,17 +56,18 @@ namespace SourceGit.ViewModels
return Task.Run(() => return Task.Run(() =>
{ {
new Commands.Stash(_repo.FullPath).Push(jobs, Message); var succ = new Commands.Stash(_repo.FullPath).Push(jobs, Message, _onlyStaged);
CallUIThread(() => CallUIThread(() =>
{ {
_repo.MarkWorkingCopyDirtyManually(); _repo.MarkWorkingCopyDirtyManually();
_repo.SetWatcherEnabled(true); _repo.SetWatcherEnabled(true);
}); });
return true; return succ;
}); });
} }
private readonly Repository _repo = null; private readonly Repository _repo = null;
private readonly List<Models.Change> _changes = null; private readonly List<Models.Change> _changes = null;
private readonly bool _onlyStaged = false;
} }
} }

View file

@ -318,9 +318,9 @@ namespace SourceGit.ViewModels
return; return;
if (autoStart) if (autoStart)
PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, true)); PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, false, true));
else else
PopupHost.ShowPopup(new StashChanges(_repo, _cached, true)); PopupHost.ShowPopup(new StashChanges(_repo, _cached, false, true));
} }
public void StageSelected(Models.Change next) public void StageSelected(Models.Change next)
@ -524,7 +524,7 @@ namespace SourceGit.ViewModels
{ {
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
{ {
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false)); PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false));
} }
e.Handled = true; e.Handled = true;
}; };
@ -843,7 +843,7 @@ namespace SourceGit.ViewModels
stash.Click += (_, e) => stash.Click += (_, e) =>
{ {
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false)); PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false));
e.Handled = true; e.Handled = true;
}; };
@ -928,7 +928,7 @@ namespace SourceGit.ViewModels
stash.Click += (_, e) => stash.Click += (_, e) =>
{ {
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false)); PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false));
e.Handled = true; e.Handled = true;
}; };
@ -1097,7 +1097,7 @@ namespace SourceGit.ViewModels
stash.Click += (_, e) => stash.Click += (_, e) =>
{ {
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false)); PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false));
e.Handled = true; e.Handled = true;
}; };
@ -1202,20 +1202,16 @@ namespace SourceGit.ViewModels
private List<Models.Change> GetStagedChanges() private List<Models.Change> GetStagedChanges()
{ {
if (_useAmend) if (_useAmend)
{
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).Result(); return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).Result();
}
else var rs = new List<Models.Change>();
foreach (var c in _cached)
{ {
var rs = new List<Models.Change>(); if (c.Index != Models.ChangeState.None &&
foreach (var c in _cached) c.Index != Models.ChangeState.Untracked)
{ rs.Add(c);
if (c.Index != Models.ChangeState.None &&
c.Index != Models.ChangeState.Untracked)
rs.Add(c);
}
return rs;
} }
return rs;
} }
private void SetDetail(Models.Change change, bool isUnstaged) private void SetDetail(Models.Change change, bool isUnstaged)