##// END OF EJS Templates
convert.monotone: fix quotes and backslashes in change descriptions.
David Reiss -
r6632:aa3f6188 default
parent child Browse files
Show More
@@ -1,198 +1,201 b''
1 1 # monotone support for the convert extension
2 2
3 3 import os, re, time
4 4 from mercurial import util
5 5 from common import NoRepo, MissingTool, commit, converter_source, checktool
6 6 from common import commandline
7 7 from mercurial.i18n import _
8 8
9 9 class monotone_source(converter_source, commandline):
10 10 def __init__(self, ui, path=None, rev=None):
11 11 converter_source.__init__(self, ui, path, rev)
12 12 commandline.__init__(self, ui, 'mtn')
13 13
14 14 self.ui = ui
15 15 self.path = path
16 16
17 17 # regular expressions for parsing monotone output
18 18 space = r'\s*'
19 name = r'\s+"((?:[^"]|\\")*)"\s*'
19 name = r'\s+"((?:\\"|[^"])*)"\s*'
20 20 value = name
21 21 revision = r'\s+\[(\w+)\]\s*'
22 22 lines = r'(?:.|\n)+'
23 23
24 24 self.dir_re = re.compile(space + "dir" + name)
25 25 self.file_re = re.compile(space + "file" + name + "content" + revision)
26 26 self.add_file_re = re.compile(space + "add_file" + name + "content" + revision)
27 27 self.patch_re = re.compile(space + "patch" + name + "from" + revision + "to" + revision)
28 28 self.rename_re = re.compile(space + "rename" + name + "to" + name)
29 29 self.delete_re = re.compile(space + "delete" + name)
30 30 self.tag_re = re.compile(space + "tag" + name + "revision" + revision)
31 31 self.cert_re = re.compile(lines + space + "name" + name + "value" + value)
32 32
33 33 attr = space + "file" + lines + space + "attr" + space
34 34 self.attr_execute_re = re.compile(attr + '"mtn:execute"' + space + '"true"')
35 35
36 36 # cached data
37 37 self.manifest_rev = None
38 38 self.manifest = None
39 39 self.files = None
40 40 self.dirs = None
41 41
42 42 norepo = NoRepo (_("%s does not look like a monotone repo") % path)
43 43 if not os.path.exists(path):
44 44 raise norepo
45 45
46 46 checktool('mtn', abort=False)
47 47
48 48 # test if there are any revisions
49 49 self.rev = None
50 50 try:
51 51 self.getheads()
52 52 except:
53 53 raise norepo
54 54 self.rev = rev
55 55
56 56 def mtnrun(self, *args, **kwargs):
57 57 kwargs['d'] = self.path
58 58 return self.run0('automate', *args, **kwargs)
59 59
60 60 def mtnloadmanifest(self, rev):
61 61 if self.manifest_rev == rev:
62 62 return
63 63 self.manifest = self.mtnrun("get_manifest_of", rev).split("\n\n")
64 64 self.manifest_rev = rev
65 65 self.files = {}
66 66 self.dirs = {}
67 67
68 68 for e in self.manifest:
69 69 m = self.file_re.match(e)
70 70 if m:
71 71 attr = ""
72 72 name = m.group(1)
73 73 node = m.group(2)
74 74 if self.attr_execute_re.match(e):
75 75 attr += "x"
76 76 self.files[name] = (node, attr)
77 77 m = self.dir_re.match(e)
78 78 if m:
79 79 self.dirs[m.group(1)] = True
80 80
81 81 def mtnisfile(self, name, rev):
82 82 # a non-file could be a directory or a deleted or renamed file
83 83 self.mtnloadmanifest(rev)
84 84 try:
85 85 self.files[name]
86 86 return True
87 87 except KeyError:
88 88 return False
89 89
90 90 def mtnisdir(self, name, rev):
91 91 self.mtnloadmanifest(rev)
92 92 try:
93 93 self.dirs[name]
94 94 return True
95 95 except KeyError:
96 96 return False
97 97
98 98 def mtngetcerts(self, rev):
99 99 certs = {"author":"<missing>", "date":"<missing>",
100 100 "changelog":"<missing>", "branch":"<missing>"}
101 cert_list = self.mtnrun("certs", rev).split("\n\n")
101 cert_list = self.mtnrun("certs", rev).split('\n\n key "')
102 102 for e in cert_list:
103 103 m = self.cert_re.match(e)
104 104 if m:
105 certs[m.group(1)] = m.group(2)
105 name, value = m.groups()
106 value = value.replace(r'\"', '"')
107 value = value.replace(r'\\', '\\')
108 certs[name] = value
106 109 return certs
107 110
108 111 def mtnrenamefiles(self, files, fromdir, todir):
109 112 renamed = {}
110 113 for tofile in files:
111 114 suffix = tofile.lstrip(todir)
112 115 if todir + suffix == tofile:
113 116 renamed[tofile] = (fromdir + suffix).lstrip("/")
114 117 return renamed
115 118
116 119
117 120 # implement the converter_source interface:
118 121
119 122 def getheads(self):
120 123 if not self.rev:
121 124 return self.mtnrun("leaves").splitlines()
122 125 else:
123 126 return [self.rev]
124 127
125 128 def getchanges(self, rev):
126 129 #revision = self.mtncmd("get_revision %s" % rev).split("\n\n")
127 130 revision = self.mtnrun("get_revision", rev).split("\n\n")
128 131 files = {}
129 132 copies = {}
130 133 for e in revision:
131 134 m = self.add_file_re.match(e)
132 135 if m:
133 136 files[m.group(1)] = rev
134 137 m = self.patch_re.match(e)
135 138 if m:
136 139 files[m.group(1)] = rev
137 140
138 141 # Delete/rename is handled later when the convert engine
139 142 # discovers an IOError exception from getfile,
140 143 # but only if we add the "from" file to the list of changes.
141 144 m = self.delete_re.match(e)
142 145 if m:
143 146 files[m.group(1)] = rev
144 147 m = self.rename_re.match(e)
145 148 if m:
146 149 toname = m.group(2)
147 150 fromname = m.group(1)
148 151 if self.mtnisfile(toname, rev):
149 152 copies[toname] = fromname
150 153 files[toname] = rev
151 154 files[fromname] = rev
152 155 if self.mtnisdir(toname, rev):
153 156 renamed = self.mtnrenamefiles(self.files, fromname, toname)
154 157 for tofile, fromfile in renamed.items():
155 158 self.ui.debug (_("copying file in renamed dir from '%s' to '%s'") % (fromfile, tofile), '\n')
156 159 files[tofile] = rev
157 160 for fromfile in renamed.values():
158 161 files[fromfile] = rev
159 162 return (files.items(), copies)
160 163
161 164 def getmode(self, name, rev):
162 165 self.mtnloadmanifest(rev)
163 166 try:
164 167 node, attr = self.files[name]
165 168 return attr
166 169 except KeyError:
167 170 return ""
168 171
169 172 def getfile(self, name, rev):
170 173 if not self.mtnisfile(name, rev):
171 174 raise IOError() # file was deleted or renamed
172 175 try:
173 176 return self.mtnrun("get_file_of", name, r=rev)
174 177 except:
175 178 raise IOError() # file was deleted or renamed
176 179
177 180 def getcommit(self, rev):
178 181 certs = self.mtngetcerts(rev)
179 182 return commit(
180 183 author=certs["author"],
181 184 date=util.datestr(util.strdate(certs["date"], "%Y-%m-%dT%H:%M:%S")),
182 185 desc=certs["changelog"],
183 186 rev=rev,
184 187 parents=self.mtnrun("parents", rev).splitlines(),
185 188 branch=certs["branch"])
186 189
187 190 def gettags(self):
188 191 tags = {}
189 192 for e in self.mtnrun("tags").split("\n\n"):
190 193 m = self.tag_re.match(e)
191 194 if m:
192 195 tags[m.group(1)] = m.group(2)
193 196 return tags
194 197
195 198 def getchangedfiles(self, rev, i):
196 199 # This function is only needed to support --filemap
197 200 # ... and we don't support that
198 201 raise NotImplementedError()
General Comments 0
You need to be logged in to leave comments. Login now