Public/Get-ChangedALFiles.ps1
|
function Get-ChangedALFiles { <# .SYNOPSIS Get list of changed AL files using native git .DESCRIPTION Detects changed .al files in the current branch compared to a base branch. Auto-detects PR context in Azure DevOps pipelines. .PARAMETER BaseBranch Base branch for comparison (default: origin/master). In PR builds, auto-detects from SYSTEM_PULLREQUEST_TARGETBRANCH environment variable. .EXAMPLE Get-ChangedALFiles Returns all changed .al files compared to origin/master .EXAMPLE Get-ChangedALFiles -BaseBranch "origin/development" Compare against specific branch .OUTPUTS System.String[] - Array of file paths relative to repository root .NOTES Author: waldo Version: 1.0.0 #> [CmdletBinding()] param( [Parameter(Mandatory = $false)] [string]$BaseBranch = "origin/master" ) begin { Write-Verbose "Starting $($MyInvocation.MyCommand.Name)" } process { try { # Auto-detect base branch in PR context if ($env:SYSTEM_PULLREQUEST_TARGETBRANCH) { $branchName = $env:SYSTEM_PULLREQUEST_TARGETBRANCH -replace '^refs/heads/', '' $BaseBranch = "origin/$branchName" Write-Host "PR detected - comparing against $BaseBranch" } Write-Verbose "Getting changed AL files using git diff against $BaseBranch" # Ensure we have latest remote refs Write-Verbose "Fetching latest from origin..." git fetch origin --quiet 2>&1 | Out-Null # Debug: Show current state Write-Host "Current HEAD: $(git rev-parse HEAD)" Write-Host "Current branch: $(git rev-parse --abbrev-ref HEAD)" Write-Host "Base branch: $BaseBranch" Write-Host "Base commit: $(git rev-parse $BaseBranch 2>&1)" # In PR builds, use BUILD_SOURCEVERSION which is the merge commit $compareCommit = if ($env:BUILD_SOURCEVERSION) { $env:BUILD_SOURCEVERSION } else { "HEAD" } Write-Host "Comparing commit: $compareCommit" # Get list of changed .al files # --name-only: just file names # --diff-filter=ACMR: Added, Copied, Modified, Renamed (skip Deleted) Write-Host "Running: git diff --name-only --diff-filter=ACMR `"$BaseBranch...$compareCommit`"" $changedFiles = git diff --name-only --diff-filter=ACMR "$BaseBranch...$compareCommit" 2>&1 | Where-Object { $_ -match '\.al$' } if (-not $changedFiles) { Write-Host "No AL files changed" Write-Host "" Write-Host "Debug: All changed files:" git diff --name-only "$BaseBranch...$compareCommit" 2>&1 | ForEach-Object { Write-Host " $_" } return @() } $changedFiles = @($changedFiles) # Ensure array Write-Host "✅ Found $($changedFiles.Count) changed AL file(s):" foreach ($file in $changedFiles) { Write-Host " - $file" } return $changedFiles } catch { Write-Error "Error in $($MyInvocation.MyCommand.Name): $_" throw } } end { Write-Verbose "Completed $($MyInvocation.MyCommand.Name)" } } |