forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveRebase.cs
More file actions
255 lines (222 loc) · 7.22 KB
/
InteractiveRebase.cs
File metadata and controls
255 lines (222 loc) · 7.22 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
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class InteractiveRebaseItem : ObservableObject
{
public Models.Commit Commit
{
get;
private set;
}
public bool CanSquashOrFixup
{
get => _canSquashOrFixup;
set
{
if (SetProperty(ref _canSquashOrFixup, value))
{
if (_action == Models.InteractiveRebaseAction.Squash || _action == Models.InteractiveRebaseAction.Fixup)
Action = Models.InteractiveRebaseAction.Pick;
}
}
}
public Models.InteractiveRebaseAction Action
{
get => _action;
set => SetProperty(ref _action, 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 InteractiveRebaseItem(Models.Commit c, string message, bool canSquashOrFixup)
{
Commit = c;
FullMessage = message;
CanSquashOrFixup = canSquashOrFixup;
}
private Models.InteractiveRebaseAction _action = Models.InteractiveRebaseAction.Pick;
private string _subject;
private string _fullMessage;
private bool _canSquashOrFixup = true;
}
public class InteractiveRebase : ObservableObject
{
public Models.Branch Current
{
get;
private set;
}
public Models.Commit On
{
get;
private set;
}
public bool AutoStash
{
get;
set;
} = true;
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
{
get => _repo.Settings.IssueTrackerRules;
}
public bool IsLoading
{
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public AvaloniaList<InteractiveRebaseItem> Items
{
get;
private set;
} = [];
public InteractiveRebaseItem SelectedItem
{
get => _selectedItem;
set
{
if (SetProperty(ref _selectedItem, value))
DetailContext.Commit = value?.Commit;
}
}
public CommitDetail DetailContext
{
get;
private set;
}
public InteractiveRebase(Repository repo, Models.Branch current, Models.Commit on)
{
var repoPath = repo.FullPath;
_repo = repo;
Current = current;
On = on;
IsLoading = true;
DetailContext = new CommitDetail(repo, false);
Task.Run(async () =>
{
var commits = await new Commands.QueryCommitsForInteractiveRebase(repoPath, 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(c.Commit, c.Message, i < commits.Count - 1));
}
Dispatcher.UIThread.Post(() =>
{
Items.AddRange(list);
if (list.Count > 0)
SelectedItem = list[0];
IsLoading = false;
});
});
}
public void MoveItemUp(InteractiveRebaseItem item)
{
var idx = Items.IndexOf(item);
if (idx > 0)
{
var prev = Items[idx - 1];
Items.RemoveAt(idx - 1);
Items.Insert(idx, prev);
SelectedItem = item;
UpdateItems();
}
}
public void MoveItemDown(InteractiveRebaseItem item)
{
var idx = Items.IndexOf(item);
if (idx < Items.Count - 1)
{
var next = Items[idx + 1];
Items.RemoveAt(idx + 1);
Items.Insert(idx, next);
SelectedItem = item;
UpdateItems();
}
}
public void ChangeAction(InteractiveRebaseItem item, Models.InteractiveRebaseAction action)
{
if (!item.CanSquashOrFixup)
{
if (action == Models.InteractiveRebaseAction.Squash || action == Models.InteractiveRebaseAction.Fixup)
return;
}
item.Action = action;
UpdateItems();
}
public async Task<bool> Start()
{
_repo.SetWatcherEnabled(false);
var saveFile = Path.Combine(_repo.GitDir, "sourcegit_rebase_jobs.json");
var collection = new Models.InteractiveRebaseJobCollection();
collection.OrigHead = _repo.CurrentBranch.Head;
collection.Onto = On.SHA;
for (int i = Items.Count - 1; i >= 0; i--)
{
var item = Items[i];
collection.Jobs.Add(new Models.InteractiveRebaseJob()
{
SHA = item.Commit.SHA,
Action = item.Action,
Message = item.FullMessage,
});
}
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();
_repo.SetWatcherEnabled(true);
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;
hasValidParent = item.Action != Models.InteractiveRebaseAction.Drop;
}
}
}
private Repository _repo = null;
private bool _isLoading = false;
private InteractiveRebaseItem _selectedItem = null;
}
}