author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Thu, 18 May 2006 22:46:27 -0700 | |
changeset 2317 | 6d0a9de9a8ac |
parent 2142 | 8a1e2a9c7013 |
child 2320 | dbdce3b99988 |
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): |
2142
8a1e2a9c7013
Replaced 0 with REVLOGV0 where this meaning is used.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2072
diff
changeset
|
15 |
def __init__(self, opener, defversion=REVLOGV0): |
0
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 |
2072 | 18 |
revlog.__init__(self, opener, "00manifest.i", "00manifest.d", |
19 |
defversion) |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
20 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
21 |
def read(self, node): |
313 | 22 |
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
|
23 |
if self.mapcache and self.mapcache[0] == node: |
561 | 24 |
return self.mapcache[1] |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
25 |
text = self.revision(node) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
26 |
map = {} |
276 | 27 |
flag = {} |
1534 | 28 |
self.listcache = array.array('c', text) |
29 |
lines = text.splitlines(1) |
|
30 |
for l in lines: |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
31 |
(f, n) = l.split('\0') |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
32 |
map[f] = bin(n[:40]) |
276 | 33 |
flag[f] = (n[40:-1] == "x") |
34 |
self.mapcache = (node, map, flag) |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
35 |
return map |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
36 |
|
276 | 37 |
def readflags(self, node): |
313 | 38 |
if node == nullid: return {} # don't upset local cache |
358
9f4077d7ef6f
[PATCH] manifest.readflags performance buglet
mpm@selenic.com
parents:
350
diff
changeset
|
39 |
if not self.mapcache or self.mapcache[0] != node: |
276 | 40 |
self.read(node) |
41 |
return self.mapcache[2] |
|
42 |
||
1534 | 43 |
def diff(self, a, b): |
44 |
return mdiff.textdiff(str(a), str(b)) |
|
45 |
||
741 | 46 |
def add(self, map, flags, transaction, link, p1=None, p2=None, |
47 |
changed=None): |
|
1534 | 48 |
|
49 |
# returns a tuple (start, end). If the string is found |
|
50 |
# m[start:end] are the line containing that string. If start == end |
|
51 |
# the string was not found and they indicate the proper sorted |
|
52 |
# insertion point. This was taken from bisect_left, and modified |
|
53 |
# to find line start/end as it goes along. |
|
54 |
# |
|
55 |
# m should be a buffer or a string |
|
56 |
# s is a string |
|
57 |
# |
|
58 |
def manifestsearch(m, s, lo=0, hi=None): |
|
59 |
def advance(i, c): |
|
60 |
while i < lenm and m[i] != c: |
|
644 | 61 |
i += 1 |
1534 | 62 |
return i |
63 |
lenm = len(m) |
|
64 |
if not hi: |
|
65 |
hi = lenm |
|
66 |
while lo < hi: |
|
67 |
mid = (lo + hi) // 2 |
|
68 |
start = mid |
|
69 |
while start > 0 and m[start-1] != '\n': |
|
70 |
start -= 1 |
|
71 |
end = advance(start, '\0') |
|
72 |
if m[start:end] < s: |
|
73 |
# we know that after the null there are 40 bytes of sha1 |
|
74 |
# this translates to the bisect lo = mid + 1 |
|
75 |
lo = advance(end + 40, '\n') + 1 |
|
76 |
else: |
|
77 |
# this translates to the bisect hi = mid |
|
78 |
hi = start |
|
79 |
end = advance(lo, '\0') |
|
80 |
found = m[lo:end] |
|
81 |
if cmp(s, found) == 0: |
|
82 |
# we know that after the null there are 40 bytes of sha1 |
|
83 |
end = advance(end + 40, '\n') |
|
84 |
return (lo, end+1) |
|
85 |
else: |
|
86 |
return (lo, lo) |
|
644 | 87 |
|
88 |
# apply the changes collected during the bisect loop to our addlist |
|
1534 | 89 |
# return a delta suitable for addrevision |
90 |
def addlistdelta(addlist, x): |
|
91 |
# start from the bottom up |
|
644 | 92 |
# so changes to the offsets don't mess things up. |
1534 | 93 |
i = len(x) |
644 | 94 |
while i > 0: |
95 |
i -= 1 |
|
1534 | 96 |
start = x[i][0] |
97 |
end = x[i][1] |
|
98 |
if x[i][2]: |
|
99 |
addlist[start:end] = array.array('c', x[i][2]) |
|
644 | 100 |
else: |
101 |
del addlist[start:end] |
|
1534 | 102 |
return "".join([struct.pack(">lll", d[0], d[1], len(d[2])) + d[2] \ |
103 |
for d in x ]) |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
104 |
|
644 | 105 |
# if we're using the listcache, make sure it is valid and |
106 |
# parented by the same node we're diffing against |
|
741 | 107 |
if not changed or not self.listcache or not p1 or \ |
108 |
self.mapcache[0] != p1: |
|
644 | 109 |
files = map.keys() |
110 |
files.sort() |
|
111 |
||
1651 | 112 |
# if this is changed to support newlines in filenames, |
113 |
# be sure to check the templates/ dir again (especially *-raw.tmpl) |
|
1534 | 114 |
text = ["%s\000%s%s\n" % |
644 | 115 |
(f, hex(map[f]), flags[f] and "x" or '') |
116 |
for f in files] |
|
1534 | 117 |
self.listcache = array.array('c', "".join(text)) |
644 | 118 |
cachedelta = None |
119 |
else: |
|
1534 | 120 |
addlist = self.listcache |
644 | 121 |
|
122 |
# combine the changed lists into one list for sorting |
|
123 |
work = [[x, 0] for x in changed[0]] |
|
124 |
work[len(work):] = [[x, 1] for x in changed[1]] |
|
125 |
work.sort() |
|
126 |
||
127 |
delta = [] |
|
1534 | 128 |
dstart = None |
129 |
dend = None |
|
130 |
dline = [""] |
|
131 |
start = 0 |
|
132 |
# zero copy representation of addlist as a buffer |
|
133 |
addbuf = buffer(addlist) |
|
644 | 134 |
|
1534 | 135 |
# start with a readonly loop that finds the offset of |
136 |
# each line and creates the deltas |
|
644 | 137 |
for w in work: |
138 |
f = w[0] |
|
741 | 139 |
# bs will either be the index of the item or the insert point |
1534 | 140 |
start, end = manifestsearch(addbuf, f, start) |
644 | 141 |
if w[1] == 0: |
741 | 142 |
l = "%s\000%s%s\n" % (f, hex(map[f]), |
143 |
flags[f] and "x" or '') |
|
644 | 144 |
else: |
1534 | 145 |
l = "" |
146 |
if start == end and w[1] == 1: |
|
147 |
# item we want to delete was not found, error out |
|
148 |
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
|
149 |
_("failed to remove %s from manifest\n") % f) |
1534 | 150 |
if dstart != None and dstart <= start and dend >= start: |
151 |
if dend < end: |
|
152 |
dend = end |
|
153 |
if l: |
|
154 |
dline.append(l) |
|
644 | 155 |
else: |
1534 | 156 |
if dstart != None: |
157 |
delta.append([dstart, dend, "".join(dline)]) |
|
158 |
dstart = start |
|
159 |
dend = end |
|
160 |
dline = [l] |
|
644 | 161 |
|
1534 | 162 |
if dstart != None: |
163 |
delta.append([dstart, dend, "".join(dline)]) |
|
164 |
# apply the delta to the addlist, and get a delta for addrevision |
|
165 |
cachedelta = addlistdelta(addlist, delta) |
|
644 | 166 |
|
1534 | 167 |
# the delta is only valid if we've been processing the tip revision |
168 |
if self.mapcache[0] != self.tip(): |
|
169 |
cachedelta = None |
|
170 |
self.listcache = addlist |
|
171 |
||
172 |
n = self.addrevision(buffer(self.listcache), transaction, link, p1, \ |
|
173 |
p2, cachedelta) |
|
302 | 174 |
self.mapcache = (n, map, flags) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
175 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
176 |
return n |