Show More
@@ -1,192 +1,192 b'' | |||
|
1 | 1 | # changelog.py - changelog class for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from revlog import * |
|
9 | 9 | from i18n import _ |
|
10 | 10 | import os, time, util |
|
11 | 11 | |
|
12 | 12 | def _string_escape(text): |
|
13 | 13 | """ |
|
14 | 14 | >>> d = {'nl': chr(10), 'bs': chr(92), 'cr': chr(13), 'nul': chr(0)} |
|
15 | 15 | >>> s = "ab%(nl)scd%(bs)s%(bs)sn%(nul)sab%(cr)scd%(bs)s%(nl)s" % d |
|
16 | 16 | >>> s |
|
17 | 17 | 'ab\\ncd\\\\\\\\n\\x00ab\\rcd\\\\\\n' |
|
18 | 18 | >>> res = _string_escape(s) |
|
19 | 19 | >>> s == res.decode('string_escape') |
|
20 | 20 | True |
|
21 | 21 | """ |
|
22 | 22 | # subset of the string_escape codec |
|
23 | 23 | text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r') |
|
24 | 24 | return text.replace('\0', '\\0') |
|
25 | 25 | |
|
26 | 26 | class appender: |
|
27 | 27 | '''the changelog index must be update last on disk, so we use this class |
|
28 | 28 | to delay writes to it''' |
|
29 | 29 | def __init__(self, fp, buf): |
|
30 | 30 | self.data = buf |
|
31 | 31 | self.fp = fp |
|
32 | 32 | self.offset = fp.tell() |
|
33 | 33 | self.size = util.fstat(fp).st_size |
|
34 | 34 | |
|
35 | 35 | def end(self): |
|
36 | 36 | return self.size + len("".join(self.data)) |
|
37 | 37 | def tell(self): |
|
38 | 38 | return self.offset |
|
39 | 39 | def flush(self): |
|
40 | 40 | pass |
|
41 | 41 | def close(self): |
|
42 | 42 | self.fp.close() |
|
43 | 43 | |
|
44 | 44 | def seek(self, offset, whence=0): |
|
45 | 45 | '''virtual file offset spans real file and data''' |
|
46 | 46 | if whence == 0: |
|
47 | 47 | self.offset = offset |
|
48 | 48 | elif whence == 1: |
|
49 | 49 | self.offset += offset |
|
50 | 50 | elif whence == 2: |
|
51 | 51 | self.offset = self.end() + offset |
|
52 | 52 | if self.offset < self.size: |
|
53 | 53 | self.fp.seek(self.offset) |
|
54 | 54 | |
|
55 | 55 | def read(self, count=-1): |
|
56 | 56 | '''only trick here is reads that span real file and data''' |
|
57 | 57 | ret = "" |
|
58 | 58 | if self.offset < self.size: |
|
59 | 59 | s = self.fp.read(count) |
|
60 | 60 | ret = s |
|
61 | 61 | self.offset += len(s) |
|
62 | 62 | if count > 0: |
|
63 | 63 | count -= len(s) |
|
64 | 64 | if count != 0: |
|
65 | 65 | doff = self.offset - self.size |
|
66 | 66 | self.data.insert(0, "".join(self.data)) |
|
67 | 67 | del self.data[1:] |
|
68 | 68 | s = self.data[0][doff:doff+count] |
|
69 | 69 | self.offset += len(s) |
|
70 | 70 | ret += s |
|
71 | 71 | return ret |
|
72 | 72 | |
|
73 | 73 | def write(self, s): |
|
74 | 74 | self.data.append(str(s)) |
|
75 | 75 | self.offset += len(s) |
|
76 | 76 | |
|
77 | 77 | class changelog(revlog): |
|
78 | 78 | def __init__(self, opener): |
|
79 | 79 | revlog.__init__(self, opener, "00changelog.i") |
|
80 | 80 | |
|
81 | 81 | def delayupdate(self): |
|
82 | 82 | "delay visibility of index updates to other readers" |
|
83 | 83 | self._realopener = self.opener |
|
84 | 84 | self.opener = self._delayopener |
|
85 | 85 | self._delaycount = self.count() |
|
86 | 86 | self._delaybuf = [] |
|
87 | 87 | self._delayname = None |
|
88 | 88 | |
|
89 | 89 | def finalize(self, tr): |
|
90 | 90 | "finalize index updates" |
|
91 | 91 | self.opener = self._realopener |
|
92 | 92 | # move redirected index data back into place |
|
93 | 93 | if self._delayname: |
|
94 | 94 | util.rename(self._delayname + ".a", self._delayname) |
|
95 | 95 | elif self._delaybuf: |
|
96 | 96 | fp = self.opener(self.indexfile, 'a') |
|
97 | 97 | fp.write("".join(self._delaybuf)) |
|
98 | 98 | fp.close() |
|
99 | 99 | del self._delaybuf |
|
100 | 100 | # split when we're done |
|
101 | 101 | self.checkinlinesize(tr) |
|
102 | 102 | |
|
103 | 103 | def _delayopener(self, name, mode='r'): |
|
104 | 104 | fp = self._realopener(name, mode) |
|
105 | 105 | # only divert the index |
|
106 | 106 | if not name == self.indexfile: |
|
107 | 107 | return fp |
|
108 | 108 | # if we're doing an initial clone, divert to another file |
|
109 | 109 | if self._delaycount == 0: |
|
110 | 110 | self._delayname = fp.name |
|
111 | 111 | return self._realopener(name + ".a", mode) |
|
112 | 112 | # otherwise, divert to memory |
|
113 | 113 | return appender(fp, self._delaybuf) |
|
114 | 114 | |
|
115 | 115 | def checkinlinesize(self, tr, fp=None): |
|
116 | 116 | if self.opener == self._delayopener: |
|
117 | 117 | return |
|
118 | 118 | return revlog.checkinlinesize(self, tr, fp) |
|
119 | 119 | |
|
120 | 120 | def decode_extra(self, text): |
|
121 | 121 | extra = {} |
|
122 | 122 | for l in text.split('\0'): |
|
123 | 123 | if l: |
|
124 |
k, v = |
|
|
124 | k, v = l.decode('string_escape').split(':', 1) | |
|
125 | 125 | extra[k] = v |
|
126 | 126 | return extra |
|
127 | 127 | |
|
128 | 128 | def encode_extra(self, d): |
|
129 | 129 | # keys must be sorted to produce a deterministic changelog entry |
|
130 | 130 | keys = d.keys() |
|
131 | 131 | keys.sort() |
|
132 | 132 | items = [_string_escape('%s:%s' % (k, d[k])) for k in keys] |
|
133 | 133 | return "\0".join(items) |
|
134 | 134 | |
|
135 | 135 | def read(self, node): |
|
136 | 136 | """ |
|
137 | 137 | format used: |
|
138 | 138 | nodeid\n : manifest node in ascii |
|
139 | 139 | user\n : user, no \n or \r allowed |
|
140 | 140 | time tz extra\n : date (time is int or float, timezone is int) |
|
141 | 141 | : extra is metadatas, encoded and separated by '\0' |
|
142 | 142 | : older versions ignore it |
|
143 | 143 | files\n\n : files modified by the cset, no \n or \r allowed |
|
144 | 144 | (.*) : comment (free text, ideally utf-8) |
|
145 | 145 | |
|
146 | 146 | changelog v0 doesn't use extra |
|
147 | 147 | """ |
|
148 | 148 | text = self.revision(node) |
|
149 | 149 | if not text: |
|
150 | 150 | return (nullid, "", (0, 0), [], "", {'branch': 'default'}) |
|
151 | 151 | last = text.index("\n\n") |
|
152 | 152 | desc = util.tolocal(text[last + 2:]) |
|
153 | 153 | l = text[:last].split('\n') |
|
154 | 154 | manifest = bin(l[0]) |
|
155 | 155 | user = util.tolocal(l[1]) |
|
156 | 156 | |
|
157 | 157 | extra_data = l[2].split(' ', 2) |
|
158 | 158 | if len(extra_data) != 3: |
|
159 | 159 | time = float(extra_data.pop(0)) |
|
160 | 160 | try: |
|
161 | 161 | # various tools did silly things with the time zone field. |
|
162 | 162 | timezone = int(extra_data[0]) |
|
163 | 163 | except: |
|
164 | 164 | timezone = 0 |
|
165 | 165 | extra = {} |
|
166 | 166 | else: |
|
167 | 167 | time, timezone, extra = extra_data |
|
168 | 168 | time, timezone = float(time), int(timezone) |
|
169 | 169 | extra = self.decode_extra(extra) |
|
170 | 170 | if not extra.get('branch'): |
|
171 | 171 | extra['branch'] = 'default' |
|
172 | 172 | files = l[3:] |
|
173 | 173 | return (manifest, user, (time, timezone), files, desc, extra) |
|
174 | 174 | |
|
175 | 175 | def add(self, manifest, list, desc, transaction, p1=None, p2=None, |
|
176 | 176 | user=None, date=None, extra={}): |
|
177 | 177 | |
|
178 | 178 | user, desc = util.fromlocal(user), util.fromlocal(desc) |
|
179 | 179 | |
|
180 | 180 | if date: |
|
181 | 181 | parseddate = "%d %d" % util.parsedate(date) |
|
182 | 182 | else: |
|
183 | 183 | parseddate = "%d %d" % util.makedate() |
|
184 | 184 | if extra and extra.get("branch") in ("default", ""): |
|
185 | 185 | del extra["branch"] |
|
186 | 186 | if extra: |
|
187 | 187 | extra = self.encode_extra(extra) |
|
188 | 188 | parseddate = "%s %s" % (parseddate, extra) |
|
189 | 189 | list.sort() |
|
190 | 190 | l = [hex(manifest), user, parseddate] + list + ["", desc] |
|
191 | 191 | text = "\n".join(l) |
|
192 | 192 | return self.addrevision(text, transaction, self.count(), p1, p2) |
General Comments 0
You need to be logged in to leave comments.
Login now