Created
August 21, 2012 12:16
-
-
Save tgmayfield/3415004 to your computer and use it in GitHub Desktop.
Combined git/mercurial powershell prompt (from http://matthewmanela.com/blog/launching-the-tortoisehg-log-more-conveniently-from-the-command-line/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (test-path function:\prompt) { | |
$oldPrompt = ls function: | ? {$_.Name -eq "prompt"} | |
remove-item -force function:\prompt | |
} | |
function prompt { | |
$color = "Green" | |
$markers = "DarkGreen" | |
$important = "Red" | |
function outputNormal([string]$message) | |
{ | |
Write-Host ($message) -NoNewLine -ForegroundColor $color | |
} | |
function outputMarker([string]$message) | |
{ | |
Write-Host ($message) -NoNewLine -ForegroundColor $markers | |
} | |
function outputImportant([string]$message) | |
{ | |
Write-Host ($message) -NoNewLine -ForegroundColor $important | |
} | |
function writeGitStatus { | |
$branch = git branch -v -v --color=never 2>&1 | |
if ($branch.Exception -ne $null) { | |
return $false | |
} | |
outputMarker " (" | |
$branch | foreach { | |
if ($_ -match "^\*\s*([^ ]*)\s*") { | |
$branch = $matches[1].trim() | |
outputNormal $branch | |
} | |
if ($_ -match "ahead ([0-9]+)") { | |
outputImportant ("+" + $matches[1]) | |
} | |
if ($_ -match "behind ([0-9]+)") { | |
outputImportant ("-" + $matches[1]) | |
} | |
} | |
$untracked = 0 | |
$updates = 0 | |
$deletes = 0 | |
$addsIndex = 0 | |
$deletesIndex = 0 | |
$updatesIndex = 0 | |
$renamesIndex = 0 | |
git status --porcelain | foreach { | |
if ($_ -match "^([\w?]|\s)([\w?])?\s*") { | |
switch ($matches[1]) { | |
"A" { $addsIndex++ } | |
"M" { $updatesIndex++ } | |
"D" { $deletesIndex++ } | |
"R" { $renamesIndex++ } | |
"?" { $untracked++ } | |
} | |
switch ($matches[2]) { | |
"M" { $updates++ } | |
"D" { $deletes++ } | |
} | |
} | |
} | |
# Treat renames as modifications | |
$updatesIndex += $renamesIndex | |
if ($addsIndex -gt 0) { | |
outputNormal " a:$addsIndex" | |
} | |
if (($updates + $updatesIndex) -gt 0) { | |
outputNormal " m:$updatesIndex" | |
if ($updates -gt 0) { | |
outputNormal "/$updates" | |
} | |
} | |
if (($deletes + $deletesIndex) -gt 0) { | |
outputNormal " d:$deletesIndex" | |
if ($deletes -gt 0) { | |
outputNormal "/$deletes" | |
} | |
} | |
if ($untracked -gt 0) { | |
outputNormal " ?:$untracked" | |
} | |
outputMarker ")" | |
return $true | |
} | |
function writeHgStatus { | |
$summary = hg summary 2>&1 | |
if ($summary.Exception -eq $null) { | |
$regex = "(?si)(parent:(?<parent>.*?)(\n|\r)+.*?)(branch:(?<branch>.*)\s)(commit:(?<commit>.*)\s)(update:(?<update>.*))"; | |
$summary = [System.String]::Join([System.Environment]::NewLine,$summary) | |
$res = $summary -match $regex | |
$status = "hg {0} c:{1}" -f $matches["branch"].Trim(), $matches["commit"].Trim() | |
outputNormal $status | |
return $true | |
} | |
else { | |
return $false | |
} | |
} | |
& $oldPrompt | |
writeGitStatus | |
writeHgStatus | |
return " " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment