git-source-provider.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. if (settings.fetchDepth <= 0) {
  140. // Fetch all branches and tags
  141. let refSpec = refHelper.getRefSpecForAllHistory(
  142. settings.ref,
  143. settings.commit
  144. )
  145. await git.fetch(refSpec)
  146. // When all history is fetched, the ref we're interested in may have moved to a different
  147. // commit (push or force push). If so, fetch again with a targeted refspec.
  148. if (!(await refHelper.testRef(git, settings.ref, settings.commit))) {
  149. refSpec = refHelper.getRefSpec(settings.ref, settings.commit)
  150. await git.fetch(refSpec)
  151. }
  152. } else {
  153. const refSpec = refHelper.getRefSpec(settings.ref, settings.commit)
  154. await git.fetch(refSpec, settings.fetchDepth)
  155. }
  156. core.endGroup()
  157. // Checkout info
  158. core.startGroup('Determining the checkout info')
  159. const checkoutInfo = await refHelper.getCheckoutInfo(
  160. git,
  161. settings.ref,
  162. settings.commit
  163. )
  164. core.endGroup()
  165. // LFS fetch
  166. // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time).
  167. // Explicit lfs fetch will fetch lfs objects in parallel.
  168. if (settings.lfs) {
  169. core.startGroup('Fetching LFS objects')
  170. await git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref)
  171. core.endGroup()
  172. }
  173. // Checkout
  174. core.startGroup('Checking out the ref')
  175. await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
  176. core.endGroup()
  177. // Submodules
  178. if (settings.submodules) {
  179. // Temporarily override global config
  180. core.startGroup('Setting up auth for fetching submodules')
  181. await authHelper.configureGlobalAuth()
  182. core.endGroup()
  183. // Checkout submodules
  184. core.startGroup('Fetching submodules')
  185. await git.submoduleSync(settings.nestedSubmodules)
  186. await git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules)
  187. await git.submoduleForeach(
  188. 'git config --local gc.auto 0',
  189. settings.nestedSubmodules
  190. )
  191. core.endGroup()
  192. // Persist credentials
  193. if (settings.persistCredentials) {
  194. core.startGroup('Persisting credentials for submodules')
  195. await authHelper.configureSubmoduleAuth()
  196. core.endGroup()
  197. }
  198. }
  199. // Get commit information
  200. const commitInfo = await git.log1()
  201. // Log commit sha
  202. await git.log1("--format='%H'")
  203. // Check for incorrect pull request merge commit
  204. await refHelper.checkCommitInfo(
  205. settings.authToken,
  206. commitInfo,
  207. settings.repositoryOwner,
  208. settings.repositoryName,
  209. settings.ref,
  210. settings.commit,
  211. settings.githubServerUrl
  212. )
  213. } finally {
  214. // Remove auth
  215. if (authHelper) {
  216. if (!settings.persistCredentials) {
  217. core.startGroup('Removing auth')
  218. await authHelper.removeAuth()
  219. core.endGroup()
  220. }
  221. authHelper.removeGlobalConfig()
  222. }
  223. }
  224. }
  225. export async function cleanup(repositoryPath: string): Promise<void> {
  226. // Repo exists?
  227. if (
  228. !repositoryPath ||
  229. !fsHelper.fileExistsSync(path.join(repositoryPath, '.git', 'config'))
  230. ) {
  231. return
  232. }
  233. let git: IGitCommandManager
  234. try {
  235. git = await gitCommandManager.createCommandManager(repositoryPath, false)
  236. } catch {
  237. return
  238. }
  239. // Remove auth
  240. const authHelper = gitAuthHelper.createAuthHelper(git)
  241. try {
  242. if (stateHelper.PostSetSafeDirectory) {
  243. // 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
  244. // Otherwise all git commands we run in a container fail
  245. await authHelper.configureTempGlobalConfig()
  246. core.info(
  247. `Adding repository directory to the temporary git global config as a safe directory`
  248. )
  249. await git
  250. .config('safe.directory', repositoryPath, true, true)
  251. .catch(error => {
  252. core.info(`Failed to initialize safe directory with error: ${error}`)
  253. })
  254. }
  255. await authHelper.removeAuth()
  256. } finally {
  257. await authHelper.removeGlobalConfig()
  258. }
  259. }
  260. async function getGitCommandManager(
  261. settings: IGitSourceSettings
  262. ): Promise<IGitCommandManager | undefined> {
  263. core.info(`Working directory is '${settings.repositoryPath}'`)
  264. try {
  265. return await gitCommandManager.createCommandManager(
  266. settings.repositoryPath,
  267. settings.lfs
  268. )
  269. } catch (err) {
  270. // Git is required for LFS
  271. if (settings.lfs) {
  272. throw err
  273. }
  274. // Otherwise fallback to REST API
  275. return undefined
  276. }
  277. }