UpdateSubmodules.ps1 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 -replace '\.','_')
  52. $newCommit = [environment]::GetEnvironmentVariable($vcs_name)
  53. if (-not $newCommit) {
  54. if ($env:TEAMCITY_PROJECT_NAME) {
  55. throw "TeamCity env variable '$vcs_name' not found. Make sure to configure a VCS root for $submodulePath"
  56. }
  57. Invoke-Block { & git submodule update --remote $submodulePath }
  58. Push-Location $submodulePath | Out-Null
  59. try {
  60. $newCommit = $(git rev-parse HEAD)
  61. }
  62. finally {
  63. Pop-Location | Out-Null
  64. }
  65. }
  66. else {
  67. Push-Location $submodulePath | Out-Null
  68. try {
  69. Invoke-Block { & git checkout $newCommit }
  70. }
  71. finally {
  72. Pop-Location | Out-Null
  73. }
  74. }
  75. $submodule.newCommit = $newCommit
  76. if ($newCommit -ne $submodule.commit) {
  77. $submodule.changed = $true
  78. Write-Host -ForegroundColor Cyan "`t=> $($submodule.module) updated to $($submodule.newCommit)"
  79. }
  80. else {
  81. Write-Host -ForegroundColor Magenta "`t$($submodule.module) did not change"
  82. }
  83. }
  84. $changes = $submodules `
  85. | ? { $_.changed } `
  86. | % {
  87. Invoke-Block { & git add $_.path }
  88. "$($_.module) => $($_.newCommit)"
  89. }
  90. if ($changes) {
  91. $shortMessage = "Updating submodule(s) `n`n$( $changes -join "`n" )"
  92. # add this to the commit message to make it possible to filter commit triggers based on message
  93. $message = "$shortMessage`n`n[auto-updated: submodules]"
  94. if (-not $NoCommit -and ($Force -or ($PSCmdlet.ShouldContinue($shortMessage, 'Create a new commit with these changes?')))) {
  95. $gitConfigArgs = @()
  96. if ($GitAuthorName) {
  97. $gitConfigArgs += '-c',"user.name=$GitAuthorName"
  98. }
  99. if ($GitAuthorEmail) {
  100. $gitConfigArgs += '-c',"user.email=$GitAuthorEmail"
  101. }
  102. Invoke-Block { & git @gitConfigArgs commit -m $message @GitCommitArgs }
  103. }
  104. else {
  105. # If composing this script with others, return the message that would have been used
  106. return @{
  107. message = $message
  108. }
  109. }
  110. }
  111. else {
  112. Write-Host -ForegroundColor Magenta 'No changes detected in git submodules'
  113. }
  114. }
  115. finally {
  116. Pop-Location
  117. }