2019-12-03 18:28:59 +03:00
import * as core from '@actions/core'
import * as exec from '@actions/exec'
2023-06-09 16:08:21 +03:00
import * as fs from 'fs'
2019-12-03 18:28:59 +03:00
import * as fshelper from './fs-helper'
import * as io from '@actions/io'
import * as path from 'path'
2020-05-27 16:54:28 +03:00
import * as refHelper from './ref-helper'
2020-03-05 22:21:59 +03:00
import * as regexpHelper from './regexp-helper'
2019-12-12 21:16:16 +03:00
import * as retryHelper from './retry-helper'
2019-12-03 18:28:59 +03:00
import { GitVersion } from './git-version'
2019-12-12 21:16:16 +03:00
// Auth header not supported before 2.9
// Wire protocol v2 not supported before 2.18
export const MinimumGitVersion = new GitVersion ( '2.18' )
2019-12-03 18:28:59 +03:00
export interface IGitCommandManager {
branchDelete ( remote : boolean , branch : string ) : Promise < void >
branchExists ( remote : boolean , pattern : string ) : Promise < boolean >
branchList ( remote : boolean ) : Promise < string [ ] >
2023-06-09 16:08:21 +03:00
sparseCheckout ( sparseCheckout : string [ ] ) : Promise < void >
sparseCheckoutNonConeMode ( sparseCheckout : string [ ] ) : Promise < void >
2019-12-03 18:28:59 +03:00
checkout ( ref : string , startPoint : string ) : Promise < void >
checkoutDetach ( ) : Promise < void >
2020-03-05 22:21:59 +03:00
config (
configKey : string ,
configValue : string ,
2021-11-01 19:43:18 +03:00
globalConfig? : boolean ,
add? : boolean
2020-03-05 22:21:59 +03:00
) : Promise < void >
configExists ( configKey : string , globalConfig? : boolean ) : Promise < boolean >
2023-06-09 16:08:21 +03:00
fetch (
refSpec : string [ ] ,
options : {
filter? : string
fetchDepth? : number
2023-08-16 23:34:54 +03:00
fetchTags? : boolean
2023-06-09 16:08:21 +03:00
}
) : Promise < void >
2020-06-18 17:20:33 +03:00
getDefaultBranch ( repositoryUrl : string ) : Promise < string >
2019-12-03 18:28:59 +03:00
getWorkingDirectory ( ) : string
init ( ) : Promise < void >
isDetached ( ) : Promise < boolean >
lfsFetch ( ref : string ) : Promise < void >
lfsInstall ( ) : Promise < void >
2020-09-23 16:41:47 +03:00
log1 ( format? : string ) : Promise < string >
2019-12-03 18:28:59 +03:00
remoteAdd ( remoteName : string , remoteUrl : string ) : Promise < void >
2020-03-05 22:21:59 +03:00
removeEnvironmentVariable ( name : string ) : void
2020-05-27 16:54:28 +03:00
revParse ( ref : string ) : Promise < string >
2020-03-02 19:33:30 +03:00
setEnvironmentVariable ( name : string , value : string ) : void
2020-05-27 16:54:28 +03:00
shaExists ( sha : string ) : Promise < boolean >
2020-03-05 22:21:59 +03:00
submoduleForeach ( command : string , recursive : boolean ) : Promise < string >
submoduleSync ( recursive : boolean ) : Promise < void >
submoduleUpdate ( fetchDepth : number , recursive : boolean ) : Promise < void >
2023-04-14 13:26:47 +03:00
submoduleStatus ( ) : Promise < boolean >
2019-12-03 18:28:59 +03:00
tagExists ( pattern : string ) : Promise < boolean >
tryClean ( ) : Promise < boolean >
2020-03-05 22:21:59 +03:00
tryConfigUnset ( configKey : string , globalConfig? : boolean ) : Promise < boolean >
2019-12-03 18:28:59 +03:00
tryDisableAutomaticGarbageCollection ( ) : Promise < boolean >
tryGetFetchUrl ( ) : Promise < string >
tryReset ( ) : Promise < boolean >
}
2020-03-02 19:33:30 +03:00
export async function createCommandManager (
2019-12-03 18:28:59 +03:00
workingDirectory : string ,
2023-06-09 16:08:21 +03:00
lfs : boolean ,
doSparseCheckout : boolean
2019-12-03 18:28:59 +03:00
) : Promise < IGitCommandManager > {
2023-06-09 16:08:21 +03:00
return await GitCommandManager . createCommandManager (
workingDirectory ,
lfs ,
doSparseCheckout
)
2019-12-03 18:28:59 +03:00
}
class GitCommandManager {
private gitEnv = {
GIT_TERMINAL_PROMPT : '0' , // Disable git prompt
GCM_INTERACTIVE : 'Never' // Disable prompting for git credential manager
}
private gitPath = ''
private lfs = false
2023-06-09 16:08:21 +03:00
private doSparseCheckout = false
2019-12-03 18:28:59 +03:00
private workingDirectory = ''
// Private constructor; use createCommandManager()
private constructor ( ) { }
async branchDelete ( remote : boolean , branch : string ) : Promise < void > {
const args = [ 'branch' , '--delete' , '--force' ]
if ( remote ) {
args . push ( '--remote' )
}
args . push ( branch )
await this . execGit ( args )
}
async branchExists ( remote : boolean , pattern : string ) : Promise < boolean > {
const args = [ 'branch' , '--list' ]
if ( remote ) {
args . push ( '--remote' )
}
args . push ( pattern )
const output = await this . execGit ( args )
return ! ! output . stdout . trim ( )
}
async branchList ( remote : boolean ) : Promise < string [ ] > {
const result : string [ ] = [ ]
2020-01-03 18:13:01 +03:00
// Note, this implementation uses "rev-parse --symbolic-full-name" because the output from
2019-12-03 18:28:59 +03:00
// "branch --list" is more difficult when in a detached HEAD state.
2022-12-15 00:08:53 +03:00
// TODO(https://github.com/actions/checkout/issues/786): this implementation uses
// "rev-parse --symbolic-full-name" because there is a bug
// in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names. When
// 2.18 is no longer supported, we can switch back to --symbolic.
2019-12-03 18:28:59 +03:00
2020-01-03 18:13:01 +03:00
const args = [ 'rev-parse' , '--symbolic-full-name' ]
2019-12-03 18:28:59 +03:00
if ( remote ) {
args . push ( '--remotes=origin' )
} else {
args . push ( '--branches' )
}
2022-12-15 00:08:53 +03:00
const stderr : string [ ] = [ ]
const errline : string [ ] = [ ]
const stdout : string [ ] = [ ]
const stdline : string [ ] = [ ]
const listeners = {
stderr : ( data : Buffer ) = > {
stderr . push ( data . toString ( ) )
} ,
errline : ( data : Buffer ) = > {
errline . push ( data . toString ( ) )
} ,
stdout : ( data : Buffer ) = > {
stdout . push ( data . toString ( ) )
} ,
stdline : ( data : Buffer ) = > {
stdline . push ( data . toString ( ) )
}
}
2019-12-03 18:28:59 +03:00
2022-12-15 00:08:53 +03:00
// Suppress the output in order to avoid flooding annotations with innocuous errors.
await this . execGit ( args , false , true , listeners )
core . debug ( ` stderr callback is: ${ stderr } ` )
core . debug ( ` errline callback is: ${ errline } ` )
core . debug ( ` stdout callback is: ${ stdout } ` )
core . debug ( ` stdline callback is: ${ stdline } ` )
for ( let branch of stdline ) {
2019-12-03 18:28:59 +03:00
branch = branch . trim ( )
2022-12-15 00:08:53 +03:00
if ( ! branch ) {
continue
}
2020-01-03 18:13:01 +03:00
2022-12-15 00:08:53 +03:00
if ( branch . startsWith ( 'refs/heads/' ) ) {
branch = branch . substring ( 'refs/heads/' . length )
} else if ( branch . startsWith ( 'refs/remotes/' ) ) {
branch = branch . substring ( 'refs/remotes/' . length )
2019-12-03 18:28:59 +03:00
}
2022-12-15 00:08:53 +03:00
result . push ( branch )
2019-12-03 18:28:59 +03:00
}
return result
}
2023-06-09 16:08:21 +03:00
async sparseCheckout ( sparseCheckout : string [ ] ) : Promise < void > {
await this . execGit ( [ 'sparse-checkout' , 'set' , . . . sparseCheckout ] )
}
async sparseCheckoutNonConeMode ( sparseCheckout : string [ ] ) : Promise < void > {
await this . execGit ( [ 'config' , 'core.sparseCheckout' , 'true' ] )
const output = await this . execGit ( [
'rev-parse' ,
'--git-path' ,
'info/sparse-checkout'
] )
const sparseCheckoutPath = path . join (
this . workingDirectory ,
output . stdout . trimRight ( )
)
await fs . promises . appendFile (
sparseCheckoutPath ,
` \ n ${ sparseCheckout . join ( '\n' ) } \ n `
)
}
2019-12-03 18:28:59 +03:00
async checkout ( ref : string , startPoint : string ) : Promise < void > {
const args = [ 'checkout' , '--progress' , '--force' ]
if ( startPoint ) {
args . push ( '-B' , ref , startPoint )
} else {
args . push ( ref )
}
await this . execGit ( args )
}
async checkoutDetach ( ) : Promise < void > {
const args = [ 'checkout' , '--detach' ]
await this . execGit ( args )
}
2020-03-05 22:21:59 +03:00
async config (
configKey : string ,
configValue : string ,
2021-11-01 19:43:18 +03:00
globalConfig? : boolean ,
add? : boolean
2020-03-05 22:21:59 +03:00
) : Promise < void > {
2021-11-01 19:43:18 +03:00
const args : string [ ] = [ 'config' , globalConfig ? '--global' : '--local' ]
if ( add ) {
args . push ( '--add' )
}
args . push ( . . . [ configKey , configValue ] )
await this . execGit ( args )
2019-12-03 18:28:59 +03:00
}
2020-03-05 22:21:59 +03:00
async configExists (
configKey : string ,
globalConfig? : boolean
) : Promise < boolean > {
const pattern = regexpHelper . escape ( configKey )
2019-12-03 18:28:59 +03:00
const output = await this . execGit (
2020-03-05 22:21:59 +03:00
[
'config' ,
globalConfig ? '--global' : '--local' ,
'--name-only' ,
'--get-regexp' ,
pattern
] ,
2019-12-03 18:28:59 +03:00
true
)
return output . exitCode === 0
}
2023-06-09 16:08:21 +03:00
async fetch (
refSpec : string [ ] ,
2023-08-16 23:34:54 +03:00
options : { filter? : string ; fetchDepth? : number ; fetchTags? : boolean }
2023-06-09 16:08:21 +03:00
) : Promise < void > {
2020-05-27 16:54:28 +03:00
const args = [ '-c' , 'protocol.version=2' , 'fetch' ]
2023-08-16 23:34:54 +03:00
if ( ! refSpec . some ( x = > x === refHelper . tagsRefSpec ) && ! options . fetchTags ) {
2020-05-27 16:54:28 +03:00
args . push ( '--no-tags' )
}
args . push ( '--prune' , '--progress' , '--no-recurse-submodules' )
2023-06-09 16:08:21 +03:00
if ( options . filter ) {
args . push ( ` --filter= ${ options . filter } ` )
}
if ( options . fetchDepth && options . fetchDepth > 0 ) {
args . push ( ` --depth= ${ options . fetchDepth } ` )
2019-12-03 18:28:59 +03:00
} else if (
fshelper . fileExistsSync (
path . join ( this . workingDirectory , '.git' , 'shallow' )
)
) {
args . push ( '--unshallow' )
}
args . push ( 'origin' )
for ( const arg of refSpec ) {
args . push ( arg )
}
2019-12-12 21:16:16 +03:00
const that = this
await retryHelper . execute ( async ( ) = > {
await that . execGit ( args )
} )
2019-12-03 18:28:59 +03:00
}
2020-06-18 17:20:33 +03:00
async getDefaultBranch ( repositoryUrl : string ) : Promise < string > {
let output : GitOutput | undefined
await retryHelper . execute ( async ( ) = > {
output = await this . execGit ( [
'ls-remote' ,
'--quiet' ,
'--exit-code' ,
'--symref' ,
repositoryUrl ,
'HEAD'
] )
} )
if ( output ) {
// Satisfy compiler, will always be set
for ( let line of output . stdout . trim ( ) . split ( '\n' ) ) {
line = line . trim ( )
if ( line . startsWith ( 'ref:' ) || line . endsWith ( 'HEAD' ) ) {
return line
. substr ( 'ref:' . length , line . length - 'ref:' . length - 'HEAD' . length )
. trim ( )
}
}
}
throw new Error ( 'Unexpected output when retrieving default branch' )
}
2019-12-03 18:28:59 +03:00
getWorkingDirectory ( ) : string {
return this . workingDirectory
}
async init ( ) : Promise < void > {
await this . execGit ( [ 'init' , this . workingDirectory ] )
}
async isDetached ( ) : Promise < boolean > {
2020-01-03 18:13:01 +03:00
// Note, "branch --show-current" would be simpler but isn't available until Git 2.22
const output = await this . execGit (
[ 'rev-parse' , '--symbolic-full-name' , '--verify' , '--quiet' , 'HEAD' ] ,
true
)
return ! output . stdout . trim ( ) . startsWith ( 'refs/heads/' )
2019-12-03 18:28:59 +03:00
}
async lfsFetch ( ref : string ) : Promise < void > {
const args = [ 'lfs' , 'fetch' , 'origin' , ref ]
2019-12-12 21:16:16 +03:00
const that = this
await retryHelper . execute ( async ( ) = > {
await that . execGit ( args )
} )
2019-12-03 18:28:59 +03:00
}
async lfsInstall ( ) : Promise < void > {
await this . execGit ( [ 'lfs' , 'install' , '--local' ] )
}
2020-09-23 16:41:47 +03:00
async log1 ( format? : string ) : Promise < string > {
2023-08-16 23:34:54 +03:00
const args = format ? [ 'log' , '-1' , format ] : [ 'log' , '-1' ]
const silent = format ? false : true
2020-09-23 16:41:47 +03:00
const output = await this . execGit ( args , false , silent )
2020-05-21 18:09:16 +03:00
return output . stdout
2019-12-03 18:28:59 +03:00
}
async remoteAdd ( remoteName : string , remoteUrl : string ) : Promise < void > {
await this . execGit ( [ 'remote' , 'add' , remoteName , remoteUrl ] )
}
2020-03-05 22:21:59 +03:00
removeEnvironmentVariable ( name : string ) : void {
delete this . gitEnv [ name ]
}
2020-05-27 16:54:28 +03:00
/ * *
* Resolves a ref to a SHA . For a branch or lightweight tag , the commit SHA is returned .
* For an annotated tag , the tag SHA is returned .
2020-07-14 16:23:30 +03:00
* @param { string } ref For example : 'refs/heads/main' or '/refs/tags/v1'
2020-05-27 16:54:28 +03:00
* @returns { Promise < string > }
* /
async revParse ( ref : string ) : Promise < string > {
const output = await this . execGit ( [ 'rev-parse' , ref ] )
return output . stdout . trim ( )
}
2020-03-02 19:33:30 +03:00
setEnvironmentVariable ( name : string , value : string ) : void {
this . gitEnv [ name ] = value
}
2020-05-27 16:54:28 +03:00
async shaExists ( sha : string ) : Promise < boolean > {
const args = [ 'rev-parse' , '--verify' , '--quiet' , ` ${ sha } ^{object} ` ]
const output = await this . execGit ( args , true )
return output . exitCode === 0
}
2020-03-05 22:21:59 +03:00
async submoduleForeach ( command : string , recursive : boolean ) : Promise < string > {
const args = [ 'submodule' , 'foreach' ]
if ( recursive ) {
args . push ( '--recursive' )
}
args . push ( command )
const output = await this . execGit ( args )
return output . stdout
}
async submoduleSync ( recursive : boolean ) : Promise < void > {
const args = [ 'submodule' , 'sync' ]
if ( recursive ) {
args . push ( '--recursive' )
}
await this . execGit ( args )
}
async submoduleUpdate ( fetchDepth : number , recursive : boolean ) : Promise < void > {
const args = [ '-c' , 'protocol.version=2' ]
args . push ( 'submodule' , 'update' , '--init' , '--force' )
if ( fetchDepth > 0 ) {
args . push ( ` --depth= ${ fetchDepth } ` )
}
if ( recursive ) {
args . push ( '--recursive' )
}
await this . execGit ( args )
}
2023-04-14 13:26:47 +03:00
async submoduleStatus ( ) : Promise < boolean > {
const output = await this . execGit ( [ 'submodule' , 'status' ] , true )
core . debug ( output . stdout )
return output . exitCode === 0
}
2019-12-03 18:28:59 +03:00
async tagExists ( pattern : string ) : Promise < boolean > {
const output = await this . execGit ( [ 'tag' , '--list' , pattern ] )
return ! ! output . stdout . trim ( )
}
async tryClean ( ) : Promise < boolean > {
const output = await this . execGit ( [ 'clean' , '-ffdx' ] , true )
return output . exitCode === 0
}
2020-03-05 22:21:59 +03:00
async tryConfigUnset (
configKey : string ,
globalConfig? : boolean
) : Promise < boolean > {
2019-12-03 18:28:59 +03:00
const output = await this . execGit (
2020-03-05 22:21:59 +03:00
[
'config' ,
globalConfig ? '--global' : '--local' ,
'--unset-all' ,
configKey
] ,
2019-12-03 18:28:59 +03:00
true
)
return output . exitCode === 0
}
async tryDisableAutomaticGarbageCollection ( ) : Promise < boolean > {
2019-12-12 21:49:26 +03:00
const output = await this . execGit (
[ 'config' , '--local' , 'gc.auto' , '0' ] ,
true
)
2019-12-03 18:28:59 +03:00
return output . exitCode === 0
}
async tryGetFetchUrl ( ) : Promise < string > {
const output = await this . execGit (
2019-12-12 21:49:26 +03:00
[ 'config' , '--local' , '--get' , 'remote.origin.url' ] ,
2019-12-03 18:28:59 +03:00
true
)
if ( output . exitCode !== 0 ) {
return ''
}
const stdout = output . stdout . trim ( )
if ( stdout . includes ( '\n' ) ) {
return ''
}
return stdout
}
async tryReset ( ) : Promise < boolean > {
const output = await this . execGit ( [ 'reset' , '--hard' , 'HEAD' ] , true )
return output . exitCode === 0
}
static async createCommandManager (
workingDirectory : string ,
2023-06-09 16:08:21 +03:00
lfs : boolean ,
doSparseCheckout : boolean
2019-12-03 18:28:59 +03:00
) : Promise < GitCommandManager > {
const result = new GitCommandManager ( )
2023-06-09 16:08:21 +03:00
await result . initializeCommandManager (
workingDirectory ,
lfs ,
doSparseCheckout
)
2019-12-03 18:28:59 +03:00
return result
}
private async execGit (
args : string [ ] ,
2020-09-23 16:41:47 +03:00
allowAllExitCodes = false ,
2022-12-15 00:08:53 +03:00
silent = false ,
customListeners = { }
2019-12-03 18:28:59 +03:00
) : Promise < GitOutput > {
fshelper . directoryExistsSync ( this . workingDirectory , true )
const result = new GitOutput ( )
const env = { }
for ( const key of Object . keys ( process . env ) ) {
env [ key ] = process . env [ key ]
}
for ( const key of Object . keys ( this . gitEnv ) ) {
env [ key ] = this . gitEnv [ key ]
}
2022-12-15 00:08:53 +03:00
const defaultListener = {
stdout : ( data : Buffer ) = > {
stdout . push ( data . toString ( ) )
}
}
2019-12-03 18:28:59 +03:00
2022-12-15 00:08:53 +03:00
const mergedListeners = { . . . defaultListener , . . . customListeners }
const stdout : string [ ] = [ ]
2019-12-03 18:28:59 +03:00
const options = {
cwd : this.workingDirectory ,
env ,
2020-09-23 16:41:47 +03:00
silent ,
2019-12-03 18:28:59 +03:00
ignoreReturnCode : allowAllExitCodes ,
2022-12-15 00:08:53 +03:00
listeners : mergedListeners
2019-12-03 18:28:59 +03:00
}
result . exitCode = await exec . exec ( ` " ${ this . gitPath } " ` , args , options )
result . stdout = stdout . join ( '' )
2022-12-15 00:08:53 +03:00
core . debug ( result . exitCode . toString ( ) )
core . debug ( result . stdout )
2019-12-03 18:28:59 +03:00
return result
}
private async initializeCommandManager (
workingDirectory : string ,
2023-06-09 16:08:21 +03:00
lfs : boolean ,
doSparseCheckout : boolean
2019-12-03 18:28:59 +03:00
) : Promise < void > {
this . workingDirectory = workingDirectory
// Git-lfs will try to pull down assets if any of the local/user/system setting exist.
// If the user didn't enable `LFS` in their pipeline definition, disable LFS fetch/checkout.
this . lfs = lfs
if ( ! this . lfs ) {
this . gitEnv [ 'GIT_LFS_SKIP_SMUDGE' ] = '1'
}
this . gitPath = await io . which ( 'git' , true )
// Git version
core . debug ( 'Getting git version' )
let gitVersion = new GitVersion ( )
let gitOutput = await this . execGit ( [ 'version' ] )
let stdout = gitOutput . stdout . trim ( )
if ( ! stdout . includes ( '\n' ) ) {
const match = stdout . match ( /\d+\.\d+(\.\d+)?/ )
if ( match ) {
gitVersion = new GitVersion ( match [ 0 ] )
}
}
if ( ! gitVersion . isValid ( ) ) {
throw new Error ( 'Unable to determine git version' )
}
// Minimum git version
2019-12-12 21:16:16 +03:00
if ( ! gitVersion . checkMinimum ( MinimumGitVersion ) ) {
2019-12-03 18:28:59 +03:00
throw new Error (
2019-12-12 21:16:16 +03:00
` Minimum required git version is ${ MinimumGitVersion } . Your git (' ${ this . gitPath } ') is ${ gitVersion } `
2019-12-03 18:28:59 +03:00
)
}
if ( this . lfs ) {
// Git-lfs version
core . debug ( 'Getting git-lfs version' )
let gitLfsVersion = new GitVersion ( )
const gitLfsPath = await io . which ( 'git-lfs' , true )
gitOutput = await this . execGit ( [ 'lfs' , 'version' ] )
stdout = gitOutput . stdout . trim ( )
if ( ! stdout . includes ( '\n' ) ) {
const match = stdout . match ( /\d+\.\d+(\.\d+)?/ )
if ( match ) {
gitLfsVersion = new GitVersion ( match [ 0 ] )
}
}
if ( ! gitLfsVersion . isValid ( ) ) {
throw new Error ( 'Unable to determine git-lfs version' )
}
// Minimum git-lfs version
// Note:
// - Auth header not supported before 2.1
const minimumGitLfsVersion = new GitVersion ( '2.1' )
if ( ! gitLfsVersion . checkMinimum ( minimumGitLfsVersion ) ) {
throw new Error (
` Minimum required git-lfs version is ${ minimumGitLfsVersion } . Your git-lfs (' ${ gitLfsPath } ') is ${ gitLfsVersion } `
)
}
}
2023-06-09 16:08:21 +03:00
this . doSparseCheckout = doSparseCheckout
if ( this . doSparseCheckout ) {
// The `git sparse-checkout` command was introduced in Git v2.25.0
const minimumGitSparseCheckoutVersion = new GitVersion ( '2.25' )
if ( ! gitVersion . checkMinimum ( minimumGitSparseCheckoutVersion ) ) {
throw new Error (
` Minimum Git version required for sparse checkout is ${ minimumGitSparseCheckoutVersion } . Your git (' ${ this . gitPath } ') is ${ gitVersion } `
)
}
}
2019-12-03 18:28:59 +03:00
// Set the user agent
const gitHttpUserAgent = ` git/ ${ gitVersion } (github-actions-checkout) `
core . debug ( ` Set git useragent to: ${ gitHttpUserAgent } ` )
this . gitEnv [ 'GIT_HTTP_USER_AGENT' ] = gitHttpUserAgent
}
}
class GitOutput {
stdout = ''
exitCode = 0
}