Click here to Skip to main content
11,708,116 members (68,496 online)
Click here to Skip to main content

TortoiseHg Manager (Repository Verification)

, 14 Dec 2012 GPL3 4.6K 73 1
Rate this:
Please Sign up or sign in to vote.
TortoiseHgManager, a repositories verification tool for TortoiseHg.

About TortoiseHg

TortoiseHg is a Windows shell extension and a series of applications for the Mercurial distributed revision control system.  


This article presents a repository tool named TortoiseHgManager created for TortoiseHg to verify all repositories in one click. I assumed that you have a basic knowledge with TortoiseHg and Mercurial.


Contents of a Mercurial repository are stored as a sub folder named ".hg". This folder contains multiple files which hold the status and history for each tracked file. The integrity of this folder is important for the repository to work properly. There is a chance that files in the repository folder will get corrupted due to either a hardware or software error. One may still be able to commit to a damaged repository but getting an earlier revision may no longer work.

Repository Verification

Mercurial and TortoiseHg do provide a verification function to verify the integrity of a repository. The verification function can be accessed from the TortoiseHg Workbench under Repository --> Verify.

This function works fine for one repository at a time. However, it is very tedious to verify all repositories as there isn't any function to do this in one click.

About TortoiseHgManager  

TortoiseHgManager is created to automate the repository verification for all Mercurial repositories registered under the TortoiseHg Repository Registry. This tool will perform repository verification on all repositories and report its status. The number of repositories which failed verification are shown in the status bar as below:

Load Repositories from Repository Registry

The TortoiseHg repository registry is stored in the following location: "%APPDATA%\TortoiseHg\thg-reporegistry.xml". TortoiseHgManager retrieves the repositories information from this file.

Verify Repositories

The Mercurial verification function is used to verify each repository as shown in the TortoiseHg Workbench. The command is given as below:

Command: hg --repository <Repository Path> verify --verbose

The Mercurial command is executed using the Process class. 

Process p = new Process();
p.StartInfo.FileName = "\"C:\\Program Files\\TortoiseHg\\hg.exe\"";
p.StartInfo.Arguments = "--repository \"" + repositoryPath + "\" verify --verbose"; ;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(repositoryPath);
p.StartInfo.CreateNoWindow = true;


Multithreading Support

To avoid freezing the main window, we employed a background worker to manage the verification process for each repository. To speed things up, we use the Parallel.For in the background worker to run multiple verification processes. Each iteration of Parallel.For will spawn one thread. 

Parallel.For(0, dgRepo.Rows.Count, taskOption, rowIndex =>
    if (UserAborted) return;

    DataGridViewRow ptrRow = dgRepo.Rows[rowIndex];
    if (VerifyRepository(Repositories[ptrRow.Index], ref errorMessage))
        UpdateStatus(ptrRow, "OK", true);
        UpdateStatus(ptrRow, "ERROR", false);

Verifying N repositories with N threads doesn't sound practical. It will run even slower when there are too many threads running at once. To optimize the performance, we limit the threads in the Parallel.For by setting the MaxDegreeofParallelism property in ParallelOption. This option is settable from the track bar located on the upper left of the Application's Main Form with max thread limited to 8.


  • Version 1.0.0 - Initial release.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Code Artist
Software Developer (Senior)
Malaysia Malaysia

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
dzmuh15-Jan-13 2:39
memberdzmuh15-Jan-13 2:39 
GeneralRe: My vote of 5 Pin
Code Artist15-Jan-13 2:46
memberCode Artist15-Jan-13 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 14 Dec 2012
Article Copyright 2012 by Code Artist
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid