From 446c806246d9f7c418d546dc39e4b205a645d95b Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 25 Mar 2026 14:59:47 +0800 Subject: [PATCH] refactor: use a single command to get staged files with `--amend` option enabled Signed-off-by: leo --- src/Commands/QueryStagedChangesWithAmend.cs | 21 ++++++++++++++------- src/ViewModels/WorkingCopy.cs | 5 +---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Commands/QueryStagedChangesWithAmend.cs b/src/Commands/QueryStagedChangesWithAmend.cs index 78109ce6..0cd42059 100644 --- a/src/Commands/QueryStagedChangesWithAmend.cs +++ b/src/Commands/QueryStagedChangesWithAmend.cs @@ -11,20 +11,29 @@ namespace SourceGit.Commands [GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{4,64}) [0-9a-f]{4,64} ([RC])\d{0,6}\t(.*\t.*)$")] private static partial Regex REG_FORMAT2(); - public QueryStagedChangesWithAmend(string repo, string parent) + public QueryStagedChangesWithAmend(string repo) { WorkingDirectory = repo; Context = repo; - Args = $"diff-index --cached -M {parent}"; - _parent = parent; } public List GetResult() { + Args = "show --no-show-signature --format=\"%H %P\" -s HEAD"; var rs = ReadToEnd(); if (!rs.IsSuccess) return []; + var shas = rs.StdOut.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (shas.Length == 0) + return []; + + var parent = shas.Length > 1 ? shas[1] : Models.EmptyTreeHash.Guess(shas[0]); + Args = $"diff-index --cached -M {parent}"; + rs = ReadToEnd(); + if (!rs.IsSuccess) + return []; + var changes = new List(); var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) @@ -39,7 +48,7 @@ namespace SourceGit.Commands { FileMode = match.Groups[1].Value, ObjectHash = match.Groups[2].Value, - ParentSHA = _parent, + ParentSHA = parent, }, }; var type = match.Groups[3].Value; @@ -58,7 +67,7 @@ namespace SourceGit.Commands { FileMode = match.Groups[1].Value, ObjectHash = match.Groups[2].Value, - ParentSHA = _parent, + ParentSHA = parent, }, }; @@ -84,7 +93,5 @@ namespace SourceGit.Commands return changes; } - - private readonly string _parent; } } diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index 215cdcb8..82155d0e 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -741,10 +741,7 @@ namespace SourceGit.ViewModels private List GetStagedChanges(List cached) { if (_useAmend) - { - var head = new Commands.QuerySingleCommit(_repo.FullPath, "HEAD").GetResult(); - return new Commands.QueryStagedChangesWithAmend(_repo.FullPath, head.FirstParentToCompare).GetResult(); - } + return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).GetResult(); var rs = new List(); foreach (var c in cached)