-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathRevisionCompare.cs
254 lines (221 loc) · 7.82 KB
/
RevisionCompare.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class RevisionCompare : ObservableObject
{
public object StartPoint
{
get => _startPoint;
private set => SetProperty(ref _startPoint, value);
}
public object EndPoint
{
get => _endPoint;
private set => SetProperty(ref _endPoint, value);
}
public bool CanSaveAsPatch
{
get => _canSaveAsPatch;
}
public List<Models.Change> VisibleChanges
{
get => _visibleChanges;
private set => SetProperty(ref _visibleChanges, value);
}
public List<Models.Change> SelectedChanges
{
get => _selectedChanges;
set
{
if (SetProperty(ref _selectedChanges, value))
{
if (value != null && value.Count == 1)
{
var option = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), value[0]);
DiffContext = new DiffContext(_repo, option, _diffContext);
}
else
{
DiffContext = null;
}
}
}
}
public string SearchFilter
{
get => _searchFilter;
set
{
if (SetProperty(ref _searchFilter, value))
{
RefreshVisible();
}
}
}
public DiffContext DiffContext
{
get => _diffContext;
private set => SetProperty(ref _diffContext, value);
}
public RevisionCompare(string repo, Models.Commit startPoint, Models.Commit endPoint)
{
_repo = repo;
_startPoint = (object)startPoint ?? new Models.Null();
_endPoint = (object)endPoint ?? new Models.Null();
_canSaveAsPatch = startPoint != null && endPoint != null;
Task.Run(Refresh);
}
public void Cleanup()
{
_repo = null;
_startPoint = null;
_endPoint = null;
if (_changes != null)
_changes.Clear();
if (_visibleChanges != null)
_visibleChanges.Clear();
if (_selectedChanges != null)
_selectedChanges.Clear();
_searchFilter = null;
_diffContext = null;
}
public void NavigateTo(string commitSHA)
{
var launcher = App.GetLauncer();
if (launcher == null)
return;
foreach (var page in launcher.Pages)
{
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{
repo.NavigateToCommit(commitSHA);
break;
}
}
}
public void Swap()
{
(StartPoint, EndPoint) = (_endPoint, _startPoint);
SelectedChanges = [];
Task.Run(Refresh);
}
public void SaveAsPatch(string saveTo)
{
Task.Run(() =>
{
var succ = Commands.SaveChangesAsPatch.ProcessRevisionCompareChanges(_repo, _changes, GetSHA(_startPoint), GetSHA(_endPoint), saveTo);
if (succ)
Dispatcher.UIThread.Invoke(() => App.SendNotification(_repo, App.Text("SaveAsPatchSuccess")));
});
}
public void ClearSearchFilter()
{
SearchFilter = string.Empty;
}
public ContextMenu CreateChangeContextMenu()
{
if (_selectedChanges == null || _selectedChanges.Count != 1)
return null;
var change = _selectedChanges[0];
var menu = new ContextMenu();
var diffWithMerger = new MenuItem();
diffWithMerger.Header = App.Text("DiffWithMerger");
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
diffWithMerger.Click += (_, ev) =>
{
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);
var toolType = Preferences.Instance.ExternalMergeToolType;
var toolPath = Preferences.Instance.ExternalMergeToolPath;
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, toolType, toolPath, opt));
ev.Handled = true;
};
menu.Items.Add(diffWithMerger);
if (change.Index != Models.ChangeState.Deleted)
{
var full = Path.GetFullPath(Path.Combine(_repo, change.Path));
var explore = new MenuItem();
explore.Header = App.Text("RevealFile");
explore.Icon = App.CreateMenuIcon("Icons.Explore");
explore.IsEnabled = File.Exists(full);
explore.Click += (_, ev) =>
{
Native.OS.OpenInFileManager(full, true);
ev.Handled = true;
};
menu.Items.Add(explore);
}
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;
};
menu.Items.Add(copyPath);
var copyFullPath = new MenuItem();
copyFullPath.Header = App.Text("CopyFullPath");
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyFullPath.Click += (_, e) =>
{
App.CopyText(Native.OS.GetAbsPath(_repo, change.Path));
e.Handled = true;
};
menu.Items.Add(copyFullPath);
return menu;
}
private void RefreshVisible()
{
if (_changes == null)
return;
if (string.IsNullOrEmpty(_searchFilter))
{
VisibleChanges = _changes;
}
else
{
var visible = new List<Models.Change>();
foreach (var c in _changes)
{
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(c);
}
VisibleChanges = visible;
}
}
private void Refresh()
{
_changes = new Commands.CompareRevisions(_repo, GetSHA(_startPoint), GetSHA(_endPoint)).Result();
var visible = _changes;
if (!string.IsNullOrWhiteSpace(_searchFilter))
{
visible = [];
foreach (var c in _changes)
{
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(c);
}
}
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
}
private string GetSHA(object obj)
{
return obj is Models.Commit commit ? commit.SHA : string.Empty;
}
private string _repo;
private object _startPoint = null;
private object _endPoint = null;
private bool _canSaveAsPatch = false;
private List<Models.Change> _changes = null;
private List<Models.Change> _visibleChanges = null;
private List<Models.Change> _selectedChanges = null;
private string _searchFilter = string.Empty;
private DiffContext _diffContext = null;
}
}