I finally decided to learn some version control system (VCS). As my script folder(s) are going bigger they are also more important to me. Till now I used just basic “copy-to-NAS” backup but I was not doing that every week. I felt for a long time that I should learn some of the available tools, but never had enough time, patience and examples to try it. Fortunately things changed last weekend.

Pluralsight has a course called Mercurial Fundamentals (presented by Todd Ropog, @ropog) and I decided to spent two hours with them. Now I can say it was well spent time :) I think that all my previous attempts failed just because I started to use graphical interfaces for different VCS tools. Whole course is based on command line, so I was able to understand the basics.

I am pretty sure I will have few more posts about Mercurial during next few weeks. If you are developer, you probably know all about that. For me it’s a completely new (nice) world. But let’s go back to the idea behind this article.

As I have a lot of testing folders, I wanted to be sure what’s the actual parent folder of my Mercurial repository. Let’s imagine the repository as separate folder structure. It should be something like this:

PS C:\Repositories\ConfigMgrModule> Show-Tree -ShowLeaf
C:\Repositories\ConfigMgrModule
├──.hg
│ ├──cache
│ │ ├──branchheads
│ │ └──tags
│ └──store
│   └──data
│     └──format
├──FormatData
│ └──PSCMX.Format.Provider.ps1xml
├──Provider.ps1
└──PSCMX.psm1

.hg folder contains files related to Mercurial itself. The other files are related to my own work I want to be tracked by Mercurial. I created new PSDrive, called Repo and I want to have different prompt when I am inside this Repo drive. For the example above – whenever I am under any folder in Repo:/<project_name>/* structure, I want to see that <project_name> folder name in my prompt. Compare prompt on the following picture:

When I am outside Repo, I use normal prompt function. When change location to Repo drive, I see list of folders (my repositories) and when going into one of these repositories it’s root name is visible in my prompt. It allows me to see where exactly I am. Code for the root folder name display is here:

function Get-HgRootDirectoryName
{
    param($path)

    if (Get-ChildItem -Path "$path\.hg" -ea 0)
    {
        $final = Split-Path -Path $path -Leaf
        return $final
    }
    else
    {
        $up = Split-Path -Path $path -Parent
        if (-not $up) { return }
        else { Get-HgRootDirectoryName $up }
    }
}

If I am in the folder and see .hg folder at the same level, I return folder name (5-8). If not, I am going up in the hierarchy, removing last folder name (12) and doing the check again using recursion (14). I am also checking if I am not in root of the PSDrive (12) and if so, I am returning nothing (13). Again, I am using this piece of code only if I am inside my Repo drive. So if I go to my C:\Repositories using “normal” way, this new prompt is not shown. That’s just how I like it.

BTW: Accidentally, the same weekend I saw Mercurial course, I also finished Jim Christoper‘s (@beefarinoEveryday PowerShell for Developers course (also from Pluralsight). Jim discussed Mercurial in one of the modules, so if you want to see more integration of Mercurial into PowerShell, you can check his course too. I learn few nice tips from him.