UpdateSubmodules.ps1 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Updates git submodules and generates a commit message with the list of changes
  5. .PARAMETER GitAuthorName
  6. The author name to use in the commit message. (Optional)
  7. .PARAMETER GitAuthorEmail
  8. The author email to use in the commit message. (Optional)
  9. .PARAMETER GitCommitArgs
  10. Additional arguments to pass into git-commit
  11. .PARAMETER NoCommit
  12. Make changes without executing git-commit
  13. .PARAMETER Force
  14. Specified this to make a commit with any changes
  15. #>
  16. [cmdletbinding(SupportsShouldProcess = $true)]
  17. param(
  18. [string]$GitAuthorName = $null,
  19. [string]$GitAuthorEmail = $null,
  20. [string[]]$GitCommitArgs = @(),
  21. [switch]$NoCommit,
  22. [switch]$Force
  23. )
  24. $ErrorActionPreference = 'Stop'
  25. Set-StrictMode -Version 2
  26. $RepoRoot = Resolve-Path "$PSScriptRoot\.."
  27. $ModuleDirectory = Join-Path $RepoRoot "modules"
  28. Import-Module "$PSScriptRoot/common.psm1" -Scope Local -Force
  29. function Get-GitChanges([string]$Path) {
  30. Write-Verbose "git diff --cached --quiet $Path"
  31. & git diff --cached --quiet $Path | Out-Null
  32. if ($LastExitCode -ne 0) {
  33. return $true
  34. }
  35. Write-Verbose "git diff --quiet $Path"
  36. & git diff --quiet $Path | Out-Null
  37. return $LastExitCode -ne 0
  38. }
  39. Push-Location $RepoRoot | Out-Null
  40. try {
  41. Assert-Git
  42. Write-Host "Checking that submodules are in a clean state first..."
  43. if (Get-GitChanges $ModuleDirectory) {
  44. Write-Error "$RepoRoot/modules is in an unclean state. Reset submodules first by running ``git submodule update``"
  45. exit 1
  46. }
  47. $submodules = Get-Submodules $RepoRoot -Verbose:$VerbosePreference
  48. foreach ($submodule in $submodules) {
  49. $submodulePath = $submodule.path
  50. Write-Host "Updating $submodulePath"
  51. $vcs_name = "BUILD_VCS_NUMBER_" + $submodule.module
  52. $newCommit = [environment]::GetEnvironmentVariable($vcs_name)
  53. if (-not $newCommit) {
  54. Write-Warning "TeamCity env variable '$vcs_name' not found. Pulling the latest submodule branch instead"
  55. Invoke-Block { & git submodule update --remote $submodulePath }
  56. Push-Location $submodulePath | Out-Null
  57. try {
  58. $newCommit = $(git rev-parse HEAD)
  59. }
  60. finally {
  61. Pop-Location | Out-Null
  62. }
  63. }
  64. else {
  65. Push-Location $submodulePath | Out-Null
  66. try {
  67. Invoke-Block { & git checkout $newCommit }
  68. }
  69. finally {
  70. Pop-Location | Out-Null
  71. }
  72. }
  73. $submodule.newCommit = $newCommit
  74. if ($newCommit -ne $submodule.commit) {
  75. $submodule.changed = $true
  76. Write-Host -ForegroundColor Cyan "`t=> $($submodule.module) updated to $($submodule.newCommit)"
  77. }
  78. else {
  79. Write-Host -ForegroundColor Magenta "`t$($submodule.module) did not change"
  80. }
  81. }
  82. $changes = $submodules `
  83. | ? { $_.changed } `
  84. | % {
  85. Invoke-Block { & git add $_.path }
  86. "$($_.module) => $($_.newCommit)"
  87. }
  88. if ($changes) {
  89. $shortMessage = "Updating submodule(s) `n`n$( $changes -join "`n" )"
  90. # add this to the commit message to make it possible to filter commit triggers based on message
  91. $message = "$shortMessage`n`n[auto-updated: submodules]"
  92. if (-not $NoCommit -and ($Force -or ($PSCmdlet.ShouldContinue($shortMessage, 'Create a new commit with these changes?')))) {
  93. $gitConfigArgs = @()
  94. if ($GitAuthorName) {
  95. $gitConfigArgs += '-c',"user.name=$GitAuthorName"
  96. }
  97. if ($GitAuthorEmail) {
  98. $gitConfigArgs += '-c',"user.email=$GitAuthorEmail"
  99. }
  100. Invoke-Block { & git @gitConfigArgs commit -m $message @GitCommitArgs }
  101. }
  102. else {
  103. # If composing this script with others, return the message that would have been used
  104. return @{
  105. message = $message
  106. }
  107. }
  108. }
  109. else {
  110. Write-Host -ForegroundColor Magenta 'No changes detected in git submodules'
  111. }
  112. }
  113. finally {
  114. Pop-Location
  115. }