2
0

git-command-manager.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import * as core from '@actions/core'
  2. import * as exec from '@actions/exec'
  3. import * as fs from 'fs'
  4. import * as fshelper from './fs-helper'
  5. import * as io from '@actions/io'
  6. import * as path from 'path'
  7. import * as refHelper from './ref-helper'
  8. import * as regexpHelper from './regexp-helper'
  9. import * as retryHelper from './retry-helper'
  10. import {GitVersion} from './git-version'
  11. // Auth header not supported before 2.9
  12. // Wire protocol v2 not supported before 2.18
  13. // sparse-checkout not [well-]supported before 2.28 (see https://github.com/actions/checkout/issues/1386)
  14. export const MinimumGitVersion = new GitVersion('2.18')
  15. export const MinimumGitSparseCheckoutVersion = new GitVersion('2.28')
  16. export interface IGitCommandManager {
  17. branchDelete(remote: boolean, branch: string): Promise<void>
  18. branchExists(remote: boolean, pattern: string): Promise<boolean>
  19. branchList(remote: boolean): Promise<string[]>
  20. disableSparseCheckout(): Promise<void>
  21. sparseCheckout(sparseCheckout: string[]): Promise<void>
  22. sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void>
  23. checkout(ref: string, startPoint: string): Promise<void>
  24. checkoutDetach(): Promise<void>
  25. config(
  26. configKey: string,
  27. configValue: string,
  28. globalConfig?: boolean,
  29. add?: boolean,
  30. configFile?: string
  31. ): Promise<void>
  32. configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
  33. fetch(
  34. refSpec: string[],
  35. options: {
  36. filter?: string
  37. fetchDepth?: number
  38. showProgress?: boolean
  39. }
  40. ): Promise<void>
  41. getDefaultBranch(repositoryUrl: string): Promise<string>
  42. getSubmoduleConfigPaths(recursive: boolean): Promise<string[]>
  43. getWorkingDirectory(): string
  44. init(): Promise<void>
  45. isDetached(): Promise<boolean>
  46. lfsFetch(ref: string): Promise<void>
  47. lfsInstall(): Promise<void>
  48. log1(format?: string): Promise<string>
  49. remoteAdd(remoteName: string, remoteUrl: string): Promise<void>
  50. removeEnvironmentVariable(name: string): void
  51. revParse(ref: string): Promise<string>
  52. setEnvironmentVariable(name: string, value: string): void
  53. shaExists(sha: string): Promise<boolean>
  54. submoduleForeach(command: string, recursive: boolean): Promise<string>
  55. submoduleSync(recursive: boolean): Promise<void>
  56. submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
  57. submoduleStatus(): Promise<boolean>
  58. tagExists(pattern: string): Promise<boolean>
  59. tryClean(): Promise<boolean>
  60. tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
  61. tryConfigUnsetValue(
  62. configKey: string,
  63. configValue: string,
  64. globalConfig?: boolean,
  65. configFile?: string
  66. ): Promise<boolean>
  67. tryDisableAutomaticGarbageCollection(): Promise<boolean>
  68. tryGetFetchUrl(): Promise<string>
  69. tryGetConfigValues(
  70. configKey: string,
  71. globalConfig?: boolean,
  72. configFile?: string
  73. ): Promise<string[]>
  74. tryGetConfigKeys(
  75. pattern: string,
  76. globalConfig?: boolean,
  77. configFile?: string
  78. ): Promise<string[]>
  79. tryReset(): Promise<boolean>
  80. version(): Promise<GitVersion>
  81. }
  82. export async function createCommandManager(
  83. workingDirectory: string,
  84. lfs: boolean,
  85. doSparseCheckout: boolean
  86. ): Promise<IGitCommandManager> {
  87. return await GitCommandManager.createCommandManager(
  88. workingDirectory,
  89. lfs,
  90. doSparseCheckout
  91. )
  92. }
  93. class GitCommandManager {
  94. private gitEnv = {
  95. GIT_TERMINAL_PROMPT: '0', // Disable git prompt
  96. GCM_INTERACTIVE: 'Never' // Disable prompting for git credential manager
  97. }
  98. private gitPath = ''
  99. private lfs = false
  100. private doSparseCheckout = false
  101. private workingDirectory = ''
  102. private gitVersion: GitVersion = new GitVersion()
  103. // Private constructor; use createCommandManager()
  104. private constructor() {}
  105. async branchDelete(remote: boolean, branch: string): Promise<void> {
  106. const args = ['branch', '--delete', '--force']
  107. if (remote) {
  108. args.push('--remote')
  109. }
  110. args.push(branch)
  111. await this.execGit(args)
  112. }
  113. async branchExists(remote: boolean, pattern: string): Promise<boolean> {
  114. const args = ['branch', '--list']
  115. if (remote) {
  116. args.push('--remote')
  117. }
  118. args.push(pattern)
  119. const output = await this.execGit(args)
  120. return !!output.stdout.trim()
  121. }
  122. async branchList(remote: boolean): Promise<string[]> {
  123. const result: string[] = []
  124. // Note, this implementation uses "rev-parse --symbolic-full-name" because the output from
  125. // "branch --list" is more difficult when in a detached HEAD state.
  126. // TODO(https://github.com/actions/checkout/issues/786): this implementation uses
  127. // "rev-parse --symbolic-full-name" because there is a bug
  128. // in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names. When
  129. // 2.18 is no longer supported, we can switch back to --symbolic.
  130. const args = ['rev-parse', '--symbolic-full-name']
  131. if (remote) {
  132. args.push('--remotes=origin')
  133. } else {
  134. args.push('--branches')
  135. }
  136. const stderr: string[] = []
  137. const errline: string[] = []
  138. const stdout: string[] = []
  139. const stdline: string[] = []
  140. const listeners = {
  141. stderr: (data: Buffer) => {
  142. stderr.push(data.toString())
  143. },
  144. errline: (data: Buffer) => {
  145. errline.push(data.toString())
  146. },
  147. stdout: (data: Buffer) => {
  148. stdout.push(data.toString())
  149. },
  150. stdline: (data: Buffer) => {
  151. stdline.push(data.toString())
  152. }
  153. }
  154. // Suppress the output in order to avoid flooding annotations with innocuous errors.
  155. await this.execGit(args, false, true, listeners)
  156. core.debug(`stderr callback is: ${stderr}`)
  157. core.debug(`errline callback is: ${errline}`)
  158. core.debug(`stdout callback is: ${stdout}`)
  159. core.debug(`stdline callback is: ${stdline}`)
  160. for (let branch of stdline) {
  161. branch = branch.trim()
  162. if (!branch) {
  163. continue
  164. }
  165. if (branch.startsWith('refs/heads/')) {
  166. branch = branch.substring('refs/heads/'.length)
  167. } else if (branch.startsWith('refs/remotes/')) {
  168. branch = branch.substring('refs/remotes/'.length)
  169. }
  170. result.push(branch)
  171. }
  172. return result
  173. }
  174. async disableSparseCheckout(): Promise<void> {
  175. await this.execGit(['sparse-checkout', 'disable'])
  176. // Disabling 'sparse-checkout` leaves behind an undesirable side-effect in config (even in a pristine environment).
  177. await this.tryConfigUnset('extensions.worktreeConfig', false)
  178. }
  179. async sparseCheckout(sparseCheckout: string[]): Promise<void> {
  180. await this.execGit(['sparse-checkout', 'set', ...sparseCheckout])
  181. }
  182. async sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void> {
  183. await this.execGit(['config', 'core.sparseCheckout', 'true'])
  184. const output = await this.execGit([
  185. 'rev-parse',
  186. '--git-path',
  187. 'info/sparse-checkout'
  188. ])
  189. const sparseCheckoutPath = path.join(
  190. this.workingDirectory,
  191. output.stdout.trimRight()
  192. )
  193. await fs.promises.appendFile(
  194. sparseCheckoutPath,
  195. `\n${sparseCheckout.join('\n')}\n`
  196. )
  197. }
  198. async checkout(ref: string, startPoint: string): Promise<void> {
  199. const args = ['checkout', '--progress', '--force']
  200. if (startPoint) {
  201. args.push('-B', ref, startPoint)
  202. } else {
  203. args.push(ref)
  204. }
  205. await this.execGit(args)
  206. }
  207. async checkoutDetach(): Promise<void> {
  208. const args = ['checkout', '--detach']
  209. await this.execGit(args)
  210. }
  211. async config(
  212. configKey: string,
  213. configValue: string,
  214. globalConfig?: boolean,
  215. add?: boolean,
  216. configFile?: string
  217. ): Promise<void> {
  218. const args: string[] = ['config']
  219. if (configFile) {
  220. args.push('--file', configFile)
  221. } else {
  222. args.push(globalConfig ? '--global' : '--local')
  223. }
  224. if (add) {
  225. args.push('--add')
  226. }
  227. args.push(...[configKey, configValue])
  228. await this.execGit(args)
  229. }
  230. async configExists(
  231. configKey: string,
  232. globalConfig?: boolean
  233. ): Promise<boolean> {
  234. const pattern = regexpHelper.escape(configKey)
  235. const output = await this.execGit(
  236. [
  237. 'config',
  238. globalConfig ? '--global' : '--local',
  239. '--name-only',
  240. '--get-regexp',
  241. pattern
  242. ],
  243. true
  244. )
  245. return output.exitCode === 0
  246. }
  247. async fetch(
  248. refSpec: string[],
  249. options: {
  250. filter?: string
  251. fetchDepth?: number
  252. showProgress?: boolean
  253. }
  254. ): Promise<void> {
  255. const args = ['-c', 'protocol.version=2', 'fetch']
  256. // Always use --no-tags for explicit control over tag fetching
  257. // Tags are fetched explicitly via refspec when needed
  258. args.push('--no-tags')
  259. args.push('--prune', '--no-recurse-submodules')
  260. if (options.showProgress) {
  261. args.push('--progress')
  262. }
  263. if (options.filter) {
  264. args.push(`--filter=${options.filter}`)
  265. }
  266. if (options.fetchDepth && options.fetchDepth > 0) {
  267. args.push(`--depth=${options.fetchDepth}`)
  268. } else if (
  269. fshelper.fileExistsSync(
  270. path.join(this.workingDirectory, '.git', 'shallow')
  271. )
  272. ) {
  273. args.push('--unshallow')
  274. }
  275. args.push('origin')
  276. for (const arg of refSpec) {
  277. args.push(arg)
  278. }
  279. const that = this
  280. await retryHelper.execute(async () => {
  281. await that.execGit(args)
  282. })
  283. }
  284. async getDefaultBranch(repositoryUrl: string): Promise<string> {
  285. let output: GitOutput | undefined
  286. await retryHelper.execute(async () => {
  287. output = await this.execGit([
  288. 'ls-remote',
  289. '--quiet',
  290. '--exit-code',
  291. '--symref',
  292. repositoryUrl,
  293. 'HEAD'
  294. ])
  295. })
  296. if (output) {
  297. // Satisfy compiler, will always be set
  298. for (let line of output.stdout.trim().split('\n')) {
  299. line = line.trim()
  300. if (line.startsWith('ref:') || line.endsWith('HEAD')) {
  301. return line
  302. .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
  303. .trim()
  304. }
  305. }
  306. }
  307. throw new Error('Unexpected output when retrieving default branch')
  308. }
  309. async getSubmoduleConfigPaths(recursive: boolean): Promise<string[]> {
  310. // Get submodule config file paths.
  311. // Use `--show-origin` to get the config file path for each submodule.
  312. const output = await this.submoduleForeach(
  313. `git config --local --show-origin --name-only --get-regexp remote.origin.url`,
  314. recursive
  315. )
  316. // Extract config file paths from the output (lines starting with "file:").
  317. const configPaths =
  318. output.match(/(?<=(^|\n)file:)[^\t]+(?=\tremote\.origin\.url)/g) || []
  319. return configPaths
  320. }
  321. getWorkingDirectory(): string {
  322. return this.workingDirectory
  323. }
  324. async init(): Promise<void> {
  325. await this.execGit(['init', this.workingDirectory])
  326. }
  327. async isDetached(): Promise<boolean> {
  328. // Note, "branch --show-current" would be simpler but isn't available until Git 2.22
  329. const output = await this.execGit(
  330. ['rev-parse', '--symbolic-full-name', '--verify', '--quiet', 'HEAD'],
  331. true
  332. )
  333. return !output.stdout.trim().startsWith('refs/heads/')
  334. }
  335. async lfsFetch(ref: string): Promise<void> {
  336. const args = ['lfs', 'fetch', 'origin', ref]
  337. const that = this
  338. await retryHelper.execute(async () => {
  339. await that.execGit(args)
  340. })
  341. }
  342. async lfsInstall(): Promise<void> {
  343. await this.execGit(['lfs', 'install', '--local'])
  344. }
  345. async log1(format?: string): Promise<string> {
  346. const args = format ? ['log', '-1', format] : ['log', '-1']
  347. const silent = format ? false : true
  348. const output = await this.execGit(args, false, silent)
  349. return output.stdout
  350. }
  351. async remoteAdd(remoteName: string, remoteUrl: string): Promise<void> {
  352. await this.execGit(['remote', 'add', remoteName, remoteUrl])
  353. }
  354. removeEnvironmentVariable(name: string): void {
  355. delete this.gitEnv[name]
  356. }
  357. /**
  358. * Resolves a ref to a SHA. For a branch or lightweight tag, the commit SHA is returned.
  359. * For an annotated tag, the tag SHA is returned.
  360. * @param {string} ref For example: 'refs/heads/main' or '/refs/tags/v1'
  361. * @returns {Promise<string>}
  362. */
  363. async revParse(ref: string): Promise<string> {
  364. const output = await this.execGit(['rev-parse', ref])
  365. return output.stdout.trim()
  366. }
  367. setEnvironmentVariable(name: string, value: string): void {
  368. this.gitEnv[name] = value
  369. }
  370. async shaExists(sha: string): Promise<boolean> {
  371. const args = ['rev-parse', '--verify', '--quiet', `${sha}^{object}`]
  372. const output = await this.execGit(args, true)
  373. return output.exitCode === 0
  374. }
  375. async submoduleForeach(command: string, recursive: boolean): Promise<string> {
  376. const args = ['submodule', 'foreach']
  377. if (recursive) {
  378. args.push('--recursive')
  379. }
  380. args.push(command)
  381. const output = await this.execGit(args)
  382. return output.stdout
  383. }
  384. async submoduleSync(recursive: boolean): Promise<void> {
  385. const args = ['submodule', 'sync']
  386. if (recursive) {
  387. args.push('--recursive')
  388. }
  389. await this.execGit(args)
  390. }
  391. async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void> {
  392. const args = ['-c', 'protocol.version=2']
  393. args.push('submodule', 'update', '--init', '--force')
  394. if (fetchDepth > 0) {
  395. args.push(`--depth=${fetchDepth}`)
  396. }
  397. if (recursive) {
  398. args.push('--recursive')
  399. }
  400. await this.execGit(args)
  401. }
  402. async submoduleStatus(): Promise<boolean> {
  403. const output = await this.execGit(['submodule', 'status'], true)
  404. core.debug(output.stdout)
  405. return output.exitCode === 0
  406. }
  407. async tagExists(pattern: string): Promise<boolean> {
  408. const output = await this.execGit(['tag', '--list', pattern])
  409. return !!output.stdout.trim()
  410. }
  411. async tryClean(): Promise<boolean> {
  412. const output = await this.execGit(['clean', '-ffdx'], true)
  413. return output.exitCode === 0
  414. }
  415. async tryConfigUnset(
  416. configKey: string,
  417. globalConfig?: boolean
  418. ): Promise<boolean> {
  419. const output = await this.execGit(
  420. [
  421. 'config',
  422. globalConfig ? '--global' : '--local',
  423. '--unset-all',
  424. configKey
  425. ],
  426. true
  427. )
  428. return output.exitCode === 0
  429. }
  430. async tryConfigUnsetValue(
  431. configKey: string,
  432. configValue: string,
  433. globalConfig?: boolean,
  434. configFile?: string
  435. ): Promise<boolean> {
  436. const args = ['config']
  437. if (configFile) {
  438. args.push('--file', configFile)
  439. } else {
  440. args.push(globalConfig ? '--global' : '--local')
  441. }
  442. args.push('--unset', configKey, configValue)
  443. const output = await this.execGit(args, true)
  444. return output.exitCode === 0
  445. }
  446. async tryDisableAutomaticGarbageCollection(): Promise<boolean> {
  447. const output = await this.execGit(
  448. ['config', '--local', 'gc.auto', '0'],
  449. true
  450. )
  451. return output.exitCode === 0
  452. }
  453. async tryGetFetchUrl(): Promise<string> {
  454. const output = await this.execGit(
  455. ['config', '--local', '--get', 'remote.origin.url'],
  456. true
  457. )
  458. if (output.exitCode !== 0) {
  459. return ''
  460. }
  461. const stdout = output.stdout.trim()
  462. if (stdout.includes('\n')) {
  463. return ''
  464. }
  465. return stdout
  466. }
  467. async tryGetConfigValues(
  468. configKey: string,
  469. globalConfig?: boolean,
  470. configFile?: string
  471. ): Promise<string[]> {
  472. const args = ['config']
  473. if (configFile) {
  474. args.push('--file', configFile)
  475. } else {
  476. args.push(globalConfig ? '--global' : '--local')
  477. }
  478. args.push('--get-all', configKey)
  479. const output = await this.execGit(args, true)
  480. if (output.exitCode !== 0) {
  481. return []
  482. }
  483. return output.stdout
  484. .trim()
  485. .split('\n')
  486. .filter(value => value.trim())
  487. }
  488. async tryGetConfigKeys(
  489. pattern: string,
  490. globalConfig?: boolean,
  491. configFile?: string
  492. ): Promise<string[]> {
  493. const args = ['config']
  494. if (configFile) {
  495. args.push('--file', configFile)
  496. } else {
  497. args.push(globalConfig ? '--global' : '--local')
  498. }
  499. args.push('--name-only', '--get-regexp', pattern)
  500. const output = await this.execGit(args, true)
  501. if (output.exitCode !== 0) {
  502. return []
  503. }
  504. return output.stdout
  505. .trim()
  506. .split('\n')
  507. .filter(key => key.trim())
  508. }
  509. async tryReset(): Promise<boolean> {
  510. const output = await this.execGit(['reset', '--hard', 'HEAD'], true)
  511. return output.exitCode === 0
  512. }
  513. async version(): Promise<GitVersion> {
  514. return this.gitVersion
  515. }
  516. static async createCommandManager(
  517. workingDirectory: string,
  518. lfs: boolean,
  519. doSparseCheckout: boolean
  520. ): Promise<GitCommandManager> {
  521. const result = new GitCommandManager()
  522. await result.initializeCommandManager(
  523. workingDirectory,
  524. lfs,
  525. doSparseCheckout
  526. )
  527. return result
  528. }
  529. private async execGit(
  530. args: string[],
  531. allowAllExitCodes = false,
  532. silent = false,
  533. customListeners = {}
  534. ): Promise<GitOutput> {
  535. fshelper.directoryExistsSync(this.workingDirectory, true)
  536. const result = new GitOutput()
  537. const env = {}
  538. for (const key of Object.keys(process.env)) {
  539. env[key] = process.env[key]
  540. }
  541. for (const key of Object.keys(this.gitEnv)) {
  542. env[key] = this.gitEnv[key]
  543. }
  544. const defaultListener = {
  545. stdout: (data: Buffer) => {
  546. stdout.push(data.toString())
  547. }
  548. }
  549. const mergedListeners = {...defaultListener, ...customListeners}
  550. const stdout: string[] = []
  551. const options = {
  552. cwd: this.workingDirectory,
  553. env,
  554. silent,
  555. ignoreReturnCode: allowAllExitCodes,
  556. listeners: mergedListeners
  557. }
  558. result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options)
  559. result.stdout = stdout.join('')
  560. core.debug(result.exitCode.toString())
  561. core.debug(result.stdout)
  562. return result
  563. }
  564. private async initializeCommandManager(
  565. workingDirectory: string,
  566. lfs: boolean,
  567. doSparseCheckout: boolean
  568. ): Promise<void> {
  569. this.workingDirectory = workingDirectory
  570. // Git-lfs will try to pull down assets if any of the local/user/system setting exist.
  571. // If the user didn't enable `LFS` in their pipeline definition, disable LFS fetch/checkout.
  572. this.lfs = lfs
  573. if (!this.lfs) {
  574. this.gitEnv['GIT_LFS_SKIP_SMUDGE'] = '1'
  575. }
  576. this.gitPath = await io.which('git', true)
  577. // Git version
  578. core.debug('Getting git version')
  579. this.gitVersion = new GitVersion()
  580. let gitOutput = await this.execGit(['version'])
  581. let stdout = gitOutput.stdout.trim()
  582. if (!stdout.includes('\n')) {
  583. const match = stdout.match(/\d+\.\d+(\.\d+)?/)
  584. if (match) {
  585. this.gitVersion = new GitVersion(match[0])
  586. }
  587. }
  588. if (!this.gitVersion.isValid()) {
  589. throw new Error('Unable to determine git version')
  590. }
  591. // Minimum git version
  592. if (!this.gitVersion.checkMinimum(MinimumGitVersion)) {
  593. throw new Error(
  594. `Minimum required git version is ${MinimumGitVersion}. Your git ('${this.gitPath}') is ${this.gitVersion}`
  595. )
  596. }
  597. if (this.lfs) {
  598. // Git-lfs version
  599. core.debug('Getting git-lfs version')
  600. let gitLfsVersion = new GitVersion()
  601. const gitLfsPath = await io.which('git-lfs', true)
  602. gitOutput = await this.execGit(['lfs', 'version'])
  603. stdout = gitOutput.stdout.trim()
  604. if (!stdout.includes('\n')) {
  605. const match = stdout.match(/\d+\.\d+(\.\d+)?/)
  606. if (match) {
  607. gitLfsVersion = new GitVersion(match[0])
  608. }
  609. }
  610. if (!gitLfsVersion.isValid()) {
  611. throw new Error('Unable to determine git-lfs version')
  612. }
  613. // Minimum git-lfs version
  614. // Note:
  615. // - Auth header not supported before 2.1
  616. const minimumGitLfsVersion = new GitVersion('2.1')
  617. if (!gitLfsVersion.checkMinimum(minimumGitLfsVersion)) {
  618. throw new Error(
  619. `Minimum required git-lfs version is ${minimumGitLfsVersion}. Your git-lfs ('${gitLfsPath}') is ${gitLfsVersion}`
  620. )
  621. }
  622. }
  623. this.doSparseCheckout = doSparseCheckout
  624. if (this.doSparseCheckout) {
  625. if (!this.gitVersion.checkMinimum(MinimumGitSparseCheckoutVersion)) {
  626. throw new Error(
  627. `Minimum Git version required for sparse checkout is ${MinimumGitSparseCheckoutVersion}. Your git ('${this.gitPath}') is ${this.gitVersion}`
  628. )
  629. }
  630. }
  631. // Set the user agent
  632. let gitHttpUserAgent = `git/${this.gitVersion} (github-actions-checkout)`
  633. // Append orchestration ID if set
  634. const orchId = process.env['ACTIONS_ORCHESTRATION_ID']
  635. if (orchId) {
  636. // Sanitize the orchestration ID to ensure it contains only valid characters
  637. // Valid characters: 0-9, a-z, _, -, .
  638. const sanitizedId = orchId.replace(/[^a-z0-9_.-]/gi, '_')
  639. if (sanitizedId) {
  640. gitHttpUserAgent = `${gitHttpUserAgent} actions_orchestration_id/${sanitizedId}`
  641. }
  642. }
  643. core.debug(`Set git useragent to: ${gitHttpUserAgent}`)
  644. this.gitEnv['GIT_HTTP_USER_AGENT'] = gitHttpUserAgent
  645. }
  646. }
  647. class GitOutput {
  648. stdout = ''
  649. exitCode = 0
  650. }