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