* Spawns a given Git command process. Does not throw if the command fails. Additionally, * if there is any stderr output, the output will be printed. This makes it easier to * info failed commands.
(args: string[], options: GitCommandRunOptions = {})
| 82 | } |
| 83 | // Omit `status` from the type so that it's obvious that the status is never |
| 84 | // non-zero as explained in the method description. |
| 85 | return result as Omit<SpawnSyncReturns<string>, 'status'>; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Spawns a given Git command process. Does not throw if the command fails. Additionally, |
| 90 | * if there is any stderr output, the output will be printed. This makes it easier to |
| 91 | * info failed commands. |
| 92 | */ |
| 93 | runGraceful(args: string[], options: GitCommandRunOptions = {}): SpawnSyncReturns<string> { |
| 94 | /** The git command to be run. */ |
| 95 | const gitCommand = args[0]; |
| 96 | |
| 97 | if (isDryRun() && gitCommand === 'push') { |
| 98 | Log.debug(`"git push" is not able to be run in dryRun mode.`); |
| 99 | throw new DryRunError(); |
| 100 | } |
| 101 | |
| 102 | // Clear the credential helper that is used, preventing the temporary token from being saved as a |
| 103 | // valid token for future use. |
| 104 | // Point the hooks path at a location which can never hold hooks. Commands such as `pr checkout` |
| 105 | // and `pr rebase` check out pull request content into the working tree, and hooks provided by |
| 106 | // that content must not be executed. |
| 107 | args = ['-c', 'credential.helper=', '-c', 'core.hooksPath=/dev/null', ...args]; |
| 108 | // To improve the debugging experience in case something fails, we print all executed Git |
| 109 | // commands at the DEBUG level to better understand the git actions occurring. |
| 110 | // Note that we sanitize the command before printing it to the console. We do not want to |
| 111 | // print an access token if it is contained in the command. It's common to share errors with |
| 112 | // others if the tool failed, and we do not want to leak tokens. |
| 113 | Log.debug('Executing: git', this.sanitizeConsoleOutput(args.join(' '))); |
| 114 | |
| 115 | const excludedDirs = Array.from(new Set([this.baseDir, ...getExcludedDirsForCwd(options.cwd)])); |
| 116 | const env = sanitizeEnvironmentPath(options.env ?? process.env, excludedDirs); |
| 117 | let gitExecutable = this.gitBinPath; |
| 118 | if (gitExecutable === 'git') { |
| 119 | const resolutionKey = `${getPathFromEnvironment(env)}\0${excludedDirs.join('\0')}`; |
| 120 | if (this.cachedGitResolution?.key !== resolutionKey) { |
| 121 | this.cachedGitResolution = { |
| 122 | key: resolutionKey, |
| 123 | binPath: resolveSafeExecutable('git', env, excludedDirs), |
| 124 | }; |
| 125 | } |
| 126 | if (this.cachedGitResolution.binPath === null) { |
| 127 | const missingGitError = Object.assign( |
| 128 | new Error('Unable to locate a system git executable outside the repository.'), |
| 129 | {code: 'ENOENT'}, |
| 130 | ); |
| 131 | Log.debug('Process Error:', this.sanitizeConsoleOutput(missingGitError.message)); |
| 132 | if (missingGitError.stack) { |
| 133 | Log.debug('Process Error Stack:', this.sanitizeConsoleOutput(missingGitError.stack)); |
| 134 | } |
| 135 | process.stderr.write(this.sanitizeConsoleOutput(missingGitError.message)); |
| 136 | return { |
| 137 | pid: 0, |
| 138 | output: ['', '', ''], |
| 139 | stdout: '', |
no test coverage detected