diff --git a/README.md b/README.md index 3028201..443344c 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,11 @@ The following inputs can be used to control the action's behavior: * `ssh-agent-cmd`: Optional. Use this to specify a custom location for the `ssh-agent` binary. * `ssh-add-cmd`: Optional. Use this to specify a custom location for the `ssh-add` binary. * `git-cmd`: Optional. Use this to specify a custom location for the `git` binary. +* `git-global-config`: Optional. Alter the global git config? If `false` only the git config in the current working directory will be altered. Defaults to `false`. + > [!IMPORTANT] + > + > Using this option also sets the `GIT_CONFIG_NOSYSTEM` environment variable. + > This prevents the global git config from being read from the system-wide configuration file. ## Exported variables diff --git a/action.yml b/action.yml index 8f32cc8..5699842 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,9 @@ inputs: git-cmd: description: 'git command' required: false + git-global-config: + description: 'Alter the global git configuration?' + default: false runs: using: 'node20' main: 'dist/index.js' diff --git a/cleanup.js b/cleanup.js index 7c7b6a8..338608f 100644 --- a/cleanup.js +++ b/cleanup.js @@ -1,6 +1,11 @@ const { execSync, execFileSync } = require("node:child_process"); const { keyFilePrefix } = require("./consts.js"); -const { gitCmd, homePath, sshAgentCmd } = require("./paths.js"); +const { + gitCmd, + gitGlobalConfig, + homePath, + sshAgentCmd, +} = require("./paths.js"); const { alterGitConfigWithRetry } = require("./utils.js"); const fs = require("node:fs"); const os = require("node:os"); @@ -20,7 +25,7 @@ function restoreGitConfig(maxTries = 3) { console.log("Restoring git config"); const result = alterGitConfigWithRetry(() => { return execSync( - `${gitCmd} config --global --get-regexp ".git@${keyFilePrefix}."`, + `${gitCmd} config${gitGlobalConfig} --get-regexp ".git@${keyFilePrefix}."`, ); }); const sections = result @@ -34,7 +39,7 @@ function restoreGitConfig(maxTries = 3) { console.log(`Removing git config section ${section}`); alterGitConfigWithRetry(() => { return execSync( - `${gitCmd} config --global --remove-section ${section}`, + `${gitCmd} config${gitGlobalConfig} --remove-section ${section}`, ); }); } diff --git a/dist/cleanup.js b/dist/cleanup.js index fb798db..77bbe59 100644 --- a/dist/cleanup.js +++ b/dist/cleanup.js @@ -3432,6 +3432,7 @@ const defaults = const sshAgentCmdInput = core.getInput("ssh-agent-cmd"); const sshAddCmdInput = core.getInput("ssh-add-cmd"); const gitCmdInput = core.getInput("git-cmd"); +const gitGlobalConfigInput = core.getBooleanInput("git-global-config"); module.exports = { homePath: defaults.homePath, @@ -3439,6 +3440,7 @@ module.exports = { sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault, sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault, gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault, + gitGlobalConfig: gitGlobalConfigInput ? " --global" : "", }; @@ -3660,7 +3662,12 @@ module.exports = require("util"); var __webpack_exports__ = {}; const { execSync, execFileSync } = __nccwpck_require__(421); const { keyFilePrefix } = __nccwpck_require__(334); -const { gitCmd, homePath, sshAgentCmd } = __nccwpck_require__(644); +const { + gitCmd, + gitGlobalConfig, + homePath, + sshAgentCmd, +} = __nccwpck_require__(644); const { alterGitConfigWithRetry } = __nccwpck_require__(561); const fs = __nccwpck_require__(24); const os = __nccwpck_require__(161); @@ -3680,7 +3687,7 @@ function restoreGitConfig(maxTries = 3) { console.log("Restoring git config"); const result = alterGitConfigWithRetry(() => { return execSync( - `${gitCmd} config --global --get-regexp ".git@${keyFilePrefix}."`, + `${gitCmd} config${gitGlobalConfig} --get-regexp ".git@${keyFilePrefix}."`, ); }); const sections = result @@ -3694,7 +3701,7 @@ function restoreGitConfig(maxTries = 3) { console.log(`Removing git config section ${section}`); alterGitConfigWithRetry(() => { return execSync( - `${gitCmd} config --global --remove-section ${section}`, + `${gitCmd} config${gitGlobalConfig} --remove-section ${section}`, ); }); } diff --git a/dist/index.js b/dist/index.js index 6804cab..82dba96 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3432,6 +3432,7 @@ const defaults = const sshAgentCmdInput = core.getInput("ssh-agent-cmd"); const sshAddCmdInput = core.getInput("ssh-add-cmd"); const gitCmdInput = core.getInput("git-cmd"); +const gitGlobalConfigInput = core.getBooleanInput("git-global-config"); module.exports = { homePath: defaults.homePath, @@ -3439,6 +3440,7 @@ module.exports = { sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault, sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault, gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault, + gitGlobalConfig: gitGlobalConfigInput ? " --global" : "", }; @@ -3670,7 +3672,13 @@ const core = __nccwpck_require__(484); const child_process = __nccwpck_require__(421); const fs = __nccwpck_require__(24); const crypto = __nccwpck_require__(598); -const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = __nccwpck_require__(644); +const { + gitGlobalConfig, + homePath, + sshAgentCmd, + sshAddCmd, + gitCmd, +} = __nccwpck_require__(644); const { keyFilePrefix } = __nccwpck_require__(334); const { alterGitConfigWithRetry } = __nccwpck_require__(561); @@ -3750,17 +3758,17 @@ try { alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`, ); }); alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`, ); }); alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`, ); }); @@ -3772,6 +3780,10 @@ try { `Added deploy-key mapping: Use identity '${homeSsh}/${keyFile}' for GitHub repository ${ownerAndRepo}`, ); }); + core.info( + "Setting GIT_CONFIG_NOSYSTEM. This prevents the global git config from being read from the system-wide configuration file.", + ); + core.exportVariable("GIT_CONFIG_NOSYSTEM", "1"); } catch (error) { if (error.code === "ENOENT") { console.log( diff --git a/index.js b/index.js index 0f3155f..2af33d5 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,13 @@ const core = require("@actions/core"); const child_process = require("node:child_process"); const fs = require("node:fs"); const crypto = require("node:crypto"); -const { homePath, sshAgentCmd, sshAddCmd, gitCmd } = require("./paths.js"); +const { + gitGlobalConfig, + homePath, + sshAgentCmd, + sshAddCmd, + gitCmd, +} = require("./paths.js"); const { keyFilePrefix } = require("./consts.js"); const { alterGitConfigWithRetry } = require("./utils.js"); @@ -82,17 +88,17 @@ try { alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --replace-all url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "https://github.com/${ownerAndRepo}"`, ); }); alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "git@github.com:${ownerAndRepo}"`, ); }); alterGitConfigWithRetry(() => { return child_process.execSync( - `${gitCmd} config --global --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`, + `${gitCmd} config${gitGlobalConfig} --add url."git@${keyFile}.github.com:${ownerAndRepo}".insteadOf "ssh://git@github.com/${ownerAndRepo}"`, ); }); @@ -104,6 +110,10 @@ try { `Added deploy-key mapping: Use identity '${homeSsh}/${keyFile}' for GitHub repository ${ownerAndRepo}`, ); }); + core.info( + "Setting GIT_CONFIG_NOSYSTEM. This prevents the global git config from being read from the system-wide configuration file.", + ); + core.exportVariable("GIT_CONFIG_NOSYSTEM", "1"); } catch (error) { if (error.code === "ENOENT") { console.log( diff --git a/paths.js b/paths.js index 1d0fcf8..6f195c6 100644 --- a/paths.js +++ b/paths.js @@ -23,6 +23,7 @@ const defaults = const sshAgentCmdInput = core.getInput("ssh-agent-cmd"); const sshAddCmdInput = core.getInput("ssh-add-cmd"); const gitCmdInput = core.getInput("git-cmd"); +const gitGlobalConfigInput = core.getBooleanInput("git-global-config"); module.exports = { homePath: defaults.homePath, @@ -30,4 +31,5 @@ module.exports = { sshAgentCmdInput !== "" ? sshAgentCmdInput : defaults.sshAgentCmdDefault, sshAddCmd: sshAddCmdInput !== "" ? sshAddCmdInput : defaults.sshAddCmdDefault, gitCmd: gitCmdInput !== "" ? gitCmdInput : defaults.gitCmdDefault, + gitGlobalConfig: gitGlobalConfigInput ? " --global" : "", };