Skip to content

Fix various problems with install path and app path variables #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fantastic that you tested all those scenarios and wrote about it in the commit message. Otherwise I would have had concerns that this break things left and right. But that testing builds confidence in the correctness of this change!

}

/// <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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is so much easier than what I would have done. 👍

}

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// caller that expect only an absolute path from mis-using this result.
// caller that expects 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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about ignoring the case here.

{
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