git-source-provider.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import * as core from '@actions/core'
  2. import * as fsHelper from './fs-helper'
  3. import * as gitAuthHelper from './git-auth-helper'
  4. import * as gitCommandManager from './git-command-manager'
  5. import * as gitDirectoryHelper from './git-directory-helper'
  6. import * as githubApiHelper from './github-api-helper'
  7. import * as io from '@actions/io'
  8. import * as path from 'path'
  9. import * as refHelper from './ref-helper'
  10. import * as stateHelper from './state-helper'
  11. import * as urlHelper from './url-helper'
  12. import {IGitCommandManager} from './git-command-manager'
  13. import {IGitSourceSettings} from './git-source-settings'
  14. export async function getSource(settings: IGitSourceSettings): Promise<void> {
  15. // Repository URL
  16. core.info(
  17. `Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`
  18. )
  19. const repositoryUrl = urlHelper.getFetchUrl(settings)
  20. // Remove conflicting file path
  21. if (fsHelper.fileExistsSync(settings.repositoryPath)) {
  22. await io.rmRF(settings.repositoryPath)
  23. }
  24. // Create directory
  25. let isExisting = true
  26. if (!fsHelper.directoryExistsSync(settings.repositoryPath)) {
  27. isExisting = false
  28. await io.mkdirP(settings.repositoryPath)
  29. }
  30. // Git command manager
  31. core.startGroup('Getting Git version info')
  32. const git = await getGitCommandManager(settings)
  33. core.endGroup()
  34. let authHelper: gitAuthHelper.IGitAuthHelper | null = null
  35. try {
  36. if (git) {
  37. authHelper = gitAuthHelper.createAuthHelper(git, settings)
  38. if (settings.setSafeDirectory) {
  39. // Setup the repository path as a safe directory, so if we pass this into a container job with a different user it doesn't fail
  40. // Otherwise all git commands we run in a container fail
  41. await authHelper.configureTempGlobalConfig()
  42. core.info(
  43. `Adding repository directory to the temporary git global config as a safe directory`
  44. )
  45. await git
  46. .config('safe.directory', settings.repositoryPath, true, true)
  47. .catch(error => {
  48. core.info(
  49. `Failed to initialize safe directory with error: ${error}`
  50. )
  51. })
  52. stateHelper.setSafeDirectory()
  53. }
  54. }
  55. // Prepare existing directory, otherwise recreate
  56. if (isExisting) {
  57. await gitDirectoryHelper.prepareExistingDirectory(
  58. git,
  59. settings.repositoryPath,
  60. repositoryUrl,
  61. settings.clean,
  62. settings.ref
  63. )
  64. }
  65. if (!git) {
  66. // Downloading using REST API
  67. core.info(`The repository will be downloaded using the GitHub REST API`)
  68. core.info(
  69. `To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH`
  70. )
  71. if (settings.submodules) {
  72. throw new Error(
  73. `Input 'submodules' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`
  74. )
  75. } else if (settings.sshKey) {
  76. throw new Error(
  77. `Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`
  78. )
  79. }
  80. await githubApiHelper.downloadRepository(
  81. settings.authToken,
  82. settings.repositoryOwner,
  83. settings.repositoryName,
  84. settings.ref,
  85. settings.commit,
  86. settings.repositoryPath,
  87. settings.githubServerUrl
  88. )
  89. return
  90. }
  91. // Save state for POST action
  92. stateHelper.setRepositoryPath(settings.repositoryPath)
  93. // Initialize the repository
  94. if (
  95. !fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))
  96. ) {
  97. core.startGroup('Initializing the repository')
  98. await git.init()
  99. await git.remoteAdd('origin', repositoryUrl)
  100. core.endGroup()
  101. }
  102. // Disable automatic garbage collection
  103. core.startGroup('Disabling automatic garbage collection')
  104. if (!(await git.tryDisableAutomaticGarbageCollection())) {
  105. core.warning(
  106. `Unable to turn off git automatic garbage collection. The git fetch operation may trigger garbage collection and cause a delay.`
  107. )
  108. }
  109. core.endGroup()
  110. // If we didn't initialize it above, do it now
  111. if (!authHelper) {
  112. authHelper = gitAuthHelper.createAuthHelper(git, settings)
  113. }
  114. // Configure auth
  115. core.startGroup('Setting up auth')
  116. await authHelper.configureAuth()
  117. core.endGroup()
  118. // Determine the default branch
  119. if (!settings.ref && !settings.commit) {
  120. core.startGroup('Determining the default branch')
  121. if (settings.sshKey) {
  122. settings.ref = await git.getDefaultBranch(repositoryUrl)
  123. } else {
  124. settings.ref = await githubApiHelper.getDefaultBranch(
  125. settings.authToken,
  126. settings.repositoryOwner,
  127. settings.repositoryName,
  128. settings.githubServerUrl
  129. )
  130. }
  131. core.endGroup()
  132. }
  133. // LFS install
  134. if (settings.lfs) {
  135. await git.lfsInstall()
  136. }
  137. // Fetch
  138. core.startGroup('Fetching the repository')
  139. const fetchOptions: {
  140. filter?: string
  141. fetchDepth?: number
  142. fetchTags?: boolean
  143. showProgress?: boolean
  144. } = {}
  145. if (settings.filter) {
  146. fetchOptions.filter = settings.filter
  147. } else if (settings.sparseCheckout) {
  148. fetchOptions.filter = 'blob:none'
  149. }
  150. if (settings.fetchDepth <= 0) {
  151. // Fetch all branches and tags
  152. let refSpec = refHelper.getRefSpecForAllHistory(
  153. settings.ref,
  154. settings.commit
  155. )
  156. await git.fetch(refSpec, fetchOptions)
  157. // When all history is fetched, the ref we're interested in may have moved to a different
  158. // commit (push or force push). If so, fetch again with a targeted refspec.
  159. if (!(await refHelper.testRef(git, settings.ref, settings.commit))) {
  160. refSpec = refHelper.getRefSpec(settings.ref, settings.commit)
  161. await git.fetch(refSpec, fetchOptions)
  162. }
  163. } else {
  164. fetchOptions.fetchDepth = settings.fetchDepth
  165. fetchOptions.fetchTags = settings.fetchTags
  166. const refSpec = refHelper.getRefSpec(settings.ref, settings.commit)
  167. await git.fetch(refSpec, fetchOptions)
  168. }
  169. core.endGroup()
  170. // Checkout info
  171. core.startGroup('Determining the checkout info')
  172. const checkoutInfo = await refHelper.getCheckoutInfo(
  173. git,
  174. settings.ref,
  175. settings.commit
  176. )
  177. core.endGroup()
  178. // LFS fetch
  179. // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time).
  180. // Explicit lfs fetch will fetch lfs objects in parallel.
  181. // For sparse checkouts, let `checkout` fetch the needed objects lazily.
  182. if (settings.lfs && !settings.sparseCheckout) {
  183. core.startGroup('Fetching LFS objects')
  184. await git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref)
  185. core.endGroup()
  186. }
  187. // Sparse checkout
  188. if (!settings.sparseCheckout) {
  189. await git.disableSparseCheckout()
  190. } else {
  191. core.startGroup('Setting up sparse checkout')
  192. if (settings.sparseCheckoutConeMode) {
  193. await git.sparseCheckout(settings.sparseCheckout)
  194. } else {
  195. await git.sparseCheckoutNonConeMode(settings.sparseCheckout)
  196. }
  197. core.endGroup()
  198. }
  199. // Checkout
  200. core.startGroup('Checking out the ref')
  201. await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
  202. core.endGroup()
  203. // Submodules
  204. if (settings.submodules) {
  205. // Temporarily override global config
  206. core.startGroup('Setting up auth for fetching submodules')
  207. await authHelper.configureGlobalAuth()
  208. core.endGroup()
  209. // Checkout submodules
  210. core.startGroup('Fetching submodules')
  211. await git.submoduleSync(settings.nestedSubmodules)
  212. await git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules)
  213. await git.submoduleForeach(
  214. 'git config --local gc.auto 0',
  215. settings.nestedSubmodules
  216. )
  217. core.endGroup()
  218. // Persist credentials
  219. if (settings.persistCredentials) {
  220. core.startGroup('Persisting credentials for submodules')
  221. await authHelper.configureSubmoduleAuth()
  222. core.endGroup()
  223. }
  224. }
  225. // Get commit information
  226. const commitInfo = await git.log1()
  227. // Log commit sha
  228. await git.log1("--format='%H'")
  229. // Check for incorrect pull request merge commit
  230. await refHelper.checkCommitInfo(
  231. settings.authToken,
  232. commitInfo,
  233. settings.repositoryOwner,
  234. settings.repositoryName,
  235. settings.ref,
  236. settings.commit,
  237. settings.githubServerUrl
  238. )
  239. } finally {
  240. // Remove auth
  241. if (authHelper) {
  242. if (!settings.persistCredentials) {
  243. core.startGroup('Removing auth')
  244. await authHelper.removeAuth()
  245. core.endGroup()
  246. }
  247. authHelper.removeGlobalConfig()
  248. }
  249. }
  250. }
  251. export async function cleanup(repositoryPath: string): Promise<void> {
  252. // Repo exists?
  253. if (
  254. !repositoryPath ||
  255. !fsHelper.fileExistsSync(path.join(repositoryPath, '.git', 'config'))
  256. ) {
  257. return
  258. }
  259. let git: IGitCommandManager
  260. try {
  261. git = await gitCommandManager.createCommandManager(
  262. repositoryPath,
  263. false,
  264. false
  265. )
  266. } catch {
  267. return
  268. }
  269. // Remove auth
  270. const authHelper = gitAuthHelper.createAuthHelper(git)
  271. try {
  272. if (stateHelper.PostSetSafeDirectory) {
  273. // Setup the repository path as a safe directory, so if we pass this into a container job with a different user it doesn't fail
  274. // Otherwise all git commands we run in a container fail
  275. await authHelper.configureTempGlobalConfig()
  276. core.info(
  277. `Adding repository directory to the temporary git global config as a safe directory`
  278. )
  279. await git
  280. .config('safe.directory', repositoryPath, true, true)
  281. .catch(error => {
  282. core.info(`Failed to initialize safe directory with error: ${error}`)
  283. })
  284. }
  285. await authHelper.removeAuth()
  286. } finally {
  287. await authHelper.removeGlobalConfig()
  288. }
  289. }
  290. async function getGitCommandManager(
  291. settings: IGitSourceSettings
  292. ): Promise<IGitCommandManager | undefined> {
  293. core.info(`Working directory is '${settings.repositoryPath}'`)
  294. try {
  295. return await gitCommandManager.createCommandManager(
  296. settings.repositoryPath,
  297. settings.lfs,
  298. settings.sparseCheckout != null
  299. )
  300. } catch (err) {
  301. // Git is required for LFS
  302. if (settings.lfs) {
  303. throw err
  304. }
  305. // Otherwise fallback to REST API
  306. return undefined
  307. }
  308. }