-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathCommandContext.cs
More file actions
250 lines (205 loc) · 8.79 KB
/
CommandContext.cs
File metadata and controls
250 lines (205 loc) · 8.79 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using GitCredentialManager.Interop.Linux;
using GitCredentialManager.Interop.MacOS;
using GitCredentialManager.Interop.Posix;
using GitCredentialManager.Interop.Windows;
namespace GitCredentialManager
{
/// <summary>
/// Represents the execution environment for a Git credential helper command.
/// </summary>
public interface ICommandContext : IDisposable
{
/// <summary>
/// Absolute path the application entry executable.
/// </summary>
string ApplicationPath { get; set; }
/// <summary>
/// Absolute path to the Git Credential Manager installation directory.
/// </summary>
string InstallationDirectory { get; }
/// <summary>
/// Settings and configuration for Git Credential Manager.
/// </summary>
ISettings Settings { get; }
/// <summary>
/// Standard I/O text streams, typically connected to the parent Git process.
/// </summary>
IStandardStreams Streams { get; }
/// <summary>
/// The attached terminal (TTY) to this process tree.
/// </summary>
ITerminal Terminal { get; }
/// <summary>
/// Provides services regarding user sessions.
/// </summary>
ISessionManager SessionManager { get; }
/// <summary>
/// Application tracing system.
/// </summary>
ITrace Trace { get; }
/// <summary>
/// Application TRACE2 tracing system.
/// </summary>
ITrace2 Trace2 { get; }
/// <summary>
/// File system abstraction (exists mainly for testing).
/// </summary>
IFileSystem FileSystem { get; }
/// <summary>
/// Secure credential storage.
/// </summary>
ICredentialStore CredentialStore { get; }
/// <summary>
/// Factory for creating new <see cref="System.Net.Http.HttpClient"/> instances.
/// </summary>
IHttpClientFactory HttpClientFactory { get; }
/// <summary>
/// Component for interacting with Git.
/// </summary>
IGit Git { get; }
/// <summary>
/// The current process environment.
/// </summary>
IEnvironment Environment { get; }
/// <summary>
/// Process manager.
/// </summary>
IProcessManager ProcessManager { get; }
}
/// <summary>
/// Real command execution environment using the actual <see cref="Console"/>, file system calls and environment.
/// </summary>
public class CommandContext : DisposableObject, ICommandContext
{
public CommandContext()
{
ApplicationPath = GetEntryApplicationPath();
InstallationDirectory = GetInstallationDirectory();
Streams = new StandardStreams();
Trace = new Trace();
Trace2 = new Trace2(this);
if (PlatformUtils.IsWindows())
{
FileSystem = new WindowsFileSystem();
Environment = new WindowsEnvironment(FileSystem);
SessionManager = new WindowsSessionManager(Environment, FileSystem);
ProcessManager = new WindowsProcessManager(Trace2);
Terminal = new WindowsTerminal(Trace, Trace2);
string gitPath = GetGitPath(Environment, FileSystem, Trace);
Git = new GitProcess(
Trace,
Trace2,
ProcessManager,
gitPath,
FileSystem.GetCurrentDirectory()
);
Settings = new WindowsSettings(Environment, Git, Trace);
}
else if (PlatformUtils.IsMacOS())
{
FileSystem = new MacOSFileSystem();
Environment = new MacOSEnvironment(FileSystem);
SessionManager = new MacOSSessionManager(Environment, FileSystem);
ProcessManager = new ProcessManager(Trace2);
Terminal = new MacOSTerminal(Trace, Trace2);
string gitPath = GetGitPath(Environment, FileSystem, Trace);
Git = new GitProcess(
Trace,
Trace2,
ProcessManager,
gitPath,
FileSystem.GetCurrentDirectory()
);
Settings = new MacOSSettings(Environment, Git, Trace);
}
else if (PlatformUtils.IsLinux())
{
FileSystem = new LinuxFileSystem();
Environment = new PosixEnvironment(FileSystem);
SessionManager = new LinuxSessionManager(Environment, FileSystem);
ProcessManager = new ProcessManager(Trace2);
Terminal = new LinuxTerminal(Trace, Trace2);
string gitPath = GetGitPath(Environment, FileSystem, Trace);
Git = new GitProcess(
Trace,
Trace2,
ProcessManager,
gitPath,
FileSystem.GetCurrentDirectory()
);
Settings = new LinuxSettings(Environment, Git, Trace, FileSystem);
}
else
{
throw new PlatformNotSupportedException();
}
HttpClientFactory = new HttpClientFactory(FileSystem, Trace, Trace2, Settings, Streams);
CredentialStore = new CredentialStore(this);
}
private static string GetGitPath(IEnvironment environment, IFileSystem fileSystem, ITrace trace)
{
const string unixGitName = "git";
const string winGitName = "git.exe";
string gitExecPath;
string programName = PlatformUtils.IsWindows() ? winGitName : unixGitName;
// Use the GIT_EXEC_PATH environment variable if set
if (environment.Variables.TryGetValue(Constants.EnvironmentVariables.GitExecutablePath,
out gitExecPath))
{
// If we're invoked from WSL we must locate the UNIX Git executable
if (PlatformUtils.IsWindows() && WslUtils.IsWslPath(gitExecPath))
{
programName = unixGitName;
}
string candidatePath = Path.Combine(gitExecPath, programName);
if (fileSystem.FileExists(candidatePath))
{
trace.WriteLine($"Using Git executable from GIT_EXEC_PATH: {candidatePath}");
return candidatePath;
}
}
// Otherwise try to locate the git(.exe) on the current PATH
gitExecPath = environment.LocateExecutable(programName);
trace.WriteLine($"Using PATH-located Git executable: {gitExecPath}");
return gitExecPath;
}
#region ICommandContext
public string ApplicationPath { get; set; }
public string InstallationDirectory { get; }
public ISettings Settings { get; }
public IStandardStreams Streams { get; }
public ITerminal Terminal { get; }
public ISessionManager SessionManager { get; }
public ITrace Trace { get; }
public ITrace2 Trace2 { get; }
public IFileSystem FileSystem { get; }
public ICredentialStore CredentialStore { get; }
public IHttpClientFactory HttpClientFactory { get; }
public IGit Git { get; }
public IEnvironment Environment { get; }
public IProcessManager ProcessManager { get; }
#endregion
#region IDisposable
protected override void ReleaseManagedResources()
{
Settings?.Dispose();
Trace?.Dispose();
base.ReleaseManagedResources();
}
#endregion
public static string GetEntryApplicationPath()
{
return PlatformUtils.GetNativeEntryPath() ??
Process.GetCurrentProcess().MainModule?.FileName ??
System.Environment.GetCommandLineArgs()[0];
}
public static string GetInstallationDirectory()
{
return AppContext.BaseDirectory;
}
}
}