author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
Mon, 13 Mar 2006 03:56:36 +0100 | |
changeset 1945 | dec6d3c13dbf |
parent 1651 | cf40d2a30fef |
child 1680 | c21b54f7f7b8 |
child 2072 | 74d3f5336b66 |
permissions | -rw-r--r-- |
1089 | 1 |
# manifest.py - manifest revision class for mercurial |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
2 |
# |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
3 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
4 |
# |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
7 |
|
1541
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1534
diff
changeset
|
8 |
import struct |
262 | 9 |
from revlog import * |
1400
cf9a1233738a
i18n first part: make '_' available for files who need it
Benoit Boissinot <benoit.boissinot@ens-lyon.org
parents:
1098
diff
changeset
|
10 |
from i18n import gettext as _ |
262 | 11 |
from demandload import * |
1534 | 12 |
demandload(globals(), "bisect array") |
79 | 13 |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
14 |
class manifest(revlog): |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
15 |
def __init__(self, opener): |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
16 |
self.mapcache = None |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
17 |
self.listcache = None |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
18 |
revlog.__init__(self, opener, "00manifest.i", "00manifest.d") |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
19 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
20 |
def read(self, node): |
313 | 21 |
if node == nullid: return {} # don't upset local cache |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
22 |
if self.mapcache and self.mapcache[0] == node: |
561 | 23 |
return self.mapcache[1] |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
24 |
text = self.revision(node) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
25 |
map = {} |
276 | 26 |
flag = {} |
1534 | 27 |
self.listcache = array.array('c', text) |
28 |
lines = text.splitlines(1) |
|
29 |
for l in lines: |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
30 |
(f, n) = l.split('\0') |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
31 |
map[f] = bin(n[:40]) |
276 | 32 |
flag[f] = (n[40:-1] == "x") |
33 |
self.mapcache = (node, map, flag) |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
34 |
return map |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
35 |
|
276 | 36 |
def readflags(self, node): |
313 | 37 |
if node == nullid: return {} # don't upset local cache |
358
9f4077d7ef6f
[PATCH] manifest.readflags performance buglet
mpm@selenic.com
parents:
350
diff
changeset
|
38 |
if not self.mapcache or self.mapcache[0] != node: |
276 | 39 |
self.read(node) |
40 |
return self.mapcache[2] |
|
41 |
||
1534 | 42 |
def diff(self, a, b): |
43 |
return mdiff.textdiff(str(a), str(b)) |
|
44 |
||
741 | 45 |
def add(self, map, flags, transaction, link, p1=None, p2=None, |
46 |
changed=None): |
|
1534 | 47 |
|
48 |
# returns a tuple (start, end). If the string is found |
|
49 |
# m[start:end] are the line containing that string. If start == end |
|
50 |
# the string was not found and they indicate the proper sorted |
|
51 |
# insertion point. This was taken from bisect_left, and modified |
|
52 |
# to find line start/end as it goes along. |
|
53 |
# |
|
54 |
# m should be a buffer or a string |
|
55 |
# s is a string |
|
56 |
# |
|
57 |
def manifestsearch(m, s, lo=0, hi=None): |
|
58 |
def advance(i, c): |
|
59 |
while i < lenm and m[i] != c: |
|
644 | 60 |
i += 1 |
1534 | 61 |
return i |
62 |
lenm = len(m) |
|
63 |
if not hi: |
|
64 |
hi = lenm |
|
65 |
while lo < hi: |
|
66 |
mid = (lo + hi) // 2 |
|
67 |
start = mid |
|
68 |
while start > 0 and m[start-1] != '\n': |
|
69 |
start -= 1 |
|
70 |
end = advance(start, '\0') |
|
71 |
if m[start:end] < s: |
|
72 |
# we know that after the null there are 40 bytes of sha1 |
|
73 |
# this translates to the bisect lo = mid + 1 |
|
74 |
lo = advance(end + 40, '\n') + 1 |
|
75 |
else: |
|
76 |
# this translates to the bisect hi = mid |
|
77 |
hi = start |
|
78 |
end = advance(lo, '\0') |
|
79 |
found = m[lo:end] |
|
80 |
if cmp(s, found) == 0: |
|
81 |
# we know that after the null there are 40 bytes of sha1 |
|
82 |
end = advance(end + 40, '\n') |
|
83 |
return (lo, end+1) |
|
84 |
else: |
|
85 |
return (lo, lo) |
|
644 | 86 |
|
87 |
# apply the changes collected during the bisect loop to our addlist |
|
1534 | 88 |
# return a delta suitable for addrevision |
89 |
def addlistdelta(addlist, x): |
|
90 |
# start from the bottom up |
|
644 | 91 |
# so changes to the offsets don't mess things up. |
1534 | 92 |
i = len(x) |
644 | 93 |
while i > 0: |
94 |
i -= 1 |
|
1534 | 95 |
start = x[i][0] |
96 |
end = x[i][1] |
|
97 |
if x[i][2]: |
|
98 |
addlist[start:end] = array.array('c', x[i][2]) |
|
644 | 99 |
else: |
100 |
del addlist[start:end] |
|
1534 | 101 |
return "".join([struct.pack(">lll", d[0], d[1], len(d[2])) + d[2] \ |
102 |
for d in x ]) |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
103 |
|
644 | 104 |
# if we're using the listcache, make sure it is valid and |
105 |
# parented by the same node we're diffing against |
|
741 | 106 |
if not changed or not self.listcache or not p1 or \ |
107 |
self.mapcache[0] != p1: |
|
644 | 108 |
files = map.keys() |
109 |
files.sort() |
|
110 |
||
1651 | 111 |
# if this is changed to support newlines in filenames, |
112 |
# be sure to check the templates/ dir again (especially *-raw.tmpl) |
|
1534 | 113 |
text = ["%s\000%s%s\n" % |
644 | 114 |
(f, hex(map[f]), flags[f] and "x" or '') |
115 |
for f in files] |
|
1534 | 116 |
self.listcache = array.array('c', "".join(text)) |
644 | 117 |
cachedelta = None |
118 |
else: |
|
1534 | 119 |
addlist = self.listcache |
644 | 120 |
|
121 |
# combine the changed lists into one list for sorting |
|
122 |
work = [[x, 0] for x in changed[0]] |
|
123 |
work[len(work):] = [[x, 1] for x in changed[1]] |
|
124 |
work.sort() |
|
125 |
||
126 |
delta = [] |
|
1534 | 127 |
dstart = None |
128 |
dend = None |
|
129 |
dline = [""] |
|
130 |
start = 0 |
|
131 |
# zero copy representation of addlist as a buffer |
|
132 |
addbuf = buffer(addlist) |
|
644 | 133 |
|
1534 | 134 |
# start with a readonly loop that finds the offset of |
135 |
# each line and creates the deltas |
|
644 | 136 |
for w in work: |
137 |
f = w[0] |
|
741 | 138 |
# bs will either be the index of the item or the insert point |
1534 | 139 |
start, end = manifestsearch(addbuf, f, start) |
644 | 140 |
if w[1] == 0: |
741 | 141 |
l = "%s\000%s%s\n" % (f, hex(map[f]), |
142 |
flags[f] and "x" or '') |
|
644 | 143 |
else: |
1534 | 144 |
l = "" |
145 |
if start == end and w[1] == 1: |
|
146 |
# item we want to delete was not found, error out |
|
147 |
raise AssertionError( |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
148 |
_("failed to remove %s from manifest\n") % f) |
1534 | 149 |
if dstart != None and dstart <= start and dend >= start: |
150 |
if dend < end: |
|
151 |
dend = end |
|
152 |
if l: |
|
153 |
dline.append(l) |
|
644 | 154 |
else: |
1534 | 155 |
if dstart != None: |
156 |
delta.append([dstart, dend, "".join(dline)]) |
|
157 |
dstart = start |
|
158 |
dend = end |
|
159 |
dline = [l] |
|
644 | 160 |
|
1534 | 161 |
if dstart != None: |
162 |
delta.append([dstart, dend, "".join(dline)]) |
|
163 |
# apply the delta to the addlist, and get a delta for addrevision |
|
164 |
cachedelta = addlistdelta(addlist, delta) |
|
644 | 165 |
|
1534 | 166 |
# the delta is only valid if we've been processing the tip revision |
167 |
if self.mapcache[0] != self.tip(): |
|
168 |
cachedelta = None |
|
169 |
self.listcache = addlist |
|
170 |
||
171 |
n = self.addrevision(buffer(self.listcache), transaction, link, p1, \ |
|
172 |
p2, cachedelta) |
|
302 | 173 |
self.mapcache = (n, map, flags) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
174 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
175 |
return n |