-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathInteractiveRebase.cs
More file actions
438 lines (374 loc) · 14.1 KB
/
InteractiveRebase.cs
File metadata and controls
438 lines (374 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public record InteractiveRebasePrefill(string SHA, Models.InteractiveRebaseAction Action);
public class InteractiveRebaseItem : ObservableObject
{
public int OriginalOrder
{
get;
}
public Models.Commit Commit
{
get;
}
public Models.InteractiveRebaseAction Action
{
get => _action;
set => SetProperty(ref _action, value);
}
public Models.InteractiveRebasePendingType PendingType
{
get => _pendingType;
set => SetProperty(ref _pendingType, value);
}
public string Subject
{
get => _subject;
private set => SetProperty(ref _subject, value);
}
public string FullMessage
{
get => _fullMessage;
set
{
if (SetProperty(ref _fullMessage, value))
{
var normalized = value.ReplaceLineEndings("\n");
var parts = normalized.Split("\n\n", 2);
Subject = parts[0].ReplaceLineEndings(" ");
}
}
}
public string OriginalFullMessage
{
get;
set;
}
public bool CanSquashOrFixup
{
get => _canSquashOrFixup;
set => SetProperty(ref _canSquashOrFixup, value);
}
public bool ShowEditMessageButton
{
get => _showEditMessageButton;
set => SetProperty(ref _showEditMessageButton, value);
}
public Thickness DropDirectionIndicator
{
get => _dropDirectionIndicator;
set => SetProperty(ref _dropDirectionIndicator, value);
}
public bool IsMessageUserEdited
{
get;
set;
} = false;
public InteractiveRebaseItem(int order, Models.Commit c, string message)
{
OriginalOrder = order;
Commit = c;
FullMessage = message;
OriginalFullMessage = message;
}
private Models.InteractiveRebaseAction _action = Models.InteractiveRebaseAction.Pick;
private Models.InteractiveRebasePendingType _pendingType = Models.InteractiveRebasePendingType.None;
private string _subject;
private string _fullMessage;
private bool _canSquashOrFixup = true;
private bool _showEditMessageButton = false;
private Thickness _dropDirectionIndicator = new Thickness(0);
}
public class InteractiveRebase : ObservableObject
{
public Models.Branch Current
{
get;
private set;
}
public Models.Commit On
{
get;
}
public bool AutoStash
{
get;
set;
} = true;
public AvaloniaList<Models.IssueTracker> IssueTrackers
{
get => _repo.IssueTrackers;
}
public string ConventionalTypesOverride
{
get => _repo.Settings.ConventionalTypesOverride;
}
public bool IsLoading
{
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public AvaloniaList<InteractiveRebaseItem> Items
{
get;
} = [];
public InteractiveRebaseItem PreSelected
{
get => _preSelected;
private set => SetProperty(ref _preSelected, value);
}
public object Detail
{
get => _detail;
private set => SetProperty(ref _detail, value);
}
public InteractiveRebase(Repository repo, Models.Commit on, InteractiveRebasePrefill prefill = null)
{
_repo = repo;
_commitDetail = new CommitDetail(repo, null);
Current = repo.CurrentBranch;
On = on;
IsLoading = true;
Task.Run(async () =>
{
var commits = await new Commands.QueryCommitsForInteractiveRebase(_repo.FullPath, on.SHA)
.GetResultAsync()
.ConfigureAwait(false);
var list = new List<InteractiveRebaseItem>();
for (var i = 0; i < commits.Count; i++)
{
var c = commits[i];
list.Add(new InteractiveRebaseItem(commits.Count - i, c.Commit, c.Message));
}
var selected = list.Count > 0 ? list[0] : null;
if (prefill != null)
{
var item = list.Find(x => x.Commit.SHA.Equals(prefill.SHA, StringComparison.Ordinal));
if (item != null)
{
item.Action = prefill.Action;
selected = item;
}
}
Dispatcher.UIThread.Post(() =>
{
Items.AddRange(list);
UpdateItems();
PreSelected = selected;
IsLoading = false;
});
});
}
public void SelectCommits(List<InteractiveRebaseItem> items)
{
if (items.Count == 0)
{
Detail = null;
}
else if (items.Count == 1)
{
_commitDetail.Commit = items[0].Commit;
Detail = _commitDetail;
}
else
{
Detail = new Models.Count(items.Count);
}
}
public void ChangeAction(List<InteractiveRebaseItem> selected, Models.InteractiveRebaseAction action)
{
if (action == Models.InteractiveRebaseAction.Squash || action == Models.InteractiveRebaseAction.Fixup)
{
foreach (var item in selected)
{
if (item.CanSquashOrFixup)
item.Action = action;
}
}
else
{
foreach (var item in selected)
item.Action = action;
}
UpdateItems();
}
public void Move(List<InteractiveRebaseItem> commits, int index)
{
var hashes = new HashSet<string>();
foreach (var c in commits)
hashes.Add(c.Commit.SHA);
var before = new List<InteractiveRebaseItem>();
var ordered = new List<InteractiveRebaseItem>();
var after = new List<InteractiveRebaseItem>();
for (int i = 0; i < index; i++)
{
var item = Items[i];
if (!hashes.Contains(item.Commit.SHA))
before.Add(item);
else
ordered.Add(item);
}
for (int i = index; i < Items.Count; i++)
{
var item = Items[i];
if (!hashes.Contains(item.Commit.SHA))
after.Add(item);
else
ordered.Add(item);
}
Items.Clear();
Items.AddRange(before);
Items.AddRange(ordered);
Items.AddRange(after);
UpdateItems();
}
public async Task<bool> Start()
{
using var lockWatcher = _repo.LockWatcher();
var saveFile = Path.Combine(_repo.GitDir, "sourcegit.interactive_rebase");
var collection = new Models.InteractiveRebaseJobCollection();
collection.OrigHead = _repo.CurrentBranch.Head;
collection.Onto = On.SHA;
InteractiveRebaseItem pending = null;
for (int i = Items.Count - 1; i >= 0; i--)
{
var item = Items[i];
var job = new Models.InteractiveRebaseJob()
{
SHA = item.Commit.SHA,
Action = item.Action,
};
if (pending != null && item.PendingType != Models.InteractiveRebasePendingType.Ignore)
job.Message = pending.FullMessage;
else
job.Message = item.FullMessage;
collection.Jobs.Add(job);
if (item.PendingType == Models.InteractiveRebasePendingType.Last)
pending = null;
else if (item.PendingType == Models.InteractiveRebasePendingType.Target)
pending = item;
}
await using (var stream = File.Create(saveFile))
{
await JsonSerializer.SerializeAsync(stream, collection, JsonCodeGen.Default.InteractiveRebaseJobCollection);
}
var log = _repo.CreateLog("Interactive Rebase");
var succ = await new Commands.InteractiveRebase(_repo.FullPath, On.SHA, AutoStash)
.Use(log)
.ExecAsync();
log.Complete();
return succ;
}
private void UpdateItems()
{
if (Items.Count == 0)
return;
var hasValidParent = false;
for (var i = Items.Count - 1; i >= 0; i--)
{
var item = Items[i];
if (hasValidParent)
{
item.CanSquashOrFixup = true;
}
else
{
item.CanSquashOrFixup = false;
if (item.Action == Models.InteractiveRebaseAction.Squash || item.Action == Models.InteractiveRebaseAction.Fixup)
item.Action = Models.InteractiveRebaseAction.Pick;
hasValidParent = item.Action != Models.InteractiveRebaseAction.Drop;
}
}
var hasPending = false;
var pendingMessages = new List<string>();
for (var i = 0; i < Items.Count; i++)
{
var item = Items[i];
if (item.Action == Models.InteractiveRebaseAction.Drop)
{
item.ShowEditMessageButton = false;
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Ignore : Models.InteractiveRebasePendingType.None;
item.FullMessage = item.OriginalFullMessage;
item.IsMessageUserEdited = false;
continue;
}
if (item.Action == Models.InteractiveRebaseAction.Fixup ||
item.Action == Models.InteractiveRebaseAction.Squash)
{
item.ShowEditMessageButton = false;
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Pending : Models.InteractiveRebasePendingType.Last;
item.FullMessage = item.OriginalFullMessage;
item.IsMessageUserEdited = false;
if (item.Action == Models.InteractiveRebaseAction.Squash)
pendingMessages.Add(item.OriginalFullMessage);
hasPending = true;
continue;
}
if (item.Action == Models.InteractiveRebaseAction.Reword ||
item.Action == Models.InteractiveRebaseAction.Edit)
{
var oldPendingType = item.PendingType;
item.ShowEditMessageButton = true;
item.PendingType = hasPending ? Models.InteractiveRebasePendingType.Target : Models.InteractiveRebasePendingType.None;
if (hasPending)
{
if (!item.IsMessageUserEdited)
{
var builder = new StringBuilder();
builder.Append(item.OriginalFullMessage);
for (var j = pendingMessages.Count - 1; j >= 0; j--)
builder.Append("\n").Append(pendingMessages[j]);
item.FullMessage = builder.ToString();
}
hasPending = false;
pendingMessages.Clear();
}
else if (oldPendingType == Models.InteractiveRebasePendingType.Target)
{
if (!item.IsMessageUserEdited)
item.FullMessage = item.OriginalFullMessage;
}
continue;
}
if (item.Action == Models.InteractiveRebaseAction.Pick)
{
item.IsMessageUserEdited = false;
if (hasPending)
{
var builder = new StringBuilder();
builder.Append(item.OriginalFullMessage);
for (var j = pendingMessages.Count - 1; j >= 0; j--)
builder.Append("\n").Append(pendingMessages[j]);
item.Action = Models.InteractiveRebaseAction.Reword;
item.PendingType = Models.InteractiveRebasePendingType.Target;
item.ShowEditMessageButton = true;
item.FullMessage = builder.ToString();
hasPending = false;
pendingMessages.Clear();
}
else
{
item.PendingType = Models.InteractiveRebasePendingType.None;
item.ShowEditMessageButton = false;
item.FullMessage = item.OriginalFullMessage;
}
}
}
}
private Repository _repo = null;
private bool _isLoading = false;
private InteractiveRebaseItem _preSelected = null;
private object _detail = null;
private CommitDetail _commitDetail = null;
}
}