chg-len.py uses the Mercurial API directly
authorAlpar Juttner <alpar@cs.elte.hu>
Mon, 10 Nov 2008 11:14:56 +0000
changeset 3764b2382fd80ef
parent 375 2d87dbd7f8c8
child 378 80ec623f529f
chg-len.py uses the Mercurial API directly

This makes chg-len.py much faster.
scripts/chg-len.py
     1.1 --- a/scripts/chg-len.py	Fri Nov 07 13:14:22 2008 +0000
     1.2 +++ b/scripts/chg-len.py	Mon Nov 10 11:14:56 2008 +0000
     1.3 @@ -1,7 +1,8 @@
     1.4  #! /usr/bin/env python
     1.5  
     1.6  import sys
     1.7 -import os
     1.8 +
     1.9 +from mercurial import ui, hg
    1.10  
    1.11  if len(sys.argv)>1 and sys.argv[1] in ["-h","--help"]:
    1.12      print """
    1.13 @@ -9,32 +10,20 @@
    1.14  in the revision graph from revison 0 to the current one.
    1.15  """
    1.16      exit(0)
    1.17 -plist = os.popen("HGRCPATH='' hg parents --template='{rev}\n'").readlines()
    1.18 -if len(plist)>1:
    1.19 -    print "You are in the process of merging"
    1.20 -    exit(1)
    1.21 -PAR = int(plist[0])
    1.22  
    1.23 -f = os.popen("HGRCPATH='' hg log -r 0:tip --template='{rev} {parents}\n'").\
    1.24 -    readlines()
    1.25 -REV = -1
    1.26 -lengths=[]
    1.27 -for l in f:
    1.28 -    REV+=1
    1.29 -    s = l.split()
    1.30 -    rev = int(s[0])
    1.31 -    if REV != rev:
    1.32 -        print "Something is seriously wrong"
    1.33 -        exit(1)
    1.34 -    if len(s) == 1:
    1.35 -        par1 = par2 = rev - 1
    1.36 -    elif len(s) == 2:
    1.37 -        par1 = par2 = int(s[1].split(":")[0])
    1.38 +u = ui.ui()
    1.39 +r = hg.repository(u, ".")
    1.40 +N = r.changectx(".").rev()
    1.41 +lengths=[0]*(N+1)
    1.42 +for i in range(N+1):
    1.43 +    p=r.changectx(i).parents()
    1.44 +    if p[0]:
    1.45 +        p0=lengths[p[0].rev()]
    1.46      else:
    1.47 -        par1 = int(s[1].split(":")[0])
    1.48 -        par2 = int(s[2].split(":")[0])
    1.49 -    if rev == 0:
    1.50 -        lengths.append(0)
    1.51 +        p0=-1
    1.52 +    if len(p)>1 and p[1]:
    1.53 +        p1=lengths[p[1].rev()]
    1.54      else:
    1.55 -        lengths.append(max(lengths[par1],lengths[par2])+1)
    1.56 -print lengths[PAR]
    1.57 +        p1=-1
    1.58 +    lengths[i]=max(p0,p1)+1
    1.59 +print lengths[N]