-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathProgram.cs
More file actions
446 lines (396 loc) · 17.3 KB
/
Program.cs
File metadata and controls
446 lines (396 loc) · 17.3 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using GVFS.Common;
using GVFS.Common.NamedPipes;
using GVFS.Hooks.HooksPlatform;
using System;
using System.Collections.Generic;
using System.Linq;
namespace GVFS.Hooks
{
public class Program
{
private const string PreCommandHook = "pre-command";
private const string PostCommandHook = "post-command";
private const string GitPidArg = "--git-pid=";
private const int InvalidProcessId = -1;
private const int PostCommandSpinnerDelayMs = 500;
private static string enlistmentRoot;
private static string enlistmentPipename;
private static Random random = new Random();
private delegate void LockRequestDelegate(bool unattended, string[] args, int pid, NamedPipeClient pipeClient);
public static void Main(string[] args)
{
try
{
if (args.Length < 2)
{
ExitWithError("Usage: gvfs.hooks.exe --git-pid=<pid> <hook> <git verb> [<other arguments>]");
}
bool unattended = GVFSEnlistment.IsUnattended(tracer: null);
string errorMessage;
string normalizedCurrentDirectory;
if (!GVFSHooksPlatform.TryGetNormalizedPath(Environment.CurrentDirectory, out normalizedCurrentDirectory, out errorMessage))
{
ExitWithError($"Failed to determine final path for current directory {Environment.CurrentDirectory}. Error: {errorMessage}");
}
if (!GVFSHooksPlatform.TryGetGVFSEnlistmentRoot(Environment.CurrentDirectory, out enlistmentRoot, out errorMessage))
{
// Nothing to hook when being run outside of a GVFS repo.
// This is also the path when run with --git-dir outside of a GVFS directory, see Story #949665
Environment.Exit(0);
}
enlistmentPipename = GVFSHooksPlatform.GetNamedPipeName(enlistmentRoot);
switch (GetHookType(args))
{
case PreCommandHook:
CheckForLegalCommands(args);
RunLockRequest(args, unattended, AcquireGVFSLockForProcess);
RunPreCommands(args);
break;
case PostCommandHook:
// Do not release the lock if this request was only run to see if it could acquire the GVFSLock,
// but did not actually acquire it.
if (!CheckGVFSLockAvailabilityOnly(args))
{
RunLockRequest(args, unattended, ReleaseGVFSLock);
}
break;
default:
ExitWithError("Unrecognized hook: " + string.Join(" ", args));
break;
}
}
catch (Exception ex)
{
ExitWithError("Unexpected exception: " + ex.ToString());
}
}
private static void RunPreCommands(string[] args)
{
string command = GetGitCommand(args);
switch (command)
{
case "fetch":
case "pull":
ProcessHelper.Run("gvfs", "prefetch --commits", redirectOutput: false);
break;
case "status":
/* If status is being run to serialize for caching, or if --porcelain is specified, skip the health display */
if (!ArgsBlockHydrationStatus(args)
&& ConfigurationAllowsHydrationStatus())
{
/* Display a message about the hydration status of the repo */
ProcessHelper.Run("gvfs", "health --status", redirectOutput: false);
}
break;
}
}
private static bool ArgsBlockHydrationStatus(string[] args)
{
return args.Any(arg =>
arg.StartsWith("--serialize", StringComparison.OrdinalIgnoreCase)
|| arg.StartsWith("--porcelain", StringComparison.OrdinalIgnoreCase));
}
private static bool ConfigurationAllowsHydrationStatus()
{
try
{
ProcessResult result = ProcessHelper.Run("git", $"config --get {GVFSConstants.GitConfig.ShowHydrationStatus}");
bool hydrationStatusEnabled;
if (bool.TryParse(result.Output.Trim(), out hydrationStatusEnabled))
{
return hydrationStatusEnabled;
}
}
catch (Exception)
{
}
return GVFSConstants.GitConfig.ShowHydrationStatusDefault;
}
private static void ExitWithError(params string[] messages)
{
foreach (string message in messages)
{
Console.WriteLine(message);
}
Environment.Exit(1);
}
private static void CheckForLegalCommands(string[] args)
{
string command = GetGitCommand(args);
switch (command)
{
case "gui":
ExitWithError(GVFSHooksPlatform.GetGitGuiBlockedMessage());
break;
}
}
private static void RunLockRequest(string[] args, bool unattended, LockRequestDelegate requestToRun)
{
try
{
if (ShouldLock(args))
{
using (NamedPipeClient pipeClient = new NamedPipeClient(enlistmentPipename))
{
if (!pipeClient.Connect())
{
ExitWithError("The repo does not appear to be mounted. Use 'gvfs status' to check.");
}
int pid = GetParentPid(args);
if (pid == Program.InvalidProcessId ||
!GVFSHooksPlatform.IsProcessActive(pid))
{
ExitWithError("GVFS.Hooks: Unable to find parent git.exe process " + "(PID: " + pid + ").");
}
requestToRun(unattended, args, pid, pipeClient);
}
}
}
catch (Exception exc)
{
ExitWithError(
"Unable to initialize Git command.",
"Ensure that GVFS is running.",
exc.ToString());
}
}
private static string GenerateFullCommand(string[] args)
{
return "git " + string.Join(" ", args.Skip(1).Where(arg => !arg.StartsWith(GitPidArg)));
}
private static int GetParentPid(string[] args)
{
string pidArg = args.SingleOrDefault(x => x.StartsWith(GitPidArg));
if (!string.IsNullOrEmpty(pidArg))
{
pidArg = pidArg.Remove(0, GitPidArg.Length);
int pid;
if (int.TryParse(pidArg, out pid))
{
return pid;
}
}
ExitWithError(
"Git did not supply the process Id.",
"Ensure you are using the correct version of the git client.");
return Program.InvalidProcessId;
}
private static void AcquireGVFSLockForProcess(bool unattended, string[] args, int pid, NamedPipeClient pipeClient)
{
string result;
bool checkGvfsLockAvailabilityOnly = CheckGVFSLockAvailabilityOnly(args);
string fullCommand = GenerateFullCommand(args);
string gitCommandSessionId = GetGitCommandSessionId();
if (!GVFSLock.TryAcquireGVFSLockForProcess(
unattended,
pipeClient,
fullCommand,
pid,
GVFSHooksPlatform.IsElevated(),
isConsoleOutputRedirectedToFile: GVFSHooksPlatform.IsConsoleOutputRedirectedToFile(),
checkAvailabilityOnly: checkGvfsLockAvailabilityOnly,
gvfsEnlistmentRoot: null,
gitCommandSessionId: gitCommandSessionId,
result: out result))
{
ExitWithError(result);
}
}
private static void ReleaseGVFSLock(bool unattended, string[] args, int pid, NamedPipeClient pipeClient)
{
string fullCommand = GenerateFullCommand(args);
GVFSLock.ReleaseGVFSLock(
unattended,
pipeClient,
fullCommand,
pid,
GVFSHooksPlatform.IsElevated(),
GVFSHooksPlatform.IsConsoleOutputRedirectedToFile(),
response =>
{
if (response == null || response.ResponseData == null)
{
Console.WriteLine("\nError communicating with GVFS: Run 'gvfs status' to check the status of your repo");
}
else if (response.ResponseData.HasFailures)
{
if (response.ResponseData.FailureCountExceedsMaxFileNames)
{
Console.WriteLine(
"\nGVFS failed to update {0} files, run 'git status' to check the status of files in the repo",
response.ResponseData.FailedToDeleteCount + response.ResponseData.FailedToUpdateCount);
}
else
{
string deleteFailuresMessage = BuildUpdatePlaceholderFailureMessage(response.ResponseData.FailedToDeleteFileList, "delete", "git clean -f ");
if (deleteFailuresMessage.Length > 0)
{
Console.WriteLine(deleteFailuresMessage);
}
string updateFailuresMessage = BuildUpdatePlaceholderFailureMessage(response.ResponseData.FailedToUpdateFileList, "update", "git checkout -- ");
if (updateFailuresMessage.Length > 0)
{
Console.WriteLine(updateFailuresMessage);
}
}
}
},
gvfsEnlistmentRoot: null,
waitingMessage: "Waiting for GVFS to parse index and update placeholder files",
spinnerDelay: PostCommandSpinnerDelayMs);
}
private static bool CheckGVFSLockAvailabilityOnly(string[] args)
{
try
{
// Don't acquire the GVFS lock if the git command is not acquiring locks.
// This enables tools to run status commands without to the index and
// blocking other commands from running. The git argument
// "--no-optional-locks" results in a 'negative'
// value GIT_OPTIONAL_LOCKS environment variable.
return GetGitCommand(args).Equals("status", StringComparison.OrdinalIgnoreCase) &&
(args.Any(arg => arg.Equals("--no-lock-index", StringComparison.OrdinalIgnoreCase)) ||
IsGitEnvVarDisabled("GIT_OPTIONAL_LOCKS"));
}
catch (Exception e)
{
ExitWithError("Failed to determine if GVFS should aquire GVFS lock: " + e.ToString());
}
return false;
}
private static string BuildUpdatePlaceholderFailureMessage(List<string> fileList, string failedOperation, string recoveryCommand)
{
if (fileList == null || fileList.Count == 0)
{
return string.Empty;
}
fileList.Sort(StringComparer.OrdinalIgnoreCase);
string message = "\nGVFS was unable to " + failedOperation + " the following files. To recover, close all handles to the files and run these commands:";
message += string.Concat(fileList.Select(x => "\n " + recoveryCommand + x));
return message;
}
private static bool IsGitEnvVarDisabled(string envVar)
{
string envVarValue = Environment.GetEnvironmentVariable(envVar);
if (!string.IsNullOrEmpty(envVarValue))
{
if (string.Equals(envVarValue, "false", StringComparison.OrdinalIgnoreCase) ||
string.Equals(envVarValue, "no", StringComparison.OrdinalIgnoreCase) ||
string.Equals(envVarValue, "off", StringComparison.OrdinalIgnoreCase) ||
string.Equals(envVarValue, "0", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private static bool ShouldLock(string[] args)
{
string gitCommand = GetGitCommand(args);
switch (gitCommand)
{
// Keep these alphabetically sorted
case "blame":
case "branch":
case "cat-file":
case "check-attr":
case "check-ignore":
case "check-mailmap":
case "commit-graph":
case "config":
case "credential":
case "diff":
case "diff-files":
case "diff-index":
case "diff-tree":
case "difftool":
case "fetch":
case "for-each-ref":
case "help":
case "hash-object":
case "index-pack":
case "log":
case "ls-files":
case "ls-tree":
case "merge-base":
case "multi-pack-index":
case "name-rev":
case "pack-objects":
case "push":
case "remote":
case "rev-list":
case "rev-parse":
case "show":
case "show-ref":
case "symbolic-ref":
case "tag":
case "unpack-objects":
case "update-ref":
case "version":
case "web--browse":
return false;
/*
* There are several git commands that are "unsupported" in virtualized (VFS4G)
* enlistments that are blocked by git. Usually, these are blocked before they acquire
* a GVFSLock, but the submodule command is different, and is blocked after acquiring the
* GVFS lock. This can cause issues if another action is attempting to create placeholders.
* As we know the submodule command is a no-op, allow it to proceed without acquiring the
* GVFSLock. I have filed issue #1164 to track having git block all unsupported commands
* before calling the pre-command hook.
*/
case "submodule":
return false;
}
if (gitCommand == "reset" && args.Contains("--soft"))
{
return false;
}
if (!KnownGitCommands.Contains(gitCommand) &&
IsAlias(gitCommand))
{
return false;
}
return true;
}
private static string GetHookType(string[] args)
{
return args[0].ToLowerInvariant();
}
private static string GetGitCommand(string[] args)
{
string command = args[1].ToLowerInvariant();
if (command.StartsWith("git-"))
{
command = command.Substring(4);
}
return command;
}
private static bool IsAlias(string command)
{
ProcessResult result = ProcessHelper.Run("git", "config --get alias." + command);
return !string.IsNullOrEmpty(result.Output);
}
private static string GetGitCommandSessionId()
{
try
{
return Environment.GetEnvironmentVariable("GIT_TR2_PARENT_SID", EnvironmentVariableTarget.Process) ?? string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
private static bool IsUpgradeMessageDeterministic()
{
try
{
return Environment.GetEnvironmentVariable("GVFS_UPGRADE_DETERMINISTIC", EnvironmentVariableTarget.Process) != null;
}
catch (Exception)
{
return false;
}
}
}
}