git-push.ps1 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. $ErrorActionPreference = 'SilentlyContinue'
  2. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  3. $root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
  4. Set-Location $root
  5. $mainBranch = 'master'
  6. git show-ref --verify --quiet refs/heads/master 2>$null | Out-Null
  7. if ($LASTEXITCODE -ne 0) {
  8. git show-ref --verify --quiet refs/heads/main 2>$null | Out-Null
  9. if ($LASTEXITCODE -eq 0) { $mainBranch = 'main' } else { $mainBranch = (git rev-parse --abbrev-ref HEAD 2>$null).Trim() }
  10. }
  11. Write-Host ''
  12. Write-Host 'Files to be pushed:'
  13. Write-Host '========================================'
  14. $diff = git diff --name-status "origin/$mainBranch..HEAD" 2>$null
  15. if ($LASTEXITCODE -eq 0 -and $diff) { Write-Host $diff } else {
  16. $log = git log --name-status --oneline HEAD 2>$null
  17. $lines = $log -split "`n" | Where-Object { $_.Trim() -and $_ -notmatch '^[a-f0-9]' }
  18. if ($lines) { Write-Host ($lines -join "`n") }
  19. }
  20. Write-Host '========================================'
  21. Write-Host ''
  22. Write-Host "Pushing to origin/$mainBranch..."
  23. Write-Host '========================================'
  24. git push origin $mainBranch
  25. if ($LASTEXITCODE -eq 0) {
  26. Write-Host ''
  27. Write-Host '========================================'
  28. Write-Host ' success (Push successful)'
  29. Write-Host '========================================'
  30. exit 0
  31. } else {
  32. Write-Host ''
  33. Write-Host '========================================'
  34. Write-Host ' failed (Push failed)'
  35. Write-Host ' Check error messages above.'
  36. Write-Host '========================================'
  37. exit 1
  38. }