Skip to content

Commit af099af

Browse files
committed
refactor: stash selected changes in staged group will apply --staged paramter for git stash push (#535)
1 parent ad3eec9 commit af099af

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

src/Commands/Stash.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,45 @@ public bool Push(string message)
1717
return Exec();
1818
}
1919

20-
public bool Push(List<Models.Change> changes, string message)
20+
public bool Push(List<Models.Change> changes, string message, bool onlyStaged)
2121
{
2222
var pathsBuilder = new StringBuilder();
23-
var needAdd = new List<Models.Change>();
24-
foreach (var c in changes)
25-
{
26-
pathsBuilder.Append($"\"{c.Path}\" ");
2723

28-
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
24+
if (onlyStaged)
25+
{
26+
foreach (var c in changes)
27+
pathsBuilder.Append($"\"{c.Path}\" ");
28+
29+
var paths = pathsBuilder.ToString();
30+
Args = $"stash push --staged -m \"{message}\" -- {paths}";
31+
}
32+
else
33+
{
34+
var needAdd = new List<Models.Change>();
35+
foreach (var c in changes)
2936
{
30-
needAdd.Add(c);
31-
if (needAdd.Count > 10)
37+
pathsBuilder.Append($"\"{c.Path}\" ");
38+
39+
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
3240
{
33-
new Add(WorkingDirectory, needAdd).Exec();
34-
needAdd.Clear();
41+
needAdd.Add(c);
42+
if (needAdd.Count > 10)
43+
{
44+
new Add(WorkingDirectory, needAdd).Exec();
45+
needAdd.Clear();
46+
}
3547
}
3648
}
49+
if (needAdd.Count > 0)
50+
{
51+
new Add(WorkingDirectory, needAdd).Exec();
52+
needAdd.Clear();
53+
}
54+
55+
var paths = pathsBuilder.ToString();
56+
Args = $"stash push -m \"{message}\" -- {paths}";
3757
}
38-
if (needAdd.Count > 0)
39-
{
40-
new Add(WorkingDirectory, needAdd).Exec();
41-
needAdd.Clear();
42-
}
43-
44-
var paths = pathsBuilder.ToString();
45-
Args = $"stash push -m \"{message}\" -- {paths}";
58+
4659
return Exec();
4760
}
4861

src/ViewModels/StashChanges.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public string Message
1414
public bool CanIgnoreUntracked
1515
{
1616
get;
17-
private set;
1817
}
1918

2019
public bool IncludeUntracked
@@ -23,10 +22,11 @@ public bool IncludeUntracked
2322
set;
2423
}
2524

26-
public StashChanges(Repository repo, List<Models.Change> changes, bool canIgnoreUntracked)
25+
public StashChanges(Repository repo, List<Models.Change> changes, bool onlyStaged, bool canIgnoreUntracked)
2726
{
2827
_repo = repo;
2928
_changes = changes;
29+
_onlyStaged = onlyStaged;
3030

3131
CanIgnoreUntracked = canIgnoreUntracked;
3232
IncludeUntracked = true;
@@ -56,17 +56,18 @@ public override Task<bool> Sure()
5656

5757
return Task.Run(() =>
5858
{
59-
new Commands.Stash(_repo.FullPath).Push(jobs, Message);
59+
var succ = new Commands.Stash(_repo.FullPath).Push(jobs, Message, _onlyStaged);
6060
CallUIThread(() =>
6161
{
6262
_repo.MarkWorkingCopyDirtyManually();
6363
_repo.SetWatcherEnabled(true);
6464
});
65-
return true;
65+
return succ;
6666
});
6767
}
6868

6969
private readonly Repository _repo = null;
7070
private readonly List<Models.Change> _changes = null;
71+
private readonly bool _onlyStaged = false;
7172
}
7273
}

src/ViewModels/WorkingCopy.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ public void StashAll(bool autoStart)
318318
return;
319319

320320
if (autoStart)
321-
PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, true));
321+
PopupHost.ShowAndStartPopup(new StashChanges(_repo, _cached, false, true));
322322
else
323-
PopupHost.ShowPopup(new StashChanges(_repo, _cached, true));
323+
PopupHost.ShowPopup(new StashChanges(_repo, _cached, false, true));
324324
}
325325

326326
public void StageSelected(Models.Change next)
@@ -524,7 +524,7 @@ public ContextMenu CreateContextMenuForUnstagedChanges()
524524
{
525525
if (PopupHost.CanCreatePopup())
526526
{
527-
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false));
527+
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false));
528528
}
529529
e.Handled = true;
530530
};
@@ -843,7 +843,7 @@ public ContextMenu CreateContextMenuForUnstagedChanges()
843843
stash.Click += (_, e) =>
844844
{
845845
if (PopupHost.CanCreatePopup())
846-
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false));
846+
PopupHost.ShowPopup(new StashChanges(_repo, _selectedUnstaged, false, false));
847847

848848
e.Handled = true;
849849
};
@@ -928,7 +928,7 @@ public ContextMenu CreateContextMenuForStagedChanges()
928928
stash.Click += (_, e) =>
929929
{
930930
if (PopupHost.CanCreatePopup())
931-
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false));
931+
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false));
932932

933933
e.Handled = true;
934934
};
@@ -1097,7 +1097,7 @@ public ContextMenu CreateContextMenuForStagedChanges()
10971097
stash.Click += (_, e) =>
10981098
{
10991099
if (PopupHost.CanCreatePopup())
1100-
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, false));
1100+
PopupHost.ShowPopup(new StashChanges(_repo, _selectedStaged, true, false));
11011101

11021102
e.Handled = true;
11031103
};
@@ -1202,20 +1202,16 @@ public ContextMenu CreateContextMenuForCommitMessages()
12021202
private List<Models.Change> GetStagedChanges()
12031203
{
12041204
if (_useAmend)
1205-
{
12061205
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).Result();
1207-
}
1208-
else
1206+
1207+
var rs = new List<Models.Change>();
1208+
foreach (var c in _cached)
12091209
{
1210-
var rs = new List<Models.Change>();
1211-
foreach (var c in _cached)
1212-
{
1213-
if (c.Index != Models.ChangeState.None &&
1214-
c.Index != Models.ChangeState.Untracked)
1215-
rs.Add(c);
1216-
}
1217-
return rs;
1210+
if (c.Index != Models.ChangeState.None &&
1211+
c.Index != Models.ChangeState.Untracked)
1212+
rs.Add(c);
12181213
}
1214+
return rs;
12191215
}
12201216

12211217
private void SetDetail(Models.Change change, bool isUnstaged)

0 commit comments

Comments
 (0)