-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update logic to only overwrite when git config is also not set
- Loading branch information
pacotedev
committed
Jun 27, 2024
1 parent
6061bd4
commit 5f58104
Showing
3 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,57 @@ | ||
// Values we want to set if they're not already defined by the end user | ||
const defaultEnv = {} | ||
module.exports = (opts = {}) => ({ | ||
stdioString: true, | ||
...opts, | ||
shell: false, | ||
env: opts.env || { ...defaultEnv, ...process.env }, | ||
}) | ||
const fs = require('fs') | ||
const os = require('os') | ||
const path = require('path') | ||
const ini = require('ini') | ||
|
||
const gitConfigPath = path.join(os.homedir(), '.gitconfig') | ||
|
||
// Function to check if sshCommand is set in the git config | ||
const isGitSshCommandSetInConfig = () => { | ||
try { | ||
if (fs.existsSync(gitConfigPath)) { | ||
const config = ini.parse(fs.readFileSync(gitConfigPath, 'utf-8')) | ||
return config.core && config.core.sshCommand !== undefined | ||
} | ||
} catch (error) { | ||
return false | ||
} | ||
return false | ||
} | ||
|
||
// Function to check if askpass is set in the git config | ||
const isGitAskPassSetInConfig = () => { | ||
try { | ||
if (fs.existsSync(gitConfigPath)) { | ||
const config = ini.parse(fs.readFileSync(gitConfigPath, 'utf-8')) | ||
return config.core && config.core.askpass !== undefined | ||
} | ||
} catch (error) { | ||
return false | ||
} | ||
return false | ||
} | ||
|
||
module.exports = (opts = {}) => { | ||
const sshCommandSetInEnv = process.env.GIT_SSH_COMMAND !== undefined | ||
const sshCommandSetInConfig = isGitSshCommandSetInConfig() | ||
const askPassSetInEnv = process.env.GIT_ASKPASS !== undefined | ||
const askPassSetInConfig = isGitAskPassSetInConfig() | ||
|
||
// Values we want to set if they're not already defined by the end user | ||
// This defaults to accepting new ssh host key fingerprints | ||
const finalGitEnv = { | ||
...(askPassSetInEnv || askPassSetInConfig ? {} : { | ||
GIT_ASKPASS: 'echo', | ||
}), | ||
...(sshCommandSetInEnv || sshCommandSetInConfig ? {} : { | ||
GIT_SSH_COMMAND: 'ssh -oStrictHostKeyChecking=accept-new', | ||
}), | ||
} | ||
|
||
return { | ||
stdioString: true, | ||
...opts, | ||
shell: false, | ||
env: opts.env || { ...finalGitEnv, ...process.env }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters