[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [string]$Path = ".", [ValidateSet('List', 'Show', 'Apply', 'Pop', 'Drop')] [string]$Action = 'List', [ValidateRange(0, 1000)] [int]$Index = 0, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Get-Command git -ErrorAction SilentlyContinue)) { throw "git not found in PATH" } $repo = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path $null = & git -C $repo rev-parse --is-inside-work-tree 2>$null if ($LASTEXITCODE -ne 0) { throw "Not a git repository: $repo" } $format = '%gd|%ci|%cr|%gs' $lines = @(& git -C $repo stash list "--date=iso" "--format=$format" 2>$null) $rows = @() foreach ($line in $lines) { if ([string]::IsNullOrWhiteSpace($line)) { continue } $parts = $line -split '\|', 4 $dateText = if ($parts.Count -gt 1) { $parts[1].Trim() } else { '' } $ageText = if ($parts.Count -gt 2) { $parts[2].Trim() } else { '' } $messageText = if ($parts.Count -gt 3) { $parts[3].Trim() } else { '' } $rows += [pscustomobject]@{ Ref = $parts[0].Trim() Date = $dateText Age = $ageText Message = $messageText } } if ($Action -eq 'List') { if ($rows.Count -eq 0) { Write-Host "No stashes." exit 0 } $rows | Format-Table -AutoSize exit 0 } if ($rows.Count -eq 0) { throw "No stashes found." } if ($Index -ge $rows.Count) { throw "Stash index $Index is out of range. Available: 0..$($rows.Count - 1)" } $stashRef = "stash@{$Index}" switch ($Action) { 'Show' { & git -C $repo stash show -p $stashRef exit $LASTEXITCODE } 'Apply' { if ($PSCmdlet.ShouldProcess("stash $stashRef", "Apply")) { & git -C $repo stash apply $stashRef exit $LASTEXITCODE } } 'Pop' { if ($PSCmdlet.ShouldProcess("stash $stashRef", "Apply and remove")) { & git -C $repo stash pop $stashRef exit $LASTEXITCODE } } 'Drop' { if (-not $Force) { throw "Drop requires -Force because the stash entry will be deleted." } if ($PSCmdlet.ShouldProcess("stash $stashRef", "Drop permanently")) { & git -C $repo stash drop $stashRef exit $LASTEXITCODE } } }