## Tuesday, 2 April 2013

### Using Powershell to perform automatic Hg Bisect

The process of bisection in the source control system Mercurial or Hg is done with the command Hg Bisect. The Hg Bisect command is usually run manually and consists of manual steps done by the user where the developer running the hg bisect command looks for a certain condition should hold true and mark each revision as either good or bad. This is done in a binary search result manner, which quickly narrows down the number of revisions to look at by half for each iteration. The goal is to find the first revision containing a bad feature. An automatic function for finding such bad features will now be presented. Here is the Powershell cmdlet or function followed by an example of a call to this function:

function Get-HgChangeSet(
){
param(
[Parameter(Mandatory=$true)] [scriptblock]$test,

[Parameter()]
$good = 0, [Parameter()]$bad = 'tip'
)

hg bisect --reset;
hg bisect --bad $bad; (hg bisect --good$good) | out-null;

while ($output -notmatch 'The first bad revision is'){$result = & $test; if ($result){
$output = (hg bisect --good ) | out-string; } else {$output = (hg bisect --bad) | out-string;
}
}

$output; } Push-Location 'C:\toaurs-he\demorepo\' Get-HgChangeSet { !(Test-Path 'test.txt') -or (Get-Content test.txt) -notmatch 'ultrabad' } Pop-Location In this example, a simple demo repository has a file called test.txt. I want to find the first revision where the text ultrabad was inserted. The truth condition then, is that either the file does not exist (yet) in a revision, or that the file exists and does not match ultrabad. This is a simple example, but it shows how one can search for a given text or source code by specifying this in the script block passed to the function or cmdlet Get-HgChangeSet. When the script is run, it finds the first occurence of the text ultrabad (which is bad) at revision number 8. PS C:\toaurs-he\demorepo[ default ]> Hit Line breakpoint on 'C:\Users\Tore Aurstad\Documents\WindowsPowerShell\Scripts\Hg\BisectTool.ps1:22' PS C:\toaurs-he\demorepo[ default ]> The first bad revision is: changeset: 8:25be1d61e90d user: Tore Aurstad date: Tue Apr 02 22:09:15 2013 +0200 summary: foo 4  So there you have it, an automatic way via a Powershell script to find an introduction of a bad feature in a Hg repository with the aid of a Powershell function or cmdlet. To use this script, it is important to understand that the passed in first argument is a truth condition. It will usually always be necessary to accept if the file to test does not exist yet combined with and -or condition and then specify -notmatch and the text or source code which is not desired. This will usually be more precise than running hg bisect manually, which is kind of tedious still. Also note that the second and third parameter is set to$good equals 0 and $bad equals 'tip'. In Mercurial or Hg, 'tip' is the newest revision. Usually, for a repository with many revisions, it will be quicker if the values for$good and $bad is specified in a well-known range, say revision 5000 to 5500 or what have you.. This is just another example of how scripting can lighten our daily workload as developers. #### 2 comments: 1. Isn't that what "hg bisect -c$CMD" is for?

2. Hello Peter, I have not tried that command. If it is a one-liner command, obviously this Powershell script is unecessary. Thanks for the tip.