UpdateSubmodules.ps1 4.0 KB

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