<commit_before>#!/usr/bin/env pwsh

<#
.SYNOPSIS
    Updates git submodules and generates a commit message with the list of changes
.PARAMETER GitAuthorName
    The author name to use in the commit message. (Optional)
.PARAMETER GitAuthorEmail
    The author email to use in the commit message. (Optional)
.PARAMETER GitCommitArgs
    Additional arguments to pass into git-commit
.PARAMETER NoCommit
    Make changes without executing git-commit
.PARAMETER Force
    Specified this to make a commit with any changes
#>
[cmdletbinding(SupportsShouldProcess = $true)]
param(
    [string]$GitAuthorName = $null,
    [string]$GitAuthorEmail = $null,
    [string[]]$GitCommitArgs = @(),
    [switch]$NoCommit,
    [switch]$Force
)

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 2

$RepoRoot = Resolve-Path "$PSScriptRoot\.."
$ModuleDirectory = Join-Path $RepoRoot "modules"

Import-Module "$PSScriptRoot/common.psm1" -Scope Local -Force

function Get-GitChanges([string]$Path) {
    Write-Verbose "git diff --cached --quiet $Path"
    & git diff --cached --quiet $Path | Out-Null
    if ($LastExitCode -ne 0) {
        return $true
    }
    Write-Verbose "git diff --quiet $Path"
    & git diff --quiet $Path | Out-Null
    return $LastExitCode -ne 0
}

Push-Location $RepoRoot | Out-Null
try {
    Assert-Git

    Write-Host "Checking that submodules are in a clean state first..."
    if (Get-GitChanges $ModuleDirectory) {
        Write-Error "$RepoRoot/modules is in an unclean state. Reset submodules first by running ``git submodule update``"
        exit 1
    }

    $submodules = Get-Submodules $RepoRoot -Verbose:$VerbosePreference

    foreach ($submodule in  $submodules) {
        $submodulePath = $submodule.path
        Write-Host "Updating $submodulePath"

        $vcs_name = "BUILD_VCS_NUMBER_" + $submodule.module
        $newCommit = [environment]::GetEnvironmentVariable($vcs_name)

        if (-not $newCommit) {
            Write-Warning "TeamCity env variable '$vcs_name' not found. Pulling the latest submodule branch instead"
            Invoke-Block { & git submodule update --remote $submodulePath }
            Push-Location $submodulePath | Out-Null
            try {
                $newCommit = $(git rev-parse HEAD)
            }
            finally {
                Pop-Location | Out-Null
            }
        }
        else {
            Push-Location $submodulePath | Out-Null
            try {
                Invoke-Block { & git checkout $newCommit }
            }
            finally {
                Pop-Location | Out-Null
            }
        }
<commit_msg>Fail the submodule script if the VCS roots are not configured to match submodules<commit_after>#!/usr/bin/env pwsh

<#
.SYNOPSIS
    Updates git submodules and generates a commit message with the list of changes
.PARAMETER GitAuthorName
    The author name to use in the commit message. (Optional)
.PARAMETER GitAuthorEmail
    The author email to use in the commit message. (Optional)
.PARAMETER GitCommitArgs
    Additional arguments to pass into git-commit
.PARAMETER NoCommit
    Make changes without executing git-commit
.PARAMETER Force
    Specified this to make a commit with any changes
#>
[cmdletbinding(SupportsShouldProcess = $true)]
param(
    [string]$GitAuthorName = $null,
    [string]$GitAuthorEmail = $null,
    [string[]]$GitCommitArgs = @(),
    [switch]$NoCommit,
    [switch]$Force
)

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 2

$RepoRoot = Resolve-Path "$PSScriptRoot\.."
$ModuleDirectory = Join-Path $RepoRoot "modules"

Import-Module "$PSScriptRoot/common.psm1" -Scope Local -Force

function Get-GitChanges([string]$Path) {
    Write-Verbose "git diff --cached --quiet $Path"
    & git diff --cached --quiet $Path | Out-Null
    if ($LastExitCode -ne 0) {
        return $true
    }
    Write-Verbose "git diff --quiet $Path"
    & git diff --quiet $Path | Out-Null
    return $LastExitCode -ne 0
}

Push-Location $RepoRoot | Out-Null
try {
    Assert-Git

    Write-Host "Checking that submodules are in a clean state first..."
    if (Get-GitChanges $ModuleDirectory) {
        Write-Error "$RepoRoot/modules is in an unclean state. Reset submodules first by running ``git submodule update``"
        exit 1
    }

    $submodules = Get-Submodules $RepoRoot -Verbose:$VerbosePreference

    foreach ($submodule in  $submodules) {
        $submodulePath = $submodule.path
        Write-Host "Updating $submodulePath"

        $vcs_name = "BUILD_VCS_NUMBER_" + ($submodule.module -replace '.','_')
        $newCommit = [environment]::GetEnvironmentVariable($vcs_name)

        if (-not $newCommit) {
            if ($env:TEAMCITY_PROJECT_NAME) {
                throw "TeamCity env variable '$vcs_name' not found. Make sure to configure a VCS root for $submodulePath"
            }
            Invoke-Block { & git submodule update --remote $submodulePath }
            Push-Location $submodulePath | Out-Null
            try {
                $newCommit = $(git rev-parse HEAD)
            }
            finally {
                Pop-Location | Out-Null
            }
        }
        else {
            Push-Location $submodulePath | Out-Null
            try {
                Invoke-Block { & git checkout $newCommit }
            }
            finally {
                Pop-Location | Out-Null
            }
        }

{'fraction_non_alphanumeric': 0.0980647562337179, 'fraction_numerical': 0.0018608113137327876, 'mean_word_length': 2.4323116219667944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369231', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1567, 'n_words': 469}