Repository: microsoft/CSS-Exchange
File: .build/CodeFormatter.ps1 (PowerShell)

Code before:
    $m = $gitlog | Select-String "^(\S+) (.*)$"

    foreach ($commitMatch in $m) {
        $commitHash = $commitMatch.Matches.Groups[1].Value
        $filesChangedInCommit = git diff-tree --no-commit-id --name-only -r $commitHash

        foreach ($fileChanged in $filesChangedInCommit) {
            $fullPath = Join-Path $repoRoot $fileChanged

            if ($filesFullPath.Contains($fullPath)) {
                Write-Host "  $fileChanged was modified in a later commit."
                continue
            }

            $filesFullPath.Add($fullPath)
        }
    }

    # Also include modified files, but not committed yet for local work.
    $gitStatus = git status --short
    $m = $gitStatus | Select-String "M (.*)"
    foreach ($match in $m) {
        $file = $match.Matches.Groups[1].Value.Trim()
        $fullPath = Join-Path $PSScriptRoot $file

        if ($filesFullPath.Contains($fullPath)) {
            Write-Host " $file was modified in a commit already."
            continue
        }
        $filesFullPath.Add($fullPath)
    }

    Write-Verbose "Files changed or modified"
    $filesFullPath | Write-Verbose

    # Only optimize CodeFormatter IF any CodeFormatter related files were not modified or PSScriptAnalyzerSettings.psd1
    $optimizeCodeFormatter = $null -eq ($filesFullPath | Where-Object { $_ -like "*.build\CodeFormatter*" -or $_ -like "*\PSScriptAnalyzerSettings.psd1" })
    Write-Host "Optimize Code: $optimizeCodeFormatter"
}

if ($optimizeCodeFormatter) {
    $filesToCheck = Get-ChildItem -Path $filesFullPath -Include "*.ps1", "*.psm1", "*.md"
    Write-Host "Files that we are looking at for code formatting:"
    $filesToCheck.FullName | Write-Host
} else {
    $filesToCheck = Get-ChildItem -Path $repoRoot -Directory | Where-Object {
        $_.Name -ne "dist" } | ForEach-Object {
        Get-ChildItem -Path $_.FullName -Include "*.ps1", "*.psm1", "*.md" -Recurse
    }
}


Review comment (bug):
Calling Contains on a List[string] is bugging me. Let's make this a HashSet[string]. At that point we don't even have to check it - we can blindly add it and it will still only be in there once. If Add() returns $false, you know it was already in there and can report it if you want.

Code after:
    $m = $gitlog | Select-String "^(\S+) (.*)$"

    foreach ($commitMatch in $m) {
        $commitHash = $commitMatch.Matches.Groups[1].Value
        $filesChangedInCommit = git diff-tree --no-commit-id --name-only -r $commitHash

        foreach ($fileChanged in $filesChangedInCommit) {
            $fullPath = Join-Path $repoRoot $fileChanged
            Write-Verbose "Adding commit file to list: $fullPath"
            [void]$filesFullPath.Add($fullPath)
        }
    }

    # Also include modified files, but not committed yet for local work.
    $gitStatus = git status --short
    $m = $gitStatus | Select-String "M (.*)"
    foreach ($match in $m) {
        $file = $match.Matches.Groups[1].Value.Trim()
        $fullPath = Join-Path $PSScriptRoot $file

        Write-Verbose "Adding modified file to list: $fullPath"
        [void]$filesFullPath.Add($fullPath)
    }

    Write-Verbose "Files changed or modified"
    $filesFullPath | Write-Verbose

    # Only optimize CodeFormatter IF any CodeFormatter related files were not modified or PSScriptAnalyzerSettings.psd1
    $optimizeCodeFormatter = $null -eq ($filesFullPath | Where-Object { $_ -like "*.build\CodeFormatter*" -or $_ -like "*\PSScriptAnalyzerSettings.psd1" })
    Write-Host "Optimize Code: $optimizeCodeFormatter"
}

if ($optimizeCodeFormatter) {
    $filesToCheck = Get-ChildItem -Path $filesFullPath -Include "*.ps1", "*.psm1", "*.md"
    Write-Host "Files that we are looking at for code formatting:"
    $filesToCheck.FullName | Write-Host
} else {
    $filesToCheck = Get-ChildItem -Path $repoRoot -Directory | Where-Object {
        $_.Name -ne "dist" } | ForEach-Object {
        Get-ChildItem -Path $_.FullName -Include "*.ps1", "*.psm1", "*.md" -Recurse
    }
}

$errorCount = 0

foreach ($fileInfo in $filesToCheck) {
    $errorCount += (CheckFileHasNewlineAtEndOfFile $fileInfo $Save) ? 1 : 0
    $errorCount += (CheckMarkdownFileHasNoBOM $fileInfo $Save) ? 1 : 0
    $errorCount += (CheckScriptFileHasBOM $fileInfo $Save) ? 1 : 0
    $errorCount += (CheckScriptFileHasComplianceHeader $fileInfo $Save) ? 1 : 0
    $errorCount += (CheckKeywordCasing $fileInfo $Save) ? 1 : 0