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