Skip to content

Commit 446c806

Browse files
committed
refactor: use a single command to get staged files with --amend option enabled
Signed-off-by: leo <longshuang@msn.cn>
1 parent 917c317 commit 446c806

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/Commands/QueryStagedChangesWithAmend.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@ public partial class QueryStagedChangesWithAmend : Command
1111
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{4,64}) [0-9a-f]{4,64} ([RC])\d{0,6}\t(.*\t.*)$")]
1212
private static partial Regex REG_FORMAT2();
1313

14-
public QueryStagedChangesWithAmend(string repo, string parent)
14+
public QueryStagedChangesWithAmend(string repo)
1515
{
1616
WorkingDirectory = repo;
1717
Context = repo;
18-
Args = $"diff-index --cached -M {parent}";
19-
_parent = parent;
2018
}
2119

2220
public List<Models.Change> GetResult()
2321
{
22+
Args = "show --no-show-signature --format=\"%H %P\" -s HEAD";
2423
var rs = ReadToEnd();
2524
if (!rs.IsSuccess)
2625
return [];
2726

27+
var shas = rs.StdOut.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
28+
if (shas.Length == 0)
29+
return [];
30+
31+
var parent = shas.Length > 1 ? shas[1] : Models.EmptyTreeHash.Guess(shas[0]);
32+
Args = $"diff-index --cached -M {parent}";
33+
rs = ReadToEnd();
34+
if (!rs.IsSuccess)
35+
return [];
36+
2837
var changes = new List<Models.Change>();
2938
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
3039
foreach (var line in lines)
@@ -39,7 +48,7 @@ public QueryStagedChangesWithAmend(string repo, string parent)
3948
{
4049
FileMode = match.Groups[1].Value,
4150
ObjectHash = match.Groups[2].Value,
42-
ParentSHA = _parent,
51+
ParentSHA = parent,
4352
},
4453
};
4554
var type = match.Groups[3].Value;
@@ -58,7 +67,7 @@ public QueryStagedChangesWithAmend(string repo, string parent)
5867
{
5968
FileMode = match.Groups[1].Value,
6069
ObjectHash = match.Groups[2].Value,
61-
ParentSHA = _parent,
70+
ParentSHA = parent,
6271
},
6372
};
6473

@@ -84,7 +93,5 @@ public QueryStagedChangesWithAmend(string repo, string parent)
8493

8594
return changes;
8695
}
87-
88-
private readonly string _parent;
8996
}
9097
}

src/ViewModels/WorkingCopy.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,7 @@ public async Task CommitAsync(bool autoStage, bool autoPush)
741741
private List<Models.Change> GetStagedChanges(List<Models.Change> cached)
742742
{
743743
if (_useAmend)
744-
{
745-
var head = new Commands.QuerySingleCommit(_repo.FullPath, "HEAD").GetResult();
746-
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath, head.FirstParentToCompare).GetResult();
747-
}
744+
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).GetResult();
748745

749746
var rs = new List<Models.Change>();
750747
foreach (var c in cached)

0 commit comments

Comments
 (0)