git-push.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. // Get project root (two levels up from this script)
  5. const projectRoot = path.resolve(__dirname, '../..')
  6. // Change to project root
  7. process.chdir(projectRoot)
  8. // Configuration: main branch name (leave empty to auto-detect)
  9. const CONFIG_BRANCH = 'master'
  10. // Detect main branch
  11. let mainBranch = CONFIG_BRANCH
  12. if (!CONFIG_BRANCH) {
  13. // Auto-detect main branch (priority: master > main > current branch)
  14. try {
  15. execSync('git show-ref --verify --quiet refs/heads/master', { stdio: 'ignore' })
  16. mainBranch = 'master'
  17. } catch (e) {
  18. try {
  19. execSync('git show-ref --verify --quiet refs/heads/main', { stdio: 'ignore' })
  20. mainBranch = 'main'
  21. } catch (e2) {
  22. // Use current branch
  23. mainBranch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim()
  24. }
  25. }
  26. }
  27. // Show files to be pushed
  28. console.log('')
  29. console.log('Files to be pushed:')
  30. console.log('========================================')
  31. try {
  32. const diffOutput = execSync(`git diff --name-status origin/${mainBranch}..HEAD`, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] })
  33. console.log(diffOutput.trim())
  34. } catch (e) {
  35. // If remote branch doesn't exist, show all files
  36. try {
  37. const logOutput = execSync('git log --name-status --oneline HEAD', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] })
  38. const lines = logOutput.split('\n').filter(line => line.trim() && !/^[a-f0-9]/.test(line))
  39. console.log(lines.join('\n'))
  40. } catch (e2) {
  41. // Ignore errors
  42. }
  43. }
  44. console.log('========================================')
  45. console.log('')
  46. // Push to main branch
  47. console.log('')
  48. console.log(`Pushing to origin/${mainBranch}...`)
  49. console.log('========================================')
  50. try {
  51. const pushOutput = execSync(`git push origin ${mainBranch}`, { encoding: 'utf-8', stdio: 'inherit' })
  52. console.log('========================================')
  53. console.log('')
  54. console.log('========================================')
  55. console.log('[OK] Push successful')
  56. console.log('========================================')
  57. console.log('')
  58. process.exit(0)
  59. } catch (error) {
  60. console.log('========================================')
  61. console.log('')
  62. console.log('========================================')
  63. console.log(`[ERROR] Push failed (exit code: ${error.status || 1})`)
  64. console.log('========================================')
  65. console.log('')
  66. console.log('Please check the error messages above.')
  67. console.log('')
  68. process.exit(error.status || 1)
  69. }