Skip to content

Commit be49480

Browse files
authored
Merge pull request #406 from vdye/356-manually-specify-gpg-path
Add option for `GCM_GPG_PATH` environment variable (Linux-only)
2 parents 3a2f1ec + ddcab81 commit be49480

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

docs/environment.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,20 @@ export GCM_PLAINTEXT_STORE_PATH=/mnt/external-drive/credentials
398398

399399
---
400400

401+
### GCM_GPG_PATH
402+
403+
Specify the path (_including_ the executable name) to the version of `gpg` used by `pass` (`gpg2` if present, otherwise `gpg`). This is primarily meant to allow manual resolution of the conflict that occurs on legacy Linux systems with parallel installs of `gpg` and `gpg2`.
404+
405+
If not specified, GCM Core defaults to using the version of `gpg2` on the `$PATH`, falling back on `gpg` if `gpg2` is not found.
406+
407+
##### Linux
408+
409+
```bash
410+
export GCM_GPG_PATH="/usr/local/bin/gpg2"
411+
```
412+
413+
---
414+
401415
### GCM_MSAUTH_FLOW
402416

403417
Specify which authentication flow should be used when performing Microsoft authentication and an interactive flow is required.

src/shared/Microsoft.Git.CredentialManager/CommandContext.cs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public CommandContext(string appPath)
9595
SystemPrompts = new WindowsSystemPrompts();
9696
Environment = new WindowsEnvironment(FileSystem);
9797
Terminal = new WindowsTerminal(Trace);
98-
string gitPath = GetGitPath(Environment, FileSystem);
98+
string gitPath = GetGitPath(Environment, FileSystem, Trace);
9999
Git = new GitProcess(
100100
Trace,
101101
gitPath,
@@ -111,14 +111,15 @@ public CommandContext(string appPath)
111111
SystemPrompts = new MacOSSystemPrompts();
112112
Environment = new PosixEnvironment(FileSystem);
113113
Terminal = new PosixTerminal(Trace);
114-
string gitPath = GetGitPath(Environment, FileSystem);
114+
string gitPath = GetGitPath(Environment, FileSystem, Trace);
115115
Git = new GitProcess(
116116
Trace,
117117
gitPath,
118118
FileSystem.GetCurrentDirectory()
119119
);
120120
Settings = new Settings(Environment, Git);
121121
CredentialStore = new MacOSKeychain(Settings.CredentialNamespace);
122+
122123
}
123124
else if (PlatformUtils.IsLinux())
124125
{
@@ -128,17 +129,16 @@ public CommandContext(string appPath)
128129
SystemPrompts = new LinuxSystemPrompts();
129130
Environment = new PosixEnvironment(FileSystem);
130131
Terminal = new PosixTerminal(Trace);
131-
string gitPath = GetGitPath(Environment, FileSystem);
132+
string gitPath = GetGitPath(Environment, FileSystem, Trace);
132133
Git = new GitProcess(
133134
Trace,
134135
gitPath,
135136
FileSystem.GetCurrentDirectory()
136137
);
137138
Settings = new Settings(Environment, Git);
138-
IGpg gpg = new Gpg(
139-
Environment.LocateExecutable("gpg"),
140-
SessionManager
141-
);
139+
140+
string gpgPath = GetGpgPath(Environment, FileSystem, Trace);
141+
IGpg gpg = new Gpg(gpgPath, SessionManager);
142142
CredentialStore = new LinuxCredentialStore(FileSystem, Settings, SessionManager, gpg, Environment, Git);
143143
}
144144
else
@@ -152,23 +152,62 @@ public CommandContext(string appPath)
152152
SystemPrompts.ParentWindowId = Settings.ParentWindowId;
153153
}
154154

155-
private static string GetGitPath(IEnvironment environment, IFileSystem fileSystem)
155+
private static string GetGitPath(IEnvironment environment, IFileSystem fileSystem, ITrace trace)
156156
{
157+
string gitExecPath;
157158
string programName = PlatformUtils.IsWindows() ? "git.exe" : "git";
158159

159160
// Use the GIT_EXEC_PATH environment variable if set
160161
if (environment.Variables.TryGetValue(Constants.EnvironmentVariables.GitExecutablePath,
161-
out string gitExecPath))
162+
out gitExecPath))
162163
{
163164
string candidatePath = Path.Combine(gitExecPath, programName);
164165
if (fileSystem.FileExists(candidatePath))
165166
{
167+
trace.WriteLine($"Using Git executable from GIT_EXEC_PATH: {candidatePath}");
166168
return candidatePath;
167169
}
168170
}
169171

170172
// Otherwise try to locate the git(.exe) on the current PATH
171-
return environment.LocateExecutable(programName);
173+
gitExecPath = environment.LocateExecutable(programName);
174+
trace.WriteLine($"Using PATH-located Git executable: {gitExecPath}");
175+
return gitExecPath;
176+
}
177+
178+
private static string GetGpgPath(IEnvironment environment, IFileSystem fileSystem, ITrace trace)
179+
{
180+
string gpgPath;
181+
182+
// Use the GCM_GPG_PATH environment variable if set
183+
if (environment.Variables.TryGetValue(Constants.EnvironmentVariables.GpgExecutablePath,
184+
out gpgPath))
185+
{
186+
if (fileSystem.FileExists(gpgPath))
187+
{
188+
trace.WriteLine($"Using Git executable from GCM_GPG_PATH: {gpgPath}");
189+
return gpgPath;
190+
}
191+
else
192+
{
193+
throw new Exception($"GPG executable does not exist with path '{gpgPath}'");
194+
}
195+
196+
}
197+
198+
// If no explicit GPG path is specified, mimic the way `pass`
199+
// determines GPG dependency (use gpg2 if available, otherwise gpg)
200+
if (environment.TryLocateExecutable("gpg2", out string gpg2Path))
201+
{
202+
trace.WriteLine($"Using PATH-located GPG (gpg2) executable: {gpg2Path}");
203+
return gpg2Path;
204+
}
205+
else
206+
{
207+
gpgPath = environment.LocateExecutable("gpg");
208+
trace.WriteLine($"Using PATH-located GPG (gpg) executable: {gpgPath}");
209+
return gpgPath;
210+
}
172211
}
173212

174213
#region ICommandContext

src/shared/Microsoft.Git.CredentialManager/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static class EnvironmentVariables
5858
public const string GcmCredCacheOptions = "GCM_CREDENTIAL_CACHE_OPTIONS";
5959
public const string GcmPlaintextStorePath = "GCM_PLAINTEXT_STORE_PATH";
6060
public const string GitExecutablePath = "GIT_EXEC_PATH";
61+
public const string GpgExecutablePath = "GCM_GPG_PATH";
6162
}
6263

6364
public static class Http

0 commit comments

Comments
 (0)