3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
6 from mercurial import repo,hg,cmdutil,util,ui,revlog,node
11 # default git branch name
15 # silly regex to see if user field has email address
16 user_re=re.compile('([^<]+) (<[^>]+>)$')
17 # silly regex to clean out user names
18 user_clean_re=re.compile('^["]([^"]+)["]$')
20 def set_default_branch(name):
24 def set_origin_name(name):
30 myui.setconfig('ui', 'interactive', 'off')
31 return myui,hg.repository(myui,url)
33 def fixup_user(user,authors):
35 # if we have an authors table, try to get mapping
36 # by defaulting to the current value of 'user'
37 user=authors.get(user,user)
38 name,mail,m='','',user_re.match(user)
40 # if we don't have 'Name <mail>' syntax, use 'user
41 # <devnull@localhost>' if use contains no at and
42 # 'user <user>' otherwise
45 mail='<devnull@localhost>'
49 # if we have 'Name <mail>' syntax, everything is fine :)
50 name,mail=m.group(1),m.group(2)
52 # remove any silly quoting from username
53 m2=user_clean_re.match(name)
56 return '%s %s' % (name,mail)
59 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
60 # other CVS imports may need it, too
61 if name=='HEAD' or name=='default' or name=='':
64 return origin_name + '/' + name
67 def get_changeset(ui,repo,revision,authors={}):
68 node=repo.lookup(revision)
69 (manifest,user,(time,timezone),files,desc,extra)=repo.changelog.read(node)
70 tz="%+03d%02d" % (-timezone / 3600, ((-timezone % 3600) / 60))
71 branch=get_branch(extra.get('branch','master'))
72 return (node,manifest,fixup_user(user,authors),(time,tz),files,desc,branch,extra)
77 def load_cache(filename,get_key=mangle_key):
79 if not os.path.exists(filename):
83 for line in f.readlines():
85 fields=line.split(' ')
86 if fields==None or not len(fields)==2 or fields[0][0]!=':':
87 sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l))
89 # put key:value in cache, key without ^:
90 cache[get_key(fields[0][1:])]=fields[1].split('\n')[0]
94 def save_cache(filename,cache):
96 map(lambda x: f.write(':%s %s\n' % (str(x),str(cache.get(x)))),cache.keys())
99 def get_git_sha1(name,type='heads'):
101 # use git-rev-parse to support packed refs
102 cmd="GIT_DIR='%s' git rev-parse --verify refs/%s/%s 2>/dev/null" % (os.getenv('GIT_DIR','/dev/null'),type,name)
106 if l == None or len(l) == 0: