Script to iterate over changesets via python interface

Daniel Santa Cruz byteshack at gmail.com
Thu May 4 09:59:03 CDT 2006


Hello all,

Maybe someone else will find these useful.  Shows how easy it is to
program against the repository through the API.

Daniel

== Iterate over all changeset in a repository ==
#!/usr/bin/env python
import time
import mercurial.ui, mercurial.node, mercurial.hg

repo = mercurial.hg.repository(mercurial.ui.ui(), '/path/to/repo')

for i in xrange(repo.changelog.count()):
    hgcid = repo.changelog.node(i)
    cset = repo.changelog.read(hgcid)
    p1, p2 = repo.changelog.parents(hgcid)

    cid = mercurial.node.hex(hgcid)
    cuser = cset[1].strip()
    ctime = time.strftime('%b %d, %Y %H:%M:%S', time.gmtime(cset[2][0]))
    cdesc = cset[4].replace("'", "''").strip()
    cp1 = mercurial.node.hex(p1)
    cp2 = mercurial.node.hex(p2)

    # Do something with the data

== Iterate over "incoming" changesets ==
#!/usr/bin/env python2.4

import re
import sys
import time
import mercurial.ui, mercurial.node, mercurial.hg

if len(sys.argv) != 3:
    print "Must pass: trunk branch"
    sys.exit()

trunk = mercurial.hg.repository(mercurial.ui.ui(), sys.argv[1])
repo = mercurial.hg.repository(mercurial.ui.ui(), sys.argv[2])

branchName = re.search('/([^/]*)/?$', sys.argv[2]).group(1)
print "BranchName:", branchName

newones = trunk.findincoming(repo)
newones = repo.changelog.nodesbetween(newones)[0]

for i in newones:
    hgcid = i
    cset = repo.changelog.read(hgcid)
    p1, p2 = repo.changelog.parents(hgcid)

    cid = mercurial.node.hex(hgcid)
    cuser = cset[1].strip()
    ctime = time.strftime('%b %d, %Y %H:%M:%S', time.gmtime(cset[2][0]))
    cdesc = cset[4].replace("'", "''").strip()
    cp1 = mercurial.node.hex(p1)
    cp2 = mercurial.node.hex(p2)

    # Do something with the data



More information about the Mercurial mailing list