|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + A script to diff the contents of folders matching a specified pattern between two specified branches. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + The script uses git to determine the set of files (under folders matching a specified pattern) that are different |
| 7 | + between the specified branches. It can also optionally display the line diffs for these files. |
| 8 | +
|
| 9 | +.PARAMETER baseline |
| 10 | + The baseline branch against which the specified target branch is to be compared. (Defaults to 'main' if omitted.) |
| 11 | +.PARAMETER target |
| 12 | + The target branch which is to be compared against the specified baseline branch. |
| 13 | +.PARAMETER folderPattern |
| 14 | + The pattern that selects the folders that are to be compared. (Defaults to '*.AI.* if omitted.) |
| 15 | +.PARAMETER showDiff |
| 16 | + Determines whether or not line diffs should be displayed for the differing files. (Defaults to 'false' if omitted.) |
| 17 | +
|
| 18 | +.EXAMPLE |
| 19 | + PS> .\DiffBranches -target "release/9.5" -folderPattern "*.Evaluation.*" |
| 20 | +.EXAMPLE |
| 21 | + PS> .\DiffBranches -baseline "release/9.4" -target "release/9.5" -folderPattern "*.Evaluation.*" |
| 22 | +.EXAMPLE |
| 23 | + PS> .\DiffBranches -target "release/9.5" -showDiff |
| 24 | +#> |
| 25 | + |
| 26 | +[CmdletBinding()] |
| 27 | +param( |
| 28 | + [Parameter(Mandatory=$false)] |
| 29 | + [string]$baseline = "main", |
| 30 | + |
| 31 | + [Parameter(Mandatory=$true)] |
| 32 | + [string]$target, |
| 33 | + |
| 34 | + [Parameter(Mandatory=$false)] |
| 35 | + [string]$folderPattern = "*.AI.*", |
| 36 | + |
| 37 | + [Parameter(Mandatory=$false)] |
| 38 | + [switch]$showDiff |
| 39 | +) |
| 40 | + |
| 41 | +function Invoke-GitCommand { |
| 42 | + param( |
| 43 | + [Parameter(Mandatory=$true)] |
| 44 | + [string]$Command, |
| 45 | + |
| 46 | + [Parameter(Mandatory=$false)] |
| 47 | + [switch]$UseCmd |
| 48 | + ) |
| 49 | + |
| 50 | + if ($UseCmd) { |
| 51 | + $Command = "cmd.exe /c $Command" |
| 52 | + } |
| 53 | + |
| 54 | + Write-Host "Executing $Command" -ForegroundColor Blue |
| 55 | + $result = Invoke-Expression $Command |
| 56 | + return $result |
| 57 | +} |
| 58 | + |
| 59 | +# Save the current directory |
| 60 | +$originalLocation = Get-Location |
| 61 | + |
| 62 | +try { |
| 63 | + # Get the root directory of the git repository |
| 64 | + $gitRootCommand = "git rev-parse --show-toplevel" |
| 65 | + $repoRoot = Invoke-GitCommand -Command $gitRootCommand |
| 66 | + Write-Host "Repo root is $repoRoot" -ForegroundColor Blue |
| 67 | + |
| 68 | + # Change to the repository root directory |
| 69 | + Set-Location $repoRoot |
| 70 | + |
| 71 | + # Get all changed files between the two branches |
| 72 | + $gitFilesCommand = "git diff --name-only $baseline..$target" |
| 73 | + $changedFiles = Invoke-GitCommand -Command $gitFilesCommand |
| 74 | + |
| 75 | + # Filter for files under folders containing the specified pattern |
| 76 | + $matchedFiles = $changedFiles | Where-Object { |
| 77 | + $path = $_ |
| 78 | + $folders = $path -split '/' |
| 79 | + $folders | Where-Object { $_ -like $folderPattern } | Select-Object -First 1 |
| 80 | + } |
| 81 | + |
| 82 | + if ($matchedFiles.Count -eq 0) { |
| 83 | + Write-Host "No changes detected." -ForegroundColor Green |
| 84 | + } else { |
| 85 | + Write-Host "Changes detected in following files:" -ForegroundColor Yellow |
| 86 | + $matchedFiles | ForEach-Object { Write-Host " $_" } |
| 87 | + |
| 88 | + if ($showDiff) { |
| 89 | + Write-Host "File diffs:" -ForegroundColor Yellow |
| 90 | + |
| 91 | + $gitDiffCommand = "git -C `"$repoRoot`" diff --color $baseline..$target -- $($matchedFiles -join ' ')" |
| 92 | + |
| 93 | + # Use the -UseCmd switch to run the command with cmd.exe (preserves color and paging) |
| 94 | + Invoke-GitCommand -Command $gitDiffCommand -UseCmd |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +finally { |
| 99 | + # Return to the original directory even if an error occurs |
| 100 | + Set-Location $originalLocation |
| 101 | +} |
0 commit comments