Friday, 29 March 2013

Creating a Mercurial-aware Powershell command line

This article will describe how we can create a Mercurial-aware Powershell command line. Mercurial is a (distributed) source control. It supports branch per feature and multiple other nice features that gives powerful control of the source code. To get Mercurial, check out the following url: Mercurial The Mercurial-aware Powershell command line will detect when the user enters a Mercurial repository folder on disk. This will be done via the hg branch command, which will return null if the user is not in a Mercurial folder and the name of the branch if the user is inside a Mercurial repository. Let's look at the needed code first, I put this code in my $profile file, use the cmdlet inside Powershell to open the $profile file: Invoke-Item $profile This will edit the profile file of your Powershell by launching Notepad. The $profile file is not perhaps created yet. If that is the case, enter the following command: new-item -ItemType file -path $profile Now retry the command Invoke-Item $profile if this command failed. If you just type: $profile at the Powershell prompt, you will get the file location of the profile file. Make note that Powershell ISE and Powershell prompt has different profile files.. Before I can show you the script, I must get the script transformed into readable colored HTML right? I type Import-Module PowershellPack, now I can use the nice cmdlet Copy-ColoredHTML. This copies the script as colored HTML into the clipboard. I can then paste the
function get-hgStatus(            
    $status = @('M','A','R','C','!','?','I')            
    hg status --all |            
        where { $_ -match "^\s*[$Status]" } |            
        foreach { $_ -replace "\s+", ',' } |            
        ConvertFrom-Csv -Header Status, Path             
function prompt {            
 $branch = (hg branch);            
 $status = get-hgStatus | Group-Object Status;            
 $modified = $status | where { $ -eq 'M' } | select -expand count;             
 $added = $status | where { $ -eq 'A' } | select -expand count;             
 $removed = $status | where { $ -eq 'R' } | select -expand count;             
 $untracked = $status | where { $ -eq '?' } | select -expand count;             
 $missing = $status | where { $ -eq '!' } | select -expand count;             
 Write-Host "PS $pwd" -NoNewline            
 if ($branch){            
  write-host "[" -NoNewline            
  write-host " $branch " -ForegroundColor White -NoNewline            
  if ($added){            
    write-host " " -NoNewline            
    write-host "+$added" -ForegroundColor green -NoNewline;            
  if ($modified){            
    write-host " " -NoNewline            
    write-host $modified -ForegroundColor yellow -NoNewline;            
  if ($removed){            
    write-host " " -NoNewline            
    write-host "-$removed" -ForegroundColor magenta -NoNewline;            
  if ($missing){            
    write-host " " -NoNewline            
    write-host "!$missing" -ForegroundColor red -NoNewline;            
  if ($untracked){            
    write-host " " -NoNewline            
    write-host "?$untracked" -ForegroundColor gray -NoNewline;            
  write-host "]" -NoNewline;            
 "> "            
Note the use of a default value in the passed in $status variable, which is default set to an array of all the possible hg status flags. The hg-GetStatus function can be called individually like hg-GetStatus A to show all the added files in the repo. The script above uses the hg status command to get the status of the current hg repository in the folder. If the current folder is not a hg repository, a default prompt is shown instead. The output of the command hg status is converted into a structured object and then grouped by status using the Group-Object cmdlet. A pipeline is used to prepare the output of the hg status before it is transformed into the structured object. The count of each group is retrieved with the Select cmdlet, and using the -extract flag. The group counts are then shown to the user in the prompt and formatted with color. Make note of the use of -NoNewLine and -ForegroundColor. In addition, the Powershell function prompt will control how your prompt in the shell will look like, i.e. the command line. I put this prompt in the $profile file such that this is inited each time.

This is just an introduction to customizing the Powershell command line to a more suited hg aware prompt for developers using hg. There is actually already a better package available on the Internet for displaying the state of the hg repository in the folder displayed in Powershell, which is called hg posh. Check out hg posh on the following url:
hg posh

However, although hg posh is more correct, I like the look of this command prompt better ... For many hg users, this will suffice.. And here is our nice new Mercurial aware command line:

No comments:

Post a Comment