g-revert.ps1
|
param( [Parameter(ValueFromPipeline)] [string]$Ref ) . (Join-Path $PSScriptRoot 'g-registry.ps1') $repo = Get-Location $branch = git -C $repo branch --show-current 2>$null if (-not $branch) { Write-Host "not a git repo"; exit 1 } $target = if ($Ref) { $Ref } else { 'HEAD' } $resolved = git -C $repo rev-parse --verify $target 2>$null if ($LASTEXITCODE -ne 0) { Write-Host "ref '$target' not found"; exit 1 } $shortHash = git -C $repo rev-parse --short $target 2>$null $subject = git -C $repo log -1 --pretty=%s $target 2>$null $customMessage = $null try { $confirm = Read-Host " revert $shortHash ('$subject') -- use default message? [Y/n]" if ($confirm -match '^[Nn]') { $customMessage = Read-Host " revert message" } } catch { } $parentCount = (git -C $repo cat-file -p $resolved 2>$null | Where-Object { $_ -match '^parent ' } | Measure-Object).Count $mFlag = if ($parentCount -gt 1) { @('-m', '1') } else { @() } if ($parentCount -gt 1) { Write-Host " (merge commit: using -m 1 mainline parent)" } if ($customMessage) { $revertOut = git -C $repo revert $target $mFlag --no-commit 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "revert failed" if ($VerbosePreference -ne 'SilentlyContinue') { $revertOut | ForEach-Object { Write-Host " $_" } } exit 1 } $commitOut = git -C $repo commit -m $customMessage 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "commit failed; revert staged but not committed -- run: git commit -m <message>" if ($VerbosePreference -ne 'SilentlyContinue') { $commitOut | ForEach-Object { Write-Host " $_" } } exit 1 } } else { $revertOut = git -C $repo revert $target $mFlag --no-edit 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "revert failed" if ($VerbosePreference -ne 'SilentlyContinue') { $revertOut | ForEach-Object { Write-Host " $_" } } exit 1 } } Write-Host "reverted $shortHash -- new commit on $branch" exit 0 |