0
1
mirror of https://github.com/actions/checkout synced 2024-06-01 18:38:03 +02:00

Change git config scope from global to system

This commit is contained in:
Milad Irannejad 2023-03-01 17:36:47 -05:00
parent ac59398561
commit dc3f9aa6d6
No known key found for this signature in database
GPG Key ID: 83D922682700B3AC
4 changed files with 34 additions and 25 deletions

View File

@ -5,6 +5,7 @@ import * as io from '@actions/io'
import * as os from 'os'
import * as path from 'path'
import * as stateHelper from '../lib/state-helper'
import {ConfigScope} from '../lib/git-command-manager'
import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitSourceSettings} from '../lib/git-source-settings'
@ -730,16 +731,16 @@ async function setup(testName: string): Promise<void> {
checkout: jest.fn(),
checkoutDetach: jest.fn(),
config: jest.fn(
async (key: string, value: string, globalConfig?: boolean) => {
const configPath = globalConfig
async (key: string, value: string, configScope?: ConfigScope) => {
const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath
await fs.promises.appendFile(configPath, `\n${key} ${value}`)
}
),
configExists: jest.fn(
async (key: string, globalConfig?: boolean): Promise<boolean> => {
const configPath = globalConfig
async (key: string, configScope?: ConfigScope): Promise<boolean> => {
const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath
const content = await fs.promises.readFile(configPath)
@ -774,8 +775,8 @@ async function setup(testName: string): Promise<void> {
tagExists: jest.fn(),
tryClean: jest.fn(),
tryConfigUnset: jest.fn(
async (key: string, globalConfig?: boolean): Promise<boolean> => {
const configPath = globalConfig
async (key: string, configScope?: ConfigScope): Promise<boolean> => {
const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath
let content = await fs.promises.readFile(configPath)

View File

@ -9,6 +9,7 @@ import * as regexpHelper from './regexp-helper'
import * as stateHelper from './state-helper'
import * as urlHelper from './url-helper'
import {default as uuid} from 'uuid/v4'
import {ConfigScope} from './git-command-manager'
import {IGitCommandManager} from './git-command-manager'
import {IGitSourceSettings} from './git-source-settings'
@ -129,13 +130,13 @@ class GitAuthHelper {
const newGitConfigPath = await this.configureTempGlobalConfig()
try {
// Configure the token
await this.configureToken(newGitConfigPath, true)
await this.configureToken(newGitConfigPath, ConfigScope.System)
// Configure HTTPS instead of SSH
await this.git.tryConfigUnset(this.insteadOfKey, true)
await this.git.tryConfigUnset(this.insteadOfKey, ConfigScope.System)
if (!this.settings.sshKey) {
for (const insteadOfValue of this.insteadOfValues) {
await this.git.config(this.insteadOfKey, insteadOfValue, true, true)
await this.git.config(this.insteadOfKey, insteadOfValue, ConfigScope.System, true)
}
}
} catch (err) {
@ -143,7 +144,7 @@ class GitAuthHelper {
core.info(
'Encountered an error when attempting to configure token. Attempting unconfigure.'
)
await this.git.tryConfigUnset(this.tokenConfigKey, true)
await this.git.tryConfigUnset(this.tokenConfigKey, ConfigScope.System)
throw err
}
}
@ -274,16 +275,16 @@ class GitAuthHelper {
private async configureToken(
configPath?: string,
globalConfig?: boolean
configScope?: ConfigScope
): Promise<void> {
// Validate args
assert.ok(
(configPath && globalConfig) || (!configPath && !globalConfig),
(configPath && configScope) || (!configPath && !configScope),
'Unexpected configureToken parameter combinations'
)
// Default config path
if (!configPath && !globalConfig) {
if (!configPath && !configScope) {
configPath = path.join(this.git.getWorkingDirectory(), '.git', 'config')
}
@ -293,7 +294,7 @@ class GitAuthHelper {
await this.git.config(
this.tokenConfigKey,
this.tokenPlaceholderConfigValue,
globalConfig
configScope,
)
// Replace the placeholder

View File

@ -12,6 +12,12 @@ import {GitVersion} from './git-version'
// Wire protocol v2 not supported before 2.18
export const MinimumGitVersion = new GitVersion('2.18')
export enum ConfigScope {
Local = "local",
Global = "global",
System = "system",
}
export interface IGitCommandManager {
branchDelete(remote: boolean, branch: string): Promise<void>
branchExists(remote: boolean, pattern: string): Promise<boolean>
@ -21,10 +27,10 @@ export interface IGitCommandManager {
config(
configKey: string,
configValue: string,
globalConfig?: boolean,
configScope?: ConfigScope,
add?: boolean
): Promise<void>
configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
configExists(configKey: string, configScope?: ConfigScope): Promise<boolean>
fetch(refSpec: string[], fetchDepth?: number): Promise<void>
getDefaultBranch(repositoryUrl: string): Promise<string>
getWorkingDirectory(): string
@ -43,7 +49,7 @@ export interface IGitCommandManager {
submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
tagExists(pattern: string): Promise<boolean>
tryClean(): Promise<boolean>
tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
tryConfigUnset(configKey: string, configScope?: ConfigScope): Promise<boolean>
tryDisableAutomaticGarbageCollection(): Promise<boolean>
tryGetFetchUrl(): Promise<string>
tryReset(): Promise<boolean>
@ -172,10 +178,10 @@ class GitCommandManager {
async config(
configKey: string,
configValue: string,
globalConfig?: boolean,
configScope?: ConfigScope,
add?: boolean
): Promise<void> {
const args: string[] = ['config', globalConfig ? '--global' : '--local']
const args: string[] = ['config', configScope ? `--${configScope}` : '--local']
if (add) {
args.push('--add')
}
@ -185,13 +191,13 @@ class GitCommandManager {
async configExists(
configKey: string,
globalConfig?: boolean
configScope?: ConfigScope
): Promise<boolean> {
const pattern = regexpHelper.escape(configKey)
const output = await this.execGit(
[
'config',
globalConfig ? '--global' : '--local',
configScope ? `--${configScope}` : '--local',
'--name-only',
'--get-regexp',
pattern
@ -369,12 +375,12 @@ class GitCommandManager {
async tryConfigUnset(
configKey: string,
globalConfig?: boolean
configScope?: ConfigScope
): Promise<boolean> {
const output = await this.execGit(
[
'config',
globalConfig ? '--global' : '--local',
configScope ? `--${configScope}` : '--local',
'--unset-all',
configKey
],

View File

@ -9,6 +9,7 @@ import * as path from 'path'
import * as refHelper from './ref-helper'
import * as stateHelper from './state-helper'
import * as urlHelper from './url-helper'
import {ConfigScope} from './git-command-manager'
import {IGitCommandManager} from './git-command-manager'
import {IGitSourceSettings} from './git-source-settings'
@ -49,7 +50,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
)
await git
.config('safe.directory', settings.repositoryPath, true, true)
.config('safe.directory', settings.repositoryPath, ConfigScope.System, true)
.catch(error => {
core.info(
`Failed to initialize safe directory with error: ${error}`
@ -278,7 +279,7 @@ export async function cleanup(repositoryPath: string): Promise<void> {
)
await git
.config('safe.directory', repositoryPath, true, true)
.config('safe.directory', repositoryPath, ConfigScope.System, true)
.catch(error => {
core.info(`Failed to initialize safe directory with error: ${error}`)
})