git-sparse.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Git Sparse Checkout Utilities
  3. * Handles git sparse-checkout operations for downloading specific paths
  4. */
  5. import { execSync } from 'child_process'
  6. import { existsSync, rmSync, mkdirSync } from 'fs'
  7. import { join } from 'path'
  8. export interface GitSparseOptions {
  9. repository: string
  10. branch: string
  11. paths: string[]
  12. targetDir: string
  13. verbose?: boolean
  14. }
  15. export interface GitSparseResult {
  16. success: boolean
  17. commit: string
  18. error?: string
  19. }
  20. /**
  21. * Clone repository with sparse checkout
  22. */
  23. export async function sparseClone(
  24. options: GitSparseOptions
  25. ): Promise<GitSparseResult> {
  26. const { repository, branch, paths, targetDir, verbose } = options
  27. try {
  28. // Clean up target directory if it exists
  29. if (existsSync(targetDir)) {
  30. rmSync(targetDir, { recursive: true, force: true })
  31. }
  32. // Create parent directory
  33. const parentDir = join(targetDir, '..')
  34. if (!existsSync(parentDir)) {
  35. mkdirSync(parentDir, { recursive: true })
  36. }
  37. // Clone with sparse checkout (no working tree files)
  38. if (verbose) {
  39. console.log(`Cloning ${repository}...`)
  40. }
  41. execSync(
  42. `git clone --depth 1 --filter=blob:none --sparse --branch ${branch} https://github.com/${repository}.git "${targetDir}"`,
  43. { stdio: verbose ? 'inherit' : 'pipe' }
  44. )
  45. // Configure sparse checkout for requested paths
  46. if (verbose) {
  47. console.log('Configuring sparse checkout...')
  48. }
  49. const sparseCheckoutPaths = paths.join(' ')
  50. execSync(`cd "${targetDir}" && git sparse-checkout set ${sparseCheckoutPaths}`, {
  51. stdio: verbose ? 'inherit' : 'pipe',
  52. })
  53. // Get commit SHA
  54. const commit = execSync(`cd "${targetDir}" && git rev-parse HEAD`, {
  55. encoding: 'utf-8',
  56. }).trim()
  57. if (verbose) {
  58. console.log(`Downloaded commit: ${commit}`)
  59. }
  60. return {
  61. success: true,
  62. commit,
  63. }
  64. } catch (error) {
  65. return {
  66. success: false,
  67. commit: '',
  68. error: error instanceof Error ? error.message : String(error),
  69. }
  70. }
  71. }
  72. /**
  73. * Copy files from sparse checkout to target location
  74. */
  75. export function copyFiles(
  76. sourceDir: string,
  77. targetDir: string,
  78. verbose?: boolean
  79. ): void {
  80. if (!existsSync(sourceDir)) {
  81. throw new Error(`Source directory not found: ${sourceDir}`)
  82. }
  83. // Create target directory if it doesn't exist
  84. if (!existsSync(targetDir)) {
  85. mkdirSync(targetDir, { recursive: true })
  86. }
  87. // Copy files
  88. if (verbose) {
  89. console.log(`Copying files from ${sourceDir} to ${targetDir}...`)
  90. }
  91. execSync(`cp -r "${sourceDir}"/* "${targetDir}"/`, {
  92. stdio: verbose ? 'inherit' : 'pipe',
  93. })
  94. }
  95. /**
  96. * Clean up temporary directory
  97. */
  98. export function cleanup(dir: string, verbose?: boolean): void {
  99. if (existsSync(dir)) {
  100. if (verbose) {
  101. console.log(`Cleaning up ${dir}...`)
  102. }
  103. rmSync(dir, { recursive: true, force: true })
  104. }
  105. }
  106. /**
  107. * Check if git is available
  108. */
  109. export function checkGitAvailable(): boolean {
  110. try {
  111. execSync('command -v git', { stdio: 'ignore' })
  112. return true
  113. } catch {
  114. return false
  115. }
  116. }