Created
July 30, 2012 15:27
-
-
Save mmanela/3207796 to your computer and use it in GitHub Desktop.
Combined Mercurial and Git PowerShell Prompt
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 { | |
function getGitStatus { | |
$branch = git branch 2>&1 | |
if($branch.Exception -eq $null) { | |
$status = "git" | |
$branch | foreach { | |
if ($_ -match "^\*(.*)"){ | |
$status += $matches[1] | |
} | |
} | |
$untracked = 0 | |
$updates = 0 | |
$deletes = 0 | |
$addsIndex = 0 | |
$deletesIndex =0 | |
$updatesIndex = 0 | |
git status --porcelain | foreach { | |
if($_ -match "^([\w?]|\s)([\w?])?\s*") { | |
switch ($matches[1]) { | |
"A" {$addsIndex++ } | |
"M" {$updatesIndex++ } | |
"D" {$deletesIndex++ } | |
"?" {$untracked++ } | |
} | |
switch ($matches[2]) { | |
"A" {$adds++ } | |
"M" {$updates++ } | |
"D" {$deletes++ } | |
} | |
} | |
} | |
$status += " a:" +$addsIndex | |
if($adds -gt 0) { | |
$status += "($adds)" | |
} | |
$status += " m:" +$updatesIndex | |
if($updates -gt 0) { | |
$status += "($updates)" | |
} | |
$status += " d:" +$deletesIndex | |
if($deletes -gt 0) { | |
$status += "($deletes)" | |
} | |
$status += " ?:$untracked" | |
return $status | |
} | |
else { | |
return $false | |
} | |
} | |
function getHgStatus { | |
$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() | |
return $status | |
} | |
else { | |
return $false | |
} | |
} | |
$status = getGitStatus | |
if(-not $status) { | |
$status = getHgStatus | |
} | |
$host.ui.rawui.WindowTitle = (get-location).Path | |
if($status) { | |
write-host ($status) -NoNewLine -ForegroundColor Green | |
write-host (">") -NoNewLine -ForegroundColor Green | |
} | |
else { | |
& $oldPrompt | |
} | |
return " " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment