Mercurial revision history search with PHP

Hgview is a fantastic tool but for some reason the search field refused to come up in my version no matter how much CTRL+F I tried.

So I had to find some other way of searching all the revision changes by a substring in the content/code.

As it happens the command hg log -v -p -r 100 will for instance give you all the details for revision 100, which code was added, deleted, changed and all. Just what I needed in order to loop revisions.

Here is the result:

$search = $_SERVER['argv'][1];
$srev 	= $_SERVER['argv'][2];
$erev 	= $_SERVER['argv'][3];

for($i = $srev; $i <= $erev; $i++){
	$content 	= shell_exec("hg log -v -p -r $i");
	$pos 		= strpos($content, $search);
	if($pos !== false){
		$sub = substr($content, $pos - 20, 40);
		echo $sub." in rev $i\n\n\n";
	}
}

As you can see we search for a substring in a span of revisions, if we have a match we print out a string containing the match with some after and before and the revision number. Those revision numbers can be used in hgview to get more information.

The script could be invoked like this: php hgsearch.php searchForMe 100 200 to search for the substring searchForMe in revisions 100 to 200.

Related Posts

Tags: , , ,