Skip to content

Commit

Permalink
Fixes git search for gitkraken#734
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 17, 2019
1 parent be6325f commit 030b6e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/git/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ function parseVersion(raw: string): string {
}

async function findSpecificGit(path: string): Promise<GitLocation> {
const version = await run<string>(path, ['--version'], 'utf8');
let version = await run<string>(path, ['--version'], 'utf8');
// If needed, let's update our path to avoid the search on every command
if (!path || path === 'git') {
path = findExecutable(path, ['--version']).cmd;
const foundPath = findExecutable(path, ['--version']).cmd;

// Ensure that the path we found works
version = await run<string>(foundPath, ['--version'], 'utf8');
path = foundPath;
}

return {
Expand Down
16 changes: 14 additions & 2 deletions src/git/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ function runDownPath(exe: string): string {

const target = paths.join('.', exe);
try {
if (fs.statSync(target)) return target;
const stats = fs.statSync(target);
if (stats && stats.isFile() && isExecutable(stats)) return target;
}
catch {}

const path = process.env.PATH;
if (path != null && path.length !== 0) {
const haystack = path.split(isWindows ? ';' : ':');
let stats;
for (const p of haystack) {
const needle = paths.join(p, exe);
try {
if (fs.statSync(needle)) return needle;
stats = fs.statSync(needle);
if (stats && stats.isFile() && isExecutable(stats)) return needle;
}
catch {}
}
Expand All @@ -44,6 +47,15 @@ function runDownPath(exe: string): string {
return exe;
}

function isExecutable(stats: fs.Stats) {
if (isWindows) return true;

const isGroup = stats.gid ? process.getgid && stats.gid === process.getgid() : true;
const isUser = stats.uid ? process.getuid && stats.uid === process.getuid() : true;

return Boolean(stats.mode & 0o0001 || (stats.mode & 0o0010 && isGroup) || (stats.mode & 0o0100 && isUser));
}

/**
* Finds the executable and parameters to run on Windows. This method
* mimics the POSIX behavior of being able to run scripts as executables by
Expand Down

0 comments on commit 030b6e7

Please sign in to comment.