1
0
Fork 0
mirror of https://github.com/actions/checkout.git synced 2025-03-31 05:20:06 +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 os from 'os'
import * as path from 'path' import * as path from 'path'
import * as stateHelper from '../lib/state-helper' import * as stateHelper from '../lib/state-helper'
import {ConfigScope} from '../lib/git-command-manager'
import {IGitCommandManager} from '../lib/git-command-manager' import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitSourceSettings} from '../lib/git-source-settings' import {IGitSourceSettings} from '../lib/git-source-settings'
@ -730,16 +731,16 @@ async function setup(testName: string): Promise<void> {
checkout: jest.fn(), checkout: jest.fn(),
checkoutDetach: jest.fn(), checkoutDetach: jest.fn(),
config: jest.fn( config: jest.fn(
async (key: string, value: string, globalConfig?: boolean) => { async (key: string, value: string, configScope?: ConfigScope) => {
const configPath = globalConfig const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig') ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath : localGitConfigPath
await fs.promises.appendFile(configPath, `\n${key} ${value}`) await fs.promises.appendFile(configPath, `\n${key} ${value}`)
} }
), ),
configExists: jest.fn( configExists: jest.fn(
async (key: string, globalConfig?: boolean): Promise<boolean> => { async (key: string, configScope?: ConfigScope): Promise<boolean> => {
const configPath = globalConfig const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig') ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath : localGitConfigPath
const content = await fs.promises.readFile(configPath) const content = await fs.promises.readFile(configPath)
@ -774,8 +775,8 @@ async function setup(testName: string): Promise<void> {
tagExists: jest.fn(), tagExists: jest.fn(),
tryClean: jest.fn(), tryClean: jest.fn(),
tryConfigUnset: jest.fn( tryConfigUnset: jest.fn(
async (key: string, globalConfig?: boolean): Promise<boolean> => { async (key: string, configScope?: ConfigScope): Promise<boolean> => {
const configPath = globalConfig const configPath = configScope
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig') ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
: localGitConfigPath : localGitConfigPath
let content = await fs.promises.readFile(configPath) 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 stateHelper from './state-helper'
import * as urlHelper from './url-helper' import * as urlHelper from './url-helper'
import {default as uuid} from 'uuid/v4' import {default as uuid} from 'uuid/v4'
import {ConfigScope} from './git-command-manager'
import {IGitCommandManager} from './git-command-manager' import {IGitCommandManager} from './git-command-manager'
import {IGitSourceSettings} from './git-source-settings' import {IGitSourceSettings} from './git-source-settings'
@ -129,13 +130,13 @@ class GitAuthHelper {
const newGitConfigPath = await this.configureTempGlobalConfig() const newGitConfigPath = await this.configureTempGlobalConfig()
try { try {
// Configure the token // Configure the token
await this.configureToken(newGitConfigPath, true) await this.configureToken(newGitConfigPath, ConfigScope.System)
// Configure HTTPS instead of SSH // Configure HTTPS instead of SSH
await this.git.tryConfigUnset(this.insteadOfKey, true) await this.git.tryConfigUnset(this.insteadOfKey, ConfigScope.System)
if (!this.settings.sshKey) { if (!this.settings.sshKey) {
for (const insteadOfValue of this.insteadOfValues) { 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) { } catch (err) {
@ -143,7 +144,7 @@ class GitAuthHelper {
core.info( core.info(
'Encountered an error when attempting to configure token. Attempting unconfigure.' '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 throw err
} }
} }
@ -274,16 +275,16 @@ class GitAuthHelper {
private async configureToken( private async configureToken(
configPath?: string, configPath?: string,
globalConfig?: boolean configScope?: ConfigScope
): Promise<void> { ): Promise<void> {
// Validate args // Validate args
assert.ok( assert.ok(
(configPath && globalConfig) || (!configPath && !globalConfig), (configPath && configScope) || (!configPath && !configScope),
'Unexpected configureToken parameter combinations' 'Unexpected configureToken parameter combinations'
) )
// Default config path // Default config path
if (!configPath && !globalConfig) { if (!configPath && !configScope) {
configPath = path.join(this.git.getWorkingDirectory(), '.git', 'config') configPath = path.join(this.git.getWorkingDirectory(), '.git', 'config')
} }
@ -293,7 +294,7 @@ class GitAuthHelper {
await this.git.config( await this.git.config(
this.tokenConfigKey, this.tokenConfigKey,
this.tokenPlaceholderConfigValue, this.tokenPlaceholderConfigValue,
globalConfig configScope,
) )
// Replace the placeholder // Replace the placeholder

View file

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

View file

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