forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHooksInstaller.cs
More file actions
247 lines (224 loc) · 10.7 KB
/
Copy pathHooksInstaller.cs
File metadata and controls
247 lines (224 loc) · 10.7 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
using GVFS.Common.Git;
using GVFS.Common.Tracing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace GVFS.Common.FileSystem
{
public static class HooksInstaller
{
private static readonly string ExecutingDirectory;
private static readonly HookData[] NativeHooks = new[]
{
new HookData(GVFSConstants.DotGit.Hooks.ReadObjectName, GVFSConstants.DotGit.Hooks.ReadObjectPath, GVFSPlatform.Instance.Constants.GVFSReadObjectHookExecutableName),
new HookData(GVFSConstants.DotGit.Hooks.VirtualFileSystemName, GVFSConstants.DotGit.Hooks.VirtualFileSystemPath, GVFSPlatform.Instance.Constants.GVFSVirtualFileSystemHookExecutableName),
new HookData(GVFSConstants.DotGit.Hooks.PostIndexChangedName, GVFSConstants.DotGit.Hooks.PostIndexChangedPath, GVFSPlatform.Instance.Constants.GVFSPostIndexChangedHookExecutableName),
};
static HooksInstaller()
{
ExecutingDirectory = Path.GetDirectoryName(Environment.ProcessPath);
}
public static string MergeHooksData(string[] defaultHooksLines, string filename, string hookName)
{
IEnumerable<string> valuableHooksLines = defaultHooksLines.Where(line => !string.IsNullOrEmpty(line.Trim()));
/* Wrap in quotes to handle spaces in the path */
string absolutePathToHooksExecutable = $"\"{Path.Combine(ExecutingDirectory, GVFSPlatform.Instance.Constants.GVFSHooksExecutableName)}\"";
if (valuableHooksLines.Contains(GVFSPlatform.Instance.Constants.GVFSHooksExecutableName, GVFSPlatform.Instance.Constants.PathComparer))
{
throw new HooksConfigurationException(
$"{GVFSPlatform.Instance.Constants.GVFSHooksExecutableName} should not be specified in the configuration for "
+ GVFSConstants.DotGit.Hooks.PostCommandHookName + " hooks (" + filename + ").");
}
else if (!valuableHooksLines.Any())
{
return absolutePathToHooksExecutable;
}
else if (hookName.Equals(GVFSConstants.DotGit.Hooks.PostCommandHookName))
{
return string.Join("\n", new string[] { absolutePathToHooksExecutable }.Concat(valuableHooksLines));
}
else
{
return string.Join("\n", valuableHooksLines.Concat(new string[] { absolutePathToHooksExecutable }));
}
}
public static bool InstallHooks(GVFSContext context, out string error)
{
error = string.Empty;
try
{
foreach (HookData hook in NativeHooks)
{
string installedHookPath = Path.Combine(ExecutingDirectory, hook.ExecutableName);
string targetHookPath = Path.Combine(context.Enlistment.WorkingDirectoryBackingRoot, hook.Path + GVFSPlatform.Instance.Constants.ExecutableExtension);
if (!TryHooksInstallationAction(() => CopyHook(context, installedHookPath, targetHookPath), out error))
{
error = "Failed to copy " + installedHookPath + "\n" + error;
return false;
}
}
string precommandHookPath = Path.Combine(context.Enlistment.WorkingDirectoryBackingRoot, GVFSConstants.DotGit.Hooks.PreCommandPath);
if (!GVFSPlatform.Instance.TryInstallGitCommandHooks(context, ExecutingDirectory, GVFSConstants.DotGit.Hooks.PreCommandHookName, precommandHookPath, out error))
{
return false;
}
string postcommandHookPath = Path.Combine(context.Enlistment.WorkingDirectoryBackingRoot, GVFSConstants.DotGit.Hooks.PostCommandPath);
if (!GVFSPlatform.Instance.TryInstallGitCommandHooks(context, ExecutingDirectory, GVFSConstants.DotGit.Hooks.PostCommandHookName, postcommandHookPath, out error))
{
return false;
}
}
catch (Exception e)
{
error = e.ToString();
return false;
}
return true;
}
public static bool TryUpdateHooks(GVFSContext context, out string errorMessage)
{
errorMessage = string.Empty;
foreach (HookData hook in NativeHooks)
{
if (!TryUpdateHook(context, hook, out errorMessage))
{
return false;
}
}
return true;
}
public static void CopyHook(GVFSContext context, string sourcePath, string destinationPath)
{
Exception ex;
if (!context.FileSystem.TryCopyToTempFileAndRename(sourcePath, destinationPath, out ex))
{
throw new RetryableException($"Error installing {sourcePath} to {destinationPath}", ex);
}
}
/// <summary>
/// Try to perform the specified action. The action will be retried (with backoff) up to 3 times.
/// </summary>
/// <param name="action">Action to perform</param>
/// <param name="errorMessage">Error message</param>
/// <returns>True if the action succeeded and false otherwise</returns>
/// <remarks>This method is optimized for the hooks installation process and should not be used
/// as a generic retry mechanism. See RetryWrapper for a general purpose retry mechanism</remarks>
public static bool TryHooksInstallationAction(Action action, out string errorMessage)
{
int retriesLeft = 3;
int retryWaitMillis = 500; // Will grow exponentially on each retry attempt
errorMessage = null;
while (true)
{
try
{
action();
return true;
}
catch (RetryableException re)
{
if (retriesLeft == 0)
{
errorMessage = re.InnerException.ToString();
return false;
}
Thread.Sleep(retryWaitMillis);
retriesLeft -= 1;
retryWaitMillis *= 2;
}
catch (Exception e)
{
errorMessage = e.ToString();
return false;
}
}
}
private static bool TryUpdateHook(
GVFSContext context,
HookData hook,
out string errorMessage)
{
bool copyHook = false;
string enlistmentHookPath = Path.Combine(context.Enlistment.WorkingDirectoryBackingRoot, hook.Path + GVFSPlatform.Instance.Constants.ExecutableExtension);
string installedHookPath = Path.Combine(ExecutingDirectory, hook.ExecutableName);
if (!context.FileSystem.FileExists(installedHookPath))
{
errorMessage = hook.ExecutableName + " cannot be found at " + installedHookPath;
return false;
}
if (!context.FileSystem.FileExists(enlistmentHookPath))
{
copyHook = true;
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", "Mount");
metadata.Add(nameof(enlistmentHookPath), enlistmentHookPath);
metadata.Add(nameof(installedHookPath), installedHookPath);
metadata.Add(TracingConstants.MessageKey.WarningMessage, hook.Name + " not found in enlistment, copying from installation folder");
context.Tracer.RelatedWarning(hook.Name + " MissingFromEnlistment", metadata);
}
else
{
try
{
FileVersionInfo enlistmentVersion = FileVersionInfo.GetVersionInfo(enlistmentHookPath);
FileVersionInfo installedVersion = FileVersionInfo.GetVersionInfo(installedHookPath);
copyHook = enlistmentVersion.FileVersion != installedVersion.FileVersion;
}
catch (Exception e)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", "Mount");
metadata.Add(nameof(enlistmentHookPath), enlistmentHookPath);
metadata.Add(nameof(installedHookPath), installedHookPath);
metadata.Add("Exception", e.ToString());
context.Tracer.RelatedError(metadata, "Failed to compare " + hook.Name + " version");
errorMessage = "Error comparing " + hook.Name + " versions. " + ConsoleHelper.GetGVFSLogMessage(context.Enlistment.EnlistmentRoot);
return false;
}
}
if (copyHook)
{
try
{
CopyHook(context, installedHookPath, enlistmentHookPath);
}
catch (Exception e)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", "Mount");
metadata.Add(nameof(enlistmentHookPath), enlistmentHookPath);
metadata.Add(nameof(installedHookPath), installedHookPath);
metadata.Add("Exception", e.ToString());
context.Tracer.RelatedError(metadata, "Failed to copy " + hook.Name + " to enlistment");
errorMessage = "Error copying " + hook.Name + " to enlistment. " + ConsoleHelper.GetGVFSLogMessage(context.Enlistment.EnlistmentRoot);
return false;
}
}
errorMessage = null;
return true;
}
public class HooksConfigurationException : Exception
{
public HooksConfigurationException(string message)
: base(message)
{
}
}
private class HookData
{
public HookData(string name, string path, string executableName)
{
this.Name = name;
this.Path = path;
this.ExecutableName = executableName;
}
public string Name { get; }
public string Path { get; }
public string ExecutableName { get; }
}
}
}