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 AurstadSo 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.date: Tue Apr 02 22:09:15 2013 +0200 summary: foo 4
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.
Isn't that what "hg bisect -c $CMD" is for?
ReplyDeleteHello Peter, I have not tried that command. If it is a one-liner command, obviously this Powershell script is unecessary. Thanks for the tip.
ReplyDelete