Skip to content

Commit e7f391f

Browse files
committed
refactor: now final commit message will use the squash/fixed target instead of the last picked
Signed-off-by: leo <[email protected]>
1 parent 609418f commit e7f391f

File tree

4 files changed

+175
-78
lines changed

4 files changed

+175
-78
lines changed

src/Models/InteractiveRebase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ public enum InteractiveRebaseAction
1212
Drop,
1313
}
1414

15+
public enum InteractiveRebasePendingType
16+
{
17+
None = 0,
18+
Target,
19+
Pending,
20+
Ignore,
21+
Last,
22+
}
23+
1524
public class InteractiveCommit
1625
{
1726
public Commit Commit { get; set; } = new Commit();

src/ViewModels/InteractiveRebase.cs

Lines changed: 115 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
45
using System.Text.Json;
56
using System.Threading.Tasks;
67

@@ -31,6 +32,12 @@ public Models.InteractiveRebaseAction Action
3132
set => SetProperty(ref _action, value);
3233
}
3334

35+
public Models.InteractiveRebasePendingType PendingType
36+
{
37+
get => _pendingType;
38+
set => SetProperty(ref _pendingType, value);
39+
}
40+
3441
public string Subject
3542
{
3643
get => _subject;
@@ -63,18 +70,18 @@ public bool CanSquashOrFixup
6370
set => SetProperty(ref _canSquashOrFixup, value);
6471
}
6572

66-
public bool CanReword
67-
{
68-
get => _canReword;
69-
set => SetProperty(ref _canReword, value);
70-
}
71-
7273
public bool ShowEditMessageButton
7374
{
7475
get => _showEditMessageButton;
7576
set => SetProperty(ref _showEditMessageButton, value);
7677
}
7778

79+
public bool IsFullMessageUsed
80+
{
81+
get => _isFullMessageUsed;
82+
set => SetProperty(ref _isFullMessageUsed, value);
83+
}
84+
7885
public bool IsDropBeforeVisible
7986
{
8087
get => _isDropBeforeVisible;
@@ -87,6 +94,12 @@ public bool IsDropAfterVisible
8794
set => SetProperty(ref _isDropAfterVisible, value);
8895
}
8996

97+
public bool IsMessageUserEdited
98+
{
99+
get;
100+
set;
101+
} = false;
102+
90103
public InteractiveRebaseItem(int order, Models.Commit c, string message, bool canSquashOrFixup)
91104
{
92105
OriginalOrder = order;
@@ -97,11 +110,12 @@ public InteractiveRebaseItem(int order, Models.Commit c, string message, bool ca
97110
}
98111

99112
private Models.InteractiveRebaseAction _action = Models.InteractiveRebaseAction.Pick;
113+
private Models.InteractiveRebasePendingType _pendingType = Models.InteractiveRebasePendingType.None;
100114
private string _subject;
101115
private string _fullMessage;
102116
private bool _canSquashOrFixup = true;
103-
private bool _canReword = true;
104117
private bool _showEditMessageButton = false;
118+
private bool _isFullMessageUsed = true;
105119
private bool _isDropBeforeVisible = false;
106120
private bool _isDropAfterVisible = false;
107121
}
@@ -278,16 +292,30 @@ public async Task<bool> Start()
278292
var collection = new Models.InteractiveRebaseJobCollection();
279293
collection.OrigHead = _repo.CurrentBranch.Head;
280294
collection.Onto = On.SHA;
295+
296+
InteractiveRebaseItem pending = null;
281297
for (int i = Items.Count - 1; i >= 0; i--)
282298
{
283299
var item = Items[i];
284-
collection.Jobs.Add(new Models.InteractiveRebaseJob()
300+
var job = new Models.InteractiveRebaseJob()
285301
{
286302
SHA = item.Commit.SHA,
287303
Action = item.Action,
288-
Message = item.FullMessage,
289-
});
304+
};
305+
306+
if (pending != null && item.PendingType != Models.InteractiveRebasePendingType.Ignore)
307+
job.Message = pending.FullMessage;
308+
else
309+
job.Message = item.FullMessage;
310+
311+
collection.Jobs.Add(job);
312+
313+
if (item.PendingType == Models.InteractiveRebasePendingType.Last)
314+
pending = null;
315+
else if (item.PendingType == Models.InteractiveRebasePendingType.Target)
316+
pending = item;
290317
}
318+
291319
await using (var stream = File.Create(saveFile))
292320
{
293321
await JsonSerializer.SerializeAsync(stream, collection, JsonCodeGen.Default.InteractiveRebaseJobCollection);
@@ -325,19 +353,89 @@ private void UpdateItems()
325353
}
326354
}
327355

328-
var hasPendingTarget = false;
356+
var hasPending = false;
357+
var pendingMessages = new List<string>();
329358
for (var i = 0; i < Items.Count; i++)
330359
{
331360
var item = Items[i];
332-
if (item.Action == Models.InteractiveRebaseAction.Pick || item.Action == Models.InteractiveRebaseAction.Edit)
361+
362+
if (item.Action == Models.InteractiveRebaseAction.Drop)
363+
{
364+
item.IsFullMessageUsed = false;
365+
item.ShowEditMessageButton = false;
366+
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Ignore : Models.InteractiveRebasePendingType.None;
367+
item.FullMessage = item.OriginalFullMessage;
368+
item.IsMessageUserEdited = false;
369+
continue;
370+
}
371+
372+
if (item.Action == Models.InteractiveRebaseAction.Fixup ||
373+
item.Action == Models.InteractiveRebaseAction.Squash)
374+
{
375+
item.IsFullMessageUsed = false;
376+
item.ShowEditMessageButton = false;
377+
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Pending : Models.InteractiveRebasePendingType.Last;
333378
item.FullMessage = item.OriginalFullMessage;
379+
item.IsMessageUserEdited = false;
380+
381+
pendingMessages.Add(item.OriginalFullMessage);
382+
hasPending = true;
383+
continue;
384+
}
385+
386+
if (item.Action == Models.InteractiveRebaseAction.Reword ||
387+
item.Action == Models.InteractiveRebaseAction.Edit)
388+
{
389+
item.IsFullMessageUsed = true;
390+
item.ShowEditMessageButton = true;
391+
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Target : Models.InteractiveRebasePendingType.None;
392+
393+
if (hasPending)
394+
{
395+
if (!item.IsMessageUserEdited)
396+
{
397+
var builder = new StringBuilder();
398+
builder.Append(item.OriginalFullMessage);
399+
for (var j = pendingMessages.Count - 1; j >= 0; j--)
400+
builder.Append("\n\n").Append(pendingMessages[j]);
401+
402+
item.FullMessage = builder.ToString();
403+
}
404+
405+
hasPending = false;
406+
pendingMessages.Clear();
407+
}
334408

335-
item.CanReword = !hasPendingTarget;
336-
item.ShowEditMessageButton = item.CanReword &&
337-
(item.Action == Models.InteractiveRebaseAction.Reword || item.Action == Models.InteractiveRebaseAction.Squash || item.Action == Models.InteractiveRebaseAction.Fixup);
409+
continue;
410+
}
411+
412+
if (item.Action == Models.InteractiveRebaseAction.Pick)
413+
{
414+
item.IsFullMessageUsed = true;
415+
item.IsMessageUserEdited = false;
338416

339-
if (item.Action != Models.InteractiveRebaseAction.Drop)
340-
hasPendingTarget = item.Action == Models.InteractiveRebaseAction.Squash || item.Action == Models.InteractiveRebaseAction.Fixup;
417+
if (hasPending)
418+
{
419+
var builder = new StringBuilder();
420+
builder.Append(item.OriginalFullMessage);
421+
for (var j = pendingMessages.Count - 1; j >= 0; j--)
422+
builder.Append("\n").Append(pendingMessages[j]);
423+
424+
item.Action = Models.InteractiveRebaseAction.Reword;
425+
item.PendingType = Models.InteractiveRebasePendingType.Target;
426+
item.ShowEditMessageButton = true;
427+
item.FullMessage = builder.ToString();
428+
429+
hasPending = false;
430+
pendingMessages.Clear();
431+
}
432+
else
433+
{
434+
item.PendingType = Models.InteractiveRebasePendingType.None;
435+
item.ShowEditMessageButton = false;
436+
item.FullMessage = item.OriginalFullMessage;
437+
}
438+
}
341439
}
342440
}
343441

src/Views/InteractiveRebase.axaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@
136136
<!-- Indicator -->
137137
<v:InteractiveRebaseIndicator Grid.Column="2"
138138
Fill="{DynamicResource Brush.FG1}"
139-
Action="{Binding Action, Mode=OneWay}"
140-
CanReword="{Binding CanReword, Mode=OneWay}"/>
139+
PendingType="{Binding PendingType, Mode=OneWay}"/>
141140

142141
<!-- Subject -->
143142
<Grid Grid.Column="3" ColumnDefinitions="Auto,*" ClipToBounds="True">
@@ -159,7 +158,7 @@
159158
Subject="{Binding Subject}"
160159
IssueTrackers="{Binding $parent[v:InteractiveRebase].((vm:InteractiveRebase)DataContext).IssueTrackers}"
161160
FontWeight="Normal"
162-
Opacity="{Binding CanReword, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
161+
Opacity="{Binding IsFullMessageUsed, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
163162
ShowStrikethrough="{Binding Action, Mode=OneWay, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:InteractiveRebaseAction.Drop}}"/>
164163
</Grid>
165164

@@ -169,26 +168,26 @@
169168
Margin="8,0,0,0"
170169
VerticalAlignment="Center"
171170
User="{Binding Commit.Author}"
172-
Opacity="{Binding CanReword, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
171+
Opacity="{Binding IsFullMessageUsed, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
173172

174173
<!-- Author Name -->
175174
<Border Grid.Column="5" ClipToBounds="True">
176175
<TextBlock Margin="6,0,12,0"
177176
Text="{Binding Commit.Author.Name}"
178-
Opacity="{Binding CanReword, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
177+
Opacity="{Binding IsFullMessageUsed, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
179178
</Border>
180179

181180
<!-- Commit SHA -->
182181
<Border Grid.Column="6" ClipToBounds="True">
183182
<TextBlock Text="{Binding Commit.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
184-
Opacity="{Binding CanReword, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
183+
Opacity="{Binding IsFullMessageUsed, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
185184
</Border>
186185

187186
<!-- Commit Time -->
188187
<Border Grid.Column="7">
189188
<TextBlock Margin="16,0,8,0"
190189
Text="{Binding Commit.CommitterTimeStr}"
191-
Opacity="{Binding CanReword, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
190+
Opacity="{Binding IsFullMessageUsed, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
192191
</Border>
193192

194193
<!-- Drop Indicator -->

0 commit comments

Comments
 (0)