Skip to content

Commit

Permalink
Fix various problems with install path and app path variables (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew John Cheetham authored Dec 7, 2022
2 parents 3795b52 + b848716 commit 79087fd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/shared/Core/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static string GetEntryApplicationPath()

public static string GetInstallationDirectory()
{
return Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
return AppContext.BaseDirectory;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/shared/Core/Authentication/AuthenticationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ protected bool TryFindHelperCommand(string envar, string configName, string defa

Context.Trace.WriteLine($"UI helper override specified: '{helperName}'.");
}
else if (string.IsNullOrWhiteSpace(defaultValue))
{
Context.Trace.WriteLine("No default UI supplied.");
return false;
}
else
{
Context.Trace.WriteLine($"Using default UI helper: '{defaultValue}'.");
Expand Down
5 changes: 4 additions & 1 deletion src/shared/Core/Interop/Windows/WindowsEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ internal WindowsEnvironment(IFileSystem fileSystem, IReadOnlyDictionary<string,

protected override string[] SplitPathVariable(string value)
{
return value.Split(';');
// Ensure we don't return empty values here - callers may use this as the base
// path for `Path.Combine(..)`, for which an empty value means 'current directory'.
// We only ever want to use the current directory for path resolution explicitly.
return value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}

public override void AddDirectoryToPath(string directoryPath, EnvironmentVariableTarget target)
Expand Down
6 changes: 5 additions & 1 deletion src/shared/Core/PlatformUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ private static string GetWindowsEntryPath()
IntPtr argv0Ptr = Marshal.ReadIntPtr(argvPtr);
string argv0 = Marshal.PtrToStringAuto(argv0Ptr);
Interop.Windows.Native.Kernel32.LocalFree(argvPtr);
return argv0;

// If this isn't absolute then we should return null to prevent any
// caller that expect only an absolute path from mis-using this result.
// They will have to fall-back to other mechanisms for getting the entry path.
return Path.IsPathRooted(argv0) ? argv0 : null;
}

#endregion
Expand Down
24 changes: 16 additions & 8 deletions src/shared/Git-Credential-Manager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,24 @@ public static void Main(string[] args)
//
// On UNIX systems we do the same check, except instead of a copy we use a symlink.
//
string oldName = PlatformUtils.IsWindows()
? "git-credential-manager-core.exe"
: "git-credential-manager-core";

if (appPath?.EndsWith(oldName, StringComparison.OrdinalIgnoreCase) ?? false)
if (!string.IsNullOrWhiteSpace(appPath))
{
context.Streams.Error.WriteLine(
"warning: git-credential-manager-core was renamed to git-credential-manager");
context.Streams.Error.WriteLine(
$"warning: see {Constants.HelpUrls.GcmExecRename} for more information");
// Trim any (.exe) file extension if we're on Windows
// Note that in some circumstances (like being called by Git when config is set
// to just `helper = manager-core`) we don't always have ".exe" at the end.
if (PlatformUtils.IsWindows() && appPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
appPath = appPath.Substring(0, appPath.Length - 4);
}

if (appPath.EndsWith("git-credential-manager-core", StringComparison.OrdinalIgnoreCase))
{
context.Streams.Error.WriteLine(
"warning: git-credential-manager-core was renamed to git-credential-manager");
context.Streams.Error.WriteLine(
$"warning: see {Constants.HelpUrls.GcmExecRename} for more information");
}
}

// Register all supported host providers at the normal priority.
Expand Down

0 comments on commit 79087fd

Please sign in to comment.