Skip to content

Commit d6834d6

Browse files
authored
Merge pull request #731 from ldennington/ignore-homebrew-git
Ignore Homebrew Git shim
2 parents 4f2608a + f39343b commit d6834d6

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

src/osx/Installer.Mac/uninstall.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212

1313
# Unconfigure (as the current user)
1414
echo "Unconfiguring credential helper..."
15-
sudo -u `/usr/bin/logname` "$GCMBIN" unconfigure
15+
sudo -u `/usr/bin/logname` -E "$GCMBIN" unconfigure
1616

1717
# Remove symlink
1818
if [ -L /usr/local/bin/git-credential-manager-core ]

src/shared/Core.Tests/EnvironmentTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using GitCredentialManager.Interop.Posix;
45
using GitCredentialManager.Interop.Windows;
56
using GitCredentialManager.Tests.Objects;
@@ -123,5 +124,31 @@ public void PosixEnvironment_TryLocateExecutable_ExistsMultiple_ReturnTrueAndFir
123124
Assert.True(actualResult);
124125
Assert.Equal(expectedPath, actualPath);
125126
}
127+
128+
[PlatformFact(Platforms.MacOS)]
129+
public void MacOSEnvironment_TryLocateExecutable_Paths_Are_Ignored()
130+
{
131+
List<string> pathsToIgnore = new List<string>()
132+
{
133+
"/home/john.doe/bin/foo"
134+
};
135+
string expectedPath = "/usr/local/bin/foo";
136+
137+
var fs = new TestFileSystem
138+
{
139+
Files = new Dictionary<string, byte[]>
140+
{
141+
[pathsToIgnore.FirstOrDefault()] = Array.Empty<byte>(),
142+
[expectedPath] = Array.Empty<byte>(),
143+
}
144+
};
145+
var envars = new Dictionary<string, string> {["PATH"] = PosixPathVar};
146+
var env = new PosixEnvironment(fs, envars);
147+
148+
bool actualResult = env.TryLocateExecutable(PosixExecName, pathsToIgnore, out string actualPath);
149+
150+
Assert.True(actualResult);
151+
Assert.Equal(expectedPath, actualPath);
152+
}
126153
}
127154
}

src/shared/Core/CommandContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public CommandContext(string appPath)
107107
FileSystem = new MacOSFileSystem();
108108
SessionManager = new MacOSSessionManager();
109109
SystemPrompts = new MacOSSystemPrompts();
110-
Environment = new PosixEnvironment(FileSystem);
110+
Environment = new MacOSEnvironment(FileSystem);
111111
Terminal = new MacOSTerminal(Trace);
112112
string gitPath = GetGitPath(Environment, FileSystem, Trace);
113113
Git = new GitProcess(

src/shared/Core/EnvironmentBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ public virtual Process CreateProcess(string path, string args, bool useShellExec
104104
return new Process { StartInfo = psi };
105105
}
106106

107-
public bool TryLocateExecutable(string program, out string path)
107+
public virtual bool TryLocateExecutable(string program, out string path)
108+
{
109+
return TryLocateExecutable(program, null, out path);
110+
}
111+
112+
internal virtual bool TryLocateExecutable(string program, ICollection<string> pathsToIgnore, out string path)
108113
{
109114
// On UNIX-like systems we would normally use the "which" utility to locate a program,
110115
// but since distributions don't always place "which" in a consistent location we cannot
@@ -124,7 +129,8 @@ public bool TryLocateExecutable(string program, out string path)
124129
foreach (var basePath in paths)
125130
{
126131
string candidatePath = Path.Combine(basePath, program);
127-
if (FileSystem.FileExists(candidatePath))
132+
if (FileSystem.FileExists(candidatePath) && (pathsToIgnore is null ||
133+
!pathsToIgnore.Contains(candidatePath, StringComparer.OrdinalIgnoreCase)))
128134
{
129135
path = candidatePath;
130136
return true;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Threading;
6+
using GitCredentialManager.Interop.Posix;
7+
8+
namespace GitCredentialManager.Interop.MacOS
9+
{
10+
public class MacOSEnvironment : PosixEnvironment
11+
{
12+
private ICollection<string> _pathsToIgnore;
13+
14+
public MacOSEnvironment(IFileSystem fileSystem)
15+
: base(fileSystem) { }
16+
17+
internal MacOSEnvironment(IFileSystem fileSystem, IReadOnlyDictionary<string, string> variables)
18+
: base(fileSystem)
19+
{
20+
EnsureArgument.NotNull(variables, nameof(variables));
21+
Variables = variables;
22+
}
23+
24+
public override bool TryLocateExecutable(string program, out string path)
25+
{
26+
if (_pathsToIgnore is null)
27+
{
28+
_pathsToIgnore = new List<string>();
29+
if (Variables.TryGetValue("HOMEBREW_PREFIX", out string homebrewPrefix))
30+
{
31+
string homebrewGit = Path.Combine(homebrewPrefix, "Homebrew/Library/Homebrew/shims/shared/git");
32+
_pathsToIgnore.Add(homebrewGit);
33+
}
34+
}
35+
return TryLocateExecutable(program, _pathsToIgnore, out path);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)