/
GitOperations.cs
413 lines (340 loc) · 15.3 KB
/
GitOperations.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
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks.SourceControl;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.Tasks.Git
{
internal static class GitOperations
{
private const string SourceControlName = "git";
private const string RemoteSectionName = "remote";
private const string UrlSectionName = "url";
public static string GetRepositoryUrl(GitRepository repository, Action<string, object[]> logWarning = null, string remoteName = null)
{
string unknownRemoteName = null;
string remoteUrl = null;
if (!string.IsNullOrEmpty(remoteName))
{
remoteUrl = repository.Config.GetVariableValue(RemoteSectionName, remoteName, "url");
if (remoteUrl == null)
{
unknownRemoteName = remoteName;
}
}
if (remoteUrl == null && !TryGetRemote(repository.Config, out remoteName, out remoteUrl))
{
logWarning?.Invoke(Resources.RepositoryHasNoRemote, Array.Empty<string>());
return null;
}
if (unknownRemoteName != null)
{
logWarning?.Invoke(Resources.RepositoryDoesNotHaveSpecifiedRemote, new[] { unknownRemoteName, remoteName });
}
var url = NormalizeUrl(repository.Config, remoteUrl, repository.WorkingDirectory);
if (url == null)
{
logWarning?.Invoke(Resources.InvalidRepositoryRemoteUrl, new[] { remoteName, remoteUrl });
}
return url;
}
private static bool TryGetRemote(GitConfig config, out string remoteName, out string remoteUrl)
{
remoteName = "origin";
remoteUrl = config.GetVariableValue(RemoteSectionName, remoteName, "url");
if (remoteUrl != null)
{
return true;
}
var remoteVariable = config.Variables.
Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName)).
OrderBy(kvp => kvp.Key.SubsectionName, GitVariableName.SubsectionNameComparer).
FirstOrDefault();
remoteName = remoteVariable.Key.SubsectionName;
if (remoteName == null)
{
return false;
}
remoteUrl = remoteVariable.Value.Last();
return true;
}
internal static string ApplyInsteadOfUrlMapping(GitConfig config, string url)
{
// See https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf
// Notes:
// - URL prefix matching is case sensitive.
// - if the replacement is empty the URL is prefixed with the replacement string
int longestPrefixLength = -1;
string replacement = null;
foreach (var variable in config.Variables)
{
if (variable.Key.SectionNameEquals(UrlSectionName) &&
variable.Key.VariableNameEquals("insteadOf"))
{
foreach (var prefix in variable.Value)
{
if (prefix.Length > longestPrefixLength && url.StartsWith(prefix, StringComparison.Ordinal))
{
longestPrefixLength = prefix.Length;
replacement = variable.Key.SubsectionName;
}
}
}
}
return (longestPrefixLength >= 0) ? replacement + url.Substring(longestPrefixLength) : url;
}
internal static string NormalizeUrl(GitConfig config, string url, string root)
=> NormalizeUrl(ApplyInsteadOfUrlMapping(config, url), root);
internal static string NormalizeUrl(string url, string root)
{
// Since git supports scp-like syntax for SSH URLs we convert it here,
// so that RepositoryUrl is actually a valid URL in that case.
// See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols and
// https://github.com/libgit2/libgit2/blob/master/src/transport.c#L72.
// Windows device path "X:"
if (url.Length == 2 && IsWindowsAbsoluteOrDriveRelativePath(url))
{
return "file:///" + url + "/";
}
if (TryParseScp(url, out var uri))
{
return uri.ToString();
}
if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri))
{
return null;
}
if (uri.IsAbsoluteUri)
{
return uri.ToString();
}
// Convert relative local path to absolute:
var rootUri = new Uri(root.EndWithSeparator('/'));
if (Uri.TryCreate(rootUri, uri, out uri))
{
return uri.ToString();
}
return null;
}
private static bool IsWindowsAbsoluteOrDriveRelativePath(string value)
=> Path.DirectorySeparatorChar == '\\' &&
value.Length >= 2 &&
value[1] == ':' &&
(value[0] >= 'A' && value[0] <= 'Z' || value[0] >= 'a' && value[0] <= 'z');
private static bool TryParseScp(string value, out Uri uri)
{
uri = null;
int colon = value.IndexOf(':');
if (colon == -1)
{
return false;
}
// URLs xxx://xxx
if (colon + 2 < value.Length && value[colon + 1] == '/' && value[colon + 2] == '/')
{
return false;
}
// Windows absolute or driver-relative paths "X:\xxx", "X:xxx"
if (IsWindowsAbsoluteOrDriveRelativePath(value))
{
return false;
}
// [user@]server:path
var url = "ssh://" + value.Substring(0, colon) + "/" + value.Substring(colon + 1);
return Uri.TryCreate(url, UriKind.Absolute, out uri);
}
public static ITaskItem[] GetSourceRoots(GitRepository repository, Action<string, object[]> logWarning)
{
var result = new List<TaskItem>();
var repoRoot = GetRepositoryRoot(repository);
var revisionId = repository.GetHeadCommitSha();
if (revisionId != null)
{
// Don't report a warning since it has already been reported by GetRepositoryUrl task.
string repositoryUrl = GetRepositoryUrl(repository);
// Item metadata are stored msbuild-escaped. GetMetadata unescapes, SetMetadata stores the value as specified.
// Escape msbuild special characters so that URL escapes in the URL are preserved when the URL is read by GetMetadata.
var item = new TaskItem(Evaluation.ProjectCollection.Escape(repoRoot));
item.SetMetadata(Names.SourceRoot.SourceControl, SourceControlName);
item.SetMetadata(Names.SourceRoot.ScmRepositoryUrl, Evaluation.ProjectCollection.Escape(repositoryUrl));
item.SetMetadata(Names.SourceRoot.RevisionId, revisionId);
result.Add(item);
}
else
{
logWarning(Resources.RepositoryHasNoCommit, Array.Empty<object>());
}
foreach (var submodule in repository.GetSubmodules())
{
var commitSha = submodule.HeadCommitSha;
if (commitSha == null)
{
logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink,
new[] { string.Format(Resources.SubmoduleWithoutCommit, new[] { submodule.Name }) });
continue;
}
// https://git-scm.com/docs/git-submodule
var submoduleUrl = NormalizeUrl(repository.Config, submodule.Url, repoRoot);
if (submoduleUrl == null)
{
logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink,
new[] { string.Format(Resources.InvalidSubmoduleUrl, submodule.Name, submodule.Url) });
continue;
}
// Item metadata are stored msbuild-escaped. GetMetadata unescapes, SetMetadata stores the value as specified.
// Escape msbuild special characters so that URL escapes and non-ascii characters in the URL and paths are
// preserved when read by GetMetadata.
var item = new TaskItem(Evaluation.ProjectCollection.Escape(submodule.WorkingDirectoryFullPath.EndWithSeparator()));
item.SetMetadata(Names.SourceRoot.SourceControl, SourceControlName);
item.SetMetadata(Names.SourceRoot.ScmRepositoryUrl, Evaluation.ProjectCollection.Escape(submoduleUrl));
item.SetMetadata(Names.SourceRoot.RevisionId, commitSha);
item.SetMetadata(Names.SourceRoot.ContainingRoot, Evaluation.ProjectCollection.Escape(repoRoot));
item.SetMetadata(Names.SourceRoot.NestedRoot, Evaluation.ProjectCollection.Escape(submodule.WorkingDirectoryRelativePath.EndWithSeparator('/')));
result.Add(item);
}
foreach (var diagnostic in repository.GetSubmoduleDiagnostics())
{
logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink, new[] { diagnostic });
}
return result.ToArray();
}
private static string GetRepositoryRoot(GitRepository repository)
=> repository.WorkingDirectory.EndWithSeparator();
public static ITaskItem[] GetUntrackedFiles(
GitRepository repository,
ITaskItem[] files,
string projectDirectory,
Func<string, GitRepository> repositoryFactory)
{
var directoryTree = BuildDirectoryTree(repository);
return files.Where(file =>
{
// file.ItemSpec are relative to projectDirectory.
var fullPath = Path.GetFullPath(Path.Combine(projectDirectory, file.ItemSpec));
var containingDirectory = GetContainingRepository(fullPath, directoryTree);
// Files that are outside of the repository are considered untracked.
if (containingDirectory == null)
{
return true;
}
return containingDirectory.GetMatcher(repositoryFactory).IsNormalizedFilePathIgnored(fullPath) ?? true;
}).ToArray();
}
internal sealed class DirectoryNode
{
public readonly string Name;
public readonly List<DirectoryNode> OrderedChildren;
// set on nodes that represent submodule working directory:
public string WorkingDirectoryFullPath;
private GitIgnore.Matcher _lazyMatcher;
public DirectoryNode(string name)
: this(name, null, new List<DirectoryNode>())
{
}
public DirectoryNode(string name, string fullPath)
: this(name, fullPath, new List<DirectoryNode>())
{
}
public DirectoryNode(string name, string workingDirectoryFullPath, List<DirectoryNode> orderedChildren)
{
Name = name;
WorkingDirectoryFullPath = workingDirectoryFullPath;
OrderedChildren = orderedChildren;
}
public void SetMatcher(string workingDirectory, GitIgnore.Matcher matcher)
{
WorkingDirectoryFullPath = workingDirectory;
_lazyMatcher = matcher;
}
public int FindChildIndex(string name)
=> BinarySearch(OrderedChildren, name, (n, v) => n.Name.CompareTo(v));
public GitIgnore.Matcher GetMatcher(Func<string, GitRepository> repositoryFactory)
=> _lazyMatcher ?? (_lazyMatcher = repositoryFactory(WorkingDirectoryFullPath).Ignore.CreateMatcher());
}
internal static DirectoryNode BuildDirectoryTree(GitRepository repository)
{
var workingDirectory = repository.WorkingDirectory;
var treeRoot = new DirectoryNode("");
AddTreeNode(treeRoot, workingDirectory, repository.Ignore.CreateMatcher());
foreach (var submodule in repository.GetSubmodules())
{
AddTreeNode(treeRoot, submodule.WorkingDirectoryFullPath, matcherOpt: null);
}
return treeRoot;
}
private static void AddTreeNode(DirectoryNode root, string workingDirectory, GitIgnore.Matcher matcherOpt)
{
var segments = PathUtilities.Split(workingDirectory);
var node = root;
for (int i = 0; i < segments.Length; i++)
{
int index = node.FindChildIndex(segments[i]);
if (index >= 0)
{
node = node.OrderedChildren[index];
}
else
{
var newNode = new DirectoryNode(segments[i]);
node.OrderedChildren.Insert(~index, newNode);
node = newNode;
}
if (i == segments.Length - 1)
{
node.SetMatcher(workingDirectory, matcherOpt);
}
}
}
// internal for testing
internal static DirectoryNode GetContainingRepository(string fullPath, DirectoryNode root)
{
var segments = PathUtilities.Split(fullPath);
Debug.Assert(segments.Length > 0);
Debug.Assert(root.WorkingDirectoryFullPath == null);
DirectoryNode containingRepositoryNode = null;
var node = root;
for (int i = 0; i < segments.Length - 1; i++)
{
int index = node.FindChildIndex(segments[i]);
if (index < 0)
{
break;
}
node = node.OrderedChildren[index];
if (node.WorkingDirectoryFullPath != null)
{
containingRepositoryNode = node;
}
}
return containingRepositoryNode;
}
internal static int BinarySearch<T, TValue>(IReadOnlyList<T> list, TValue value, Func<T, TValue, int> compare)
{
var low = 0;
var high = list.Count - 1;
while (low <= high)
{
var middle = low + ((high - low) >> 1);
var midValue = list[middle];
var comparison = compare(midValue, value);
if (comparison == 0)
{
return middle;
}
if (comparison > 0)
{
high = middle - 1;
}
else
{
low = middle + 1;
}
}
return ~low;
}
}
}