-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGitFileHashProvider.cs
More file actions
339 lines (297 loc) · 12.9 KB
/
GitFileHashProvider.cs
File metadata and controls
339 lines (297 loc) · 12.9 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
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BuildXL.Utilities.Core.Tasks;
using Microsoft.Build.Experimental.ProjectCache;
namespace Microsoft.MSBuildCache.SourceControl;
public sealed class GitParsingException : Exception
{
public GitParsingException()
{
}
public GitParsingException(string message)
: base(message)
{
}
public GitParsingException(string message, Exception innerException)
: base(message, innerException)
{
}
}
internal sealed class GitFileHashProvider : ISourceControlFileHashProvider
{
private static readonly char[] LineDelimiters = { '\n' };
private static readonly char[] SegmentDelimiters = { ' ', '\t' };
private readonly PluginLoggerBase _logger;
public GitFileHashProvider(PluginLoggerBase logger)
{
_logger = logger;
}
/// <summary>
/// Git all files known to git from ls-files.
/// </summary>
public async Task<IReadOnlyDictionary<string, byte[]>> GetFileHashesAsync(string repoRoot, CancellationToken cancellationToken)
{
// Kick off grabbing the hashes for the "main/root" module
Task<Dictionary<string, byte[]>> hashesTask = GetModuleFileHashesAsync(repoRoot, cancellationToken);
// The common case is to not have any submodules, so just short-circuit
if (!File.Exists(Path.Combine(repoRoot, ".gitmodules")))
{
return await hashesTask;
}
// Iterate through the initialized submodules and add those hashes
List<string> submodules = await GetInitializedSubmodulesAsync(repoRoot, cancellationToken);
if (submodules.Count == 0)
{
// The .gitmodules file existed, but there aren't actually any submodules.
return await hashesTask;
}
// Fetch all submodule file hashes in parallel.
Task<Dictionary<string, byte[]>>[] subModuleHashesTasks = submodules
.Select(submodule => GetModuleFileHashesAsync(Path.Combine(repoRoot, submodule), cancellationToken))
.ToArray();
Dictionary<string, byte[]> hashes = await hashesTask;
// Git provides a hash for each of the submodules because in the index, the submodule
// is modeled as a symlink file where the hash is the commit ID that the submodule is
// pinned to -- we need to remove the hash from the result set
foreach (string submodule in submodules)
{
hashes.Remove(submodule);
}
// Aggregate in all submodule hashes
IReadOnlyDictionary<string, byte[]>[] subModuleHashes = await Task.WhenAll(subModuleHashesTasks);
foreach (IReadOnlyDictionary<string, byte[]> h in subModuleHashes)
{
foreach (KeyValuePair<string, byte[]> entry in h)
{
hashes.Add(entry.Key, entry.Value);
}
}
return hashes;
}
private async Task<Dictionary<string, byte[]>> GetModuleFileHashesAsync(string basePath, CancellationToken cancellationToken)
{
return await Git.RunAsync(
_logger,
workingDir: basePath,
"ls-files -z -cmos --exclude-standard",
(_, stdout) => Task.Run(() => ParseGitLsFiles(basePath, stdout, (filesToRehash, fileHashes) => Git.HashObjectAsync(basePath, filesToRehash, fileHashes, _logger, cancellationToken))),
(exitCode, result) =>
{
if (exitCode != 0)
{
throw new SourceControlHashException("git ls-files failed with exit code " + exitCode);
}
return result;
},
cancellationToken);
}
internal async Task<Dictionary<string, byte[]>> ParseGitLsFiles(
string basePath,
TextReader gitOutput,
Func<List<string>, Dictionary<string, byte[]>, Task> hasher)
{
// 100644<space>05e33066454fb2823990688888877b0c2856a56e<space>0<tab>foo.txt<null>
// GeneralFormat is that each file entry is delimited by a null character because we're using -z
// file paths are relative and have / instead of \ but otherwise unmodified (-z is important here)
// and delimited by a tab from other staging info (if there is any)
// staging info which includes the hash are space delimited. See UT's for more examples
using var reader = new GitLsFileOutputReader(gitOutput);
var fileHashes = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
var filesToRehash = new List<string>();
StringBuilder? line;
while ((line = reader.ReadLine()) != null)
{
// tab's can't be part of filenames https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
int tabIdx = 0;
while (tabIdx < line.Length && line[tabIdx] != '\t') // line.IndexOf('\t')
{
tabIdx++;
}
if (tabIdx == line.Length) // untracked files are just the path name.
{
line.Replace('/', '\\');
string file = Path.Combine(basePath, line.ToString());
_logger.LogMessage($"Found untracked file {file}.");
filesToRehash.Add(file);
if (fileHashes.ContainsKey(file))
{
throw new GitParsingException(file + " already had a hash this doesn't make sense");
}
}
else // 100644 9f4ca612082be4ed7055ccd6f1e005ede8a211f3 0 InternalsVisibleTo.cs
{
int pathLength = line.Length - tabIdx - 1;
line.Replace('/', '\\', tabIdx + 1, pathLength);
string file = Path.Combine(basePath, line.ToString(tabIdx + 1, pathLength));
if (fileHashes.ContainsKey(file))
{
// If we already saw this it means it's a previously committed/added file that's been modified
_logger.LogMessage($"Found modified file {file}.");
filesToRehash.Add(file);
continue;
}
int startIdx = 0;
while (startIdx < tabIdx && line[startIdx] != ' ') // line.IndexOf(' ', startIdx)
{
startIdx++;
}
int endIdx = ++startIdx;
while (endIdx < tabIdx && line[endIdx] != ' ') // line.IndexOf(' ', endIdx)
{
endIdx++;
}
if (startIdx >= tabIdx || endIdx >= tabIdx)
{
throw new GitParsingException("expect staging bits to have 3 parts seperated by spaces " + line.ToString());
}
fileHashes[file] = HexUtilities.HexToBytes(line.ToString(startIdx, endIdx - startIdx));
}
}
if (filesToRehash.Count > 0)
{
Stopwatch sw = Stopwatch.StartNew();
// we could do this as new files come in just not clear it's worth it
await hasher(filesToRehash, fileHashes);
_logger.LogMessage($"{fileHashes.Count} files Rehashing {filesToRehash.Count} modified files took {sw.ElapsedMilliseconds} msec");
}
return fileHashes;
}
private Task<List<string>> GetInitializedSubmodulesAsync(string repoRoot, CancellationToken cancellationToken)
{
string stdErrString = string.Empty;
return Git.RunAsync(
_logger,
workingDir: repoRoot,
"submodule status --recursive",
async (stdin, stdout) =>
{
string stdOutString = await stdout.ReadToEndAsync();
return ParseGitSubmoduleStatus(stdOutString);
},
(exitCode, result) =>
{
if (exitCode == 0)
{
return result;
}
{
// Errors from "git submodule status" are ignored here and an empty submodule set is returned;
// so we drop an error in the log file and include the text from StdErr
return new List<string>();
}
},
cancellationToken);
}
internal List<string> ParseGitSubmoduleStatus(string output)
{
// <[\s|+|-|U]>05e33066454fb2823990688888877b0c2856a56e<space>submodule/path<tab>(sha1-description)
string[] moduleStatus = output.Split(LineDelimiters, StringSplitOptions.RemoveEmptyEntries);
var submodules = new List<string>();
foreach (string module in moduleStatus)
{
string[] entry = module.Split(SegmentDelimiters, 2, StringSplitOptions.RemoveEmptyEntries);
if (entry.Length != 2)
{
// Some versions of git.exe add an extra lf at the end of the results for "better display" according
// to the github logs for that version; we need to ignore empty lines or improperly formatted ones...
continue;
}
if (entry[0][0] == '-')
{
// Skip any uninitialized submodules
continue;
}
// first segment is the commit ID
int index = entry[0][0] == '+' || entry[0][0] == 'U' ? 1 : 0;
string commit = entry[0].Substring(index);
// Second segment is the path; but to isolate it, we have to trim off the third
// segment which is the sha1 description for the commit ID; keep the description
index = entry[1].LastIndexOfAny(SegmentDelimiters);
if (index <= 0)
{
// Again, the we simply are going to ignore any malformatted subtrings and exclude
// them from the list of submodules.
continue;
}
// Peel off the description...
string description = entry[1].Substring(index).Trim();
// ...then grab the path
string modulePath = entry[1].Substring(0, index);
submodules.Add(modulePath.Replace('/', '\\'));
_logger.LogMessage($"Found submodule: {modulePath}.{commit}.{description}");
}
return submodules;
}
private sealed class GitLsFileOutputReader : IDisposable
{
readonly BlockingCollection<StringBuilder> _lines = new BlockingCollection<StringBuilder>();
public GitLsFileOutputReader(TextReader reader)
{
Task.Run(() => PopulateAsync(reader));
}
private void PopulateAsync(TextReader reader)
{
int overflowLength = 0;
var buffer = new char[4096]; // must be large enough to hold at least one line of output
while (true)
{
int readCnt = reader.Read(buffer, overflowLength, buffer.Length - overflowLength);
if (readCnt == 0) // end of stream
{
if (overflowLength > 0)
{
_lines.Add(new StringBuilder(overflowLength).Append(buffer, 0, overflowLength));
}
_lines.CompleteAdding();
return;
}
readCnt += overflowLength;
int startIdx = 0, eolIdx;
while (startIdx < readCnt && (eolIdx = Array.IndexOf(buffer, '\0', startIdx)) != -1)
{
int lineLength = eolIdx - startIdx;
if (overflowLength > 0)
{
overflowLength = 0;
startIdx = 0;
}
_lines.Add(new StringBuilder(lineLength).Append(buffer, startIdx, lineLength));
startIdx = eolIdx + 1;
}
if (startIdx < readCnt)
{
if (overflowLength > 0) // we already have some overflow left, but the line could not fit the buffer
{
throw new InvalidDataException($"Internal: git ls-files output line length {readCnt - startIdx} exceeds {nameof(buffer)} size {buffer.Length}. Increase the latter.");
}
overflowLength = readCnt - startIdx;
Array.Copy(buffer, startIdx, buffer, 0, overflowLength);
}
}
}
public StringBuilder? ReadLine()
{
while (!_lines.IsCompleted)
{
if (_lines.TryTake(out StringBuilder? result, -1))
{
return result;
}
}
return null;
}
public void Dispose()
{
_lines.Dispose();
}
}
}