forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitUIExtensions.cs
283 lines (245 loc) · 10.2 KB
/
GitUIExtensions.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GitCommands;
using GitCommands.Patches;
using GitUI.Editor;
using GitUI.UserControls;
using GitUI.UserControls.RevisionGrid;
using GitUIPluginInterfaces;
using ResourceManager;
namespace GitUI
{
public static class GitUIExtensions
{
/// <summary>
/// View the changes between the revisions, if possible as a diff.
/// </summary>
/// <param name="fileViewer">Current FileViewer.</param>
/// <param name="item">The FileStatusItem to present changes for.</param>
/// <param name="defaultText">default text if no diff is possible.</param>
/// <param name="openWithDiffTool">The difftool command to open with.</param>
/// <returns>Task to view.</returns>
public static async Task ViewChangesAsync(this FileViewer fileViewer,
FileStatusItem? item,
CancellationToken cancellationToken,
string defaultText = "",
Action? openWithDiffTool = null)
{
if (item?.Item.IsStatusOnly ?? false)
{
// Present error (e.g. parsing Git)
await fileViewer.ViewTextAsync(item.Item.Name, item.Item.ErrorMessage ?? "");
return;
}
if (item?.Item is null || item.SecondRevision?.ObjectId is null)
{
if (!string.IsNullOrWhiteSpace(defaultText))
{
await fileViewer.ViewTextAsync(item?.Item?.Name, defaultText);
return;
}
await fileViewer.ClearAsync();
return;
}
var firstId = item.FirstRevision?.ObjectId ?? item.SecondRevision.FirstParentId;
openWithDiffTool ??= OpenWithDiffTool;
if (item.Item.IsNew || firstId is null || (!item.Item.IsDeleted && FileHelper.IsImage(item.Item.Name)))
{
// View blob guid from revision, or file for worktree
await fileViewer.ViewGitItemAsync(item, openWithDiffTool);
return;
}
if (item.Item.IsRangeDiff)
{
// Git range-diff has cubic runtime complexity and can be slow and memory consuming,
// give an indication of what is going on
string range = item.BaseA is null || item.BaseB is null
? $"{firstId}...{item.SecondRevision.ObjectId}"
: $"{item.BaseA}..{firstId} {item.BaseB}..{item.SecondRevision.ObjectId}";
await fileViewer.ViewTextAsync("git-range-diff.sh", $"git range-diff {range}");
string output = await fileViewer.Module.GetRangeDiffAsync(
firstId,
item.SecondRevision.ObjectId,
item.BaseA,
item.BaseB,
fileViewer.GetExtraDiffArguments(isRangeDiff: true),
cancellationToken);
// Try set highlighting from first found filename
Match match = new Regex(@"\n\s*(@@|##)\s+(?<file>[^#:\n]+)").Match(output ?? "");
string filename = match.Groups["file"].Success ? match.Groups["file"].Value : item.Item.Name;
cancellationToken.ThrowIfCancellationRequested();
await fileViewer.ViewRangeDiffAsync(filename, output ?? defaultText);
return;
}
string selectedPatch = (await GetSelectedPatchAsync(fileViewer, firstId, item.SecondRevision.ObjectId, item.Item, cancellationToken))
?? defaultText;
cancellationToken.ThrowIfCancellationRequested();
if (item.Item.IsSubmodule)
{
await fileViewer.ViewTextAsync(item.Item.Name, text: selectedPatch, openWithDifftool: openWithDiffTool);
}
else
{
await fileViewer.ViewPatchAsync(item, text: selectedPatch, openWithDifftool: openWithDiffTool);
}
return;
void OpenWithDiffTool()
{
fileViewer.Module.OpenWithDifftool(
item.Item.Name,
item.Item.OldName,
firstId?.ToString(),
item.SecondRevision.ToString(),
isTracked: item.Item.IsTracked);
}
static async Task<string?> GetSelectedPatchAsync(
FileViewer fileViewer,
ObjectId firstId,
ObjectId selectedId,
GitItemStatus file,
CancellationToken cancellationToken)
{
if (firstId == ObjectId.CombinedDiffId)
{
var diffOfConflict = fileViewer.Module.GetCombinedDiffContent(selectedId, file.Name,
fileViewer.GetExtraDiffArguments(), fileViewer.Encoding);
cancellationToken.ThrowIfCancellationRequested();
return string.IsNullOrWhiteSpace(diffOfConflict)
? TranslatedStrings.UninterestingDiffOmitted
: diffOfConflict;
}
var task = file.GetSubmoduleStatusAsync();
if (file.IsSubmodule && task is not null)
{
// Patch already evaluated
var status = await task;
cancellationToken.ThrowIfCancellationRequested();
return status is not null
? LocalizationHelpers.ProcessSubmoduleStatus(fileViewer.Module, status)
: $"Failed to get status for submodule \"{file.Name}\"";
}
var patch = await GetItemPatchAsync(fileViewer.Module, file, firstId, selectedId,
fileViewer.GetExtraDiffArguments(), fileViewer.Encoding);
cancellationToken.ThrowIfCancellationRequested();
return file.IsSubmodule
? LocalizationHelpers.ProcessSubmodulePatch(fileViewer.Module, file.Name, patch)
: patch?.Text;
static async Task<Patch?> GetItemPatchAsync(
GitModule module,
GitItemStatus file,
ObjectId? firstId,
ObjectId? secondId,
string diffArgs,
Encoding encoding)
{
// Files with tree guid should be presented with normal diff
var isTracked = file.IsTracked || (file.TreeGuid is not null && secondId is not null);
return await module.GetSingleDiffAsync(firstId, secondId, file.Name, file.OldName, diffArgs, encoding, true, isTracked);
}
}
}
public static void RemoveIfExists(this TabControl tabControl, TabPage page)
{
if (tabControl.TabPages.Contains(page))
{
tabControl.TabPages.Remove(page);
}
}
public static void InsertIfNotExists(this TabControl tabControl, int index, TabPage page)
{
if (!tabControl.TabPages.Contains(page))
{
tabControl.TabPages.Insert(index, page);
}
}
public static void Mask(this Control control)
{
if (FindMaskPanel(control) is null)
{
LoadingControl panel = new()
{
Dock = DockStyle.Fill,
IsAnimating = true,
BackColor = SystemColors.AppWorkspace
};
control.Controls.Add(panel);
panel.BringToFront();
}
}
public static void UnMask(this Control control)
{
var panel = FindMaskPanel(control);
if (panel is not null)
{
control.Controls.Remove(panel);
panel.Dispose();
}
}
private static LoadingControl? FindMaskPanel(Control control)
{
return control.Controls.Cast<Control>().OfType<LoadingControl>().FirstOrDefault();
}
public static IEnumerable<TreeNode> AllNodes(this TreeView tree)
{
return tree.Nodes.AllNodes();
}
private static IEnumerable<TreeNode> AllNodes(this TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
yield return node;
foreach (TreeNode subNode in node.Nodes.AllNodes())
{
yield return subNode;
}
}
}
public static async Task InvokeAsync(this Control control, Action action, CancellationToken token = default)
{
await control.SwitchToMainThreadAsync(token);
action();
}
public static async Task InvokeAsync<T>(this Control control, Action<T> action, T state, CancellationToken token = default)
{
await control.SwitchToMainThreadAsync(token);
action(state);
}
public static void InvokeSync(this Control control, Action action)
{
ThreadHelper.JoinableTaskFactory.Run(
async () =>
{
try
{
await InvokeAsync(control, action);
}
catch (Exception e)
{
e.Data["StackTrace" + e.Data.Count] = e.StackTrace;
throw;
}
});
}
public static Control FindFocusedControl(this ContainerControl container)
{
while (true)
{
if (container.ActiveControl is ContainerControl activeContainer)
{
container = activeContainer;
}
else
{
return container.ActiveControl;
}
}
}
}
}