PowerShell Prompt with Mercurial Status
Posted: December 24th, 2009 | Author: Kornelije Sajler | Filed under: Test Lab | Tags: bitbucket, Git, mercurial, PowerShell | 3 Comments »I like Linux shell a lot, and like Rails commands, and I like PowerShell. It is about time that someone from Microsoft wakes up from GUI dream and give power to the command prompt.
I wanted to play a little with PowerShell and read one interesting blog from Mark Embling and his blog post named My Ideal Powershell Prompt with Git Integration.
My last blog post was about transition from Subversion to Git, but the day after I give chance to Mercurial and stay with it. It’s totally nonsensical to speak which is better, because they both are great. I think this is same like question about Rails vs. Django, Github vs. BitBucket.
I found myself in Mercurial, and it is (for me) easier to use in Windows environment. So in spirit of Mark Embling post, I’ll write my Ideal PowerShell Prompt for Mercurial or hg if you want.
I haven’t use PowerShell a lot, I like it because I can use Linux Shell commands ls, cp, … And never used/write PowerShell scripting. But I think I can read code and I can quick adapt in new code/language and find way to accomplish tasks but as many with a little help from Google
There are two scripts, one is PowerShell Profile script and another with functions to get mosto of hg status/branch, named:
- Microsoft.PowerShell_profile.ps1 (inside it calls the second one)
- hgutils.ps1 (functions for hg)
Because I have no time right now to explain all code, I urge you to read Mark Embling great blog post for more information.
Here is the code for Microsoft.PowerShell_profile.ps1:
# My preferred prompt for Powershell.
# Displays mercurial branch and stats when inside a mercurial repository.
# You can clone it by
# hg clone https://xajler@bitbucket.org/xajler/powershell-prompt-for-mercurial/
# And find source here:
# http://bitbucket.org/xajler/powershell-prompt-for-mercurial/src/
# including mercurial functions to use it for prompt
. (Resolve-Path D:/Documents/WindowsPowershell/hgutils.ps1)
function prompt {
$path = ""
$pathbits = ([string]$pwd).split("\", [System.StringSplitOptions]::RemoveEmptyEntries)
if($pathbits.length -eq 1) {
$path = $pathbits[0] + "\"
} else {
$path = $pathbits[$pathbits.length - 1]
}
$userLocation = '@ ' + $path
$Host.UI.RawUi.WindowTitle = $userLocation
Write-Host($userLocation) -nonewline -foregroundcolor Green
if (isCurrentDirectoryMercurialRepository) {
$status = mercurialStatus
$currentBranch = mercurialBranchName
Write-Host(' [') -nonewline -foregroundcolor Yellow
Write-Host($currentBranch) -nonewline -foregroundcolor Magenta
Write-Host(' A' + $status["added"]) -nonewline -foregroundcolor Green
Write-Host(' M' + $status["modified"]) -nonewline -foregroundcolor Yellow
Write-Host(' D' + $status["deleted"]) -nonewline -foregroundcolor Cyan
Write-Host(' !' + $status["missing"]) -nonewline -foregroundcolor Magenta
Write-Host(' ?' + $status["untracked"]) -nonewline -foregroundcolor Red
Write-Host(']') -nonewline -foregroundcolor Yellow
}
Write-Host('>') -nonewline -foregroundcolor Green
return " "
}
And here is the code for hgutils.ps1:
# Mercurial (hg) functions
# Kornelije Sajler (http://learnaholic.me)
# Adopted from Git version of:
# Mark Embling (http://www.markembling.info/)
# You can clone it by
# hg clone https://xajler@bitbucket.org/xajler/powershell-prompt-for-mercurial/
# And find source here:
# http://bitbucket.org/xajler/powershell-prompt-for-mercurial/src/
# Is the current directory a Mercurial repository/working copy?
function isCurrentDirectoryMercurialRepository {
if ((Test-Path ".hg") -eq $TRUE) {
return $TRUE
}
# Test within parent dirs
$checkIn = (Get-Item .).parent
while ($checkIn -ne $NULL) {
$pathToTest = $checkIn.fullname + '/.hg'
if ((Test-Path $pathToTest) -eq $TRUE) {
return $TRUE
} else {
$checkIn = $checkIn.parent
}
}
return $FALSE
}
# Get the current branch
function mercurialBranchName {
$currentBranch = ''
hg branch | foreach {
$currentBranch += $_
}
# Write-Host($currentBranch)
return $currentBranch
}
# Extracts status details about the repo
function mercurialStatus {
$untracked = 0
$added = 0
$modified = 0
$deleted = 0
$missing = 0
$output = hg status
#$branchbits = $output[0].Split(' ')
#$branch = $branchbits[$branchbits.length - 1]
# Write-Host($output)
$output | foreach {
if ($_ -match "^R") {
$deleted += 1
}
elseif ($_ -match "^M") {
$modified += 1
}
elseif ($_ -match "^A") {
$added += 1
}
elseif ($_ -match "^\!") {
$missing += 1
}
elseif ($_ -match "^\?") {
$untracked += 1
}
}
return @{"untracked" = $untracked;
"added" = $added;
"modified" = $modified;
"deleted" = $deleted;
"missing" = $missing}
}
I put scripts on great Mrecurail repository hosting BitBucket so you can clone it by command
hg clone https://xajler@bitbucket.org/xajler/powershell-prompt-for-mercurial/
And find it on this BitBucket page. Also have README if you don’t know how to use it.
I also use great Console2 to host my PowerShell, if you like my below screenshot in BitBucket repository you can also find Console settings xml (console.xml).
Sorry for any mistakes that made in this blog post, because I’m in hurry!!!
Hope you’ll find it useful and ask questions if you have it.





January 29th, 2011 at 14:51
What a nice post. I definitely adore reading these sorts or content articles. I can?t wait to see what other people have to say.
December 30th, 2010 at 14:29
You made some clear points there. I looked on the internet for the issue and found most people will agree with your site.
March 22nd, 2010 at 07:57
[...] information can be found about this here. VN:F [1.8.5_1061]please wait…Rating: 0.0/5 (0 votes [...]