Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions scripts/DiffBranches.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$baseline = "main",

[Parameter(Mandatory=$true)]
[string]$target,

[Parameter(Mandatory=$false)]
[string]$folderPattern = "*.AI.*",

[Parameter(Mandatory=$false)]
[switch]$showDiff
)

function Invoke-GitCommand {
param(
[Parameter(Mandatory=$true)]
[string]$Command,

[Parameter(Mandatory=$false)]
[switch]$UseCmd
)

if ($UseCmd) {
$Command = "cmd.exe /c $Command"
}

Write-Host "Executing $Command" -ForegroundColor Blue
$result = Invoke-Expression $Command
return $result
}

# Save the current directory
$originalLocation = Get-Location

try {
# Get the root directory of the git repository
$gitRootCommand = "git rev-parse --show-toplevel"
$repoRoot = Invoke-GitCommand -Command $gitRootCommand
Write-Host "Repo root is $repoRoot" -ForegroundColor Blue

# Change to the repository root directory
Set-Location $repoRoot

# Get all changed files between the two branches
$gitFilesCommand = "git diff --name-only $baseline..$target"
$changedFiles = Invoke-GitCommand -Command $gitFilesCommand

# Filter for files under folders containing the specified pattern
$matchedFiles = $changedFiles | Where-Object {
$path = $_
$folders = $path -split '/'
$folders | Where-Object { $_ -like $folderPattern } | Select-Object -First 1
}

if ($matchedFiles.Count -eq 0) {
Write-Host "No changes detected." -ForegroundColor Green
} else {
Write-Host "Changes detected in following files:" -ForegroundColor Yellow
$matchedFiles | ForEach-Object { Write-Host " $_" }

if ($showDiff) {
Write-Host "File diffs:" -ForegroundColor Yellow

$gitDiffCommand = "git -C `"$repoRoot`" diff --color $baseline..$target -- $($matchedFiles -join ' ')"

# Use the -UseCmd switch to run the command with cmd.exe (preserves color and paging)
Invoke-GitCommand -Command $gitDiffCommand -UseCmd
}
}
}
finally {
# Return to the original directory even if an error occurs
Set-Location $originalLocation
}
Loading