Skip to content

Commit

Permalink
fix: fix addGitBashToEnv and addGitToEnv on Windows
Browse files Browse the repository at this point in the history
Functional changes:

- Include the mingw directory in the PATH
(this directory contains many of the actual git functions,
such as `git-pull`, `git-push`, and `git-upload-pack`).

- Search `env.ProgramW6432` for Git before `env.ProgramFiles`,
as `ProgramFiles` is not guaranteed to always point to
the x64 Program Files directory.

Technically, we should get the mingw path from `git --exec-path`
(and this is what the GitHub package does), but that would mean
making `git.addGitToEnv(env)` async,
which would have been a much larger PR.

From atom/apm#839

Co-Authored-By: Winston Liu <2766036+50Wliu@users.noreply.github.com>
  • Loading branch information
aminya and winstliu committed Aug 9, 2021
1 parent 8a34f8a commit 5888969
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,40 @@ function addPortableGitToEnv(env: Record<string, string | undefined>) {
if (child.indexOf("PortableGit_") === 0) {
const cmdPath = path.join(githubPath, child, "cmd")
const binPath = path.join(githubPath, child, "bin")
let corePath = path.join(githubPath, child, "mingw64", "libexec", "git-core")
if (!fs.isDirectorySync(corePath)) {
corePath = path.join(githubPath, child, "mingw32", "libexec", "git-core")
}

if (env.Path) {
env.Path += `${path.delimiter}${cmdPath}${path.delimiter}${binPath}`
env.Path += `${path.delimiter}${cmdPath}${path.delimiter}${binPath}${path.delimiter}${corePath}`
} else {
env.Path = `${cmdPath}${path.delimiter}${binPath}`
env.Path = `${cmdPath}${path.delimiter}${binPath}${path.delimiter}${corePath}`
}
break
}
}
}

function addGitBashToEnv(env: Record<string, string | undefined>) {
// First, check ProgramW6432, as it will _always_ point to the 64-bit Program Files directory
let gitPath: string
if (env.ProgramFiles) {
gitPath = path.join(env.ProgramFiles, "Git")
if (env.ProgramW6432) {
gitPath = path.join(env.ProgramW6432, "Git")
}

// Next, check ProgramFiles - this will point to:
// - x64 Program Files when running a 64-bit process on 64-bit Windows
// - x86 Program Files when running a 32-bit process on 64-bit Windows
// - x86 Program Files when running on 32-bit Windows
if (!fs.isDirectorySync(gitPath)) {
if (env.ProgramFiles) {
gitPath = path.join(env.ProgramFiles, "Git")
}
}

// Finally, check ProgramFiles(x86) to see if 32-bit Git was installed on 64-bit Windows

if (!fs.isDirectorySync(gitPath)) {
if (env["ProgramFiles(x86)"]) {
gitPath = path.join(env["ProgramFiles(x86)"], "Git")
Expand All @@ -55,10 +73,15 @@ function addGitBashToEnv(env: Record<string, string | undefined>) {

const cmdPath = path.join(gitPath, "cmd")
const binPath = path.join(gitPath, "bin")
let corePath = path.join(gitPath, "mingw64", "libexec", "git-core")
if (!fs.isDirectorySync(corePath)) {
corePath = path.join(gitPath, "mingw32", "libexec", "git-core")
}

if (env.Path) {
return (env.Path += `${path.delimiter}${cmdPath}${path.delimiter}${binPath}`)
return (env.Path += `${path.delimiter}${cmdPath}${path.delimiter}${binPath}${path.delimiter}${corePath}`)
} else {
return (env.Path = `${cmdPath}${path.delimiter}${binPath}`)
return (env.Path = `${cmdPath}${path.delimiter}${binPath}${path.delimiter}${corePath}`)
}
}

Expand Down

0 comments on commit 5888969

Please sign in to comment.