annotate hgsubversion/maps.py @ 360:27e9fea5d114

Author maps: strip comments.
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Thu, 28 May 2009 09:44:47 +0200
parents e74321f6f8a1
children f137231f9d30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
1 ''' Module for self-contained maps. '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
2
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
3 import os
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
4 from mercurial import util as hgutil
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
5
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
6 class AuthorMap(dict):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
7 '''A mapping from Subversion-style authors to Mercurial-style
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
8 authors, and back. The data is stored persistently on disk.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
9
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
10 If the 'hgsubversion.defaultauthors' configuration option is set to false,
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
11 attempting to obtain an unknown author will fail with an Abort.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
12 '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
13
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
14 def __init__(self, ui, path, defaulthost=None):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
15 '''Initialise a new AuthorMap.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
16
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
17 The ui argument is used to print diagnostic messages.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
18
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
19 The path argument is the location of the backing store,
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
20 typically .hg/authormap.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
21 '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
22 self.ui = ui
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
23 self.path = path
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
24 if defaulthost:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
25 self.defaulthost = '@%s' % defaulthost.lstrip('@')
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
26 else:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
27 self.defaulthost = ''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
28 self.super = super(AuthorMap, self)
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
29 self.super.__init__()
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
30 self.load(path)
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
31
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
32 def load(self, path):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
33 ''' Load mappings from a file at the specified path. '''
359
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
34 if not os.path.exists(path):
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
35 return
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
36 self.ui.note('reading authormap from %s\n' % path)
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
37 f = open(path, 'r')
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
38 for number, line in enumerate(f):
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
39
360
27e9fea5d114 Author maps: strip comments.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 359
diff changeset
40 line = line.split('#')[0]
359
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
41 if not line.strip():
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
42 continue
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
43
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
44 try:
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
45 src, dst = line.split('=', 1)
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
46 except (IndexError, ValueError):
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
47 msg = 'ignoring line %i in author map %s: %s\n'
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
48 self.ui.warn(msg % (number, path, line.rstrip()))
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
49 continue
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
50
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
51 src = src.strip()
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
52 dst = dst.strip()
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
53 if src in self and dst != self[src]:
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
54 msg = 'overriding author: "%s" to "%s" (%s)\n'
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
55 self.ui.warn(msg % (self[src], dst, src))
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
56 else:
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
57 self[src] = dst
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
58
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
59 f.close()
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
60
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
61 def __setitem__(self, key, value):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
62 ''' Similar to dict.__setitem__, but also updates the new mapping in the
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
63 backing store. '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
64 self.super.__setitem__(key, value)
359
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
65 self.ui.debug('adding author %s to author map\n' % self.path)
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
66 f = open(self.path, 'w+')
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
67 for k, v in self.iteritems():
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
68 f.write("%s=%s\n" % (k, v))
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
69 f.close()
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
70
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
71 def __getitem__(self, author):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
72 ''' Similar to dict.__getitem__, except in case of an unknown author.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
73 In such cases, a new value is generated and added to the dictionary
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
74 as well as the backing store. '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
75 if author in self:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
76 result = self.super.__getitem__(author)
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
77 elif self.ui.configbool('hgsubversion', 'defaultauthors', True):
310
15b8bab03504 Change default author substitution to avoid updating test hashes.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 307
diff changeset
78 self[author] = result = '%s%s' % (author, self.defaulthost)
359
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
79 msg = 'substituting author "%s" for default "%s"\n'
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
80 self.ui.note(msg % (author, result))
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
81 else:
359
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
82 msg = 'author %s has no entry in the author map!'
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
83 raise hgutil.Abort(msg % author)
e74321f6f8a1 Author maps: code/message style (prevent line continuations).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 358
diff changeset
84 self.ui.debug('mapping author "%s" to "%s"\n' % (author, result))
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
85 return result
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
86
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
87 def reverselookup(self, author):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
88 for svnauthor, hgauthor in self.iteritems():
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
89 if author == hgauthor:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
90 return svnauthor
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
91 else:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
92 # Mercurial incorrectly splits at e.g. '.', so we roll our own.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
93 return author.rsplit('@', 1)[0]