Skip to content

Commit e6ba053

Browse files
committed
feature: reorder fixup commits to its right position when loading commits for interactive rebase (#588)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 68a409c commit e6ba053

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Commands/QueryCommitsForInteractiveRebase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public QueryCommitsForInteractiveRebase(string repo, string on)
1212

1313
WorkingDirectory = repo;
1414
Context = repo;
15-
Args = $"log --topo-order --cherry-pick --right-only --no-merges --no-show-signature --decorate=full --format=\"%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%B%n{_boundary}\" {on}...HEAD";
15+
Args = $"log --topo-order --cherry-pick --right-only --no-merges --no-show-signature --decorate=full --format=\"%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%s%n%B%n{_boundary}\" {on}...HEAD";
1616
}
1717

1818
public async Task<List<Models.InteractiveCommit>> GetResultAsync()
@@ -58,6 +58,9 @@ public QueryCommitsForInteractiveRebase(string repo, string on)
5858
case 6:
5959
current.Commit.CommitterTime = ulong.Parse(line);
6060
break;
61+
case 7:
62+
current.Commit.Subject = line;
63+
break;
6164
default:
6265
var boundary = rs.StdOut.IndexOf(_boundary, end + 1, StringComparison.Ordinal);
6366
if (boundary > end)

src/ViewModels/InteractiveRebase.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,41 @@ public InteractiveRebase(Repository repo, Models.Commit on, InteractiveRebasePre
172172
.ConfigureAwait(false);
173173

174174
var list = new List<InteractiveRebaseItem>();
175+
var fixups = new List<InteractiveRebaseItem>();
175176
for (var i = 0; i < commits.Count; i++)
176177
{
177178
var c = commits[i];
178-
list.Add(new InteractiveRebaseItem(commits.Count - i, c.Commit, c.Message));
179+
var item = new InteractiveRebaseItem(commits.Count - i, c.Commit, c.Message);
180+
if (c.Commit.Subject.StartsWith("fixup! ", StringComparison.Ordinal) && item.OriginalOrder > 1)
181+
{
182+
fixups.Add(item);
183+
continue;
184+
}
185+
186+
var reordered = new List<InteractiveRebaseItem>();
187+
foreach (var f in fixups)
188+
{
189+
if (c.Commit.Subject.StartsWith(f.Commit.Subject.Substring(7), StringComparison.Ordinal))
190+
{
191+
f.Action = Models.InteractiveRebaseAction.Fixup;
192+
reordered.Add(f);
193+
list.Add(f);
194+
}
195+
}
196+
fixups.RemoveAll(x => reordered.Contains(x));
197+
list.Add(item);
198+
}
199+
200+
foreach (var f in fixups)
201+
{
202+
for (var i = 0; i < list.Count; i++)
203+
{
204+
if (f.OriginalOrder > list[i].OriginalOrder)
205+
{
206+
list.Insert(i, f);
207+
break;
208+
}
209+
}
179210
}
180211

181212
var selected = list.Count > 0 ? list[0] : null;

0 commit comments

Comments
 (0)