Files
renovate/lib/util/git/config.ts
Thomas Andersen 86890d2142 refactor(git): extract runtime config appending (#45165)
* refactor(git): centralize runtime config environment

Preserve caller-supplied and inherited GIT_CONFIG_KEY_n and GIT_CONFIG_VALUE_n entries when appending runtime Git configuration.

Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>

* refactor(git): extract runtime config appending

* style(git): format runtime config tests

* test(git): remove superseded environment cases

* Apply suggestion from `@RahulGautamSingh`

Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>

* test(git): align invalid config count warning

---------

Co-authored-by: MrVampy <4302946+MrVampy@users.noreply.github.com>
Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>
Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>
2026-09-14 06:42:38 +00:00

87 lines
2.5 KiB
TypeScript

import { isArray, isNumber, isString } from '@sindresorhus/is';
import type { SimpleGitOptions } from 'simple-git';
import { GlobalConfig } from '../../config/global.ts';
import { logger } from '../../logger/index.ts';
import { getEnv } from '../env.ts';
import type { ResolvedChildEnv } from '../exec/utils.ts';
import type { GitNoVerifyOption } from './types.ts';
let noVerify: GitNoVerifyOption[] = ['push', 'commit'];
export function setNoVerify(value: GitNoVerifyOption[]): void {
if (!isArray(value, isString)) {
throw new Error('config error: gitNoVerify should be an array of strings');
}
noVerify = value;
}
export function getNoVerify(): GitNoVerifyOption[] {
return noVerify;
}
export function simpleGitConfig(): Partial<SimpleGitOptions> {
const unsafe: SimpleGitOptions['unsafe'] = {
allowUnsafeSshCommand: true, // For custom `GIT_SSH_COMMAND`.
allowUnsafeConfigEnvCount: true, // For custom `GIT_CONFIG_COUNT`, `GIT_CONFIG_KEY_*` and `GIT_CONFIG_VALUE_*`.
allowUnsafeAskPass: true, // For custom `GIT_ASKPASS`
};
if (getEnv().RENOVATE_X_CLEAR_HOOKS) {
unsafe.allowUnsafeHooksPath = true;
}
const config: Partial<SimpleGitOptions> = {
completion: {
onClose: true,
onExit: false,
},
config: ['core.quotePath=false'],
unsafe,
};
// https://github.com/steveukx/git-js/pull/591
const gitTimeout = GlobalConfig.get('gitTimeout');
if (isNumber(gitTimeout) && gitTimeout > 0) {
config.timeout = { block: gitTimeout };
}
return config;
}
export interface GitConfigEntry {
key: string;
value: string;
}
export function addGitConfigEnvironmentVariables(
environment: Readonly<ResolvedChildEnv>,
entries: readonly GitConfigEntry[],
): ResolvedChildEnv {
let gitConfigCount = getGitConfigCount(environment.GIT_CONFIG_COUNT);
const newEnvironment = { ...environment };
for (const entry of entries) {
newEnvironment[`GIT_CONFIG_KEY_${gitConfigCount}`] = entry.key;
newEnvironment[`GIT_CONFIG_VALUE_${gitConfigCount}`] = entry.value;
gitConfigCount++;
}
newEnvironment.GIT_CONFIG_COUNT = gitConfigCount.toString();
return newEnvironment;
}
function getGitConfigCount(value: string | undefined): number {
if (!value) {
return 0;
}
const gitConfigCount = parseInt(value, 10);
if (Number.isNaN(gitConfigCount)) {
logger.warn(
{ GIT_CONFIG_COUNT: value },
`Found GIT_CONFIG_COUNT env variable, but could not parse the value to an integer. Ignoring it.`,
);
return 0;
}
return gitConfigCount;
}