-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathStashesPage.cs
316 lines (276 loc) · 11.1 KB
/
StashesPage.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class StashesPage : ObservableObject
{
public List<Models.Stash> Stashes
{
get => _stashes;
set
{
if (SetProperty(ref _stashes, value))
RefreshVisible();
}
}
public List<Models.Stash> VisibleStashes
{
get => _visibleStashes;
private set
{
if (SetProperty(ref _visibleStashes, value))
SelectedStash = null;
}
}
public string SearchFilter
{
get => _searchFilter;
set
{
if (SetProperty(ref _searchFilter, value))
RefreshVisible();
}
}
public Models.Stash SelectedStash
{
get => _selectedStash;
set
{
if (SetProperty(ref _selectedStash, value))
{
if (value == null)
{
Changes = null;
}
else
{
Task.Run(() =>
{
var changes = null as List<Models.Change>;
if (Native.OS.GitVersion >= Models.GitVersions.STASH_SHOW_WITH_UNTRACKED)
{
changes = new Commands.QueryStashChanges(_repo.FullPath, value.Name).Result();
}
else
{
changes = new Commands.CompareRevisions(_repo.FullPath, $"{value.SHA}^", value.SHA).Result();
if (value.Parents.Count == 3)
{
var untracked = new Commands.CompareRevisions(_repo.FullPath, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", value.Parents[2]).Result();
var needSort = changes.Count > 0;
foreach (var c in untracked)
changes.Add(c);
if (needSort)
changes.Sort((l, r) => string.Compare(l.Path, r.Path, StringComparison.Ordinal));
}
}
Dispatcher.UIThread.Invoke(() => Changes = changes);
});
}
}
}
}
public List<Models.Change> Changes
{
get => _changes;
private set
{
if (SetProperty(ref _changes, value))
SelectedChange = value is { Count: > 0 } ? value[0] : null;
}
}
public Models.Change SelectedChange
{
get => _selectedChange;
set
{
if (SetProperty(ref _selectedChange, value))
{
if (value == null)
DiffContext = null;
else if (value.Index == Models.ChangeState.Added && _selectedStash.Parents.Count == 3)
DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption("4b825dc642cb6eb9a060e54bf8d69288fbee4904", _selectedStash.Parents[2], value), _diffContext);
else
DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, value), _diffContext);
}
}
}
public DiffContext DiffContext
{
get => _diffContext;
private set => SetProperty(ref _diffContext, value);
}
public StashesPage(Repository repo)
{
_repo = repo;
}
public void Cleanup()
{
_repo = null;
if (_stashes != null)
_stashes.Clear();
_selectedStash = null;
if (_changes != null)
_changes.Clear();
_selectedChange = null;
_diffContext = null;
}
public ContextMenu MakeContextMenu(Models.Stash stash)
{
if (stash == null)
return null;
var apply = new MenuItem();
apply.Header = App.Text("StashCM.Apply");
apply.Click += (_, ev) =>
{
if (_repo.CanCreatePopup())
_repo.ShowPopup(new ApplyStash(_repo, stash));
ev.Handled = true;
};
var drop = new MenuItem();
drop.Header = App.Text("StashCM.Drop");
drop.Click += (_, ev) =>
{
if (_repo.CanCreatePopup())
_repo.ShowPopup(new DropStash(_repo, stash));
ev.Handled = true;
};
var patch = new MenuItem();
patch.Header = App.Text("StashCM.SaveAsPatch");
patch.Icon = App.CreateMenuIcon("Icons.Diff");
patch.Click += async (_, e) =>
{
var storageProvider = App.GetStorageProvider();
if (storageProvider == null)
return;
var options = new FilePickerSaveOptions();
options.Title = App.Text("StashCM.SaveAsPatch");
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var opts = new List<Models.DiffOption>();
foreach (var c in _changes)
{
if (c.Index == Models.ChangeState.Added && _selectedStash.Parents.Count == 3)
opts.Add(new Models.DiffOption("4b825dc642cb6eb9a060e54bf8d69288fbee4904", _selectedStash.Parents[2], c));
else
opts.Add(new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, c));
}
var succ = await Task.Run(() => Commands.SaveChangesAsPatch.ProcessStashChanges(_repo.FullPath, opts, storageFile.Path.LocalPath));
if (succ)
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
}
e.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(apply);
menu.Items.Add(drop);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(patch);
return menu;
}
public ContextMenu MakeContextMenuForChange(Models.Change change)
{
if (change == null)
return null;
var diffWithMerger = new MenuItem();
diffWithMerger.Header = App.Text("DiffWithMerger");
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
diffWithMerger.Click += (_, ev) =>
{
var toolType = Preferences.Instance.ExternalMergeToolType;
var toolPath = Preferences.Instance.ExternalMergeToolPath;
var opt = new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, change);
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo.FullPath, toolType, toolPath, opt));
ev.Handled = true;
};
var fullPath = Path.Combine(_repo.FullPath, change.Path);
var explore = new MenuItem();
explore.Header = App.Text("RevealFile");
explore.Icon = App.CreateMenuIcon("Icons.Explore");
explore.IsEnabled = File.Exists(fullPath);
explore.Click += (_, ev) =>
{
Native.OS.OpenInFileManager(fullPath, true);
ev.Handled = true;
};
var resetToThisRevision = new MenuItem();
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
resetToThisRevision.Click += (_, ev) =>
{
var log = _repo.CreateLog($"Reset File to '{_selectedStash.SHA}'");
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(change.Path, $"{_selectedStash.SHA}");
log.Complete();
ev.Handled = true;
};
var copyPath = new MenuItem();
copyPath.Header = App.Text("CopyPath");
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyPath.Click += (_, ev) =>
{
App.CopyText(change.Path);
ev.Handled = true;
};
var copyFullPath = new MenuItem();
copyFullPath.Header = App.Text("CopyFullPath");
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyFullPath.Click += (_, e) =>
{
App.CopyText(Native.OS.GetAbsPath(_repo.FullPath, change.Path));
e.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(diffWithMerger);
menu.Items.Add(explore);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(resetToThisRevision);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(copyPath);
menu.Items.Add(copyFullPath);
return menu;
}
public void Clear()
{
if (_repo.CanCreatePopup())
_repo.ShowPopup(new ClearStashes(_repo));
}
public void ClearSearchFilter()
{
SearchFilter = string.Empty;
}
private void RefreshVisible()
{
if (string.IsNullOrEmpty(_searchFilter))
{
VisibleStashes = _stashes;
}
else
{
var visible = new List<Models.Stash>();
foreach (var s in _stashes)
{
if (s.Message.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(s);
}
VisibleStashes = visible;
}
}
private Repository _repo = null;
private List<Models.Stash> _stashes = new List<Models.Stash>();
private List<Models.Stash> _visibleStashes = new List<Models.Stash>();
private string _searchFilter = string.Empty;
private Models.Stash _selectedStash = null;
private List<Models.Change> _changes = null;
private Models.Change _selectedChange = null;
private DiffContext _diffContext = null;
}
}