chris@1544
|
1 #!/usr/bin/env python
|
chris@1544
|
2
|
chris@1544
|
3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
|
chris@1544
|
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
|
chris@1544
|
5
|
chris@1544
|
6 from mercurial import hg,util,ui,templatefilters
|
chris@1544
|
7 import re
|
chris@1544
|
8 import os
|
chris@1544
|
9 import sys
|
Chris@1567
|
10 import subprocess
|
chris@1544
|
11
|
chris@1544
|
12 # default git branch name
|
chris@1544
|
13 cfg_master='master'
|
chris@1544
|
14 # default origin name
|
chris@1544
|
15 origin_name=''
|
chris@1544
|
16 # silly regex to see if user field has email address
|
chris@1544
|
17 user_re=re.compile('([^<]+) (<[^>]*>)$')
|
chris@1544
|
18 # silly regex to clean out user names
|
chris@1544
|
19 user_clean_re=re.compile('^["]([^"]+)["]$')
|
chris@1544
|
20
|
chris@1544
|
21 def set_default_branch(name):
|
chris@1544
|
22 global cfg_master
|
chris@1544
|
23 cfg_master = name
|
chris@1544
|
24
|
chris@1544
|
25 def set_origin_name(name):
|
chris@1544
|
26 global origin_name
|
chris@1544
|
27 origin_name = name
|
chris@1544
|
28
|
chris@1544
|
29 def setup_repo(url):
|
chris@1544
|
30 try:
|
chris@1544
|
31 myui=ui.ui(interactive=False)
|
chris@1544
|
32 except TypeError:
|
chris@1544
|
33 myui=ui.ui()
|
chris@1544
|
34 myui.setconfig('ui', 'interactive', 'off')
|
chris@1544
|
35 return myui,hg.repository(myui,url)
|
chris@1544
|
36
|
chris@1544
|
37 def fixup_user(user,authors):
|
chris@1544
|
38 user=user.strip("\"")
|
chris@1544
|
39 if authors!=None:
|
chris@1544
|
40 # if we have an authors table, try to get mapping
|
chris@1544
|
41 # by defaulting to the current value of 'user'
|
chris@1544
|
42 user=authors.get(user,user)
|
chris@1544
|
43 name,mail,m='','',user_re.match(user)
|
chris@1544
|
44 if m==None:
|
chris@1544
|
45 # if we don't have 'Name <mail>' syntax, extract name
|
chris@1544
|
46 # and mail from hg helpers. this seems to work pretty well.
|
chris@1544
|
47 # if email doesn't contain @, replace it with devnull@localhost
|
chris@1544
|
48 name=templatefilters.person(user)
|
chris@1544
|
49 mail='<%s>' % util.email(user)
|
chris@1544
|
50 if '@' not in mail:
|
chris@1544
|
51 mail = '<devnull@localhost>'
|
chris@1544
|
52 else:
|
chris@1544
|
53 # if we have 'Name <mail>' syntax, everything is fine :)
|
chris@1544
|
54 name,mail=m.group(1),m.group(2)
|
chris@1544
|
55
|
chris@1544
|
56 # remove any silly quoting from username
|
chris@1544
|
57 m2=user_clean_re.match(name)
|
chris@1544
|
58 if m2!=None:
|
chris@1544
|
59 name=m2.group(1)
|
chris@1544
|
60 return '%s %s' % (name,mail)
|
chris@1544
|
61
|
chris@1544
|
62 def get_branch(name):
|
chris@1544
|
63 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
|
chris@1544
|
64 # other CVS imports may need it, too
|
chris@1544
|
65 if name=='HEAD' or name=='default' or name=='':
|
chris@1544
|
66 name=cfg_master
|
chris@1544
|
67 if origin_name:
|
chris@1544
|
68 return origin_name + '/' + name
|
chris@1544
|
69 return name
|
chris@1544
|
70
|
chris@1544
|
71 def get_changeset(ui,repo,revision,authors={},encoding=''):
|
chris@1544
|
72 node=repo.lookup(revision)
|
chris@1544
|
73 (manifest,user,(time,timezone),files,desc,extra)=repo.changelog.read(node)
|
chris@1544
|
74 if encoding:
|
chris@1544
|
75 user=user.decode(encoding).encode('utf8')
|
chris@1544
|
76 desc=desc.decode(encoding).encode('utf8')
|
chris@1544
|
77 tz="%+03d%02d" % (-timezone / 3600, ((-timezone % 3600) / 60))
|
chris@1544
|
78 branch=get_branch(extra.get('branch','master'))
|
chris@1544
|
79 return (node,manifest,fixup_user(user,authors),(time,tz),files,desc,branch,extra)
|
chris@1544
|
80
|
chris@1544
|
81 def mangle_key(key):
|
chris@1544
|
82 return key
|
chris@1544
|
83
|
chris@1544
|
84 def load_cache(filename,get_key=mangle_key):
|
chris@1544
|
85 cache={}
|
chris@1544
|
86 if not os.path.exists(filename):
|
chris@1544
|
87 return cache
|
chris@1544
|
88 f=open(filename,'r')
|
chris@1544
|
89 l=0
|
chris@1544
|
90 for line in f.readlines():
|
chris@1544
|
91 l+=1
|
chris@1544
|
92 fields=line.split(' ')
|
chris@1544
|
93 if fields==None or not len(fields)==2 or fields[0][0]!=':':
|
chris@1544
|
94 sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l))
|
chris@1544
|
95 continue
|
chris@1544
|
96 # put key:value in cache, key without ^:
|
chris@1544
|
97 cache[get_key(fields[0][1:])]=fields[1].split('\n')[0]
|
chris@1544
|
98 f.close()
|
chris@1544
|
99 return cache
|
chris@1544
|
100
|
chris@1544
|
101 def save_cache(filename,cache):
|
chris@1544
|
102 f=open(filename,'w+')
|
chris@1544
|
103 map(lambda x: f.write(':%s %s\n' % (str(x),str(cache.get(x)))),cache.keys())
|
chris@1544
|
104 f.close()
|
chris@1544
|
105
|
chris@1544
|
106 def get_git_sha1(name,type='heads'):
|
chris@1544
|
107 try:
|
chris@1544
|
108 # use git-rev-parse to support packed refs
|
Chris@1567
|
109 ref="refs/%s/%s" % (type,name)
|
Chris@1567
|
110 l=subprocess.check_output(["git", "rev-parse", "--verify", "--quiet", ref])
|
chris@1544
|
111 if l == None or len(l) == 0:
|
chris@1544
|
112 return None
|
chris@1544
|
113 return l[0:40]
|
Chris@1567
|
114 except subprocess.CalledProcessError:
|
chris@1544
|
115 return None
|