Skip to content

Commit 583eb3b

Browse files
committed
apppath: introduce ICommandContext.InstallationDirectory
Introduce a new property on the ICommandContext that represents the GCM installation directory, separate from the application executable path. The app executable is not always found in the installation directory if the app exe path is a symlink (or we're a .NET tool where the AppHost is outside of the tool store).
1 parent ca19938 commit 583eb3b

File tree

15 files changed

+47
-42
lines changed

15 files changed

+47
-42
lines changed

src/shared/Atlassian.Bitbucket.UI.Avalonia/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ private static void AppMain(object o)
4646
string[] args = (string[]) o;
4747

4848
string appPath = ApplicationBase.GetEntryApplicationPath();
49-
using (var context = new CommandContext(appPath))
49+
string installDir = ApplicationBase.GetInstallationDirectory();
50+
using (var context = new CommandContext(appPath, installDir))
5051
using (var app = new HelperApplication(context))
5152
{
5253
app.RegisterCommand(new CredentialsCommandImpl(context));

src/shared/Core/Application.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ protected override async Task<int> RunInternalAsync(string[] args)
9797
Context.Trace.WriteLine($"Platform: {info.OperatingSystemType} ({info.CpuArchitecture})");
9898
Context.Trace.WriteLine($"OSVersion: {info.OperatingSystemVersion}");
9999
Context.Trace.WriteLine($"AppPath: {Context.ApplicationPath}");
100+
Context.Trace.WriteLine($"InstallDir: {Context.InstallationDirectory}");
100101
Context.Trace.WriteLine($"Arguments: {string.Join(" ", args)}");
101102

102103
var parser = new CommandLineBuilder(rootCommand)
@@ -252,25 +253,13 @@ Task IConfigurableComponent.UnconfigureAsync(ConfigurationTarget target)
252253
}
253254

254255
// Clear app entry
255-
config.UnsetAll(configLevel, helperKey, Regex.Escape(appPath));
256+
string appEntryValue = currentValues[appIndex];
257+
config.UnsetAll(configLevel, helperKey, Regex.Escape(appEntryValue));
256258
}
257259

258260
return Task.CompletedTask;
259261
}
260262

261-
private string GetGitConfigAppName()
262-
{
263-
const string gitCredentialPrefix = "git-credential-";
264-
265-
string appName = Path.GetFileNameWithoutExtension(Context.ApplicationPath);
266-
if (appName != null && appName.StartsWith(gitCredentialPrefix, StringComparison.OrdinalIgnoreCase))
267-
{
268-
return appName.Substring(gitCredentialPrefix.Length);
269-
}
270-
271-
return Context.ApplicationPath;
272-
}
273-
274263
private string GetGitConfigAppPath()
275264
{
276265
string path = Context.ApplicationPath;

src/shared/Core/ApplicationBase.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Reflection;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -83,18 +84,14 @@ public Task<int> RunAsync(string[] args)
8384

8485
public static string GetEntryApplicationPath()
8586
{
86-
string argv0 = PlatformUtils.GetNativeArgv0()
87-
?? Process.GetCurrentProcess().MainModule?.FileName
88-
?? Environment.GetCommandLineArgs()[0];
89-
90-
if (Path.IsPathRooted(argv0))
91-
{
92-
return argv0;
93-
}
87+
return PlatformUtils.GetNativeArgv0() ??
88+
Process.GetCurrentProcess().MainModule?.FileName ??
89+
Environment.GetCommandLineArgs()[0];
90+
}
9491

95-
return Path.GetFullPath(
96-
Path.Combine(Environment.CurrentDirectory, argv0)
97-
);
92+
public static string GetInstallationDirectory()
93+
{
94+
return Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
9895
}
9996

10097
/// <summary>

src/shared/Core/Authentication/AuthenticationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected bool TryFindHelperCommand(string envar, string configName, string defa
166166
//
167167
// Search the installation directory for an in-box helper
168168
//
169-
string appDir = Path.GetDirectoryName(Context.ApplicationPath);
169+
string appDir = Context.InstallationDirectory;
170170
string inBoxExePath = Path.Combine(appDir, PlatformUtils.IsWindows() ? $"{helperName}.exe" : helperName);
171171
string inBoxDllPath = Path.Combine(appDir, $"{helperName}.dll");
172172

src/shared/Core/CommandContext.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public interface ICommandContext : IDisposable
1717
/// </summary>
1818
string ApplicationPath { get; }
1919

20+
/// <summary>
21+
/// Absolute path to the Git Credential Manager installation directory.
22+
/// </summary>
23+
string InstallationDirectory { get; }
24+
2025
/// <summary>
2126
/// Settings and configuration for Git Credential Manager.
2227
/// </summary>
@@ -73,11 +78,13 @@ public interface ICommandContext : IDisposable
7378
/// </summary>
7479
public class CommandContext : DisposableObject, ICommandContext
7580
{
76-
public CommandContext(string appPath)
81+
public CommandContext(string appPath, string installDir)
7782
{
7883
EnsureArgument.NotNullOrWhiteSpace(appPath, nameof (appPath));
7984

8085
ApplicationPath = appPath;
86+
InstallationDirectory = installDir;
87+
8188
Streams = new StandardStreams();
8289
Trace = new Trace();
8390

@@ -172,6 +179,8 @@ private static string GetGitPath(IEnvironment environment, IFileSystem fileSyste
172179

173180
public string ApplicationPath { get; }
174181

182+
public string InstallationDirectory { get; }
183+
175184
public ISettings Settings { get; }
176185

177186
public IStandardStreams Streams { get; }

src/shared/Core/Commands/DiagnoseCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ private async Task<int> ExecuteAsync(string output)
8383
using var fullLog = new StreamWriter(logFilePath, append: false, Encoding.UTF8);
8484
fullLog.WriteLine("Diagnose log at {0:s}Z", DateTime.UtcNow);
8585
fullLog.WriteLine();
86-
fullLog.WriteLine($"Executable: {_context.ApplicationPath}");
86+
fullLog.WriteLine($"AppPath: {_context.ApplicationPath}");
87+
fullLog.WriteLine($"InstallDir: {_context.InstallationDirectory}");
8788
fullLog.WriteLine(
8889
TryGetAssemblyVersion(out string version)
8990
? $"Version: {version}"

src/shared/Git-Credential-Manager.UI.Avalonia/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ private static void AppMain(object o)
4343
string[] args = (string[]) o;
4444

4545
string appPath = ApplicationBase.GetEntryApplicationPath();
46-
using (var context = new CommandContext(appPath))
46+
string installDir = ApplicationBase.GetInstallationDirectory();
47+
using (var context = new CommandContext(appPath, installDir))
4748
using (var app = new HelperApplication(context))
4849
{
4950
app.RegisterCommand(new CredentialsCommandImpl(context));

src/shared/Git-Credential-Manager/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public static class Program
1212
public static void Main(string[] args)
1313
{
1414
string appPath = ApplicationBase.GetEntryApplicationPath();
15-
using (var context = new CommandContext(appPath))
15+
string installDir = ApplicationBase.GetInstallationDirectory();
16+
using (var context = new CommandContext(appPath, installDir))
1617
using (var app = new Application(context))
1718
{
1819
// Workaround for https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2560

src/shared/GitHub.UI.Avalonia/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ private static void AppMain(object o)
4545
string[] args = (string[]) o;
4646

4747
string appPath = ApplicationBase.GetEntryApplicationPath();
48-
using (var context = new CommandContext(appPath))
48+
string installDir = ApplicationBase.GetInstallationDirectory();
49+
using (var context = new CommandContext(appPath, installDir))
4950
using (var app = new HelperApplication(context))
5051
{
5152
app.RegisterCommand(new CredentialsCommandImpl(context));

src/shared/GitLab.UI.Avalonia/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ private static void AppMain(object o)
4545
string[] args = (string[]) o;
4646

4747
string appPath = ApplicationBase.GetEntryApplicationPath();
48-
using (var context = new CommandContext(appPath))
48+
string installDir = ApplicationBase.GetInstallationDirectory();
49+
using (var context = new CommandContext(appPath, installDir))
4950
using (var app = new HelperApplication(context))
5051
{
5152
app.RegisterCommand(new CredentialsCommandImpl(context));

0 commit comments

Comments
 (0)