/
FastFetchVerb.cs
325 lines (283 loc) · 12.8 KB
/
FastFetchVerb.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
using CommandLine;
using GVFS.Common;
using GVFS.Common.Git;
using GVFS.Common.Http;
using GVFS.Common.Tracing;
using Microsoft.Diagnostics.Tracing;
using System;
using System.Diagnostics;
namespace FastFetch
{
[Verb("fastfetch", HelpText = "Fast-fetch a branch")]
public class FastFetchVerb
{
// Testing has shown that more than 16 download threads does not improve
// performance even with 56 core machines with 40G NICs. More threads does
// create more load on the servers as they have to handle extra connections.
private const int MaxDefaultDownloadThreads = 16;
private const int ExitFailure = 1;
private const int ExitSuccess = 0;
[Option(
'c',
"commit",
Required = false,
HelpText = "Commit to fetch")]
public string Commit { get; set; }
[Option(
'b',
"branch",
Required = false,
HelpText = "Branch to fetch")]
public string Branch { get; set; }
[Option(
"cache-server-url",
Required = false,
Default = "",
HelpText = "Defines the url of the cache server")]
public string CacheServerUrl { get; set; }
[Option(
"chunk-size",
Required = false,
Default = 4000,
HelpText = "Sets the number of objects to be downloaded in a single pack")]
public int ChunkSize { get; set; }
[Option(
"checkout",
Required = false,
Default = false,
HelpText = "Checkout the target commit into the working directory after fetching")]
public bool Checkout { get; set; }
[Option(
"search-thread-count",
Required = false,
Default = 0,
HelpText = "Sets the number of threads to use for finding missing blobs. (0 for number of logical cores)")]
public int SearchThreadCount { get; set; }
[Option(
"download-thread-count",
Required = false,
Default = 0,
HelpText = "Sets the number of threads to use for downloading. (0 for number of logical cores)")]
public int DownloadThreadCount { get; set; }
[Option(
"index-thread-count",
Required = false,
Default = 0,
HelpText = "Sets the number of threads to use for indexing. (0 for number of logical cores)")]
public int IndexThreadCount { get; set; }
[Option(
"checkout-thread-count",
Required = false,
Default = 0,
HelpText = "Sets the number of threads to use for checkout. (0 for number of logical cores)")]
public int CheckoutThreadCount { get; set; }
[Option(
'r',
"max-retries",
Required = false,
Default = 10,
HelpText = "Sets the maximum number of attempts for downloading a pack")]
public int MaxAttempts { get; set; }
[Option(
"git-path",
Default = "",
Required = false,
HelpText = "Sets the path and filename for git.exe if it isn't expected to be on %PATH%.")]
public string GitBinPath { get; set; }
[Option(
"folders",
Required = false,
Default = "",
HelpText = "A semicolon-delimited list of paths to fetch")]
public string PathWhitelist { get; set; }
[Option(
"folders-list",
Required = false,
Default = "",
HelpText = "A file containing line-delimited list of paths to fetch")]
public string PathWhitelistFile { get; set; }
[Option(
"Allow-index-metadata-update-from-working-tree",
Required = false,
Default = false,
HelpText = "When specified, index metadata (file times and sizes) is updated from disk if not already in the index. " +
"This flag should only be used when the working tree is known to be in a good state. " +
"Do not use this flag if the working tree is not 100% known to be good as it would cause 'git status' to misreport.")]
public bool AllowIndexMetadataUpdateFromWorkingTree { get; set; }
[Option(
"verbose",
Required = false,
Default = false,
HelpText = "Show all outputs on the console in addition to writing them to a log file")]
public bool Verbose { get; set; }
[Option(
"parent-activity-id",
Required = false,
Default = "",
HelpText = "The GUID of the caller - used for telemetry purposes.")]
public string ParentActivityId { get; set; }
public void Execute()
{
Environment.ExitCode = this.ExecuteWithExitCode();
}
private int ExecuteWithExitCode()
{
// CmdParser doesn't strip quotes, and Path.Combine will throw
this.GitBinPath = this.GitBinPath.Replace("\"", string.Empty);
if (!GitProcess.GitExists(this.GitBinPath))
{
Console.WriteLine(
"Could not find git.exe {0}",
!string.IsNullOrWhiteSpace(this.GitBinPath) ? "at " + this.GitBinPath : "on %PATH%");
return ExitFailure;
}
if (this.Commit != null && this.Branch != null)
{
Console.WriteLine("Cannot specify both a commit sha and a branch name.");
return ExitFailure;
}
this.SearchThreadCount = this.SearchThreadCount > 0 ? this.SearchThreadCount : Environment.ProcessorCount;
this.DownloadThreadCount = this.DownloadThreadCount > 0 ? this.DownloadThreadCount : Math.Min(Environment.ProcessorCount, MaxDefaultDownloadThreads);
this.IndexThreadCount = this.IndexThreadCount > 0 ? this.IndexThreadCount : Environment.ProcessorCount;
this.CheckoutThreadCount = this.CheckoutThreadCount > 0 ? this.CheckoutThreadCount : Environment.ProcessorCount;
this.GitBinPath = !string.IsNullOrWhiteSpace(this.GitBinPath) ? this.GitBinPath : GitProcess.GetInstalledGitBinPath();
GitEnlistment enlistment = GitEnlistment.CreateFromCurrentDirectory(this.GitBinPath);
if (enlistment == null)
{
Console.WriteLine("Must be run within a git repo");
return ExitFailure;
}
string commitish = this.Commit ?? this.Branch;
if (string.IsNullOrWhiteSpace(commitish))
{
GitProcess.Result result = new GitProcess(enlistment).GetCurrentBranchName();
if (result.HasErrors || string.IsNullOrWhiteSpace(result.Output))
{
Console.WriteLine("Could not retrieve current branch name: " + result.Errors);
return ExitFailure;
}
commitish = result.Output.Trim();
}
Guid parentActivityId = Guid.Empty;
if (!string.IsNullOrWhiteSpace(this.ParentActivityId) && !Guid.TryParse(this.ParentActivityId, out parentActivityId))
{
Console.WriteLine("The ParentActivityId provided (" + this.ParentActivityId + ") is not a valid GUID.");
}
using (JsonEtwTracer tracer = new JsonEtwTracer("Microsoft.Git.FastFetch", parentActivityId, "FastFetch"))
{
if (this.Verbose)
{
tracer.AddDiagnosticConsoleEventListener(EventLevel.Informational, Keywords.Any);
}
else
{
tracer.AddPrettyConsoleEventListener(EventLevel.Error, Keywords.Any);
}
string fastfetchLogFile = Enlistment.GetNewLogFileName(enlistment.FastFetchLogRoot, "fastfetch");
tracer.AddLogFileEventListener(fastfetchLogFile, EventLevel.Informational, Keywords.Any);
RetryConfig retryConfig = new RetryConfig(this.MaxAttempts, TimeSpan.FromMinutes(RetryConfig.FetchAndCloneTimeoutMinutes));
string error;
CacheServerInfo cacheServer;
if (!CacheServerInfo.TryDetermineCacheServer(this.CacheServerUrl, tracer, enlistment, retryConfig, out cacheServer, out error))
{
tracer.RelatedError(error);
return ExitFailure;
}
tracer.WriteStartEvent(
enlistment.EnlistmentRoot,
enlistment.RepoUrl,
cacheServer.Url,
new EventMetadata
{
{ "TargetCommitish", commitish },
{ "Checkout", this.Checkout },
});
FetchHelper fetchHelper = this.GetFetchHelper(tracer, enlistment, cacheServer, retryConfig);
if (!FetchHelper.TryLoadPathWhitelist(tracer, this.PathWhitelist, this.PathWhitelistFile, enlistment, fetchHelper.PathWhitelist))
{
return ExitFailure;
}
bool isSuccess;
try
{
Func<bool> doPrefetch =
() =>
{
try
{
bool isBranch = this.Commit == null;
fetchHelper.FastFetch(commitish, isBranch);
return !fetchHelper.HasFailures;
}
catch (FetchHelper.FetchException e)
{
tracer.RelatedError(e.Message);
return false;
}
};
if (this.Verbose)
{
isSuccess = doPrefetch();
}
else
{
isSuccess = ConsoleHelper.ShowStatusWhileRunning(
doPrefetch,
"Fetching",
output: Console.Out,
showSpinner: !Console.IsOutputRedirected,
gvfsLogEnlistmentRoot: null);
Console.WriteLine();
Console.WriteLine("See the full log at " + fastfetchLogFile);
}
isSuccess &= !fetchHelper.HasFailures;
}
catch (AggregateException e)
{
isSuccess = false;
foreach (Exception ex in e.Flatten().InnerExceptions)
{
tracer.RelatedError(ex.ToString());
}
}
catch (Exception e)
{
isSuccess = false;
tracer.RelatedError(e.ToString());
}
EventMetadata stopMetadata = new EventMetadata();
stopMetadata.Add("Success", isSuccess);
tracer.Stop(stopMetadata);
return isSuccess ? ExitSuccess : ExitFailure;
}
}
private FetchHelper GetFetchHelper(ITracer tracer, Enlistment enlistment, CacheServerInfo cacheServer, RetryConfig retryConfig)
{
GitObjectsHttpRequestor objectRequestor = new GitObjectsHttpRequestor(tracer, enlistment, cacheServer, retryConfig);
if (this.Checkout)
{
return new CheckoutFetchHelper(
tracer,
enlistment,
objectRequestor,
this.ChunkSize,
this.SearchThreadCount,
this.DownloadThreadCount,
this.IndexThreadCount,
this.CheckoutThreadCount,
this.AllowIndexMetadataUpdateFromWorkingTree);
}
else
{
return new FetchHelper(
tracer,
enlistment,
objectRequestor,
this.ChunkSize,
this.SearchThreadCount,
this.DownloadThreadCount,
this.IndexThreadCount);
}
}
}
}