Getting TIP of remote Mercurial server

July 18, 2010

I needed to get hold of the tip and parent version of a remote mercurial repository. It isn't possible to do this directly through mercurial, but it is possible using paramiko.

This script will print out the output of hg tip, just change the command to do a hg parent instead.

import paramiko, base64, os, select, time

hostname='remotehost'
port = 22
username = 'drumcoder'
password = 'secret'

client = paramiko.SSHClient()  
client.load_system_host_keys()  
client.connect(hostname=hostname, username=username, password=password)  
transport = client.get_transport()  
channel = transport.open_session()  
channel.exec_command("cd /path/to/repos && hg tip")  
while True:  
    rl, wl, xl = select.select([channel],[],[],0.0)  
    if len(rl) > 0:  
        # Must be stdout  
        lResults = channel.recv(1024)
        print lResults
        if len(lResults) == 0:
            break