##// END OF EJS Templates
bundle: get rid of chunkiter
Matt Mackall -
r12335:e21fe9c5 default
parent child Browse files
Show More
@@ -1,288 +1,288 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 """\
3 """\
4 reorder a revlog (the manifest by default) to save space
4 reorder a revlog (the manifest by default) to save space
5
5
6 Specifically, this topologically sorts the revisions in the revlog so that
6 Specifically, this topologically sorts the revisions in the revlog so that
7 revisions on the same branch are adjacent as much as possible. This is a
7 revisions on the same branch are adjacent as much as possible. This is a
8 workaround for the fact that Mercurial computes deltas relative to the
8 workaround for the fact that Mercurial computes deltas relative to the
9 previous revision rather than relative to a parent revision.
9 previous revision rather than relative to a parent revision.
10
10
11 This is *not* safe to run on a changelog.
11 This is *not* safe to run on a changelog.
12 """
12 """
13
13
14 # Originally written by Benoit Boissinot <benoit.boissinot at ens-lyon.org>
14 # Originally written by Benoit Boissinot <benoit.boissinot at ens-lyon.org>
15 # as a patch to rewrite-log. Cleaned up, refactored, documented, and
15 # as a patch to rewrite-log. Cleaned up, refactored, documented, and
16 # renamed by Greg Ward <greg at gerg.ca>.
16 # renamed by Greg Ward <greg at gerg.ca>.
17
17
18 # XXX would be nice to have a way to verify the repository after shrinking,
18 # XXX would be nice to have a way to verify the repository after shrinking,
19 # e.g. by comparing "before" and "after" states of random changesets
19 # e.g. by comparing "before" and "after" states of random changesets
20 # (maybe: export before, shrink, export after, diff).
20 # (maybe: export before, shrink, export after, diff).
21
21
22 import os, tempfile, errno
22 import os, tempfile, errno
23 from mercurial import revlog, transaction, node, util
23 from mercurial import revlog, transaction, node, util
24 from mercurial import changegroup
24 from mercurial import changegroup
25 from mercurial.i18n import _
25 from mercurial.i18n import _
26
26
27
27
28 def postorder(start, edges):
28 def postorder(start, edges):
29 result = []
29 result = []
30 visit = list(start)
30 visit = list(start)
31 finished = set()
31 finished = set()
32
32
33 while visit:
33 while visit:
34 cur = visit[-1]
34 cur = visit[-1]
35 for p in edges[cur]:
35 for p in edges[cur]:
36 if p not in finished:
36 if p not in finished:
37 visit.append(p)
37 visit.append(p)
38 break
38 break
39 else:
39 else:
40 result.append(cur)
40 result.append(cur)
41 finished.add(cur)
41 finished.add(cur)
42 visit.pop()
42 visit.pop()
43
43
44 return result
44 return result
45
45
46 def toposort_reversepostorder(ui, rl):
46 def toposort_reversepostorder(ui, rl):
47 # postorder of the reverse directed graph
47 # postorder of the reverse directed graph
48
48
49 # map rev to list of parent revs (p2 first)
49 # map rev to list of parent revs (p2 first)
50 parents = {}
50 parents = {}
51 heads = set()
51 heads = set()
52 ui.status(_('reading revs\n'))
52 ui.status(_('reading revs\n'))
53 try:
53 try:
54 for rev in rl:
54 for rev in rl:
55 ui.progress(_('reading'), rev, total=len(rl))
55 ui.progress(_('reading'), rev, total=len(rl))
56 (p1, p2) = rl.parentrevs(rev)
56 (p1, p2) = rl.parentrevs(rev)
57 if p1 == p2 == node.nullrev:
57 if p1 == p2 == node.nullrev:
58 parents[rev] = () # root node
58 parents[rev] = () # root node
59 elif p1 == p2 or p2 == node.nullrev:
59 elif p1 == p2 or p2 == node.nullrev:
60 parents[rev] = (p1,) # normal node
60 parents[rev] = (p1,) # normal node
61 else:
61 else:
62 parents[rev] = (p2, p1) # merge node
62 parents[rev] = (p2, p1) # merge node
63 heads.add(rev)
63 heads.add(rev)
64 for p in parents[rev]:
64 for p in parents[rev]:
65 heads.discard(p)
65 heads.discard(p)
66 finally:
66 finally:
67 ui.progress(_('reading'), None)
67 ui.progress(_('reading'), None)
68
68
69 heads = list(heads)
69 heads = list(heads)
70 heads.sort(reverse=True)
70 heads.sort(reverse=True)
71
71
72 ui.status(_('sorting revs\n'))
72 ui.status(_('sorting revs\n'))
73 return postorder(heads, parents)
73 return postorder(heads, parents)
74
74
75 def toposort_postorderreverse(ui, rl):
75 def toposort_postorderreverse(ui, rl):
76 # reverse-postorder of the reverse directed graph
76 # reverse-postorder of the reverse directed graph
77
77
78 children = {}
78 children = {}
79 roots = set()
79 roots = set()
80 ui.status(_('reading revs\n'))
80 ui.status(_('reading revs\n'))
81 try:
81 try:
82 for rev in rl:
82 for rev in rl:
83 ui.progress(_('reading'), rev, total=len(rl))
83 ui.progress(_('reading'), rev, total=len(rl))
84 (p1, p2) = rl.parentrevs(rev)
84 (p1, p2) = rl.parentrevs(rev)
85 if p1 == p2 == node.nullrev:
85 if p1 == p2 == node.nullrev:
86 roots.add(rev)
86 roots.add(rev)
87 children[rev] = []
87 children[rev] = []
88 if p1 != node.nullrev:
88 if p1 != node.nullrev:
89 children[p1].append(rev)
89 children[p1].append(rev)
90 if p2 != node.nullrev:
90 if p2 != node.nullrev:
91 children[p2].append(rev)
91 children[p2].append(rev)
92 finally:
92 finally:
93 ui.progress(_('reading'), None)
93 ui.progress(_('reading'), None)
94
94
95 roots = list(roots)
95 roots = list(roots)
96 roots.sort()
96 roots.sort()
97
97
98 ui.status(_('sorting revs\n'))
98 ui.status(_('sorting revs\n'))
99 result = postorder(roots, children)
99 result = postorder(roots, children)
100 result.reverse()
100 result.reverse()
101 return result
101 return result
102
102
103 def writerevs(ui, r1, r2, order, tr):
103 def writerevs(ui, r1, r2, order, tr):
104
104
105 ui.status(_('writing revs\n'))
105 ui.status(_('writing revs\n'))
106
106
107 count = [0]
107 count = [0]
108 def progress(*args):
108 def progress(*args):
109 ui.progress(_('writing'), count[0], total=len(order))
109 ui.progress(_('writing'), count[0], total=len(order))
110 count[0] += 1
110 count[0] += 1
111
111
112 order = [r1.node(r) for r in order]
112 order = [r1.node(r) for r in order]
113
113
114 # this is a bit ugly, but it works
114 # this is a bit ugly, but it works
115 lookup = lambda x: "%020d" % r1.linkrev(r1.rev(x))
115 lookup = lambda x: "%020d" % r1.linkrev(r1.rev(x))
116 unlookup = lambda x: int(x, 10)
116 unlookup = lambda x: int(x, 10)
117
117
118 try:
118 try:
119 group = util.chunkbuffer(r1.group(order, lookup, progress))
119 group = util.chunkbuffer(r1.group(order, lookup, progress))
120 r2.addgroup(group.chunks(), unlookup, tr)
120 r2.addgroup(group, unlookup, tr)
121 finally:
121 finally:
122 ui.progress(_('writing'), None)
122 ui.progress(_('writing'), None)
123
123
124 def report(ui, r1, r2):
124 def report(ui, r1, r2):
125 def getsize(r):
125 def getsize(r):
126 s = 0
126 s = 0
127 for fn in (r.indexfile, r.datafile):
127 for fn in (r.indexfile, r.datafile):
128 try:
128 try:
129 s += os.stat(fn).st_size
129 s += os.stat(fn).st_size
130 except OSError, inst:
130 except OSError, inst:
131 if inst.errno != errno.ENOENT:
131 if inst.errno != errno.ENOENT:
132 raise
132 raise
133 return s
133 return s
134
134
135 oldsize = float(getsize(r1))
135 oldsize = float(getsize(r1))
136 newsize = float(getsize(r2))
136 newsize = float(getsize(r2))
137
137
138 # argh: have to pass an int to %d, because a float >= 2^32
138 # argh: have to pass an int to %d, because a float >= 2^32
139 # blows up under Python 2.5 or earlier
139 # blows up under Python 2.5 or earlier
140 ui.write(_('old file size: %12d bytes (%6.1f MiB)\n')
140 ui.write(_('old file size: %12d bytes (%6.1f MiB)\n')
141 % (int(oldsize), oldsize / 1024 / 1024))
141 % (int(oldsize), oldsize / 1024 / 1024))
142 ui.write(_('new file size: %12d bytes (%6.1f MiB)\n')
142 ui.write(_('new file size: %12d bytes (%6.1f MiB)\n')
143 % (int(newsize), newsize / 1024 / 1024))
143 % (int(newsize), newsize / 1024 / 1024))
144
144
145 shrink_percent = (oldsize - newsize) / oldsize * 100
145 shrink_percent = (oldsize - newsize) / oldsize * 100
146 shrink_factor = oldsize / newsize
146 shrink_factor = oldsize / newsize
147 ui.write(_('shrinkage: %.1f%% (%.1fx)\n')
147 ui.write(_('shrinkage: %.1f%% (%.1fx)\n')
148 % (shrink_percent, shrink_factor))
148 % (shrink_percent, shrink_factor))
149
149
150 def shrink(ui, repo, **opts):
150 def shrink(ui, repo, **opts):
151 """shrink a revlog by reordering revisions
151 """shrink a revlog by reordering revisions
152
152
153 Rewrites all the entries in some revlog of the current repository
153 Rewrites all the entries in some revlog of the current repository
154 (by default, the manifest log) to save space.
154 (by default, the manifest log) to save space.
155
155
156 Different sort algorithms have different performance
156 Different sort algorithms have different performance
157 characteristics. Use ``--sort`` to select a sort algorithm so you
157 characteristics. Use ``--sort`` to select a sort algorithm so you
158 can determine which works best for your data.
158 can determine which works best for your data.
159 """
159 """
160
160
161 if not repo.local():
161 if not repo.local():
162 raise util.Abort(_('not a local repository: %s') % repo.root)
162 raise util.Abort(_('not a local repository: %s') % repo.root)
163
163
164 fn = opts.get('revlog')
164 fn = opts.get('revlog')
165 if not fn:
165 if not fn:
166 indexfn = repo.sjoin('00manifest.i')
166 indexfn = repo.sjoin('00manifest.i')
167 else:
167 else:
168 if not fn.endswith('.i'):
168 if not fn.endswith('.i'):
169 raise util.Abort(_('--revlog option must specify the revlog index '
169 raise util.Abort(_('--revlog option must specify the revlog index '
170 'file (*.i), not %s') % opts.get('revlog'))
170 'file (*.i), not %s') % opts.get('revlog'))
171
171
172 indexfn = os.path.realpath(fn)
172 indexfn = os.path.realpath(fn)
173 store = repo.sjoin('')
173 store = repo.sjoin('')
174 if not indexfn.startswith(store):
174 if not indexfn.startswith(store):
175 raise util.Abort(_('--revlog option must specify a revlog in %s, '
175 raise util.Abort(_('--revlog option must specify a revlog in %s, '
176 'not %s') % (store, indexfn))
176 'not %s') % (store, indexfn))
177
177
178 sortname = opts['sort']
178 sortname = opts['sort']
179 try:
179 try:
180 toposort = globals()['toposort_' + sortname]
180 toposort = globals()['toposort_' + sortname]
181 except KeyError:
181 except KeyError:
182 raise util.Abort(_('no such toposort algorithm: %s') % sortname)
182 raise util.Abort(_('no such toposort algorithm: %s') % sortname)
183
183
184 if not os.path.exists(indexfn):
184 if not os.path.exists(indexfn):
185 raise util.Abort(_('no such file: %s') % indexfn)
185 raise util.Abort(_('no such file: %s') % indexfn)
186 if '00changelog' in indexfn:
186 if '00changelog' in indexfn:
187 raise util.Abort(_('shrinking the changelog '
187 raise util.Abort(_('shrinking the changelog '
188 'will corrupt your repository'))
188 'will corrupt your repository'))
189
189
190 ui.write(_('shrinking %s\n') % indexfn)
190 ui.write(_('shrinking %s\n') % indexfn)
191 prefix = os.path.basename(indexfn)[:-1]
191 prefix = os.path.basename(indexfn)[:-1]
192 tmpindexfn = util.mktempcopy(indexfn, emptyok=True)
192 tmpindexfn = util.mktempcopy(indexfn, emptyok=True)
193
193
194 r1 = revlog.revlog(util.opener(os.getcwd(), audit=False), indexfn)
194 r1 = revlog.revlog(util.opener(os.getcwd(), audit=False), indexfn)
195 r2 = revlog.revlog(util.opener(os.getcwd(), audit=False), tmpindexfn)
195 r2 = revlog.revlog(util.opener(os.getcwd(), audit=False), tmpindexfn)
196
196
197 datafn, tmpdatafn = r1.datafile, r2.datafile
197 datafn, tmpdatafn = r1.datafile, r2.datafile
198
198
199 oldindexfn = indexfn + '.old'
199 oldindexfn = indexfn + '.old'
200 olddatafn = datafn + '.old'
200 olddatafn = datafn + '.old'
201 if os.path.exists(oldindexfn) or os.path.exists(olddatafn):
201 if os.path.exists(oldindexfn) or os.path.exists(olddatafn):
202 raise util.Abort(_('one or both of\n'
202 raise util.Abort(_('one or both of\n'
203 ' %s\n'
203 ' %s\n'
204 ' %s\n'
204 ' %s\n'
205 'exists from a previous run; please clean up '
205 'exists from a previous run; please clean up '
206 'before running again') % (oldindexfn, olddatafn))
206 'before running again') % (oldindexfn, olddatafn))
207
207
208 # Don't use repo.transaction(), because then things get hairy with
208 # Don't use repo.transaction(), because then things get hairy with
209 # paths: some need to be relative to .hg, and some need to be
209 # paths: some need to be relative to .hg, and some need to be
210 # absolute. Doing it this way keeps things simple: everything is an
210 # absolute. Doing it this way keeps things simple: everything is an
211 # absolute path.
211 # absolute path.
212 lock = repo.lock(wait=False)
212 lock = repo.lock(wait=False)
213 tr = transaction.transaction(ui.warn,
213 tr = transaction.transaction(ui.warn,
214 open,
214 open,
215 repo.sjoin('journal'))
215 repo.sjoin('journal'))
216
216
217 def ignoremissing(func):
217 def ignoremissing(func):
218 def f(*args, **kw):
218 def f(*args, **kw):
219 try:
219 try:
220 return func(*args, **kw)
220 return func(*args, **kw)
221 except OSError, inst:
221 except OSError, inst:
222 if inst.errno != errno.ENOENT:
222 if inst.errno != errno.ENOENT:
223 raise
223 raise
224 return f
224 return f
225
225
226 try:
226 try:
227 try:
227 try:
228 order = toposort(ui, r1)
228 order = toposort(ui, r1)
229
229
230 suboptimal = 0
230 suboptimal = 0
231 for i in xrange(1, len(order)):
231 for i in xrange(1, len(order)):
232 parents = [p for p in r1.parentrevs(order[i])
232 parents = [p for p in r1.parentrevs(order[i])
233 if p != node.nullrev]
233 if p != node.nullrev]
234 if parents and order[i - 1] not in parents:
234 if parents and order[i - 1] not in parents:
235 suboptimal += 1
235 suboptimal += 1
236 ui.note(_('%d suboptimal nodes\n') % suboptimal)
236 ui.note(_('%d suboptimal nodes\n') % suboptimal)
237
237
238 writerevs(ui, r1, r2, order, tr)
238 writerevs(ui, r1, r2, order, tr)
239 report(ui, r1, r2)
239 report(ui, r1, r2)
240 tr.close()
240 tr.close()
241 except:
241 except:
242 # Abort transaction first, so we truncate the files before
242 # Abort transaction first, so we truncate the files before
243 # deleting them.
243 # deleting them.
244 tr.abort()
244 tr.abort()
245 for fn in (tmpindexfn, tmpdatafn):
245 for fn in (tmpindexfn, tmpdatafn):
246 ignoremissing(os.unlink)(fn)
246 ignoremissing(os.unlink)(fn)
247 raise
247 raise
248 if not opts.get('dry_run'):
248 if not opts.get('dry_run'):
249 # racy, both files cannot be renamed atomically
249 # racy, both files cannot be renamed atomically
250 # copy files
250 # copy files
251 util.os_link(indexfn, oldindexfn)
251 util.os_link(indexfn, oldindexfn)
252 ignoremissing(util.os_link)(datafn, olddatafn)
252 ignoremissing(util.os_link)(datafn, olddatafn)
253
253
254 # rename
254 # rename
255 util.rename(tmpindexfn, indexfn)
255 util.rename(tmpindexfn, indexfn)
256 try:
256 try:
257 os.chmod(tmpdatafn, os.stat(datafn).st_mode)
257 os.chmod(tmpdatafn, os.stat(datafn).st_mode)
258 util.rename(tmpdatafn, datafn)
258 util.rename(tmpdatafn, datafn)
259 except OSError, inst:
259 except OSError, inst:
260 if inst.errno != errno.ENOENT:
260 if inst.errno != errno.ENOENT:
261 raise
261 raise
262 ignoremissing(os.unlink)(datafn)
262 ignoremissing(os.unlink)(datafn)
263 else:
263 else:
264 for fn in (tmpindexfn, tmpdatafn):
264 for fn in (tmpindexfn, tmpdatafn):
265 ignoremissing(os.unlink)(fn)
265 ignoremissing(os.unlink)(fn)
266 finally:
266 finally:
267 lock.release()
267 lock.release()
268
268
269 if not opts.get('dry_run'):
269 if not opts.get('dry_run'):
270 ui.write(_('note: old revlog saved in:\n'
270 ui.write(_('note: old revlog saved in:\n'
271 ' %s\n'
271 ' %s\n'
272 ' %s\n'
272 ' %s\n'
273 '(You can delete those files when you are satisfied that your\n'
273 '(You can delete those files when you are satisfied that your\n'
274 'repository is still sane. '
274 'repository is still sane. '
275 'Running \'hg verify\' is strongly recommended.)\n')
275 'Running \'hg verify\' is strongly recommended.)\n')
276 % (oldindexfn, olddatafn))
276 % (oldindexfn, olddatafn))
277
277
278 cmdtable = {
278 cmdtable = {
279 'shrink': (shrink,
279 'shrink': (shrink,
280 [('', 'revlog', '', _('index (.i) file of the revlog to shrink')),
280 [('', 'revlog', '', _('index (.i) file of the revlog to shrink')),
281 ('n', 'dry-run', None, _('do not shrink, simulate only')),
281 ('n', 'dry-run', None, _('do not shrink, simulate only')),
282 ('', 'sort', 'reversepostorder', 'name of sort algorithm to use'),
282 ('', 'sort', 'reversepostorder', 'name of sort algorithm to use'),
283 ],
283 ],
284 _('hg shrink [--revlog PATH]'))
284 _('hg shrink [--revlog PATH]'))
285 }
285 }
286
286
287 if __name__ == "__main__":
287 if __name__ == "__main__":
288 print "shrink-revlog.py is now an extension (see hg help extensions)"
288 print "shrink-revlog.py is now an extension (see hg help extensions)"
@@ -1,282 +1,287 b''
1 # bundlerepo.py - repository class for viewing uncompressed bundles
1 # bundlerepo.py - repository class for viewing uncompressed bundles
2 #
2 #
3 # Copyright 2006, 2007 Benoit Boissinot <bboissin@gmail.com>
3 # Copyright 2006, 2007 Benoit Boissinot <bboissin@gmail.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """Repository class for viewing uncompressed bundles.
8 """Repository class for viewing uncompressed bundles.
9
9
10 This provides a read-only repository interface to bundles as if they
10 This provides a read-only repository interface to bundles as if they
11 were part of the actual repository.
11 were part of the actual repository.
12 """
12 """
13
13
14 from node import nullid
14 from node import nullid
15 from i18n import _
15 from i18n import _
16 import os, struct, tempfile, shutil
16 import os, struct, tempfile, shutil
17 import changegroup, util, mdiff
17 import changegroup, util, mdiff
18 import localrepo, changelog, manifest, filelog, revlog, error
18 import localrepo, changelog, manifest, filelog, revlog, error
19
19
20 class bundlerevlog(revlog.revlog):
20 class bundlerevlog(revlog.revlog):
21 def __init__(self, opener, indexfile, bundle,
21 def __init__(self, opener, indexfile, bundle,
22 linkmapper=None):
22 linkmapper=None):
23 # How it works:
23 # How it works:
24 # to retrieve a revision, we need to know the offset of
24 # to retrieve a revision, we need to know the offset of
25 # the revision in the bundle (an unbundle object).
25 # the revision in the bundle (an unbundle object).
26 #
26 #
27 # We store this offset in the index (start), to differentiate a
27 # We store this offset in the index (start), to differentiate a
28 # rev in the bundle and from a rev in the revlog, we check
28 # rev in the bundle and from a rev in the revlog, we check
29 # len(index[r]). If the tuple is bigger than 7, it is a bundle
29 # len(index[r]). If the tuple is bigger than 7, it is a bundle
30 # (it is bigger since we store the node to which the delta is)
30 # (it is bigger since we store the node to which the delta is)
31 #
31 #
32 revlog.revlog.__init__(self, opener, indexfile)
32 revlog.revlog.__init__(self, opener, indexfile)
33 self.bundle = bundle
33 self.bundle = bundle
34 self.basemap = {}
34 self.basemap = {}
35 def chunkpositer():
35 def chunkpositer():
36 for chunk in bundle.chunks():
36 while 1:
37 chunk = bundle.chunk()
38 if not chunk:
39 break
37 pos = bundle.tell()
40 pos = bundle.tell()
38 yield chunk, pos - len(chunk)
41 yield chunk, pos - len(chunk)
39 n = len(self)
42 n = len(self)
40 prev = None
43 prev = None
41 for chunk, start in chunkpositer():
44 for chunk, start in chunkpositer():
42 size = len(chunk)
45 size = len(chunk)
43 if size < 80:
46 if size < 80:
44 raise util.Abort(_("invalid changegroup"))
47 raise util.Abort(_("invalid changegroup"))
45 start += 80
48 start += 80
46 size -= 80
49 size -= 80
47 node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80])
50 node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80])
48 if node in self.nodemap:
51 if node in self.nodemap:
49 prev = node
52 prev = node
50 continue
53 continue
51 for p in (p1, p2):
54 for p in (p1, p2):
52 if not p in self.nodemap:
55 if not p in self.nodemap:
53 raise error.LookupError(p, self.indexfile,
56 raise error.LookupError(p, self.indexfile,
54 _("unknown parent"))
57 _("unknown parent"))
55 if linkmapper is None:
58 if linkmapper is None:
56 link = n
59 link = n
57 else:
60 else:
58 link = linkmapper(cs)
61 link = linkmapper(cs)
59
62
60 if not prev:
63 if not prev:
61 prev = p1
64 prev = p1
62 # start, size, full unc. size, base (unused), link, p1, p2, node
65 # start, size, full unc. size, base (unused), link, p1, p2, node
63 e = (revlog.offset_type(start, 0), size, -1, -1, link,
66 e = (revlog.offset_type(start, 0), size, -1, -1, link,
64 self.rev(p1), self.rev(p2), node)
67 self.rev(p1), self.rev(p2), node)
65 self.basemap[n] = prev
68 self.basemap[n] = prev
66 self.index.insert(-1, e)
69 self.index.insert(-1, e)
67 self.nodemap[node] = n
70 self.nodemap[node] = n
68 prev = node
71 prev = node
69 n += 1
72 n += 1
70
73
71 def inbundle(self, rev):
74 def inbundle(self, rev):
72 """is rev from the bundle"""
75 """is rev from the bundle"""
73 if rev < 0:
76 if rev < 0:
74 return False
77 return False
75 return rev in self.basemap
78 return rev in self.basemap
76 def bundlebase(self, rev):
79 def bundlebase(self, rev):
77 return self.basemap[rev]
80 return self.basemap[rev]
78 def _chunk(self, rev):
81 def _chunk(self, rev):
79 # Warning: in case of bundle, the diff is against bundlebase,
82 # Warning: in case of bundle, the diff is against bundlebase,
80 # not against rev - 1
83 # not against rev - 1
81 # XXX: could use some caching
84 # XXX: could use some caching
82 if not self.inbundle(rev):
85 if not self.inbundle(rev):
83 return revlog.revlog._chunk(self, rev)
86 return revlog.revlog._chunk(self, rev)
84 self.bundle.seek(self.start(rev))
87 self.bundle.seek(self.start(rev))
85 return self.bundle.read(self.length(rev))
88 return self.bundle.read(self.length(rev))
86
89
87 def revdiff(self, rev1, rev2):
90 def revdiff(self, rev1, rev2):
88 """return or calculate a delta between two revisions"""
91 """return or calculate a delta between two revisions"""
89 if self.inbundle(rev1) and self.inbundle(rev2):
92 if self.inbundle(rev1) and self.inbundle(rev2):
90 # hot path for bundle
93 # hot path for bundle
91 revb = self.rev(self.bundlebase(rev2))
94 revb = self.rev(self.bundlebase(rev2))
92 if revb == rev1:
95 if revb == rev1:
93 return self._chunk(rev2)
96 return self._chunk(rev2)
94 elif not self.inbundle(rev1) and not self.inbundle(rev2):
97 elif not self.inbundle(rev1) and not self.inbundle(rev2):
95 return revlog.revlog.revdiff(self, rev1, rev2)
98 return revlog.revlog.revdiff(self, rev1, rev2)
96
99
97 return mdiff.textdiff(self.revision(self.node(rev1)),
100 return mdiff.textdiff(self.revision(self.node(rev1)),
98 self.revision(self.node(rev2)))
101 self.revision(self.node(rev2)))
99
102
100 def revision(self, node):
103 def revision(self, node):
101 """return an uncompressed revision of a given"""
104 """return an uncompressed revision of a given"""
102 if node == nullid:
105 if node == nullid:
103 return ""
106 return ""
104
107
105 text = None
108 text = None
106 chain = []
109 chain = []
107 iter_node = node
110 iter_node = node
108 rev = self.rev(iter_node)
111 rev = self.rev(iter_node)
109 # reconstruct the revision if it is from a changegroup
112 # reconstruct the revision if it is from a changegroup
110 while self.inbundle(rev):
113 while self.inbundle(rev):
111 if self._cache and self._cache[0] == iter_node:
114 if self._cache and self._cache[0] == iter_node:
112 text = self._cache[2]
115 text = self._cache[2]
113 break
116 break
114 chain.append(rev)
117 chain.append(rev)
115 iter_node = self.bundlebase(rev)
118 iter_node = self.bundlebase(rev)
116 rev = self.rev(iter_node)
119 rev = self.rev(iter_node)
117 if text is None:
120 if text is None:
118 text = revlog.revlog.revision(self, iter_node)
121 text = revlog.revlog.revision(self, iter_node)
119
122
120 while chain:
123 while chain:
121 delta = self._chunk(chain.pop())
124 delta = self._chunk(chain.pop())
122 text = mdiff.patches(text, [delta])
125 text = mdiff.patches(text, [delta])
123
126
124 p1, p2 = self.parents(node)
127 p1, p2 = self.parents(node)
125 if node != revlog.hash(text, p1, p2):
128 if node != revlog.hash(text, p1, p2):
126 raise error.RevlogError(_("integrity check failed on %s:%d")
129 raise error.RevlogError(_("integrity check failed on %s:%d")
127 % (self.datafile, self.rev(node)))
130 % (self.datafile, self.rev(node)))
128
131
129 self._cache = (node, self.rev(node), text)
132 self._cache = (node, self.rev(node), text)
130 return text
133 return text
131
134
132 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None):
135 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None):
133 raise NotImplementedError
136 raise NotImplementedError
134 def addgroup(self, revs, linkmapper, transaction):
137 def addgroup(self, revs, linkmapper, transaction):
135 raise NotImplementedError
138 raise NotImplementedError
136 def strip(self, rev, minlink):
139 def strip(self, rev, minlink):
137 raise NotImplementedError
140 raise NotImplementedError
138 def checksize(self):
141 def checksize(self):
139 raise NotImplementedError
142 raise NotImplementedError
140
143
141 class bundlechangelog(bundlerevlog, changelog.changelog):
144 class bundlechangelog(bundlerevlog, changelog.changelog):
142 def __init__(self, opener, bundle):
145 def __init__(self, opener, bundle):
143 changelog.changelog.__init__(self, opener)
146 changelog.changelog.__init__(self, opener)
144 bundlerevlog.__init__(self, opener, self.indexfile, bundle)
147 bundlerevlog.__init__(self, opener, self.indexfile, bundle)
145
148
146 class bundlemanifest(bundlerevlog, manifest.manifest):
149 class bundlemanifest(bundlerevlog, manifest.manifest):
147 def __init__(self, opener, bundle, linkmapper):
150 def __init__(self, opener, bundle, linkmapper):
148 manifest.manifest.__init__(self, opener)
151 manifest.manifest.__init__(self, opener)
149 bundlerevlog.__init__(self, opener, self.indexfile, bundle,
152 bundlerevlog.__init__(self, opener, self.indexfile, bundle,
150 linkmapper)
153 linkmapper)
151
154
152 class bundlefilelog(bundlerevlog, filelog.filelog):
155 class bundlefilelog(bundlerevlog, filelog.filelog):
153 def __init__(self, opener, path, bundle, linkmapper):
156 def __init__(self, opener, path, bundle, linkmapper):
154 filelog.filelog.__init__(self, opener, path)
157 filelog.filelog.__init__(self, opener, path)
155 bundlerevlog.__init__(self, opener, self.indexfile, bundle,
158 bundlerevlog.__init__(self, opener, self.indexfile, bundle,
156 linkmapper)
159 linkmapper)
157
160
158 class bundlerepository(localrepo.localrepository):
161 class bundlerepository(localrepo.localrepository):
159 def __init__(self, ui, path, bundlename):
162 def __init__(self, ui, path, bundlename):
160 self._tempparent = None
163 self._tempparent = None
161 try:
164 try:
162 localrepo.localrepository.__init__(self, ui, path)
165 localrepo.localrepository.__init__(self, ui, path)
163 except error.RepoError:
166 except error.RepoError:
164 self._tempparent = tempfile.mkdtemp()
167 self._tempparent = tempfile.mkdtemp()
165 localrepo.instance(ui, self._tempparent, 1)
168 localrepo.instance(ui, self._tempparent, 1)
166 localrepo.localrepository.__init__(self, ui, self._tempparent)
169 localrepo.localrepository.__init__(self, ui, self._tempparent)
167
170
168 if path:
171 if path:
169 self._url = 'bundle:' + util.expandpath(path) + '+' + bundlename
172 self._url = 'bundle:' + util.expandpath(path) + '+' + bundlename
170 else:
173 else:
171 self._url = 'bundle:' + bundlename
174 self._url = 'bundle:' + bundlename
172
175
173 self.tempfile = None
176 self.tempfile = None
174 f = open(bundlename, "rb")
177 f = open(bundlename, "rb")
175 self.bundle = changegroup.readbundle(f, bundlename)
178 self.bundle = changegroup.readbundle(f, bundlename)
176 if self.bundle.compressed():
179 if self.bundle.compressed():
177 # we need a seekable, decompressed bundle
180 # we need a seekable, decompressed bundle
178 fdtemp, temp = tempfile.mkstemp(prefix="hg-bundle-",
181 fdtemp, temp = tempfile.mkstemp(prefix="hg-bundle-",
179 suffix=".hg10un", dir=self.path)
182 suffix=".hg10un", dir=self.path)
180 self.tempfile = temp
183 self.tempfile = temp
181 fptemp = os.fdopen(fdtemp, 'wb')
184 fptemp = os.fdopen(fdtemp, 'wb')
182
185
183 try:
186 try:
184 fptemp.write("HG10UN")
187 fptemp.write("HG10UN")
185 while 1:
188 while 1:
186 chunk = self.bundle.read(2**18)
189 chunk = self.bundle.read(2**18)
187 if not chunk:
190 if not chunk:
188 break
191 break
189 fptemp.write(chunk)
192 fptemp.write(chunk)
190 finally:
193 finally:
191 fptemp.close()
194 fptemp.close()
192
195
193 f = open(self.tempfile, "rb")
196 f = open(self.tempfile, "rb")
194 self.bundle = changegroup.readbundle(f, bundlename)
197 self.bundle = changegroup.readbundle(f, bundlename)
195
198
196 # dict with the mapping 'filename' -> position in the bundle
199 # dict with the mapping 'filename' -> position in the bundle
197 self.bundlefilespos = {}
200 self.bundlefilespos = {}
198
201
199 @util.propertycache
202 @util.propertycache
200 def changelog(self):
203 def changelog(self):
201 c = bundlechangelog(self.sopener, self.bundle)
204 c = bundlechangelog(self.sopener, self.bundle)
202 self.manstart = self.bundle.tell()
205 self.manstart = self.bundle.tell()
203 return c
206 return c
204
207
205 @util.propertycache
208 @util.propertycache
206 def manifest(self):
209 def manifest(self):
207 self.bundle.seek(self.manstart)
210 self.bundle.seek(self.manstart)
208 m = bundlemanifest(self.sopener, self.bundle, self.changelog.rev)
211 m = bundlemanifest(self.sopener, self.bundle, self.changelog.rev)
209 self.filestart = self.bundle.tell()
212 self.filestart = self.bundle.tell()
210 return m
213 return m
211
214
212 @util.propertycache
215 @util.propertycache
213 def manstart(self):
216 def manstart(self):
214 self.changelog
217 self.changelog
215 return self.manstart
218 return self.manstart
216
219
217 @util.propertycache
220 @util.propertycache
218 def filestart(self):
221 def filestart(self):
219 self.manifest
222 self.manifest
220 return self.filestart
223 return self.filestart
221
224
222 def url(self):
225 def url(self):
223 return self._url
226 return self._url
224
227
225 def file(self, f):
228 def file(self, f):
226 if not self.bundlefilespos:
229 if not self.bundlefilespos:
227 self.bundle.seek(self.filestart)
230 self.bundle.seek(self.filestart)
228 while 1:
231 while 1:
229 chunk = self.bundle.chunk()
232 chunk = self.bundle.chunk()
230 if not chunk:
233 if not chunk:
231 break
234 break
232 self.bundlefilespos[chunk] = self.bundle.tell()
235 self.bundlefilespos[chunk] = self.bundle.tell()
233 for c in self.bundle.chunks():
236 while 1:
234 pass
237 c = self.bundle.chunk()
238 if not c:
239 break
235
240
236 if f[0] == '/':
241 if f[0] == '/':
237 f = f[1:]
242 f = f[1:]
238 if f in self.bundlefilespos:
243 if f in self.bundlefilespos:
239 self.bundle.seek(self.bundlefilespos[f])
244 self.bundle.seek(self.bundlefilespos[f])
240 return bundlefilelog(self.sopener, f, self.bundle,
245 return bundlefilelog(self.sopener, f, self.bundle,
241 self.changelog.rev)
246 self.changelog.rev)
242 else:
247 else:
243 return filelog.filelog(self.sopener, f)
248 return filelog.filelog(self.sopener, f)
244
249
245 def __del__(self):
250 def __del__(self):
246 del self.bundle
251 del self.bundle
247 if tempfile is not None:
252 if tempfile is not None:
248 os.unlink(tempfile)
253 os.unlink(tempfile)
249 if self._tempparent:
254 if self._tempparent:
250 shutil.rmtree(self._tempparent, True)
255 shutil.rmtree(self._tempparent, True)
251
256
252 def cancopy(self):
257 def cancopy(self):
253 return False
258 return False
254
259
255 def getcwd(self):
260 def getcwd(self):
256 return os.getcwd() # always outside the repo
261 return os.getcwd() # always outside the repo
257
262
258 def instance(ui, path, create):
263 def instance(ui, path, create):
259 if create:
264 if create:
260 raise util.Abort(_('cannot create new bundle repository'))
265 raise util.Abort(_('cannot create new bundle repository'))
261 parentpath = ui.config("bundle", "mainreporoot", "")
266 parentpath = ui.config("bundle", "mainreporoot", "")
262 if parentpath:
267 if parentpath:
263 # Try to make the full path relative so we get a nice, short URL.
268 # Try to make the full path relative so we get a nice, short URL.
264 # In particular, we don't want temp dir names in test outputs.
269 # In particular, we don't want temp dir names in test outputs.
265 cwd = os.getcwd()
270 cwd = os.getcwd()
266 if parentpath == cwd:
271 if parentpath == cwd:
267 parentpath = ''
272 parentpath = ''
268 else:
273 else:
269 cwd = os.path.join(cwd,'')
274 cwd = os.path.join(cwd,'')
270 if parentpath.startswith(cwd):
275 if parentpath.startswith(cwd):
271 parentpath = parentpath[len(cwd):]
276 parentpath = parentpath[len(cwd):]
272 path = util.drop_scheme('file', path)
277 path = util.drop_scheme('file', path)
273 if path.startswith('bundle:'):
278 if path.startswith('bundle:'):
274 path = util.drop_scheme('bundle', path)
279 path = util.drop_scheme('bundle', path)
275 s = path.split("+", 1)
280 s = path.split("+", 1)
276 if len(s) == 1:
281 if len(s) == 1:
277 repopath, bundlename = parentpath, s[0]
282 repopath, bundlename = parentpath, s[0]
278 else:
283 else:
279 repopath, bundlename = s
284 repopath, bundlename = s
280 else:
285 else:
281 repopath, bundlename = parentpath, path
286 repopath, bundlename = parentpath, path
282 return bundlerepository(ui, repopath, bundlename)
287 return bundlerepository(ui, repopath, bundlename)
@@ -1,208 +1,193 b''
1 # changegroup.py - Mercurial changegroup manipulation functions
1 # changegroup.py - Mercurial changegroup manipulation functions
2 #
2 #
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from i18n import _
8 from i18n import _
9 import util
9 import util
10 import struct, os, bz2, zlib, tempfile
10 import struct, os, bz2, zlib, tempfile
11
11
12 def getchunk(source):
12 def getchunk(source):
13 """return the next chunk from changegroup 'source' as a string"""
13 """return the next chunk from changegroup 'source' as a string"""
14 d = source.read(4)
14 d = source.read(4)
15 if not d:
15 if not d:
16 return ""
16 return ""
17 l = struct.unpack(">l", d)[0]
17 l = struct.unpack(">l", d)[0]
18 if l <= 4:
18 if l <= 4:
19 return ""
19 return ""
20 d = source.read(l - 4)
20 d = source.read(l - 4)
21 if len(d) < l - 4:
21 if len(d) < l - 4:
22 raise util.Abort(_("premature EOF reading chunk"
22 raise util.Abort(_("premature EOF reading chunk"
23 " (got %d bytes, expected %d)")
23 " (got %d bytes, expected %d)")
24 % (len(d), l - 4))
24 % (len(d), l - 4))
25 return d
25 return d
26
26
27 def chunkiter(source, progress=None):
28 """iterate through the chunks in source, yielding a sequence of chunks
29 (strings)"""
30 while 1:
31 c = getchunk(source)
32 if not c:
33 break
34 elif progress is not None:
35 progress()
36 yield c
37
38 def chunkheader(length):
27 def chunkheader(length):
39 """return a changegroup chunk header (string)"""
28 """return a changegroup chunk header (string)"""
40 return struct.pack(">l", length + 4)
29 return struct.pack(">l", length + 4)
41
30
42 def closechunk():
31 def closechunk():
43 """return a changegroup chunk header (string) for a zero-length chunk"""
32 """return a changegroup chunk header (string) for a zero-length chunk"""
44 return struct.pack(">l", 0)
33 return struct.pack(">l", 0)
45
34
46 class nocompress(object):
35 class nocompress(object):
47 def compress(self, x):
36 def compress(self, x):
48 return x
37 return x
49 def flush(self):
38 def flush(self):
50 return ""
39 return ""
51
40
52 bundletypes = {
41 bundletypes = {
53 "": ("", nocompress),
42 "": ("", nocompress),
54 "HG10UN": ("HG10UN", nocompress),
43 "HG10UN": ("HG10UN", nocompress),
55 "HG10BZ": ("HG10", lambda: bz2.BZ2Compressor()),
44 "HG10BZ": ("HG10", lambda: bz2.BZ2Compressor()),
56 "HG10GZ": ("HG10GZ", lambda: zlib.compressobj()),
45 "HG10GZ": ("HG10GZ", lambda: zlib.compressobj()),
57 }
46 }
58
47
59 def collector(cl, mmfs, files):
48 def collector(cl, mmfs, files):
60 # Gather information about changeset nodes going out in a bundle.
49 # Gather information about changeset nodes going out in a bundle.
61 # We want to gather manifests needed and filelogs affected.
50 # We want to gather manifests needed and filelogs affected.
62 def collect(node):
51 def collect(node):
63 c = cl.read(node)
52 c = cl.read(node)
64 files.update(c[3])
53 files.update(c[3])
65 mmfs.setdefault(c[0], node)
54 mmfs.setdefault(c[0], node)
66 return collect
55 return collect
67
56
68 # hgweb uses this list to communicate its preferred type
57 # hgweb uses this list to communicate its preferred type
69 bundlepriority = ['HG10GZ', 'HG10BZ', 'HG10UN']
58 bundlepriority = ['HG10GZ', 'HG10BZ', 'HG10UN']
70
59
71 def writebundle(cg, filename, bundletype):
60 def writebundle(cg, filename, bundletype):
72 """Write a bundle file and return its filename.
61 """Write a bundle file and return its filename.
73
62
74 Existing files will not be overwritten.
63 Existing files will not be overwritten.
75 If no filename is specified, a temporary file is created.
64 If no filename is specified, a temporary file is created.
76 bz2 compression can be turned off.
65 bz2 compression can be turned off.
77 The bundle file will be deleted in case of errors.
66 The bundle file will be deleted in case of errors.
78 """
67 """
79
68
80 fh = None
69 fh = None
81 cleanup = None
70 cleanup = None
82 try:
71 try:
83 if filename:
72 if filename:
84 fh = open(filename, "wb")
73 fh = open(filename, "wb")
85 else:
74 else:
86 fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
75 fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
87 fh = os.fdopen(fd, "wb")
76 fh = os.fdopen(fd, "wb")
88 cleanup = filename
77 cleanup = filename
89
78
90 header, compressor = bundletypes[bundletype]
79 header, compressor = bundletypes[bundletype]
91 fh.write(header)
80 fh.write(header)
92 z = compressor()
81 z = compressor()
93
82
94 # parse the changegroup data, otherwise we will block
83 # parse the changegroup data, otherwise we will block
95 # in case of sshrepo because we don't know the end of the stream
84 # in case of sshrepo because we don't know the end of the stream
96
85
97 # an empty chunkiter is the end of the changegroup
86 # an empty chunkgroup is the end of the changegroup
98 # a changegroup has at least 2 chunkiters (changelog and manifest).
87 # a changegroup has at least 2 chunkgroups (changelog and manifest).
99 # after that, an empty chunkiter is the end of the changegroup
88 # after that, an empty chunkgroup is the end of the changegroup
100 empty = False
89 empty = False
101 count = 0
90 count = 0
102 while not empty or count <= 2:
91 while not empty or count <= 2:
103 empty = True
92 empty = True
104 count += 1
93 count += 1
105 for chunk in chunkiter(cg):
94 while 1:
95 chunk = getchunk(cg)
96 if not chunk:
97 break
106 empty = False
98 empty = False
107 fh.write(z.compress(chunkheader(len(chunk))))
99 fh.write(z.compress(chunkheader(len(chunk))))
108 pos = 0
100 pos = 0
109 while pos < len(chunk):
101 while pos < len(chunk):
110 next = pos + 2**20
102 next = pos + 2**20
111 fh.write(z.compress(chunk[pos:next]))
103 fh.write(z.compress(chunk[pos:next]))
112 pos = next
104 pos = next
113 fh.write(z.compress(closechunk()))
105 fh.write(z.compress(closechunk()))
114 fh.write(z.flush())
106 fh.write(z.flush())
115 cleanup = None
107 cleanup = None
116 return filename
108 return filename
117 finally:
109 finally:
118 if fh is not None:
110 if fh is not None:
119 fh.close()
111 fh.close()
120 if cleanup is not None:
112 if cleanup is not None:
121 os.unlink(cleanup)
113 os.unlink(cleanup)
122
114
123 def decompressor(fh, alg):
115 def decompressor(fh, alg):
124 if alg == 'UN':
116 if alg == 'UN':
125 return fh
117 return fh
126 elif alg == 'GZ':
118 elif alg == 'GZ':
127 def generator(f):
119 def generator(f):
128 zd = zlib.decompressobj()
120 zd = zlib.decompressobj()
129 for chunk in f:
121 for chunk in f:
130 yield zd.decompress(chunk)
122 yield zd.decompress(chunk)
131 elif alg == 'BZ':
123 elif alg == 'BZ':
132 def generator(f):
124 def generator(f):
133 zd = bz2.BZ2Decompressor()
125 zd = bz2.BZ2Decompressor()
134 zd.decompress("BZ")
126 zd.decompress("BZ")
135 for chunk in util.filechunkiter(f, 4096):
127 for chunk in util.filechunkiter(f, 4096):
136 yield zd.decompress(chunk)
128 yield zd.decompress(chunk)
137 else:
129 else:
138 raise util.Abort("unknown bundle compression '%s'" % alg)
130 raise util.Abort("unknown bundle compression '%s'" % alg)
139 return util.chunkbuffer(generator(fh))
131 return util.chunkbuffer(generator(fh))
140
132
141 class unbundle10(object):
133 class unbundle10(object):
142 def __init__(self, fh, alg):
134 def __init__(self, fh, alg):
143 self._stream = decompressor(fh, alg)
135 self._stream = decompressor(fh, alg)
144 self._type = alg
136 self._type = alg
145 self.callback = None
137 self.callback = None
146 def compressed(self):
138 def compressed(self):
147 return self._type != 'UN'
139 return self._type != 'UN'
148 def read(self, l):
140 def read(self, l):
149 return self._stream.read(l)
141 return self._stream.read(l)
150 def seek(self, pos):
142 def seek(self, pos):
151 return self._stream.seek(pos)
143 return self._stream.seek(pos)
152 def tell(self):
144 def tell(self):
153 return self._stream.tell()
145 return self._stream.tell()
154
146
155 def chunklength(self):
147 def chunklength(self):
156 d = self.read(4)
148 d = self.read(4)
157 if not d:
149 if not d:
158 return 0
150 return 0
159 l = max(0, struct.unpack(">l", d)[0] - 4)
151 l = max(0, struct.unpack(">l", d)[0] - 4)
160 if l and self.callback:
152 if l and self.callback:
161 self.callback()
153 self.callback()
162 return l
154 return l
163
155
164 def chunk(self):
156 def chunk(self):
165 """return the next chunk from changegroup 'source' as a string"""
157 """return the next chunk from changegroup 'source' as a string"""
166 l = self.chunklength()
158 l = self.chunklength()
167 d = self.read(l)
159 d = self.read(l)
168 if len(d) < l:
160 if len(d) < l:
169 raise util.Abort(_("premature EOF reading chunk"
161 raise util.Abort(_("premature EOF reading chunk"
170 " (got %d bytes, expected %d)")
162 " (got %d bytes, expected %d)")
171 % (len(d), l))
163 % (len(d), l))
172 return d
164 return d
173
165
174 def chunks(self):
175 while 1:
176 c = self.chunk()
177 if not c:
178 break
179 yield c
180
181 class headerlessfixup(object):
166 class headerlessfixup(object):
182 def __init__(self, fh, h):
167 def __init__(self, fh, h):
183 self._h = h
168 self._h = h
184 self._fh = fh
169 self._fh = fh
185 def read(self, n):
170 def read(self, n):
186 if self._h:
171 if self._h:
187 d, self._h = self._h[:n], self._h[n:]
172 d, self._h = self._h[:n], self._h[n:]
188 if len(d) < n:
173 if len(d) < n:
189 d += self._fh.read(n - len(d))
174 d += self._fh.read(n - len(d))
190 return d
175 return d
191 return self._fh.read(n)
176 return self._fh.read(n)
192
177
193 def readbundle(fh, fname):
178 def readbundle(fh, fname):
194 header = fh.read(6)
179 header = fh.read(6)
195
180
196 if not fname:
181 if not fname:
197 fname = "stream"
182 fname = "stream"
198 if not header.startswith('HG') and header.startswith('\0'):
183 if not header.startswith('HG') and header.startswith('\0'):
199 fh = headerlessfixup(fh, header)
184 fh = headerlessfixup(fh, header)
200 header = "HG10UN"
185 header = "HG10UN"
201
186
202 magic, version, alg = header[0:2], header[2:4], header[4:6]
187 magic, version, alg = header[0:2], header[2:4], header[4:6]
203
188
204 if magic != 'HG':
189 if magic != 'HG':
205 raise util.Abort(_('%s: not a Mercurial bundle') % fname)
190 raise util.Abort(_('%s: not a Mercurial bundle') % fname)
206 if version != '10':
191 if version != '10':
207 raise util.Abort(_('%s: unknown bundle version %s') % (fname, version))
192 raise util.Abort(_('%s: unknown bundle version %s') % (fname, version))
208 return unbundle10(fh, alg)
193 return unbundle10(fh, alg)
@@ -1,1893 +1,1893 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from node import bin, hex, nullid, nullrev, short
8 from node import bin, hex, nullid, nullrev, short
9 from i18n import _
9 from i18n import _
10 import repo, changegroup, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context
11 import changelog, dirstate, filelog, manifest, context
12 import lock, transaction, store, encoding
12 import lock, transaction, store, encoding
13 import util, extensions, hook, error
13 import util, extensions, hook, error
14 import match as matchmod
14 import match as matchmod
15 import merge as mergemod
15 import merge as mergemod
16 import tags as tagsmod
16 import tags as tagsmod
17 import url as urlmod
17 import url as urlmod
18 from lock import release
18 from lock import release
19 import weakref, errno, os, time, inspect
19 import weakref, errno, os, time, inspect
20 propertycache = util.propertycache
20 propertycache = util.propertycache
21
21
22 class localrepository(repo.repository):
22 class localrepository(repo.repository):
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
25 supported = supportedformats | set(('store', 'fncache', 'shared'))
25 supported = supportedformats | set(('store', 'fncache', 'shared'))
26
26
27 def __init__(self, baseui, path=None, create=0):
27 def __init__(self, baseui, path=None, create=0):
28 repo.repository.__init__(self)
28 repo.repository.__init__(self)
29 self.root = os.path.realpath(util.expandpath(path))
29 self.root = os.path.realpath(util.expandpath(path))
30 self.path = os.path.join(self.root, ".hg")
30 self.path = os.path.join(self.root, ".hg")
31 self.origroot = path
31 self.origroot = path
32 self.auditor = util.path_auditor(self.root, self._checknested)
32 self.auditor = util.path_auditor(self.root, self._checknested)
33 self.opener = util.opener(self.path)
33 self.opener = util.opener(self.path)
34 self.wopener = util.opener(self.root)
34 self.wopener = util.opener(self.root)
35 self.baseui = baseui
35 self.baseui = baseui
36 self.ui = baseui.copy()
36 self.ui = baseui.copy()
37
37
38 try:
38 try:
39 self.ui.readconfig(self.join("hgrc"), self.root)
39 self.ui.readconfig(self.join("hgrc"), self.root)
40 extensions.loadall(self.ui)
40 extensions.loadall(self.ui)
41 except IOError:
41 except IOError:
42 pass
42 pass
43
43
44 if not os.path.isdir(self.path):
44 if not os.path.isdir(self.path):
45 if create:
45 if create:
46 if not os.path.exists(path):
46 if not os.path.exists(path):
47 util.makedirs(path)
47 util.makedirs(path)
48 os.mkdir(self.path)
48 os.mkdir(self.path)
49 requirements = ["revlogv1"]
49 requirements = ["revlogv1"]
50 if self.ui.configbool('format', 'usestore', True):
50 if self.ui.configbool('format', 'usestore', True):
51 os.mkdir(os.path.join(self.path, "store"))
51 os.mkdir(os.path.join(self.path, "store"))
52 requirements.append("store")
52 requirements.append("store")
53 if self.ui.configbool('format', 'usefncache', True):
53 if self.ui.configbool('format', 'usefncache', True):
54 requirements.append("fncache")
54 requirements.append("fncache")
55 # create an invalid changelog
55 # create an invalid changelog
56 self.opener("00changelog.i", "a").write(
56 self.opener("00changelog.i", "a").write(
57 '\0\0\0\2' # represents revlogv2
57 '\0\0\0\2' # represents revlogv2
58 ' dummy changelog to prevent using the old repo layout'
58 ' dummy changelog to prevent using the old repo layout'
59 )
59 )
60 if self.ui.configbool('format', 'parentdelta', False):
60 if self.ui.configbool('format', 'parentdelta', False):
61 requirements.append("parentdelta")
61 requirements.append("parentdelta")
62 else:
62 else:
63 raise error.RepoError(_("repository %s not found") % path)
63 raise error.RepoError(_("repository %s not found") % path)
64 elif create:
64 elif create:
65 raise error.RepoError(_("repository %s already exists") % path)
65 raise error.RepoError(_("repository %s already exists") % path)
66 else:
66 else:
67 # find requirements
67 # find requirements
68 requirements = set()
68 requirements = set()
69 try:
69 try:
70 requirements = set(self.opener("requires").read().splitlines())
70 requirements = set(self.opener("requires").read().splitlines())
71 except IOError, inst:
71 except IOError, inst:
72 if inst.errno != errno.ENOENT:
72 if inst.errno != errno.ENOENT:
73 raise
73 raise
74 for r in requirements - self.supported:
74 for r in requirements - self.supported:
75 raise error.RepoError(_("requirement '%s' not supported") % r)
75 raise error.RepoError(_("requirement '%s' not supported") % r)
76
76
77 self.sharedpath = self.path
77 self.sharedpath = self.path
78 try:
78 try:
79 s = os.path.realpath(self.opener("sharedpath").read())
79 s = os.path.realpath(self.opener("sharedpath").read())
80 if not os.path.exists(s):
80 if not os.path.exists(s):
81 raise error.RepoError(
81 raise error.RepoError(
82 _('.hg/sharedpath points to nonexistent directory %s') % s)
82 _('.hg/sharedpath points to nonexistent directory %s') % s)
83 self.sharedpath = s
83 self.sharedpath = s
84 except IOError, inst:
84 except IOError, inst:
85 if inst.errno != errno.ENOENT:
85 if inst.errno != errno.ENOENT:
86 raise
86 raise
87
87
88 self.store = store.store(requirements, self.sharedpath, util.opener)
88 self.store = store.store(requirements, self.sharedpath, util.opener)
89 self.spath = self.store.path
89 self.spath = self.store.path
90 self.sopener = self.store.opener
90 self.sopener = self.store.opener
91 self.sjoin = self.store.join
91 self.sjoin = self.store.join
92 self.opener.createmode = self.store.createmode
92 self.opener.createmode = self.store.createmode
93 self._applyrequirements(requirements)
93 self._applyrequirements(requirements)
94 if create:
94 if create:
95 self._writerequirements()
95 self._writerequirements()
96
96
97 # These two define the set of tags for this repository. _tags
97 # These two define the set of tags for this repository. _tags
98 # maps tag name to node; _tagtypes maps tag name to 'global' or
98 # maps tag name to node; _tagtypes maps tag name to 'global' or
99 # 'local'. (Global tags are defined by .hgtags across all
99 # 'local'. (Global tags are defined by .hgtags across all
100 # heads, and local tags are defined in .hg/localtags.) They
100 # heads, and local tags are defined in .hg/localtags.) They
101 # constitute the in-memory cache of tags.
101 # constitute the in-memory cache of tags.
102 self._tags = None
102 self._tags = None
103 self._tagtypes = None
103 self._tagtypes = None
104
104
105 self._branchcache = None # in UTF-8
105 self._branchcache = None # in UTF-8
106 self._branchcachetip = None
106 self._branchcachetip = None
107 self.nodetagscache = None
107 self.nodetagscache = None
108 self.filterpats = {}
108 self.filterpats = {}
109 self._datafilters = {}
109 self._datafilters = {}
110 self._transref = self._lockref = self._wlockref = None
110 self._transref = self._lockref = self._wlockref = None
111
111
112 def _applyrequirements(self, requirements):
112 def _applyrequirements(self, requirements):
113 self.requirements = requirements
113 self.requirements = requirements
114 self.sopener.options = {}
114 self.sopener.options = {}
115 if 'parentdelta' in requirements:
115 if 'parentdelta' in requirements:
116 self.sopener.options['parentdelta'] = 1
116 self.sopener.options['parentdelta'] = 1
117
117
118 def _writerequirements(self):
118 def _writerequirements(self):
119 reqfile = self.opener("requires", "w")
119 reqfile = self.opener("requires", "w")
120 for r in self.requirements:
120 for r in self.requirements:
121 reqfile.write("%s\n" % r)
121 reqfile.write("%s\n" % r)
122 reqfile.close()
122 reqfile.close()
123
123
124 def _checknested(self, path):
124 def _checknested(self, path):
125 """Determine if path is a legal nested repository."""
125 """Determine if path is a legal nested repository."""
126 if not path.startswith(self.root):
126 if not path.startswith(self.root):
127 return False
127 return False
128 subpath = path[len(self.root) + 1:]
128 subpath = path[len(self.root) + 1:]
129
129
130 # XXX: Checking against the current working copy is wrong in
130 # XXX: Checking against the current working copy is wrong in
131 # the sense that it can reject things like
131 # the sense that it can reject things like
132 #
132 #
133 # $ hg cat -r 10 sub/x.txt
133 # $ hg cat -r 10 sub/x.txt
134 #
134 #
135 # if sub/ is no longer a subrepository in the working copy
135 # if sub/ is no longer a subrepository in the working copy
136 # parent revision.
136 # parent revision.
137 #
137 #
138 # However, it can of course also allow things that would have
138 # However, it can of course also allow things that would have
139 # been rejected before, such as the above cat command if sub/
139 # been rejected before, such as the above cat command if sub/
140 # is a subrepository now, but was a normal directory before.
140 # is a subrepository now, but was a normal directory before.
141 # The old path auditor would have rejected by mistake since it
141 # The old path auditor would have rejected by mistake since it
142 # panics when it sees sub/.hg/.
142 # panics when it sees sub/.hg/.
143 #
143 #
144 # All in all, checking against the working copy seems sensible
144 # All in all, checking against the working copy seems sensible
145 # since we want to prevent access to nested repositories on
145 # since we want to prevent access to nested repositories on
146 # the filesystem *now*.
146 # the filesystem *now*.
147 ctx = self[None]
147 ctx = self[None]
148 parts = util.splitpath(subpath)
148 parts = util.splitpath(subpath)
149 while parts:
149 while parts:
150 prefix = os.sep.join(parts)
150 prefix = os.sep.join(parts)
151 if prefix in ctx.substate:
151 if prefix in ctx.substate:
152 if prefix == subpath:
152 if prefix == subpath:
153 return True
153 return True
154 else:
154 else:
155 sub = ctx.sub(prefix)
155 sub = ctx.sub(prefix)
156 return sub.checknested(subpath[len(prefix) + 1:])
156 return sub.checknested(subpath[len(prefix) + 1:])
157 else:
157 else:
158 parts.pop()
158 parts.pop()
159 return False
159 return False
160
160
161
161
162 @propertycache
162 @propertycache
163 def changelog(self):
163 def changelog(self):
164 c = changelog.changelog(self.sopener)
164 c = changelog.changelog(self.sopener)
165 if 'HG_PENDING' in os.environ:
165 if 'HG_PENDING' in os.environ:
166 p = os.environ['HG_PENDING']
166 p = os.environ['HG_PENDING']
167 if p.startswith(self.root):
167 if p.startswith(self.root):
168 c.readpending('00changelog.i.a')
168 c.readpending('00changelog.i.a')
169 self.sopener.options['defversion'] = c.version
169 self.sopener.options['defversion'] = c.version
170 return c
170 return c
171
171
172 @propertycache
172 @propertycache
173 def manifest(self):
173 def manifest(self):
174 return manifest.manifest(self.sopener)
174 return manifest.manifest(self.sopener)
175
175
176 @propertycache
176 @propertycache
177 def dirstate(self):
177 def dirstate(self):
178 return dirstate.dirstate(self.opener, self.ui, self.root)
178 return dirstate.dirstate(self.opener, self.ui, self.root)
179
179
180 def __getitem__(self, changeid):
180 def __getitem__(self, changeid):
181 if changeid is None:
181 if changeid is None:
182 return context.workingctx(self)
182 return context.workingctx(self)
183 return context.changectx(self, changeid)
183 return context.changectx(self, changeid)
184
184
185 def __contains__(self, changeid):
185 def __contains__(self, changeid):
186 try:
186 try:
187 return bool(self.lookup(changeid))
187 return bool(self.lookup(changeid))
188 except error.RepoLookupError:
188 except error.RepoLookupError:
189 return False
189 return False
190
190
191 def __nonzero__(self):
191 def __nonzero__(self):
192 return True
192 return True
193
193
194 def __len__(self):
194 def __len__(self):
195 return len(self.changelog)
195 return len(self.changelog)
196
196
197 def __iter__(self):
197 def __iter__(self):
198 for i in xrange(len(self)):
198 for i in xrange(len(self)):
199 yield i
199 yield i
200
200
201 def url(self):
201 def url(self):
202 return 'file:' + self.root
202 return 'file:' + self.root
203
203
204 def hook(self, name, throw=False, **args):
204 def hook(self, name, throw=False, **args):
205 return hook.hook(self.ui, self, name, throw, **args)
205 return hook.hook(self.ui, self, name, throw, **args)
206
206
207 tag_disallowed = ':\r\n'
207 tag_disallowed = ':\r\n'
208
208
209 def _tag(self, names, node, message, local, user, date, extra={}):
209 def _tag(self, names, node, message, local, user, date, extra={}):
210 if isinstance(names, str):
210 if isinstance(names, str):
211 allchars = names
211 allchars = names
212 names = (names,)
212 names = (names,)
213 else:
213 else:
214 allchars = ''.join(names)
214 allchars = ''.join(names)
215 for c in self.tag_disallowed:
215 for c in self.tag_disallowed:
216 if c in allchars:
216 if c in allchars:
217 raise util.Abort(_('%r cannot be used in a tag name') % c)
217 raise util.Abort(_('%r cannot be used in a tag name') % c)
218
218
219 branches = self.branchmap()
219 branches = self.branchmap()
220 for name in names:
220 for name in names:
221 self.hook('pretag', throw=True, node=hex(node), tag=name,
221 self.hook('pretag', throw=True, node=hex(node), tag=name,
222 local=local)
222 local=local)
223 if name in branches:
223 if name in branches:
224 self.ui.warn(_("warning: tag %s conflicts with existing"
224 self.ui.warn(_("warning: tag %s conflicts with existing"
225 " branch name\n") % name)
225 " branch name\n") % name)
226
226
227 def writetags(fp, names, munge, prevtags):
227 def writetags(fp, names, munge, prevtags):
228 fp.seek(0, 2)
228 fp.seek(0, 2)
229 if prevtags and prevtags[-1] != '\n':
229 if prevtags and prevtags[-1] != '\n':
230 fp.write('\n')
230 fp.write('\n')
231 for name in names:
231 for name in names:
232 m = munge and munge(name) or name
232 m = munge and munge(name) or name
233 if self._tagtypes and name in self._tagtypes:
233 if self._tagtypes and name in self._tagtypes:
234 old = self._tags.get(name, nullid)
234 old = self._tags.get(name, nullid)
235 fp.write('%s %s\n' % (hex(old), m))
235 fp.write('%s %s\n' % (hex(old), m))
236 fp.write('%s %s\n' % (hex(node), m))
236 fp.write('%s %s\n' % (hex(node), m))
237 fp.close()
237 fp.close()
238
238
239 prevtags = ''
239 prevtags = ''
240 if local:
240 if local:
241 try:
241 try:
242 fp = self.opener('localtags', 'r+')
242 fp = self.opener('localtags', 'r+')
243 except IOError:
243 except IOError:
244 fp = self.opener('localtags', 'a')
244 fp = self.opener('localtags', 'a')
245 else:
245 else:
246 prevtags = fp.read()
246 prevtags = fp.read()
247
247
248 # local tags are stored in the current charset
248 # local tags are stored in the current charset
249 writetags(fp, names, None, prevtags)
249 writetags(fp, names, None, prevtags)
250 for name in names:
250 for name in names:
251 self.hook('tag', node=hex(node), tag=name, local=local)
251 self.hook('tag', node=hex(node), tag=name, local=local)
252 return
252 return
253
253
254 try:
254 try:
255 fp = self.wfile('.hgtags', 'rb+')
255 fp = self.wfile('.hgtags', 'rb+')
256 except IOError:
256 except IOError:
257 fp = self.wfile('.hgtags', 'ab')
257 fp = self.wfile('.hgtags', 'ab')
258 else:
258 else:
259 prevtags = fp.read()
259 prevtags = fp.read()
260
260
261 # committed tags are stored in UTF-8
261 # committed tags are stored in UTF-8
262 writetags(fp, names, encoding.fromlocal, prevtags)
262 writetags(fp, names, encoding.fromlocal, prevtags)
263
263
264 if '.hgtags' not in self.dirstate:
264 if '.hgtags' not in self.dirstate:
265 self[None].add(['.hgtags'])
265 self[None].add(['.hgtags'])
266
266
267 m = matchmod.exact(self.root, '', ['.hgtags'])
267 m = matchmod.exact(self.root, '', ['.hgtags'])
268 tagnode = self.commit(message, user, date, extra=extra, match=m)
268 tagnode = self.commit(message, user, date, extra=extra, match=m)
269
269
270 for name in names:
270 for name in names:
271 self.hook('tag', node=hex(node), tag=name, local=local)
271 self.hook('tag', node=hex(node), tag=name, local=local)
272
272
273 return tagnode
273 return tagnode
274
274
275 def tag(self, names, node, message, local, user, date):
275 def tag(self, names, node, message, local, user, date):
276 '''tag a revision with one or more symbolic names.
276 '''tag a revision with one or more symbolic names.
277
277
278 names is a list of strings or, when adding a single tag, names may be a
278 names is a list of strings or, when adding a single tag, names may be a
279 string.
279 string.
280
280
281 if local is True, the tags are stored in a per-repository file.
281 if local is True, the tags are stored in a per-repository file.
282 otherwise, they are stored in the .hgtags file, and a new
282 otherwise, they are stored in the .hgtags file, and a new
283 changeset is committed with the change.
283 changeset is committed with the change.
284
284
285 keyword arguments:
285 keyword arguments:
286
286
287 local: whether to store tags in non-version-controlled file
287 local: whether to store tags in non-version-controlled file
288 (default False)
288 (default False)
289
289
290 message: commit message to use if committing
290 message: commit message to use if committing
291
291
292 user: name of user to use if committing
292 user: name of user to use if committing
293
293
294 date: date tuple to use if committing'''
294 date: date tuple to use if committing'''
295
295
296 for x in self.status()[:5]:
296 for x in self.status()[:5]:
297 if '.hgtags' in x:
297 if '.hgtags' in x:
298 raise util.Abort(_('working copy of .hgtags is changed '
298 raise util.Abort(_('working copy of .hgtags is changed '
299 '(please commit .hgtags manually)'))
299 '(please commit .hgtags manually)'))
300
300
301 self.tags() # instantiate the cache
301 self.tags() # instantiate the cache
302 self._tag(names, node, message, local, user, date)
302 self._tag(names, node, message, local, user, date)
303
303
304 def tags(self):
304 def tags(self):
305 '''return a mapping of tag to node'''
305 '''return a mapping of tag to node'''
306 if self._tags is None:
306 if self._tags is None:
307 (self._tags, self._tagtypes) = self._findtags()
307 (self._tags, self._tagtypes) = self._findtags()
308
308
309 return self._tags
309 return self._tags
310
310
311 def _findtags(self):
311 def _findtags(self):
312 '''Do the hard work of finding tags. Return a pair of dicts
312 '''Do the hard work of finding tags. Return a pair of dicts
313 (tags, tagtypes) where tags maps tag name to node, and tagtypes
313 (tags, tagtypes) where tags maps tag name to node, and tagtypes
314 maps tag name to a string like \'global\' or \'local\'.
314 maps tag name to a string like \'global\' or \'local\'.
315 Subclasses or extensions are free to add their own tags, but
315 Subclasses or extensions are free to add their own tags, but
316 should be aware that the returned dicts will be retained for the
316 should be aware that the returned dicts will be retained for the
317 duration of the localrepo object.'''
317 duration of the localrepo object.'''
318
318
319 # XXX what tagtype should subclasses/extensions use? Currently
319 # XXX what tagtype should subclasses/extensions use? Currently
320 # mq and bookmarks add tags, but do not set the tagtype at all.
320 # mq and bookmarks add tags, but do not set the tagtype at all.
321 # Should each extension invent its own tag type? Should there
321 # Should each extension invent its own tag type? Should there
322 # be one tagtype for all such "virtual" tags? Or is the status
322 # be one tagtype for all such "virtual" tags? Or is the status
323 # quo fine?
323 # quo fine?
324
324
325 alltags = {} # map tag name to (node, hist)
325 alltags = {} # map tag name to (node, hist)
326 tagtypes = {}
326 tagtypes = {}
327
327
328 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
328 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
329 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
329 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
330
330
331 # Build the return dicts. Have to re-encode tag names because
331 # Build the return dicts. Have to re-encode tag names because
332 # the tags module always uses UTF-8 (in order not to lose info
332 # the tags module always uses UTF-8 (in order not to lose info
333 # writing to the cache), but the rest of Mercurial wants them in
333 # writing to the cache), but the rest of Mercurial wants them in
334 # local encoding.
334 # local encoding.
335 tags = {}
335 tags = {}
336 for (name, (node, hist)) in alltags.iteritems():
336 for (name, (node, hist)) in alltags.iteritems():
337 if node != nullid:
337 if node != nullid:
338 tags[encoding.tolocal(name)] = node
338 tags[encoding.tolocal(name)] = node
339 tags['tip'] = self.changelog.tip()
339 tags['tip'] = self.changelog.tip()
340 tagtypes = dict([(encoding.tolocal(name), value)
340 tagtypes = dict([(encoding.tolocal(name), value)
341 for (name, value) in tagtypes.iteritems()])
341 for (name, value) in tagtypes.iteritems()])
342 return (tags, tagtypes)
342 return (tags, tagtypes)
343
343
344 def tagtype(self, tagname):
344 def tagtype(self, tagname):
345 '''
345 '''
346 return the type of the given tag. result can be:
346 return the type of the given tag. result can be:
347
347
348 'local' : a local tag
348 'local' : a local tag
349 'global' : a global tag
349 'global' : a global tag
350 None : tag does not exist
350 None : tag does not exist
351 '''
351 '''
352
352
353 self.tags()
353 self.tags()
354
354
355 return self._tagtypes.get(tagname)
355 return self._tagtypes.get(tagname)
356
356
357 def tagslist(self):
357 def tagslist(self):
358 '''return a list of tags ordered by revision'''
358 '''return a list of tags ordered by revision'''
359 l = []
359 l = []
360 for t, n in self.tags().iteritems():
360 for t, n in self.tags().iteritems():
361 try:
361 try:
362 r = self.changelog.rev(n)
362 r = self.changelog.rev(n)
363 except:
363 except:
364 r = -2 # sort to the beginning of the list if unknown
364 r = -2 # sort to the beginning of the list if unknown
365 l.append((r, t, n))
365 l.append((r, t, n))
366 return [(t, n) for r, t, n in sorted(l)]
366 return [(t, n) for r, t, n in sorted(l)]
367
367
368 def nodetags(self, node):
368 def nodetags(self, node):
369 '''return the tags associated with a node'''
369 '''return the tags associated with a node'''
370 if not self.nodetagscache:
370 if not self.nodetagscache:
371 self.nodetagscache = {}
371 self.nodetagscache = {}
372 for t, n in self.tags().iteritems():
372 for t, n in self.tags().iteritems():
373 self.nodetagscache.setdefault(n, []).append(t)
373 self.nodetagscache.setdefault(n, []).append(t)
374 for tags in self.nodetagscache.itervalues():
374 for tags in self.nodetagscache.itervalues():
375 tags.sort()
375 tags.sort()
376 return self.nodetagscache.get(node, [])
376 return self.nodetagscache.get(node, [])
377
377
378 def _branchtags(self, partial, lrev):
378 def _branchtags(self, partial, lrev):
379 # TODO: rename this function?
379 # TODO: rename this function?
380 tiprev = len(self) - 1
380 tiprev = len(self) - 1
381 if lrev != tiprev:
381 if lrev != tiprev:
382 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
382 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
383 self._updatebranchcache(partial, ctxgen)
383 self._updatebranchcache(partial, ctxgen)
384 self._writebranchcache(partial, self.changelog.tip(), tiprev)
384 self._writebranchcache(partial, self.changelog.tip(), tiprev)
385
385
386 return partial
386 return partial
387
387
388 def updatebranchcache(self):
388 def updatebranchcache(self):
389 tip = self.changelog.tip()
389 tip = self.changelog.tip()
390 if self._branchcache is not None and self._branchcachetip == tip:
390 if self._branchcache is not None and self._branchcachetip == tip:
391 return self._branchcache
391 return self._branchcache
392
392
393 oldtip = self._branchcachetip
393 oldtip = self._branchcachetip
394 self._branchcachetip = tip
394 self._branchcachetip = tip
395 if oldtip is None or oldtip not in self.changelog.nodemap:
395 if oldtip is None or oldtip not in self.changelog.nodemap:
396 partial, last, lrev = self._readbranchcache()
396 partial, last, lrev = self._readbranchcache()
397 else:
397 else:
398 lrev = self.changelog.rev(oldtip)
398 lrev = self.changelog.rev(oldtip)
399 partial = self._branchcache
399 partial = self._branchcache
400
400
401 self._branchtags(partial, lrev)
401 self._branchtags(partial, lrev)
402 # this private cache holds all heads (not just tips)
402 # this private cache holds all heads (not just tips)
403 self._branchcache = partial
403 self._branchcache = partial
404
404
405 def branchmap(self):
405 def branchmap(self):
406 '''returns a dictionary {branch: [branchheads]}'''
406 '''returns a dictionary {branch: [branchheads]}'''
407 self.updatebranchcache()
407 self.updatebranchcache()
408 return self._branchcache
408 return self._branchcache
409
409
410 def branchtags(self):
410 def branchtags(self):
411 '''return a dict where branch names map to the tipmost head of
411 '''return a dict where branch names map to the tipmost head of
412 the branch, open heads come before closed'''
412 the branch, open heads come before closed'''
413 bt = {}
413 bt = {}
414 for bn, heads in self.branchmap().iteritems():
414 for bn, heads in self.branchmap().iteritems():
415 tip = heads[-1]
415 tip = heads[-1]
416 for h in reversed(heads):
416 for h in reversed(heads):
417 if 'close' not in self.changelog.read(h)[5]:
417 if 'close' not in self.changelog.read(h)[5]:
418 tip = h
418 tip = h
419 break
419 break
420 bt[bn] = tip
420 bt[bn] = tip
421 return bt
421 return bt
422
422
423
423
424 def _readbranchcache(self):
424 def _readbranchcache(self):
425 partial = {}
425 partial = {}
426 try:
426 try:
427 f = self.opener("branchheads.cache")
427 f = self.opener("branchheads.cache")
428 lines = f.read().split('\n')
428 lines = f.read().split('\n')
429 f.close()
429 f.close()
430 except (IOError, OSError):
430 except (IOError, OSError):
431 return {}, nullid, nullrev
431 return {}, nullid, nullrev
432
432
433 try:
433 try:
434 last, lrev = lines.pop(0).split(" ", 1)
434 last, lrev = lines.pop(0).split(" ", 1)
435 last, lrev = bin(last), int(lrev)
435 last, lrev = bin(last), int(lrev)
436 if lrev >= len(self) or self[lrev].node() != last:
436 if lrev >= len(self) or self[lrev].node() != last:
437 # invalidate the cache
437 # invalidate the cache
438 raise ValueError('invalidating branch cache (tip differs)')
438 raise ValueError('invalidating branch cache (tip differs)')
439 for l in lines:
439 for l in lines:
440 if not l:
440 if not l:
441 continue
441 continue
442 node, label = l.split(" ", 1)
442 node, label = l.split(" ", 1)
443 partial.setdefault(label.strip(), []).append(bin(node))
443 partial.setdefault(label.strip(), []).append(bin(node))
444 except KeyboardInterrupt:
444 except KeyboardInterrupt:
445 raise
445 raise
446 except Exception, inst:
446 except Exception, inst:
447 if self.ui.debugflag:
447 if self.ui.debugflag:
448 self.ui.warn(str(inst), '\n')
448 self.ui.warn(str(inst), '\n')
449 partial, last, lrev = {}, nullid, nullrev
449 partial, last, lrev = {}, nullid, nullrev
450 return partial, last, lrev
450 return partial, last, lrev
451
451
452 def _writebranchcache(self, branches, tip, tiprev):
452 def _writebranchcache(self, branches, tip, tiprev):
453 try:
453 try:
454 f = self.opener("branchheads.cache", "w", atomictemp=True)
454 f = self.opener("branchheads.cache", "w", atomictemp=True)
455 f.write("%s %s\n" % (hex(tip), tiprev))
455 f.write("%s %s\n" % (hex(tip), tiprev))
456 for label, nodes in branches.iteritems():
456 for label, nodes in branches.iteritems():
457 for node in nodes:
457 for node in nodes:
458 f.write("%s %s\n" % (hex(node), label))
458 f.write("%s %s\n" % (hex(node), label))
459 f.rename()
459 f.rename()
460 except (IOError, OSError):
460 except (IOError, OSError):
461 pass
461 pass
462
462
463 def _updatebranchcache(self, partial, ctxgen):
463 def _updatebranchcache(self, partial, ctxgen):
464 # collect new branch entries
464 # collect new branch entries
465 newbranches = {}
465 newbranches = {}
466 for c in ctxgen:
466 for c in ctxgen:
467 newbranches.setdefault(c.branch(), []).append(c.node())
467 newbranches.setdefault(c.branch(), []).append(c.node())
468 # if older branchheads are reachable from new ones, they aren't
468 # if older branchheads are reachable from new ones, they aren't
469 # really branchheads. Note checking parents is insufficient:
469 # really branchheads. Note checking parents is insufficient:
470 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
470 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
471 for branch, newnodes in newbranches.iteritems():
471 for branch, newnodes in newbranches.iteritems():
472 bheads = partial.setdefault(branch, [])
472 bheads = partial.setdefault(branch, [])
473 bheads.extend(newnodes)
473 bheads.extend(newnodes)
474 if len(bheads) <= 1:
474 if len(bheads) <= 1:
475 continue
475 continue
476 # starting from tip means fewer passes over reachable
476 # starting from tip means fewer passes over reachable
477 while newnodes:
477 while newnodes:
478 latest = newnodes.pop()
478 latest = newnodes.pop()
479 if latest not in bheads:
479 if latest not in bheads:
480 continue
480 continue
481 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
481 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
482 reachable = self.changelog.reachable(latest, minbhrev)
482 reachable = self.changelog.reachable(latest, minbhrev)
483 reachable.remove(latest)
483 reachable.remove(latest)
484 bheads = [b for b in bheads if b not in reachable]
484 bheads = [b for b in bheads if b not in reachable]
485 partial[branch] = bheads
485 partial[branch] = bheads
486
486
487 def lookup(self, key):
487 def lookup(self, key):
488 if isinstance(key, int):
488 if isinstance(key, int):
489 return self.changelog.node(key)
489 return self.changelog.node(key)
490 elif key == '.':
490 elif key == '.':
491 return self.dirstate.parents()[0]
491 return self.dirstate.parents()[0]
492 elif key == 'null':
492 elif key == 'null':
493 return nullid
493 return nullid
494 elif key == 'tip':
494 elif key == 'tip':
495 return self.changelog.tip()
495 return self.changelog.tip()
496 n = self.changelog._match(key)
496 n = self.changelog._match(key)
497 if n:
497 if n:
498 return n
498 return n
499 if key in self.tags():
499 if key in self.tags():
500 return self.tags()[key]
500 return self.tags()[key]
501 if key in self.branchtags():
501 if key in self.branchtags():
502 return self.branchtags()[key]
502 return self.branchtags()[key]
503 n = self.changelog._partialmatch(key)
503 n = self.changelog._partialmatch(key)
504 if n:
504 if n:
505 return n
505 return n
506
506
507 # can't find key, check if it might have come from damaged dirstate
507 # can't find key, check if it might have come from damaged dirstate
508 if key in self.dirstate.parents():
508 if key in self.dirstate.parents():
509 raise error.Abort(_("working directory has unknown parent '%s'!")
509 raise error.Abort(_("working directory has unknown parent '%s'!")
510 % short(key))
510 % short(key))
511 try:
511 try:
512 if len(key) == 20:
512 if len(key) == 20:
513 key = hex(key)
513 key = hex(key)
514 except:
514 except:
515 pass
515 pass
516 raise error.RepoLookupError(_("unknown revision '%s'") % key)
516 raise error.RepoLookupError(_("unknown revision '%s'") % key)
517
517
518 def lookupbranch(self, key, remote=None):
518 def lookupbranch(self, key, remote=None):
519 repo = remote or self
519 repo = remote or self
520 if key in repo.branchmap():
520 if key in repo.branchmap():
521 return key
521 return key
522
522
523 repo = (remote and remote.local()) and remote or self
523 repo = (remote and remote.local()) and remote or self
524 return repo[key].branch()
524 return repo[key].branch()
525
525
526 def local(self):
526 def local(self):
527 return True
527 return True
528
528
529 def join(self, f):
529 def join(self, f):
530 return os.path.join(self.path, f)
530 return os.path.join(self.path, f)
531
531
532 def wjoin(self, f):
532 def wjoin(self, f):
533 return os.path.join(self.root, f)
533 return os.path.join(self.root, f)
534
534
535 def file(self, f):
535 def file(self, f):
536 if f[0] == '/':
536 if f[0] == '/':
537 f = f[1:]
537 f = f[1:]
538 return filelog.filelog(self.sopener, f)
538 return filelog.filelog(self.sopener, f)
539
539
540 def changectx(self, changeid):
540 def changectx(self, changeid):
541 return self[changeid]
541 return self[changeid]
542
542
543 def parents(self, changeid=None):
543 def parents(self, changeid=None):
544 '''get list of changectxs for parents of changeid'''
544 '''get list of changectxs for parents of changeid'''
545 return self[changeid].parents()
545 return self[changeid].parents()
546
546
547 def filectx(self, path, changeid=None, fileid=None):
547 def filectx(self, path, changeid=None, fileid=None):
548 """changeid can be a changeset revision, node, or tag.
548 """changeid can be a changeset revision, node, or tag.
549 fileid can be a file revision or node."""
549 fileid can be a file revision or node."""
550 return context.filectx(self, path, changeid, fileid)
550 return context.filectx(self, path, changeid, fileid)
551
551
552 def getcwd(self):
552 def getcwd(self):
553 return self.dirstate.getcwd()
553 return self.dirstate.getcwd()
554
554
555 def pathto(self, f, cwd=None):
555 def pathto(self, f, cwd=None):
556 return self.dirstate.pathto(f, cwd)
556 return self.dirstate.pathto(f, cwd)
557
557
558 def wfile(self, f, mode='r'):
558 def wfile(self, f, mode='r'):
559 return self.wopener(f, mode)
559 return self.wopener(f, mode)
560
560
561 def _link(self, f):
561 def _link(self, f):
562 return os.path.islink(self.wjoin(f))
562 return os.path.islink(self.wjoin(f))
563
563
564 def _loadfilter(self, filter):
564 def _loadfilter(self, filter):
565 if filter not in self.filterpats:
565 if filter not in self.filterpats:
566 l = []
566 l = []
567 for pat, cmd in self.ui.configitems(filter):
567 for pat, cmd in self.ui.configitems(filter):
568 if cmd == '!':
568 if cmd == '!':
569 continue
569 continue
570 mf = matchmod.match(self.root, '', [pat])
570 mf = matchmod.match(self.root, '', [pat])
571 fn = None
571 fn = None
572 params = cmd
572 params = cmd
573 for name, filterfn in self._datafilters.iteritems():
573 for name, filterfn in self._datafilters.iteritems():
574 if cmd.startswith(name):
574 if cmd.startswith(name):
575 fn = filterfn
575 fn = filterfn
576 params = cmd[len(name):].lstrip()
576 params = cmd[len(name):].lstrip()
577 break
577 break
578 if not fn:
578 if not fn:
579 fn = lambda s, c, **kwargs: util.filter(s, c)
579 fn = lambda s, c, **kwargs: util.filter(s, c)
580 # Wrap old filters not supporting keyword arguments
580 # Wrap old filters not supporting keyword arguments
581 if not inspect.getargspec(fn)[2]:
581 if not inspect.getargspec(fn)[2]:
582 oldfn = fn
582 oldfn = fn
583 fn = lambda s, c, **kwargs: oldfn(s, c)
583 fn = lambda s, c, **kwargs: oldfn(s, c)
584 l.append((mf, fn, params))
584 l.append((mf, fn, params))
585 self.filterpats[filter] = l
585 self.filterpats[filter] = l
586
586
587 def _filter(self, filter, filename, data):
587 def _filter(self, filter, filename, data):
588 self._loadfilter(filter)
588 self._loadfilter(filter)
589
589
590 for mf, fn, cmd in self.filterpats[filter]:
590 for mf, fn, cmd in self.filterpats[filter]:
591 if mf(filename):
591 if mf(filename):
592 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
592 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
593 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
593 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
594 break
594 break
595
595
596 return data
596 return data
597
597
598 def adddatafilter(self, name, filter):
598 def adddatafilter(self, name, filter):
599 self._datafilters[name] = filter
599 self._datafilters[name] = filter
600
600
601 def wread(self, filename):
601 def wread(self, filename):
602 if self._link(filename):
602 if self._link(filename):
603 data = os.readlink(self.wjoin(filename))
603 data = os.readlink(self.wjoin(filename))
604 else:
604 else:
605 data = self.wopener(filename, 'r').read()
605 data = self.wopener(filename, 'r').read()
606 return self._filter("encode", filename, data)
606 return self._filter("encode", filename, data)
607
607
608 def wwrite(self, filename, data, flags):
608 def wwrite(self, filename, data, flags):
609 data = self._filter("decode", filename, data)
609 data = self._filter("decode", filename, data)
610 try:
610 try:
611 os.unlink(self.wjoin(filename))
611 os.unlink(self.wjoin(filename))
612 except OSError:
612 except OSError:
613 pass
613 pass
614 if 'l' in flags:
614 if 'l' in flags:
615 self.wopener.symlink(data, filename)
615 self.wopener.symlink(data, filename)
616 else:
616 else:
617 self.wopener(filename, 'w').write(data)
617 self.wopener(filename, 'w').write(data)
618 if 'x' in flags:
618 if 'x' in flags:
619 util.set_flags(self.wjoin(filename), False, True)
619 util.set_flags(self.wjoin(filename), False, True)
620
620
621 def wwritedata(self, filename, data):
621 def wwritedata(self, filename, data):
622 return self._filter("decode", filename, data)
622 return self._filter("decode", filename, data)
623
623
624 def transaction(self, desc):
624 def transaction(self, desc):
625 tr = self._transref and self._transref() or None
625 tr = self._transref and self._transref() or None
626 if tr and tr.running():
626 if tr and tr.running():
627 return tr.nest()
627 return tr.nest()
628
628
629 # abort here if the journal already exists
629 # abort here if the journal already exists
630 if os.path.exists(self.sjoin("journal")):
630 if os.path.exists(self.sjoin("journal")):
631 raise error.RepoError(
631 raise error.RepoError(
632 _("abandoned transaction found - run hg recover"))
632 _("abandoned transaction found - run hg recover"))
633
633
634 # save dirstate for rollback
634 # save dirstate for rollback
635 try:
635 try:
636 ds = self.opener("dirstate").read()
636 ds = self.opener("dirstate").read()
637 except IOError:
637 except IOError:
638 ds = ""
638 ds = ""
639 self.opener("journal.dirstate", "w").write(ds)
639 self.opener("journal.dirstate", "w").write(ds)
640 self.opener("journal.branch", "w").write(self.dirstate.branch())
640 self.opener("journal.branch", "w").write(self.dirstate.branch())
641 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
641 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
642
642
643 renames = [(self.sjoin("journal"), self.sjoin("undo")),
643 renames = [(self.sjoin("journal"), self.sjoin("undo")),
644 (self.join("journal.dirstate"), self.join("undo.dirstate")),
644 (self.join("journal.dirstate"), self.join("undo.dirstate")),
645 (self.join("journal.branch"), self.join("undo.branch")),
645 (self.join("journal.branch"), self.join("undo.branch")),
646 (self.join("journal.desc"), self.join("undo.desc"))]
646 (self.join("journal.desc"), self.join("undo.desc"))]
647 tr = transaction.transaction(self.ui.warn, self.sopener,
647 tr = transaction.transaction(self.ui.warn, self.sopener,
648 self.sjoin("journal"),
648 self.sjoin("journal"),
649 aftertrans(renames),
649 aftertrans(renames),
650 self.store.createmode)
650 self.store.createmode)
651 self._transref = weakref.ref(tr)
651 self._transref = weakref.ref(tr)
652 return tr
652 return tr
653
653
654 def recover(self):
654 def recover(self):
655 lock = self.lock()
655 lock = self.lock()
656 try:
656 try:
657 if os.path.exists(self.sjoin("journal")):
657 if os.path.exists(self.sjoin("journal")):
658 self.ui.status(_("rolling back interrupted transaction\n"))
658 self.ui.status(_("rolling back interrupted transaction\n"))
659 transaction.rollback(self.sopener, self.sjoin("journal"),
659 transaction.rollback(self.sopener, self.sjoin("journal"),
660 self.ui.warn)
660 self.ui.warn)
661 self.invalidate()
661 self.invalidate()
662 return True
662 return True
663 else:
663 else:
664 self.ui.warn(_("no interrupted transaction available\n"))
664 self.ui.warn(_("no interrupted transaction available\n"))
665 return False
665 return False
666 finally:
666 finally:
667 lock.release()
667 lock.release()
668
668
669 def rollback(self, dryrun=False):
669 def rollback(self, dryrun=False):
670 wlock = lock = None
670 wlock = lock = None
671 try:
671 try:
672 wlock = self.wlock()
672 wlock = self.wlock()
673 lock = self.lock()
673 lock = self.lock()
674 if os.path.exists(self.sjoin("undo")):
674 if os.path.exists(self.sjoin("undo")):
675 try:
675 try:
676 args = self.opener("undo.desc", "r").read().splitlines()
676 args = self.opener("undo.desc", "r").read().splitlines()
677 if len(args) >= 3 and self.ui.verbose:
677 if len(args) >= 3 and self.ui.verbose:
678 desc = _("rolling back to revision %s"
678 desc = _("rolling back to revision %s"
679 " (undo %s: %s)\n") % (
679 " (undo %s: %s)\n") % (
680 int(args[0]) - 1, args[1], args[2])
680 int(args[0]) - 1, args[1], args[2])
681 elif len(args) >= 2:
681 elif len(args) >= 2:
682 desc = _("rolling back to revision %s (undo %s)\n") % (
682 desc = _("rolling back to revision %s (undo %s)\n") % (
683 int(args[0]) - 1, args[1])
683 int(args[0]) - 1, args[1])
684 except IOError:
684 except IOError:
685 desc = _("rolling back unknown transaction\n")
685 desc = _("rolling back unknown transaction\n")
686 self.ui.status(desc)
686 self.ui.status(desc)
687 if dryrun:
687 if dryrun:
688 return
688 return
689 transaction.rollback(self.sopener, self.sjoin("undo"),
689 transaction.rollback(self.sopener, self.sjoin("undo"),
690 self.ui.warn)
690 self.ui.warn)
691 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
691 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
692 try:
692 try:
693 branch = self.opener("undo.branch").read()
693 branch = self.opener("undo.branch").read()
694 self.dirstate.setbranch(branch)
694 self.dirstate.setbranch(branch)
695 except IOError:
695 except IOError:
696 self.ui.warn(_("Named branch could not be reset, "
696 self.ui.warn(_("Named branch could not be reset, "
697 "current branch still is: %s\n")
697 "current branch still is: %s\n")
698 % encoding.tolocal(self.dirstate.branch()))
698 % encoding.tolocal(self.dirstate.branch()))
699 self.invalidate()
699 self.invalidate()
700 self.dirstate.invalidate()
700 self.dirstate.invalidate()
701 self.destroyed()
701 self.destroyed()
702 else:
702 else:
703 self.ui.warn(_("no rollback information available\n"))
703 self.ui.warn(_("no rollback information available\n"))
704 return 1
704 return 1
705 finally:
705 finally:
706 release(lock, wlock)
706 release(lock, wlock)
707
707
708 def invalidatecaches(self):
708 def invalidatecaches(self):
709 self._tags = None
709 self._tags = None
710 self._tagtypes = None
710 self._tagtypes = None
711 self.nodetagscache = None
711 self.nodetagscache = None
712 self._branchcache = None # in UTF-8
712 self._branchcache = None # in UTF-8
713 self._branchcachetip = None
713 self._branchcachetip = None
714
714
715 def invalidate(self):
715 def invalidate(self):
716 for a in "changelog manifest".split():
716 for a in "changelog manifest".split():
717 if a in self.__dict__:
717 if a in self.__dict__:
718 delattr(self, a)
718 delattr(self, a)
719 self.invalidatecaches()
719 self.invalidatecaches()
720
720
721 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
721 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
722 try:
722 try:
723 l = lock.lock(lockname, 0, releasefn, desc=desc)
723 l = lock.lock(lockname, 0, releasefn, desc=desc)
724 except error.LockHeld, inst:
724 except error.LockHeld, inst:
725 if not wait:
725 if not wait:
726 raise
726 raise
727 self.ui.warn(_("waiting for lock on %s held by %r\n") %
727 self.ui.warn(_("waiting for lock on %s held by %r\n") %
728 (desc, inst.locker))
728 (desc, inst.locker))
729 # default to 600 seconds timeout
729 # default to 600 seconds timeout
730 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
730 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
731 releasefn, desc=desc)
731 releasefn, desc=desc)
732 if acquirefn:
732 if acquirefn:
733 acquirefn()
733 acquirefn()
734 return l
734 return l
735
735
736 def lock(self, wait=True):
736 def lock(self, wait=True):
737 '''Lock the repository store (.hg/store) and return a weak reference
737 '''Lock the repository store (.hg/store) and return a weak reference
738 to the lock. Use this before modifying the store (e.g. committing or
738 to the lock. Use this before modifying the store (e.g. committing or
739 stripping). If you are opening a transaction, get a lock as well.)'''
739 stripping). If you are opening a transaction, get a lock as well.)'''
740 l = self._lockref and self._lockref()
740 l = self._lockref and self._lockref()
741 if l is not None and l.held:
741 if l is not None and l.held:
742 l.lock()
742 l.lock()
743 return l
743 return l
744
744
745 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
745 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
746 _('repository %s') % self.origroot)
746 _('repository %s') % self.origroot)
747 self._lockref = weakref.ref(l)
747 self._lockref = weakref.ref(l)
748 return l
748 return l
749
749
750 def wlock(self, wait=True):
750 def wlock(self, wait=True):
751 '''Lock the non-store parts of the repository (everything under
751 '''Lock the non-store parts of the repository (everything under
752 .hg except .hg/store) and return a weak reference to the lock.
752 .hg except .hg/store) and return a weak reference to the lock.
753 Use this before modifying files in .hg.'''
753 Use this before modifying files in .hg.'''
754 l = self._wlockref and self._wlockref()
754 l = self._wlockref and self._wlockref()
755 if l is not None and l.held:
755 if l is not None and l.held:
756 l.lock()
756 l.lock()
757 return l
757 return l
758
758
759 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
759 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
760 self.dirstate.invalidate, _('working directory of %s') %
760 self.dirstate.invalidate, _('working directory of %s') %
761 self.origroot)
761 self.origroot)
762 self._wlockref = weakref.ref(l)
762 self._wlockref = weakref.ref(l)
763 return l
763 return l
764
764
765 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
765 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
766 """
766 """
767 commit an individual file as part of a larger transaction
767 commit an individual file as part of a larger transaction
768 """
768 """
769
769
770 fname = fctx.path()
770 fname = fctx.path()
771 text = fctx.data()
771 text = fctx.data()
772 flog = self.file(fname)
772 flog = self.file(fname)
773 fparent1 = manifest1.get(fname, nullid)
773 fparent1 = manifest1.get(fname, nullid)
774 fparent2 = fparent2o = manifest2.get(fname, nullid)
774 fparent2 = fparent2o = manifest2.get(fname, nullid)
775
775
776 meta = {}
776 meta = {}
777 copy = fctx.renamed()
777 copy = fctx.renamed()
778 if copy and copy[0] != fname:
778 if copy and copy[0] != fname:
779 # Mark the new revision of this file as a copy of another
779 # Mark the new revision of this file as a copy of another
780 # file. This copy data will effectively act as a parent
780 # file. This copy data will effectively act as a parent
781 # of this new revision. If this is a merge, the first
781 # of this new revision. If this is a merge, the first
782 # parent will be the nullid (meaning "look up the copy data")
782 # parent will be the nullid (meaning "look up the copy data")
783 # and the second one will be the other parent. For example:
783 # and the second one will be the other parent. For example:
784 #
784 #
785 # 0 --- 1 --- 3 rev1 changes file foo
785 # 0 --- 1 --- 3 rev1 changes file foo
786 # \ / rev2 renames foo to bar and changes it
786 # \ / rev2 renames foo to bar and changes it
787 # \- 2 -/ rev3 should have bar with all changes and
787 # \- 2 -/ rev3 should have bar with all changes and
788 # should record that bar descends from
788 # should record that bar descends from
789 # bar in rev2 and foo in rev1
789 # bar in rev2 and foo in rev1
790 #
790 #
791 # this allows this merge to succeed:
791 # this allows this merge to succeed:
792 #
792 #
793 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
793 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
794 # \ / merging rev3 and rev4 should use bar@rev2
794 # \ / merging rev3 and rev4 should use bar@rev2
795 # \- 2 --- 4 as the merge base
795 # \- 2 --- 4 as the merge base
796 #
796 #
797
797
798 cfname = copy[0]
798 cfname = copy[0]
799 crev = manifest1.get(cfname)
799 crev = manifest1.get(cfname)
800 newfparent = fparent2
800 newfparent = fparent2
801
801
802 if manifest2: # branch merge
802 if manifest2: # branch merge
803 if fparent2 == nullid or crev is None: # copied on remote side
803 if fparent2 == nullid or crev is None: # copied on remote side
804 if cfname in manifest2:
804 if cfname in manifest2:
805 crev = manifest2[cfname]
805 crev = manifest2[cfname]
806 newfparent = fparent1
806 newfparent = fparent1
807
807
808 # find source in nearest ancestor if we've lost track
808 # find source in nearest ancestor if we've lost track
809 if not crev:
809 if not crev:
810 self.ui.debug(" %s: searching for copy revision for %s\n" %
810 self.ui.debug(" %s: searching for copy revision for %s\n" %
811 (fname, cfname))
811 (fname, cfname))
812 for ancestor in self['.'].ancestors():
812 for ancestor in self['.'].ancestors():
813 if cfname in ancestor:
813 if cfname in ancestor:
814 crev = ancestor[cfname].filenode()
814 crev = ancestor[cfname].filenode()
815 break
815 break
816
816
817 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
817 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
818 meta["copy"] = cfname
818 meta["copy"] = cfname
819 meta["copyrev"] = hex(crev)
819 meta["copyrev"] = hex(crev)
820 fparent1, fparent2 = nullid, newfparent
820 fparent1, fparent2 = nullid, newfparent
821 elif fparent2 != nullid:
821 elif fparent2 != nullid:
822 # is one parent an ancestor of the other?
822 # is one parent an ancestor of the other?
823 fparentancestor = flog.ancestor(fparent1, fparent2)
823 fparentancestor = flog.ancestor(fparent1, fparent2)
824 if fparentancestor == fparent1:
824 if fparentancestor == fparent1:
825 fparent1, fparent2 = fparent2, nullid
825 fparent1, fparent2 = fparent2, nullid
826 elif fparentancestor == fparent2:
826 elif fparentancestor == fparent2:
827 fparent2 = nullid
827 fparent2 = nullid
828
828
829 # is the file changed?
829 # is the file changed?
830 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
830 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
831 changelist.append(fname)
831 changelist.append(fname)
832 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
832 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
833
833
834 # are just the flags changed during merge?
834 # are just the flags changed during merge?
835 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
835 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
836 changelist.append(fname)
836 changelist.append(fname)
837
837
838 return fparent1
838 return fparent1
839
839
840 def commit(self, text="", user=None, date=None, match=None, force=False,
840 def commit(self, text="", user=None, date=None, match=None, force=False,
841 editor=False, extra={}):
841 editor=False, extra={}):
842 """Add a new revision to current repository.
842 """Add a new revision to current repository.
843
843
844 Revision information is gathered from the working directory,
844 Revision information is gathered from the working directory,
845 match can be used to filter the committed files. If editor is
845 match can be used to filter the committed files. If editor is
846 supplied, it is called to get a commit message.
846 supplied, it is called to get a commit message.
847 """
847 """
848
848
849 def fail(f, msg):
849 def fail(f, msg):
850 raise util.Abort('%s: %s' % (f, msg))
850 raise util.Abort('%s: %s' % (f, msg))
851
851
852 if not match:
852 if not match:
853 match = matchmod.always(self.root, '')
853 match = matchmod.always(self.root, '')
854
854
855 if not force:
855 if not force:
856 vdirs = []
856 vdirs = []
857 match.dir = vdirs.append
857 match.dir = vdirs.append
858 match.bad = fail
858 match.bad = fail
859
859
860 wlock = self.wlock()
860 wlock = self.wlock()
861 try:
861 try:
862 wctx = self[None]
862 wctx = self[None]
863 merge = len(wctx.parents()) > 1
863 merge = len(wctx.parents()) > 1
864
864
865 if (not force and merge and match and
865 if (not force and merge and match and
866 (match.files() or match.anypats())):
866 (match.files() or match.anypats())):
867 raise util.Abort(_('cannot partially commit a merge '
867 raise util.Abort(_('cannot partially commit a merge '
868 '(do not specify files or patterns)'))
868 '(do not specify files or patterns)'))
869
869
870 changes = self.status(match=match, clean=force)
870 changes = self.status(match=match, clean=force)
871 if force:
871 if force:
872 changes[0].extend(changes[6]) # mq may commit unchanged files
872 changes[0].extend(changes[6]) # mq may commit unchanged files
873
873
874 # check subrepos
874 # check subrepos
875 subs = []
875 subs = []
876 removedsubs = set()
876 removedsubs = set()
877 for p in wctx.parents():
877 for p in wctx.parents():
878 removedsubs.update(s for s in p.substate if match(s))
878 removedsubs.update(s for s in p.substate if match(s))
879 for s in wctx.substate:
879 for s in wctx.substate:
880 removedsubs.discard(s)
880 removedsubs.discard(s)
881 if match(s) and wctx.sub(s).dirty():
881 if match(s) and wctx.sub(s).dirty():
882 subs.append(s)
882 subs.append(s)
883 if (subs or removedsubs):
883 if (subs or removedsubs):
884 if (not match('.hgsub') and
884 if (not match('.hgsub') and
885 '.hgsub' in (wctx.modified() + wctx.added())):
885 '.hgsub' in (wctx.modified() + wctx.added())):
886 raise util.Abort(_("can't commit subrepos without .hgsub"))
886 raise util.Abort(_("can't commit subrepos without .hgsub"))
887 if '.hgsubstate' not in changes[0]:
887 if '.hgsubstate' not in changes[0]:
888 changes[0].insert(0, '.hgsubstate')
888 changes[0].insert(0, '.hgsubstate')
889
889
890 # make sure all explicit patterns are matched
890 # make sure all explicit patterns are matched
891 if not force and match.files():
891 if not force and match.files():
892 matched = set(changes[0] + changes[1] + changes[2])
892 matched = set(changes[0] + changes[1] + changes[2])
893
893
894 for f in match.files():
894 for f in match.files():
895 if f == '.' or f in matched or f in wctx.substate:
895 if f == '.' or f in matched or f in wctx.substate:
896 continue
896 continue
897 if f in changes[3]: # missing
897 if f in changes[3]: # missing
898 fail(f, _('file not found!'))
898 fail(f, _('file not found!'))
899 if f in vdirs: # visited directory
899 if f in vdirs: # visited directory
900 d = f + '/'
900 d = f + '/'
901 for mf in matched:
901 for mf in matched:
902 if mf.startswith(d):
902 if mf.startswith(d):
903 break
903 break
904 else:
904 else:
905 fail(f, _("no match under directory!"))
905 fail(f, _("no match under directory!"))
906 elif f not in self.dirstate:
906 elif f not in self.dirstate:
907 fail(f, _("file not tracked!"))
907 fail(f, _("file not tracked!"))
908
908
909 if (not force and not extra.get("close") and not merge
909 if (not force and not extra.get("close") and not merge
910 and not (changes[0] or changes[1] or changes[2])
910 and not (changes[0] or changes[1] or changes[2])
911 and wctx.branch() == wctx.p1().branch()):
911 and wctx.branch() == wctx.p1().branch()):
912 return None
912 return None
913
913
914 ms = mergemod.mergestate(self)
914 ms = mergemod.mergestate(self)
915 for f in changes[0]:
915 for f in changes[0]:
916 if f in ms and ms[f] == 'u':
916 if f in ms and ms[f] == 'u':
917 raise util.Abort(_("unresolved merge conflicts "
917 raise util.Abort(_("unresolved merge conflicts "
918 "(see hg resolve)"))
918 "(see hg resolve)"))
919
919
920 cctx = context.workingctx(self, text, user, date, extra, changes)
920 cctx = context.workingctx(self, text, user, date, extra, changes)
921 if editor:
921 if editor:
922 cctx._text = editor(self, cctx, subs)
922 cctx._text = editor(self, cctx, subs)
923 edited = (text != cctx._text)
923 edited = (text != cctx._text)
924
924
925 # commit subs
925 # commit subs
926 if subs or removedsubs:
926 if subs or removedsubs:
927 state = wctx.substate.copy()
927 state = wctx.substate.copy()
928 for s in sorted(subs):
928 for s in sorted(subs):
929 sub = wctx.sub(s)
929 sub = wctx.sub(s)
930 self.ui.status(_('committing subrepository %s\n') %
930 self.ui.status(_('committing subrepository %s\n') %
931 subrepo.relpath(sub))
931 subrepo.relpath(sub))
932 sr = sub.commit(cctx._text, user, date)
932 sr = sub.commit(cctx._text, user, date)
933 state[s] = (state[s][0], sr)
933 state[s] = (state[s][0], sr)
934 subrepo.writestate(self, state)
934 subrepo.writestate(self, state)
935
935
936 # Save commit message in case this transaction gets rolled back
936 # Save commit message in case this transaction gets rolled back
937 # (e.g. by a pretxncommit hook). Leave the content alone on
937 # (e.g. by a pretxncommit hook). Leave the content alone on
938 # the assumption that the user will use the same editor again.
938 # the assumption that the user will use the same editor again.
939 msgfile = self.opener('last-message.txt', 'wb')
939 msgfile = self.opener('last-message.txt', 'wb')
940 msgfile.write(cctx._text)
940 msgfile.write(cctx._text)
941 msgfile.close()
941 msgfile.close()
942
942
943 p1, p2 = self.dirstate.parents()
943 p1, p2 = self.dirstate.parents()
944 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
944 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
945 try:
945 try:
946 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
946 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
947 ret = self.commitctx(cctx, True)
947 ret = self.commitctx(cctx, True)
948 except:
948 except:
949 if edited:
949 if edited:
950 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
950 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
951 self.ui.write(
951 self.ui.write(
952 _('note: commit message saved in %s\n') % msgfn)
952 _('note: commit message saved in %s\n') % msgfn)
953 raise
953 raise
954
954
955 # update dirstate and mergestate
955 # update dirstate and mergestate
956 for f in changes[0] + changes[1]:
956 for f in changes[0] + changes[1]:
957 self.dirstate.normal(f)
957 self.dirstate.normal(f)
958 for f in changes[2]:
958 for f in changes[2]:
959 self.dirstate.forget(f)
959 self.dirstate.forget(f)
960 self.dirstate.setparents(ret)
960 self.dirstate.setparents(ret)
961 ms.reset()
961 ms.reset()
962 finally:
962 finally:
963 wlock.release()
963 wlock.release()
964
964
965 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
965 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
966 return ret
966 return ret
967
967
968 def commitctx(self, ctx, error=False):
968 def commitctx(self, ctx, error=False):
969 """Add a new revision to current repository.
969 """Add a new revision to current repository.
970 Revision information is passed via the context argument.
970 Revision information is passed via the context argument.
971 """
971 """
972
972
973 tr = lock = None
973 tr = lock = None
974 removed = ctx.removed()
974 removed = ctx.removed()
975 p1, p2 = ctx.p1(), ctx.p2()
975 p1, p2 = ctx.p1(), ctx.p2()
976 m1 = p1.manifest().copy()
976 m1 = p1.manifest().copy()
977 m2 = p2.manifest()
977 m2 = p2.manifest()
978 user = ctx.user()
978 user = ctx.user()
979
979
980 lock = self.lock()
980 lock = self.lock()
981 try:
981 try:
982 tr = self.transaction("commit")
982 tr = self.transaction("commit")
983 trp = weakref.proxy(tr)
983 trp = weakref.proxy(tr)
984
984
985 # check in files
985 # check in files
986 new = {}
986 new = {}
987 changed = []
987 changed = []
988 linkrev = len(self)
988 linkrev = len(self)
989 for f in sorted(ctx.modified() + ctx.added()):
989 for f in sorted(ctx.modified() + ctx.added()):
990 self.ui.note(f + "\n")
990 self.ui.note(f + "\n")
991 try:
991 try:
992 fctx = ctx[f]
992 fctx = ctx[f]
993 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
993 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
994 changed)
994 changed)
995 m1.set(f, fctx.flags())
995 m1.set(f, fctx.flags())
996 except OSError, inst:
996 except OSError, inst:
997 self.ui.warn(_("trouble committing %s!\n") % f)
997 self.ui.warn(_("trouble committing %s!\n") % f)
998 raise
998 raise
999 except IOError, inst:
999 except IOError, inst:
1000 errcode = getattr(inst, 'errno', errno.ENOENT)
1000 errcode = getattr(inst, 'errno', errno.ENOENT)
1001 if error or errcode and errcode != errno.ENOENT:
1001 if error or errcode and errcode != errno.ENOENT:
1002 self.ui.warn(_("trouble committing %s!\n") % f)
1002 self.ui.warn(_("trouble committing %s!\n") % f)
1003 raise
1003 raise
1004 else:
1004 else:
1005 removed.append(f)
1005 removed.append(f)
1006
1006
1007 # update manifest
1007 # update manifest
1008 m1.update(new)
1008 m1.update(new)
1009 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1009 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1010 drop = [f for f in removed if f in m1]
1010 drop = [f for f in removed if f in m1]
1011 for f in drop:
1011 for f in drop:
1012 del m1[f]
1012 del m1[f]
1013 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1013 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1014 p2.manifestnode(), (new, drop))
1014 p2.manifestnode(), (new, drop))
1015
1015
1016 # update changelog
1016 # update changelog
1017 self.changelog.delayupdate()
1017 self.changelog.delayupdate()
1018 n = self.changelog.add(mn, changed + removed, ctx.description(),
1018 n = self.changelog.add(mn, changed + removed, ctx.description(),
1019 trp, p1.node(), p2.node(),
1019 trp, p1.node(), p2.node(),
1020 user, ctx.date(), ctx.extra().copy())
1020 user, ctx.date(), ctx.extra().copy())
1021 p = lambda: self.changelog.writepending() and self.root or ""
1021 p = lambda: self.changelog.writepending() and self.root or ""
1022 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1022 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1023 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1023 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1024 parent2=xp2, pending=p)
1024 parent2=xp2, pending=p)
1025 self.changelog.finalize(trp)
1025 self.changelog.finalize(trp)
1026 tr.close()
1026 tr.close()
1027
1027
1028 if self._branchcache:
1028 if self._branchcache:
1029 self.updatebranchcache()
1029 self.updatebranchcache()
1030 return n
1030 return n
1031 finally:
1031 finally:
1032 if tr:
1032 if tr:
1033 tr.release()
1033 tr.release()
1034 lock.release()
1034 lock.release()
1035
1035
1036 def destroyed(self):
1036 def destroyed(self):
1037 '''Inform the repository that nodes have been destroyed.
1037 '''Inform the repository that nodes have been destroyed.
1038 Intended for use by strip and rollback, so there's a common
1038 Intended for use by strip and rollback, so there's a common
1039 place for anything that has to be done after destroying history.'''
1039 place for anything that has to be done after destroying history.'''
1040 # XXX it might be nice if we could take the list of destroyed
1040 # XXX it might be nice if we could take the list of destroyed
1041 # nodes, but I don't see an easy way for rollback() to do that
1041 # nodes, but I don't see an easy way for rollback() to do that
1042
1042
1043 # Ensure the persistent tag cache is updated. Doing it now
1043 # Ensure the persistent tag cache is updated. Doing it now
1044 # means that the tag cache only has to worry about destroyed
1044 # means that the tag cache only has to worry about destroyed
1045 # heads immediately after a strip/rollback. That in turn
1045 # heads immediately after a strip/rollback. That in turn
1046 # guarantees that "cachetip == currenttip" (comparing both rev
1046 # guarantees that "cachetip == currenttip" (comparing both rev
1047 # and node) always means no nodes have been added or destroyed.
1047 # and node) always means no nodes have been added or destroyed.
1048
1048
1049 # XXX this is suboptimal when qrefresh'ing: we strip the current
1049 # XXX this is suboptimal when qrefresh'ing: we strip the current
1050 # head, refresh the tag cache, then immediately add a new head.
1050 # head, refresh the tag cache, then immediately add a new head.
1051 # But I think doing it this way is necessary for the "instant
1051 # But I think doing it this way is necessary for the "instant
1052 # tag cache retrieval" case to work.
1052 # tag cache retrieval" case to work.
1053 self.invalidatecaches()
1053 self.invalidatecaches()
1054
1054
1055 def walk(self, match, node=None):
1055 def walk(self, match, node=None):
1056 '''
1056 '''
1057 walk recursively through the directory tree or a given
1057 walk recursively through the directory tree or a given
1058 changeset, finding all files matched by the match
1058 changeset, finding all files matched by the match
1059 function
1059 function
1060 '''
1060 '''
1061 return self[node].walk(match)
1061 return self[node].walk(match)
1062
1062
1063 def status(self, node1='.', node2=None, match=None,
1063 def status(self, node1='.', node2=None, match=None,
1064 ignored=False, clean=False, unknown=False,
1064 ignored=False, clean=False, unknown=False,
1065 listsubrepos=False):
1065 listsubrepos=False):
1066 """return status of files between two nodes or node and working directory
1066 """return status of files between two nodes or node and working directory
1067
1067
1068 If node1 is None, use the first dirstate parent instead.
1068 If node1 is None, use the first dirstate parent instead.
1069 If node2 is None, compare node1 with working directory.
1069 If node2 is None, compare node1 with working directory.
1070 """
1070 """
1071
1071
1072 def mfmatches(ctx):
1072 def mfmatches(ctx):
1073 mf = ctx.manifest().copy()
1073 mf = ctx.manifest().copy()
1074 for fn in mf.keys():
1074 for fn in mf.keys():
1075 if not match(fn):
1075 if not match(fn):
1076 del mf[fn]
1076 del mf[fn]
1077 return mf
1077 return mf
1078
1078
1079 if isinstance(node1, context.changectx):
1079 if isinstance(node1, context.changectx):
1080 ctx1 = node1
1080 ctx1 = node1
1081 else:
1081 else:
1082 ctx1 = self[node1]
1082 ctx1 = self[node1]
1083 if isinstance(node2, context.changectx):
1083 if isinstance(node2, context.changectx):
1084 ctx2 = node2
1084 ctx2 = node2
1085 else:
1085 else:
1086 ctx2 = self[node2]
1086 ctx2 = self[node2]
1087
1087
1088 working = ctx2.rev() is None
1088 working = ctx2.rev() is None
1089 parentworking = working and ctx1 == self['.']
1089 parentworking = working and ctx1 == self['.']
1090 match = match or matchmod.always(self.root, self.getcwd())
1090 match = match or matchmod.always(self.root, self.getcwd())
1091 listignored, listclean, listunknown = ignored, clean, unknown
1091 listignored, listclean, listunknown = ignored, clean, unknown
1092
1092
1093 # load earliest manifest first for caching reasons
1093 # load earliest manifest first for caching reasons
1094 if not working and ctx2.rev() < ctx1.rev():
1094 if not working and ctx2.rev() < ctx1.rev():
1095 ctx2.manifest()
1095 ctx2.manifest()
1096
1096
1097 if not parentworking:
1097 if not parentworking:
1098 def bad(f, msg):
1098 def bad(f, msg):
1099 if f not in ctx1:
1099 if f not in ctx1:
1100 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1100 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1101 match.bad = bad
1101 match.bad = bad
1102
1102
1103 if working: # we need to scan the working dir
1103 if working: # we need to scan the working dir
1104 subrepos = []
1104 subrepos = []
1105 if '.hgsub' in self.dirstate:
1105 if '.hgsub' in self.dirstate:
1106 subrepos = ctx1.substate.keys()
1106 subrepos = ctx1.substate.keys()
1107 s = self.dirstate.status(match, subrepos, listignored,
1107 s = self.dirstate.status(match, subrepos, listignored,
1108 listclean, listunknown)
1108 listclean, listunknown)
1109 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1109 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1110
1110
1111 # check for any possibly clean files
1111 # check for any possibly clean files
1112 if parentworking and cmp:
1112 if parentworking and cmp:
1113 fixup = []
1113 fixup = []
1114 # do a full compare of any files that might have changed
1114 # do a full compare of any files that might have changed
1115 for f in sorted(cmp):
1115 for f in sorted(cmp):
1116 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1116 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1117 or ctx1[f].cmp(ctx2[f])):
1117 or ctx1[f].cmp(ctx2[f])):
1118 modified.append(f)
1118 modified.append(f)
1119 else:
1119 else:
1120 fixup.append(f)
1120 fixup.append(f)
1121
1121
1122 # update dirstate for files that are actually clean
1122 # update dirstate for files that are actually clean
1123 if fixup:
1123 if fixup:
1124 if listclean:
1124 if listclean:
1125 clean += fixup
1125 clean += fixup
1126
1126
1127 try:
1127 try:
1128 # updating the dirstate is optional
1128 # updating the dirstate is optional
1129 # so we don't wait on the lock
1129 # so we don't wait on the lock
1130 wlock = self.wlock(False)
1130 wlock = self.wlock(False)
1131 try:
1131 try:
1132 for f in fixup:
1132 for f in fixup:
1133 self.dirstate.normal(f)
1133 self.dirstate.normal(f)
1134 finally:
1134 finally:
1135 wlock.release()
1135 wlock.release()
1136 except error.LockError:
1136 except error.LockError:
1137 pass
1137 pass
1138
1138
1139 if not parentworking:
1139 if not parentworking:
1140 mf1 = mfmatches(ctx1)
1140 mf1 = mfmatches(ctx1)
1141 if working:
1141 if working:
1142 # we are comparing working dir against non-parent
1142 # we are comparing working dir against non-parent
1143 # generate a pseudo-manifest for the working dir
1143 # generate a pseudo-manifest for the working dir
1144 mf2 = mfmatches(self['.'])
1144 mf2 = mfmatches(self['.'])
1145 for f in cmp + modified + added:
1145 for f in cmp + modified + added:
1146 mf2[f] = None
1146 mf2[f] = None
1147 mf2.set(f, ctx2.flags(f))
1147 mf2.set(f, ctx2.flags(f))
1148 for f in removed:
1148 for f in removed:
1149 if f in mf2:
1149 if f in mf2:
1150 del mf2[f]
1150 del mf2[f]
1151 else:
1151 else:
1152 # we are comparing two revisions
1152 # we are comparing two revisions
1153 deleted, unknown, ignored = [], [], []
1153 deleted, unknown, ignored = [], [], []
1154 mf2 = mfmatches(ctx2)
1154 mf2 = mfmatches(ctx2)
1155
1155
1156 modified, added, clean = [], [], []
1156 modified, added, clean = [], [], []
1157 for fn in mf2:
1157 for fn in mf2:
1158 if fn in mf1:
1158 if fn in mf1:
1159 if (mf1.flags(fn) != mf2.flags(fn) or
1159 if (mf1.flags(fn) != mf2.flags(fn) or
1160 (mf1[fn] != mf2[fn] and
1160 (mf1[fn] != mf2[fn] and
1161 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1161 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1162 modified.append(fn)
1162 modified.append(fn)
1163 elif listclean:
1163 elif listclean:
1164 clean.append(fn)
1164 clean.append(fn)
1165 del mf1[fn]
1165 del mf1[fn]
1166 else:
1166 else:
1167 added.append(fn)
1167 added.append(fn)
1168 removed = mf1.keys()
1168 removed = mf1.keys()
1169
1169
1170 r = modified, added, removed, deleted, unknown, ignored, clean
1170 r = modified, added, removed, deleted, unknown, ignored, clean
1171
1171
1172 if listsubrepos:
1172 if listsubrepos:
1173 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1173 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1174 if working:
1174 if working:
1175 rev2 = None
1175 rev2 = None
1176 else:
1176 else:
1177 rev2 = ctx2.substate[subpath][1]
1177 rev2 = ctx2.substate[subpath][1]
1178 try:
1178 try:
1179 submatch = matchmod.narrowmatcher(subpath, match)
1179 submatch = matchmod.narrowmatcher(subpath, match)
1180 s = sub.status(rev2, match=submatch, ignored=listignored,
1180 s = sub.status(rev2, match=submatch, ignored=listignored,
1181 clean=listclean, unknown=listunknown,
1181 clean=listclean, unknown=listunknown,
1182 listsubrepos=True)
1182 listsubrepos=True)
1183 for rfiles, sfiles in zip(r, s):
1183 for rfiles, sfiles in zip(r, s):
1184 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1184 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1185 except error.LookupError:
1185 except error.LookupError:
1186 self.ui.status(_("skipping missing subrepository: %s\n")
1186 self.ui.status(_("skipping missing subrepository: %s\n")
1187 % subpath)
1187 % subpath)
1188
1188
1189 [l.sort() for l in r]
1189 [l.sort() for l in r]
1190 return r
1190 return r
1191
1191
1192 def heads(self, start=None):
1192 def heads(self, start=None):
1193 heads = self.changelog.heads(start)
1193 heads = self.changelog.heads(start)
1194 # sort the output in rev descending order
1194 # sort the output in rev descending order
1195 heads = [(-self.changelog.rev(h), h) for h in heads]
1195 heads = [(-self.changelog.rev(h), h) for h in heads]
1196 return [n for (r, n) in sorted(heads)]
1196 return [n for (r, n) in sorted(heads)]
1197
1197
1198 def branchheads(self, branch=None, start=None, closed=False):
1198 def branchheads(self, branch=None, start=None, closed=False):
1199 '''return a (possibly filtered) list of heads for the given branch
1199 '''return a (possibly filtered) list of heads for the given branch
1200
1200
1201 Heads are returned in topological order, from newest to oldest.
1201 Heads are returned in topological order, from newest to oldest.
1202 If branch is None, use the dirstate branch.
1202 If branch is None, use the dirstate branch.
1203 If start is not None, return only heads reachable from start.
1203 If start is not None, return only heads reachable from start.
1204 If closed is True, return heads that are marked as closed as well.
1204 If closed is True, return heads that are marked as closed as well.
1205 '''
1205 '''
1206 if branch is None:
1206 if branch is None:
1207 branch = self[None].branch()
1207 branch = self[None].branch()
1208 branches = self.branchmap()
1208 branches = self.branchmap()
1209 if branch not in branches:
1209 if branch not in branches:
1210 return []
1210 return []
1211 # the cache returns heads ordered lowest to highest
1211 # the cache returns heads ordered lowest to highest
1212 bheads = list(reversed(branches[branch]))
1212 bheads = list(reversed(branches[branch]))
1213 if start is not None:
1213 if start is not None:
1214 # filter out the heads that cannot be reached from startrev
1214 # filter out the heads that cannot be reached from startrev
1215 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1215 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1216 bheads = [h for h in bheads if h in fbheads]
1216 bheads = [h for h in bheads if h in fbheads]
1217 if not closed:
1217 if not closed:
1218 bheads = [h for h in bheads if
1218 bheads = [h for h in bheads if
1219 ('close' not in self.changelog.read(h)[5])]
1219 ('close' not in self.changelog.read(h)[5])]
1220 return bheads
1220 return bheads
1221
1221
1222 def branches(self, nodes):
1222 def branches(self, nodes):
1223 if not nodes:
1223 if not nodes:
1224 nodes = [self.changelog.tip()]
1224 nodes = [self.changelog.tip()]
1225 b = []
1225 b = []
1226 for n in nodes:
1226 for n in nodes:
1227 t = n
1227 t = n
1228 while 1:
1228 while 1:
1229 p = self.changelog.parents(n)
1229 p = self.changelog.parents(n)
1230 if p[1] != nullid or p[0] == nullid:
1230 if p[1] != nullid or p[0] == nullid:
1231 b.append((t, n, p[0], p[1]))
1231 b.append((t, n, p[0], p[1]))
1232 break
1232 break
1233 n = p[0]
1233 n = p[0]
1234 return b
1234 return b
1235
1235
1236 def between(self, pairs):
1236 def between(self, pairs):
1237 r = []
1237 r = []
1238
1238
1239 for top, bottom in pairs:
1239 for top, bottom in pairs:
1240 n, l, i = top, [], 0
1240 n, l, i = top, [], 0
1241 f = 1
1241 f = 1
1242
1242
1243 while n != bottom and n != nullid:
1243 while n != bottom and n != nullid:
1244 p = self.changelog.parents(n)[0]
1244 p = self.changelog.parents(n)[0]
1245 if i == f:
1245 if i == f:
1246 l.append(n)
1246 l.append(n)
1247 f = f * 2
1247 f = f * 2
1248 n = p
1248 n = p
1249 i += 1
1249 i += 1
1250
1250
1251 r.append(l)
1251 r.append(l)
1252
1252
1253 return r
1253 return r
1254
1254
1255 def pull(self, remote, heads=None, force=False):
1255 def pull(self, remote, heads=None, force=False):
1256 lock = self.lock()
1256 lock = self.lock()
1257 try:
1257 try:
1258 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1258 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1259 force=force)
1259 force=force)
1260 common, fetch, rheads = tmp
1260 common, fetch, rheads = tmp
1261 if not fetch:
1261 if not fetch:
1262 self.ui.status(_("no changes found\n"))
1262 self.ui.status(_("no changes found\n"))
1263 return 0
1263 return 0
1264
1264
1265 if fetch == [nullid]:
1265 if fetch == [nullid]:
1266 self.ui.status(_("requesting all changes\n"))
1266 self.ui.status(_("requesting all changes\n"))
1267 elif heads is None and remote.capable('changegroupsubset'):
1267 elif heads is None and remote.capable('changegroupsubset'):
1268 # issue1320, avoid a race if remote changed after discovery
1268 # issue1320, avoid a race if remote changed after discovery
1269 heads = rheads
1269 heads = rheads
1270
1270
1271 if heads is None:
1271 if heads is None:
1272 cg = remote.changegroup(fetch, 'pull')
1272 cg = remote.changegroup(fetch, 'pull')
1273 else:
1273 else:
1274 if not remote.capable('changegroupsubset'):
1274 if not remote.capable('changegroupsubset'):
1275 raise util.Abort(_("partial pull cannot be done because "
1275 raise util.Abort(_("partial pull cannot be done because "
1276 "other repository doesn't support "
1276 "other repository doesn't support "
1277 "changegroupsubset."))
1277 "changegroupsubset."))
1278 cg = remote.changegroupsubset(fetch, heads, 'pull')
1278 cg = remote.changegroupsubset(fetch, heads, 'pull')
1279 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1279 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1280 finally:
1280 finally:
1281 lock.release()
1281 lock.release()
1282
1282
1283 def push(self, remote, force=False, revs=None, newbranch=False):
1283 def push(self, remote, force=False, revs=None, newbranch=False):
1284 '''Push outgoing changesets (limited by revs) from the current
1284 '''Push outgoing changesets (limited by revs) from the current
1285 repository to remote. Return an integer:
1285 repository to remote. Return an integer:
1286 - 0 means HTTP error *or* nothing to push
1286 - 0 means HTTP error *or* nothing to push
1287 - 1 means we pushed and remote head count is unchanged *or*
1287 - 1 means we pushed and remote head count is unchanged *or*
1288 we have outgoing changesets but refused to push
1288 we have outgoing changesets but refused to push
1289 - other values as described by addchangegroup()
1289 - other values as described by addchangegroup()
1290 '''
1290 '''
1291 # there are two ways to push to remote repo:
1291 # there are two ways to push to remote repo:
1292 #
1292 #
1293 # addchangegroup assumes local user can lock remote
1293 # addchangegroup assumes local user can lock remote
1294 # repo (local filesystem, old ssh servers).
1294 # repo (local filesystem, old ssh servers).
1295 #
1295 #
1296 # unbundle assumes local user cannot lock remote repo (new ssh
1296 # unbundle assumes local user cannot lock remote repo (new ssh
1297 # servers, http servers).
1297 # servers, http servers).
1298
1298
1299 lock = None
1299 lock = None
1300 unbundle = remote.capable('unbundle')
1300 unbundle = remote.capable('unbundle')
1301 if not unbundle:
1301 if not unbundle:
1302 lock = remote.lock()
1302 lock = remote.lock()
1303 try:
1303 try:
1304 ret = discovery.prepush(self, remote, force, revs, newbranch)
1304 ret = discovery.prepush(self, remote, force, revs, newbranch)
1305 if ret[0] is None:
1305 if ret[0] is None:
1306 # and here we return 0 for "nothing to push" or 1 for
1306 # and here we return 0 for "nothing to push" or 1 for
1307 # "something to push but I refuse"
1307 # "something to push but I refuse"
1308 return ret[1]
1308 return ret[1]
1309
1309
1310 cg, remote_heads = ret
1310 cg, remote_heads = ret
1311 if unbundle:
1311 if unbundle:
1312 # local repo finds heads on server, finds out what revs it must
1312 # local repo finds heads on server, finds out what revs it must
1313 # push. once revs transferred, if server finds it has
1313 # push. once revs transferred, if server finds it has
1314 # different heads (someone else won commit/push race), server
1314 # different heads (someone else won commit/push race), server
1315 # aborts.
1315 # aborts.
1316 if force:
1316 if force:
1317 remote_heads = ['force']
1317 remote_heads = ['force']
1318 # ssh: return remote's addchangegroup()
1318 # ssh: return remote's addchangegroup()
1319 # http: return remote's addchangegroup() or 0 for error
1319 # http: return remote's addchangegroup() or 0 for error
1320 return remote.unbundle(cg, remote_heads, 'push')
1320 return remote.unbundle(cg, remote_heads, 'push')
1321 else:
1321 else:
1322 # we return an integer indicating remote head count change
1322 # we return an integer indicating remote head count change
1323 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1323 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1324 finally:
1324 finally:
1325 if lock is not None:
1325 if lock is not None:
1326 lock.release()
1326 lock.release()
1327
1327
1328 def changegroupinfo(self, nodes, source):
1328 def changegroupinfo(self, nodes, source):
1329 if self.ui.verbose or source == 'bundle':
1329 if self.ui.verbose or source == 'bundle':
1330 self.ui.status(_("%d changesets found\n") % len(nodes))
1330 self.ui.status(_("%d changesets found\n") % len(nodes))
1331 if self.ui.debugflag:
1331 if self.ui.debugflag:
1332 self.ui.debug("list of changesets:\n")
1332 self.ui.debug("list of changesets:\n")
1333 for node in nodes:
1333 for node in nodes:
1334 self.ui.debug("%s\n" % hex(node))
1334 self.ui.debug("%s\n" % hex(node))
1335
1335
1336 def changegroupsubset(self, bases, heads, source, extranodes=None):
1336 def changegroupsubset(self, bases, heads, source, extranodes=None):
1337 """Compute a changegroup consisting of all the nodes that are
1337 """Compute a changegroup consisting of all the nodes that are
1338 descendents of any of the bases and ancestors of any of the heads.
1338 descendents of any of the bases and ancestors of any of the heads.
1339 Return a chunkbuffer object whose read() method will return
1339 Return a chunkbuffer object whose read() method will return
1340 successive changegroup chunks.
1340 successive changegroup chunks.
1341
1341
1342 It is fairly complex as determining which filenodes and which
1342 It is fairly complex as determining which filenodes and which
1343 manifest nodes need to be included for the changeset to be complete
1343 manifest nodes need to be included for the changeset to be complete
1344 is non-trivial.
1344 is non-trivial.
1345
1345
1346 Another wrinkle is doing the reverse, figuring out which changeset in
1346 Another wrinkle is doing the reverse, figuring out which changeset in
1347 the changegroup a particular filenode or manifestnode belongs to.
1347 the changegroup a particular filenode or manifestnode belongs to.
1348
1348
1349 The caller can specify some nodes that must be included in the
1349 The caller can specify some nodes that must be included in the
1350 changegroup using the extranodes argument. It should be a dict
1350 changegroup using the extranodes argument. It should be a dict
1351 where the keys are the filenames (or 1 for the manifest), and the
1351 where the keys are the filenames (or 1 for the manifest), and the
1352 values are lists of (node, linknode) tuples, where node is a wanted
1352 values are lists of (node, linknode) tuples, where node is a wanted
1353 node and linknode is the changelog node that should be transmitted as
1353 node and linknode is the changelog node that should be transmitted as
1354 the linkrev.
1354 the linkrev.
1355 """
1355 """
1356
1356
1357 # Set up some initial variables
1357 # Set up some initial variables
1358 # Make it easy to refer to self.changelog
1358 # Make it easy to refer to self.changelog
1359 cl = self.changelog
1359 cl = self.changelog
1360 # Compute the list of changesets in this changegroup.
1360 # Compute the list of changesets in this changegroup.
1361 # Some bases may turn out to be superfluous, and some heads may be
1361 # Some bases may turn out to be superfluous, and some heads may be
1362 # too. nodesbetween will return the minimal set of bases and heads
1362 # too. nodesbetween will return the minimal set of bases and heads
1363 # necessary to re-create the changegroup.
1363 # necessary to re-create the changegroup.
1364 if not bases:
1364 if not bases:
1365 bases = [nullid]
1365 bases = [nullid]
1366 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1366 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1367
1367
1368 if extranodes is None:
1368 if extranodes is None:
1369 # can we go through the fast path ?
1369 # can we go through the fast path ?
1370 heads.sort()
1370 heads.sort()
1371 allheads = self.heads()
1371 allheads = self.heads()
1372 allheads.sort()
1372 allheads.sort()
1373 if heads == allheads:
1373 if heads == allheads:
1374 return self._changegroup(msng_cl_lst, source)
1374 return self._changegroup(msng_cl_lst, source)
1375
1375
1376 # slow path
1376 # slow path
1377 self.hook('preoutgoing', throw=True, source=source)
1377 self.hook('preoutgoing', throw=True, source=source)
1378
1378
1379 self.changegroupinfo(msng_cl_lst, source)
1379 self.changegroupinfo(msng_cl_lst, source)
1380
1380
1381 # We assume that all ancestors of bases are known
1381 # We assume that all ancestors of bases are known
1382 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1382 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1383
1383
1384 # Make it easy to refer to self.manifest
1384 # Make it easy to refer to self.manifest
1385 mnfst = self.manifest
1385 mnfst = self.manifest
1386 # We don't know which manifests are missing yet
1386 # We don't know which manifests are missing yet
1387 msng_mnfst_set = {}
1387 msng_mnfst_set = {}
1388 # Nor do we know which filenodes are missing.
1388 # Nor do we know which filenodes are missing.
1389 msng_filenode_set = {}
1389 msng_filenode_set = {}
1390
1390
1391 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1391 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1392 junk = None
1392 junk = None
1393
1393
1394 # A changeset always belongs to itself, so the changenode lookup
1394 # A changeset always belongs to itself, so the changenode lookup
1395 # function for a changenode is identity.
1395 # function for a changenode is identity.
1396 def identity(x):
1396 def identity(x):
1397 return x
1397 return x
1398
1398
1399 # A function generating function that sets up the initial environment
1399 # A function generating function that sets up the initial environment
1400 # the inner function.
1400 # the inner function.
1401 def filenode_collector(changedfiles):
1401 def filenode_collector(changedfiles):
1402 # This gathers information from each manifestnode included in the
1402 # This gathers information from each manifestnode included in the
1403 # changegroup about which filenodes the manifest node references
1403 # changegroup about which filenodes the manifest node references
1404 # so we can include those in the changegroup too.
1404 # so we can include those in the changegroup too.
1405 #
1405 #
1406 # It also remembers which changenode each filenode belongs to. It
1406 # It also remembers which changenode each filenode belongs to. It
1407 # does this by assuming the a filenode belongs to the changenode
1407 # does this by assuming the a filenode belongs to the changenode
1408 # the first manifest that references it belongs to.
1408 # the first manifest that references it belongs to.
1409 def collect_msng_filenodes(mnfstnode):
1409 def collect_msng_filenodes(mnfstnode):
1410 r = mnfst.rev(mnfstnode)
1410 r = mnfst.rev(mnfstnode)
1411 if r - 1 in mnfst.parentrevs(r):
1411 if r - 1 in mnfst.parentrevs(r):
1412 # If the previous rev is one of the parents,
1412 # If the previous rev is one of the parents,
1413 # we only need to see a diff.
1413 # we only need to see a diff.
1414 deltamf = mnfst.readdelta(mnfstnode)
1414 deltamf = mnfst.readdelta(mnfstnode)
1415 # For each line in the delta
1415 # For each line in the delta
1416 for f, fnode in deltamf.iteritems():
1416 for f, fnode in deltamf.iteritems():
1417 # And if the file is in the list of files we care
1417 # And if the file is in the list of files we care
1418 # about.
1418 # about.
1419 if f in changedfiles:
1419 if f in changedfiles:
1420 # Get the changenode this manifest belongs to
1420 # Get the changenode this manifest belongs to
1421 clnode = msng_mnfst_set[mnfstnode]
1421 clnode = msng_mnfst_set[mnfstnode]
1422 # Create the set of filenodes for the file if
1422 # Create the set of filenodes for the file if
1423 # there isn't one already.
1423 # there isn't one already.
1424 ndset = msng_filenode_set.setdefault(f, {})
1424 ndset = msng_filenode_set.setdefault(f, {})
1425 # And set the filenode's changelog node to the
1425 # And set the filenode's changelog node to the
1426 # manifest's if it hasn't been set already.
1426 # manifest's if it hasn't been set already.
1427 ndset.setdefault(fnode, clnode)
1427 ndset.setdefault(fnode, clnode)
1428 else:
1428 else:
1429 # Otherwise we need a full manifest.
1429 # Otherwise we need a full manifest.
1430 m = mnfst.read(mnfstnode)
1430 m = mnfst.read(mnfstnode)
1431 # For every file in we care about.
1431 # For every file in we care about.
1432 for f in changedfiles:
1432 for f in changedfiles:
1433 fnode = m.get(f, None)
1433 fnode = m.get(f, None)
1434 # If it's in the manifest
1434 # If it's in the manifest
1435 if fnode is not None:
1435 if fnode is not None:
1436 # See comments above.
1436 # See comments above.
1437 clnode = msng_mnfst_set[mnfstnode]
1437 clnode = msng_mnfst_set[mnfstnode]
1438 ndset = msng_filenode_set.setdefault(f, {})
1438 ndset = msng_filenode_set.setdefault(f, {})
1439 ndset.setdefault(fnode, clnode)
1439 ndset.setdefault(fnode, clnode)
1440 return collect_msng_filenodes
1440 return collect_msng_filenodes
1441
1441
1442 # If we determine that a particular file or manifest node must be a
1442 # If we determine that a particular file or manifest node must be a
1443 # node that the recipient of the changegroup will already have, we can
1443 # node that the recipient of the changegroup will already have, we can
1444 # also assume the recipient will have all the parents. This function
1444 # also assume the recipient will have all the parents. This function
1445 # prunes them from the set of missing nodes.
1445 # prunes them from the set of missing nodes.
1446 def prune(revlog, missingnodes):
1446 def prune(revlog, missingnodes):
1447 hasset = set()
1447 hasset = set()
1448 # If a 'missing' filenode thinks it belongs to a changenode we
1448 # If a 'missing' filenode thinks it belongs to a changenode we
1449 # assume the recipient must have, then the recipient must have
1449 # assume the recipient must have, then the recipient must have
1450 # that filenode.
1450 # that filenode.
1451 for n in missingnodes:
1451 for n in missingnodes:
1452 clrev = revlog.linkrev(revlog.rev(n))
1452 clrev = revlog.linkrev(revlog.rev(n))
1453 if clrev in commonrevs:
1453 if clrev in commonrevs:
1454 hasset.add(n)
1454 hasset.add(n)
1455 for n in hasset:
1455 for n in hasset:
1456 missingnodes.pop(n, None)
1456 missingnodes.pop(n, None)
1457 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1457 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1458 missingnodes.pop(revlog.node(r), None)
1458 missingnodes.pop(revlog.node(r), None)
1459
1459
1460 # Add the nodes that were explicitly requested.
1460 # Add the nodes that were explicitly requested.
1461 def add_extra_nodes(name, nodes):
1461 def add_extra_nodes(name, nodes):
1462 if not extranodes or name not in extranodes:
1462 if not extranodes or name not in extranodes:
1463 return
1463 return
1464
1464
1465 for node, linknode in extranodes[name]:
1465 for node, linknode in extranodes[name]:
1466 if node not in nodes:
1466 if node not in nodes:
1467 nodes[node] = linknode
1467 nodes[node] = linknode
1468
1468
1469 # Now that we have all theses utility functions to help out and
1469 # Now that we have all theses utility functions to help out and
1470 # logically divide up the task, generate the group.
1470 # logically divide up the task, generate the group.
1471 def gengroup():
1471 def gengroup():
1472 # The set of changed files starts empty.
1472 # The set of changed files starts empty.
1473 changedfiles = set()
1473 changedfiles = set()
1474 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1474 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1475
1475
1476 # Create a changenode group generator that will call our functions
1476 # Create a changenode group generator that will call our functions
1477 # back to lookup the owning changenode and collect information.
1477 # back to lookup the owning changenode and collect information.
1478 group = cl.group(msng_cl_lst, identity, collect)
1478 group = cl.group(msng_cl_lst, identity, collect)
1479 for cnt, chnk in enumerate(group):
1479 for cnt, chnk in enumerate(group):
1480 yield chnk
1480 yield chnk
1481 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1481 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1482 self.ui.progress(_('bundling changes'), None)
1482 self.ui.progress(_('bundling changes'), None)
1483
1483
1484 prune(mnfst, msng_mnfst_set)
1484 prune(mnfst, msng_mnfst_set)
1485 add_extra_nodes(1, msng_mnfst_set)
1485 add_extra_nodes(1, msng_mnfst_set)
1486 msng_mnfst_lst = msng_mnfst_set.keys()
1486 msng_mnfst_lst = msng_mnfst_set.keys()
1487 # Sort the manifestnodes by revision number.
1487 # Sort the manifestnodes by revision number.
1488 msng_mnfst_lst.sort(key=mnfst.rev)
1488 msng_mnfst_lst.sort(key=mnfst.rev)
1489 # Create a generator for the manifestnodes that calls our lookup
1489 # Create a generator for the manifestnodes that calls our lookup
1490 # and data collection functions back.
1490 # and data collection functions back.
1491 group = mnfst.group(msng_mnfst_lst,
1491 group = mnfst.group(msng_mnfst_lst,
1492 lambda mnode: msng_mnfst_set[mnode],
1492 lambda mnode: msng_mnfst_set[mnode],
1493 filenode_collector(changedfiles))
1493 filenode_collector(changedfiles))
1494 for cnt, chnk in enumerate(group):
1494 for cnt, chnk in enumerate(group):
1495 yield chnk
1495 yield chnk
1496 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1496 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1497 self.ui.progress(_('bundling manifests'), None)
1497 self.ui.progress(_('bundling manifests'), None)
1498
1498
1499 # These are no longer needed, dereference and toss the memory for
1499 # These are no longer needed, dereference and toss the memory for
1500 # them.
1500 # them.
1501 msng_mnfst_lst = None
1501 msng_mnfst_lst = None
1502 msng_mnfst_set.clear()
1502 msng_mnfst_set.clear()
1503
1503
1504 if extranodes:
1504 if extranodes:
1505 for fname in extranodes:
1505 for fname in extranodes:
1506 if isinstance(fname, int):
1506 if isinstance(fname, int):
1507 continue
1507 continue
1508 msng_filenode_set.setdefault(fname, {})
1508 msng_filenode_set.setdefault(fname, {})
1509 changedfiles.add(fname)
1509 changedfiles.add(fname)
1510 # Go through all our files in order sorted by name.
1510 # Go through all our files in order sorted by name.
1511 cnt = 0
1511 cnt = 0
1512 for fname in sorted(changedfiles):
1512 for fname in sorted(changedfiles):
1513 filerevlog = self.file(fname)
1513 filerevlog = self.file(fname)
1514 if not len(filerevlog):
1514 if not len(filerevlog):
1515 raise util.Abort(_("empty or missing revlog for %s") % fname)
1515 raise util.Abort(_("empty or missing revlog for %s") % fname)
1516 # Toss out the filenodes that the recipient isn't really
1516 # Toss out the filenodes that the recipient isn't really
1517 # missing.
1517 # missing.
1518 missingfnodes = msng_filenode_set.pop(fname, {})
1518 missingfnodes = msng_filenode_set.pop(fname, {})
1519 prune(filerevlog, missingfnodes)
1519 prune(filerevlog, missingfnodes)
1520 add_extra_nodes(fname, missingfnodes)
1520 add_extra_nodes(fname, missingfnodes)
1521 # If any filenodes are left, generate the group for them,
1521 # If any filenodes are left, generate the group for them,
1522 # otherwise don't bother.
1522 # otherwise don't bother.
1523 if missingfnodes:
1523 if missingfnodes:
1524 yield changegroup.chunkheader(len(fname))
1524 yield changegroup.chunkheader(len(fname))
1525 yield fname
1525 yield fname
1526 # Sort the filenodes by their revision # (topological order)
1526 # Sort the filenodes by their revision # (topological order)
1527 nodeiter = list(missingfnodes)
1527 nodeiter = list(missingfnodes)
1528 nodeiter.sort(key=filerevlog.rev)
1528 nodeiter.sort(key=filerevlog.rev)
1529 # Create a group generator and only pass in a changenode
1529 # Create a group generator and only pass in a changenode
1530 # lookup function as we need to collect no information
1530 # lookup function as we need to collect no information
1531 # from filenodes.
1531 # from filenodes.
1532 group = filerevlog.group(nodeiter,
1532 group = filerevlog.group(nodeiter,
1533 lambda fnode: missingfnodes[fnode])
1533 lambda fnode: missingfnodes[fnode])
1534 for chnk in group:
1534 for chnk in group:
1535 self.ui.progress(
1535 self.ui.progress(
1536 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1536 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1537 cnt += 1
1537 cnt += 1
1538 yield chnk
1538 yield chnk
1539 # Signal that no more groups are left.
1539 # Signal that no more groups are left.
1540 yield changegroup.closechunk()
1540 yield changegroup.closechunk()
1541 self.ui.progress(_('bundling files'), None)
1541 self.ui.progress(_('bundling files'), None)
1542
1542
1543 if msng_cl_lst:
1543 if msng_cl_lst:
1544 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1544 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1545
1545
1546 return util.chunkbuffer(gengroup())
1546 return util.chunkbuffer(gengroup())
1547
1547
1548 def changegroup(self, basenodes, source):
1548 def changegroup(self, basenodes, source):
1549 # to avoid a race we use changegroupsubset() (issue1320)
1549 # to avoid a race we use changegroupsubset() (issue1320)
1550 return self.changegroupsubset(basenodes, self.heads(), source)
1550 return self.changegroupsubset(basenodes, self.heads(), source)
1551
1551
1552 def _changegroup(self, nodes, source):
1552 def _changegroup(self, nodes, source):
1553 """Compute the changegroup of all nodes that we have that a recipient
1553 """Compute the changegroup of all nodes that we have that a recipient
1554 doesn't. Return a chunkbuffer object whose read() method will return
1554 doesn't. Return a chunkbuffer object whose read() method will return
1555 successive changegroup chunks.
1555 successive changegroup chunks.
1556
1556
1557 This is much easier than the previous function as we can assume that
1557 This is much easier than the previous function as we can assume that
1558 the recipient has any changenode we aren't sending them.
1558 the recipient has any changenode we aren't sending them.
1559
1559
1560 nodes is the set of nodes to send"""
1560 nodes is the set of nodes to send"""
1561
1561
1562 self.hook('preoutgoing', throw=True, source=source)
1562 self.hook('preoutgoing', throw=True, source=source)
1563
1563
1564 cl = self.changelog
1564 cl = self.changelog
1565 revset = set([cl.rev(n) for n in nodes])
1565 revset = set([cl.rev(n) for n in nodes])
1566 self.changegroupinfo(nodes, source)
1566 self.changegroupinfo(nodes, source)
1567
1567
1568 def identity(x):
1568 def identity(x):
1569 return x
1569 return x
1570
1570
1571 def gennodelst(log):
1571 def gennodelst(log):
1572 for r in log:
1572 for r in log:
1573 if log.linkrev(r) in revset:
1573 if log.linkrev(r) in revset:
1574 yield log.node(r)
1574 yield log.node(r)
1575
1575
1576 def lookuplinkrev_func(revlog):
1576 def lookuplinkrev_func(revlog):
1577 def lookuplinkrev(n):
1577 def lookuplinkrev(n):
1578 return cl.node(revlog.linkrev(revlog.rev(n)))
1578 return cl.node(revlog.linkrev(revlog.rev(n)))
1579 return lookuplinkrev
1579 return lookuplinkrev
1580
1580
1581 def gengroup():
1581 def gengroup():
1582 '''yield a sequence of changegroup chunks (strings)'''
1582 '''yield a sequence of changegroup chunks (strings)'''
1583 # construct a list of all changed files
1583 # construct a list of all changed files
1584 changedfiles = set()
1584 changedfiles = set()
1585 mmfs = {}
1585 mmfs = {}
1586 collect = changegroup.collector(cl, mmfs, changedfiles)
1586 collect = changegroup.collector(cl, mmfs, changedfiles)
1587
1587
1588 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1588 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1589 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1589 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1590 yield chnk
1590 yield chnk
1591 self.ui.progress(_('bundling changes'), None)
1591 self.ui.progress(_('bundling changes'), None)
1592
1592
1593 mnfst = self.manifest
1593 mnfst = self.manifest
1594 nodeiter = gennodelst(mnfst)
1594 nodeiter = gennodelst(mnfst)
1595 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1595 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1596 lookuplinkrev_func(mnfst))):
1596 lookuplinkrev_func(mnfst))):
1597 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1597 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1598 yield chnk
1598 yield chnk
1599 self.ui.progress(_('bundling manifests'), None)
1599 self.ui.progress(_('bundling manifests'), None)
1600
1600
1601 cnt = 0
1601 cnt = 0
1602 for fname in sorted(changedfiles):
1602 for fname in sorted(changedfiles):
1603 filerevlog = self.file(fname)
1603 filerevlog = self.file(fname)
1604 if not len(filerevlog):
1604 if not len(filerevlog):
1605 raise util.Abort(_("empty or missing revlog for %s") % fname)
1605 raise util.Abort(_("empty or missing revlog for %s") % fname)
1606 nodeiter = gennodelst(filerevlog)
1606 nodeiter = gennodelst(filerevlog)
1607 nodeiter = list(nodeiter)
1607 nodeiter = list(nodeiter)
1608 if nodeiter:
1608 if nodeiter:
1609 yield changegroup.chunkheader(len(fname))
1609 yield changegroup.chunkheader(len(fname))
1610 yield fname
1610 yield fname
1611 lookup = lookuplinkrev_func(filerevlog)
1611 lookup = lookuplinkrev_func(filerevlog)
1612 for chnk in filerevlog.group(nodeiter, lookup):
1612 for chnk in filerevlog.group(nodeiter, lookup):
1613 self.ui.progress(
1613 self.ui.progress(
1614 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1614 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1615 cnt += 1
1615 cnt += 1
1616 yield chnk
1616 yield chnk
1617 self.ui.progress(_('bundling files'), None)
1617 self.ui.progress(_('bundling files'), None)
1618
1618
1619 yield changegroup.closechunk()
1619 yield changegroup.closechunk()
1620
1620
1621 if nodes:
1621 if nodes:
1622 self.hook('outgoing', node=hex(nodes[0]), source=source)
1622 self.hook('outgoing', node=hex(nodes[0]), source=source)
1623
1623
1624 return util.chunkbuffer(gengroup())
1624 return util.chunkbuffer(gengroup())
1625
1625
1626 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1626 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1627 """Add the changegroup returned by source.read() to this repo.
1627 """Add the changegroup returned by source.read() to this repo.
1628 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1628 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1629 the URL of the repo where this changegroup is coming from.
1629 the URL of the repo where this changegroup is coming from.
1630
1630
1631 Return an integer summarizing the change to this repo:
1631 Return an integer summarizing the change to this repo:
1632 - nothing changed or no source: 0
1632 - nothing changed or no source: 0
1633 - more heads than before: 1+added heads (2..n)
1633 - more heads than before: 1+added heads (2..n)
1634 - fewer heads than before: -1-removed heads (-2..-n)
1634 - fewer heads than before: -1-removed heads (-2..-n)
1635 - number of heads stays the same: 1
1635 - number of heads stays the same: 1
1636 """
1636 """
1637 def csmap(x):
1637 def csmap(x):
1638 self.ui.debug("add changeset %s\n" % short(x))
1638 self.ui.debug("add changeset %s\n" % short(x))
1639 return len(cl)
1639 return len(cl)
1640
1640
1641 def revmap(x):
1641 def revmap(x):
1642 return cl.rev(x)
1642 return cl.rev(x)
1643
1643
1644 if not source:
1644 if not source:
1645 return 0
1645 return 0
1646
1646
1647 if not hasattr(source, 'chunk'):
1647 if not hasattr(source, 'chunk'):
1648 source = changegroup.unbundle10(source, 'UN')
1648 source = changegroup.unbundle10(source, 'UN')
1649
1649
1650 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1650 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1651
1651
1652 changesets = files = revisions = 0
1652 changesets = files = revisions = 0
1653 efiles = set()
1653 efiles = set()
1654
1654
1655 # write changelog data to temp files so concurrent readers will not see
1655 # write changelog data to temp files so concurrent readers will not see
1656 # inconsistent view
1656 # inconsistent view
1657 cl = self.changelog
1657 cl = self.changelog
1658 cl.delayupdate()
1658 cl.delayupdate()
1659 oldheads = len(cl.heads())
1659 oldheads = len(cl.heads())
1660
1660
1661 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1661 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1662 try:
1662 try:
1663 trp = weakref.proxy(tr)
1663 trp = weakref.proxy(tr)
1664 # pull off the changeset group
1664 # pull off the changeset group
1665 self.ui.status(_("adding changesets\n"))
1665 self.ui.status(_("adding changesets\n"))
1666 clstart = len(cl)
1666 clstart = len(cl)
1667 class prog(object):
1667 class prog(object):
1668 step = _('changesets')
1668 step = _('changesets')
1669 count = 1
1669 count = 1
1670 ui = self.ui
1670 ui = self.ui
1671 total = None
1671 total = None
1672 def __call__(self):
1672 def __call__(self):
1673 self.ui.progress(self.step, self.count, unit=_('chunks'),
1673 self.ui.progress(self.step, self.count, unit=_('chunks'),
1674 total=self.total)
1674 total=self.total)
1675 self.count += 1
1675 self.count += 1
1676 pr = prog()
1676 pr = prog()
1677 source.callback = pr
1677 source.callback = pr
1678
1678
1679 if (cl.addgroup(source.chunks(), csmap, trp) is None
1679 if (cl.addgroup(source, csmap, trp) is None
1680 and not emptyok):
1680 and not emptyok):
1681 raise util.Abort(_("received changelog group is empty"))
1681 raise util.Abort(_("received changelog group is empty"))
1682 clend = len(cl)
1682 clend = len(cl)
1683 changesets = clend - clstart
1683 changesets = clend - clstart
1684 for c in xrange(clstart, clend):
1684 for c in xrange(clstart, clend):
1685 efiles.update(self[c].files())
1685 efiles.update(self[c].files())
1686 efiles = len(efiles)
1686 efiles = len(efiles)
1687 self.ui.progress(_('changesets'), None)
1687 self.ui.progress(_('changesets'), None)
1688
1688
1689 # pull off the manifest group
1689 # pull off the manifest group
1690 self.ui.status(_("adding manifests\n"))
1690 self.ui.status(_("adding manifests\n"))
1691 pr.step = _('manifests')
1691 pr.step = _('manifests')
1692 pr.count = 1
1692 pr.count = 1
1693 pr.total = changesets # manifests <= changesets
1693 pr.total = changesets # manifests <= changesets
1694 # no need to check for empty manifest group here:
1694 # no need to check for empty manifest group here:
1695 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1695 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1696 # no new manifest will be created and the manifest group will
1696 # no new manifest will be created and the manifest group will
1697 # be empty during the pull
1697 # be empty during the pull
1698 self.manifest.addgroup(source.chunks(), revmap, trp)
1698 self.manifest.addgroup(source, revmap, trp)
1699 self.ui.progress(_('manifests'), None)
1699 self.ui.progress(_('manifests'), None)
1700
1700
1701 needfiles = {}
1701 needfiles = {}
1702 if self.ui.configbool('server', 'validate', default=False):
1702 if self.ui.configbool('server', 'validate', default=False):
1703 # validate incoming csets have their manifests
1703 # validate incoming csets have their manifests
1704 for cset in xrange(clstart, clend):
1704 for cset in xrange(clstart, clend):
1705 mfest = self.changelog.read(self.changelog.node(cset))[0]
1705 mfest = self.changelog.read(self.changelog.node(cset))[0]
1706 mfest = self.manifest.readdelta(mfest)
1706 mfest = self.manifest.readdelta(mfest)
1707 # store file nodes we must see
1707 # store file nodes we must see
1708 for f, n in mfest.iteritems():
1708 for f, n in mfest.iteritems():
1709 needfiles.setdefault(f, set()).add(n)
1709 needfiles.setdefault(f, set()).add(n)
1710
1710
1711 # process the files
1711 # process the files
1712 self.ui.status(_("adding file changes\n"))
1712 self.ui.status(_("adding file changes\n"))
1713 pr.step = 'files'
1713 pr.step = 'files'
1714 pr.count = 1
1714 pr.count = 1
1715 pr.total = efiles
1715 pr.total = efiles
1716 source.callback = None
1716 source.callback = None
1717
1717
1718 while 1:
1718 while 1:
1719 f = source.chunk()
1719 f = source.chunk()
1720 if not f:
1720 if not f:
1721 break
1721 break
1722 self.ui.debug("adding %s revisions\n" % f)
1722 self.ui.debug("adding %s revisions\n" % f)
1723 pr()
1723 pr()
1724 fl = self.file(f)
1724 fl = self.file(f)
1725 o = len(fl)
1725 o = len(fl)
1726 if fl.addgroup(source.chunks(), revmap, trp) is None:
1726 if fl.addgroup(source, revmap, trp) is None:
1727 raise util.Abort(_("received file revlog group is empty"))
1727 raise util.Abort(_("received file revlog group is empty"))
1728 revisions += len(fl) - o
1728 revisions += len(fl) - o
1729 files += 1
1729 files += 1
1730 if f in needfiles:
1730 if f in needfiles:
1731 needs = needfiles[f]
1731 needs = needfiles[f]
1732 for new in xrange(o, len(fl)):
1732 for new in xrange(o, len(fl)):
1733 n = fl.node(new)
1733 n = fl.node(new)
1734 if n in needs:
1734 if n in needs:
1735 needs.remove(n)
1735 needs.remove(n)
1736 if not needs:
1736 if not needs:
1737 del needfiles[f]
1737 del needfiles[f]
1738 self.ui.progress(_('files'), None)
1738 self.ui.progress(_('files'), None)
1739
1739
1740 for f, needs in needfiles.iteritems():
1740 for f, needs in needfiles.iteritems():
1741 fl = self.file(f)
1741 fl = self.file(f)
1742 for n in needs:
1742 for n in needs:
1743 try:
1743 try:
1744 fl.rev(n)
1744 fl.rev(n)
1745 except error.LookupError:
1745 except error.LookupError:
1746 raise util.Abort(
1746 raise util.Abort(
1747 _('missing file data for %s:%s - run hg verify') %
1747 _('missing file data for %s:%s - run hg verify') %
1748 (f, hex(n)))
1748 (f, hex(n)))
1749
1749
1750 newheads = len(cl.heads())
1750 newheads = len(cl.heads())
1751 heads = ""
1751 heads = ""
1752 if oldheads and newheads != oldheads:
1752 if oldheads and newheads != oldheads:
1753 heads = _(" (%+d heads)") % (newheads - oldheads)
1753 heads = _(" (%+d heads)") % (newheads - oldheads)
1754
1754
1755 self.ui.status(_("added %d changesets"
1755 self.ui.status(_("added %d changesets"
1756 " with %d changes to %d files%s\n")
1756 " with %d changes to %d files%s\n")
1757 % (changesets, revisions, files, heads))
1757 % (changesets, revisions, files, heads))
1758
1758
1759 if changesets > 0:
1759 if changesets > 0:
1760 p = lambda: cl.writepending() and self.root or ""
1760 p = lambda: cl.writepending() and self.root or ""
1761 self.hook('pretxnchangegroup', throw=True,
1761 self.hook('pretxnchangegroup', throw=True,
1762 node=hex(cl.node(clstart)), source=srctype,
1762 node=hex(cl.node(clstart)), source=srctype,
1763 url=url, pending=p)
1763 url=url, pending=p)
1764
1764
1765 # make changelog see real files again
1765 # make changelog see real files again
1766 cl.finalize(trp)
1766 cl.finalize(trp)
1767
1767
1768 tr.close()
1768 tr.close()
1769 finally:
1769 finally:
1770 tr.release()
1770 tr.release()
1771 if lock:
1771 if lock:
1772 lock.release()
1772 lock.release()
1773
1773
1774 if changesets > 0:
1774 if changesets > 0:
1775 # forcefully update the on-disk branch cache
1775 # forcefully update the on-disk branch cache
1776 self.ui.debug("updating the branch cache\n")
1776 self.ui.debug("updating the branch cache\n")
1777 self.updatebranchcache()
1777 self.updatebranchcache()
1778 self.hook("changegroup", node=hex(cl.node(clstart)),
1778 self.hook("changegroup", node=hex(cl.node(clstart)),
1779 source=srctype, url=url)
1779 source=srctype, url=url)
1780
1780
1781 for i in xrange(clstart, clend):
1781 for i in xrange(clstart, clend):
1782 self.hook("incoming", node=hex(cl.node(i)),
1782 self.hook("incoming", node=hex(cl.node(i)),
1783 source=srctype, url=url)
1783 source=srctype, url=url)
1784
1784
1785 # never return 0 here:
1785 # never return 0 here:
1786 if newheads < oldheads:
1786 if newheads < oldheads:
1787 return newheads - oldheads - 1
1787 return newheads - oldheads - 1
1788 else:
1788 else:
1789 return newheads - oldheads + 1
1789 return newheads - oldheads + 1
1790
1790
1791
1791
1792 def stream_in(self, remote, requirements):
1792 def stream_in(self, remote, requirements):
1793 fp = remote.stream_out()
1793 fp = remote.stream_out()
1794 l = fp.readline()
1794 l = fp.readline()
1795 try:
1795 try:
1796 resp = int(l)
1796 resp = int(l)
1797 except ValueError:
1797 except ValueError:
1798 raise error.ResponseError(
1798 raise error.ResponseError(
1799 _('Unexpected response from remote server:'), l)
1799 _('Unexpected response from remote server:'), l)
1800 if resp == 1:
1800 if resp == 1:
1801 raise util.Abort(_('operation forbidden by server'))
1801 raise util.Abort(_('operation forbidden by server'))
1802 elif resp == 2:
1802 elif resp == 2:
1803 raise util.Abort(_('locking the remote repository failed'))
1803 raise util.Abort(_('locking the remote repository failed'))
1804 elif resp != 0:
1804 elif resp != 0:
1805 raise util.Abort(_('the server sent an unknown error code'))
1805 raise util.Abort(_('the server sent an unknown error code'))
1806 self.ui.status(_('streaming all changes\n'))
1806 self.ui.status(_('streaming all changes\n'))
1807 l = fp.readline()
1807 l = fp.readline()
1808 try:
1808 try:
1809 total_files, total_bytes = map(int, l.split(' ', 1))
1809 total_files, total_bytes = map(int, l.split(' ', 1))
1810 except (ValueError, TypeError):
1810 except (ValueError, TypeError):
1811 raise error.ResponseError(
1811 raise error.ResponseError(
1812 _('Unexpected response from remote server:'), l)
1812 _('Unexpected response from remote server:'), l)
1813 self.ui.status(_('%d files to transfer, %s of data\n') %
1813 self.ui.status(_('%d files to transfer, %s of data\n') %
1814 (total_files, util.bytecount(total_bytes)))
1814 (total_files, util.bytecount(total_bytes)))
1815 start = time.time()
1815 start = time.time()
1816 for i in xrange(total_files):
1816 for i in xrange(total_files):
1817 # XXX doesn't support '\n' or '\r' in filenames
1817 # XXX doesn't support '\n' or '\r' in filenames
1818 l = fp.readline()
1818 l = fp.readline()
1819 try:
1819 try:
1820 name, size = l.split('\0', 1)
1820 name, size = l.split('\0', 1)
1821 size = int(size)
1821 size = int(size)
1822 except (ValueError, TypeError):
1822 except (ValueError, TypeError):
1823 raise error.ResponseError(
1823 raise error.ResponseError(
1824 _('Unexpected response from remote server:'), l)
1824 _('Unexpected response from remote server:'), l)
1825 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1825 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1826 # for backwards compat, name was partially encoded
1826 # for backwards compat, name was partially encoded
1827 ofp = self.sopener(store.decodedir(name), 'w')
1827 ofp = self.sopener(store.decodedir(name), 'w')
1828 for chunk in util.filechunkiter(fp, limit=size):
1828 for chunk in util.filechunkiter(fp, limit=size):
1829 ofp.write(chunk)
1829 ofp.write(chunk)
1830 ofp.close()
1830 ofp.close()
1831 elapsed = time.time() - start
1831 elapsed = time.time() - start
1832 if elapsed <= 0:
1832 if elapsed <= 0:
1833 elapsed = 0.001
1833 elapsed = 0.001
1834 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1834 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1835 (util.bytecount(total_bytes), elapsed,
1835 (util.bytecount(total_bytes), elapsed,
1836 util.bytecount(total_bytes / elapsed)))
1836 util.bytecount(total_bytes / elapsed)))
1837
1837
1838 # new requirements = old non-format requirements + new format-related
1838 # new requirements = old non-format requirements + new format-related
1839 # requirements from the streamed-in repository
1839 # requirements from the streamed-in repository
1840 requirements.update(set(self.requirements) - self.supportedformats)
1840 requirements.update(set(self.requirements) - self.supportedformats)
1841 self._applyrequirements(requirements)
1841 self._applyrequirements(requirements)
1842 self._writerequirements()
1842 self._writerequirements()
1843
1843
1844 self.invalidate()
1844 self.invalidate()
1845 return len(self.heads()) + 1
1845 return len(self.heads()) + 1
1846
1846
1847 def clone(self, remote, heads=[], stream=False):
1847 def clone(self, remote, heads=[], stream=False):
1848 '''clone remote repository.
1848 '''clone remote repository.
1849
1849
1850 keyword arguments:
1850 keyword arguments:
1851 heads: list of revs to clone (forces use of pull)
1851 heads: list of revs to clone (forces use of pull)
1852 stream: use streaming clone if possible'''
1852 stream: use streaming clone if possible'''
1853
1853
1854 # now, all clients that can request uncompressed clones can
1854 # now, all clients that can request uncompressed clones can
1855 # read repo formats supported by all servers that can serve
1855 # read repo formats supported by all servers that can serve
1856 # them.
1856 # them.
1857
1857
1858 # if revlog format changes, client will have to check version
1858 # if revlog format changes, client will have to check version
1859 # and format flags on "stream" capability, and use
1859 # and format flags on "stream" capability, and use
1860 # uncompressed only if compatible.
1860 # uncompressed only if compatible.
1861
1861
1862 if stream and not heads:
1862 if stream and not heads:
1863 # 'stream' means remote revlog format is revlogv1 only
1863 # 'stream' means remote revlog format is revlogv1 only
1864 if remote.capable('stream'):
1864 if remote.capable('stream'):
1865 return self.stream_in(remote, set(('revlogv1',)))
1865 return self.stream_in(remote, set(('revlogv1',)))
1866 # otherwise, 'streamreqs' contains the remote revlog format
1866 # otherwise, 'streamreqs' contains the remote revlog format
1867 streamreqs = remote.capable('streamreqs')
1867 streamreqs = remote.capable('streamreqs')
1868 if streamreqs:
1868 if streamreqs:
1869 streamreqs = set(streamreqs.split(','))
1869 streamreqs = set(streamreqs.split(','))
1870 # if we support it, stream in and adjust our requirements
1870 # if we support it, stream in and adjust our requirements
1871 if not streamreqs - self.supportedformats:
1871 if not streamreqs - self.supportedformats:
1872 return self.stream_in(remote, streamreqs)
1872 return self.stream_in(remote, streamreqs)
1873 return self.pull(remote, heads)
1873 return self.pull(remote, heads)
1874
1874
1875 def pushkey(self, namespace, key, old, new):
1875 def pushkey(self, namespace, key, old, new):
1876 return pushkey.push(self, namespace, key, old, new)
1876 return pushkey.push(self, namespace, key, old, new)
1877
1877
1878 def listkeys(self, namespace):
1878 def listkeys(self, namespace):
1879 return pushkey.list(self, namespace)
1879 return pushkey.list(self, namespace)
1880
1880
1881 # used to avoid circular references so destructors work
1881 # used to avoid circular references so destructors work
1882 def aftertrans(files):
1882 def aftertrans(files):
1883 renamefiles = [tuple(t) for t in files]
1883 renamefiles = [tuple(t) for t in files]
1884 def a():
1884 def a():
1885 for src, dest in renamefiles:
1885 for src, dest in renamefiles:
1886 util.rename(src, dest)
1886 util.rename(src, dest)
1887 return a
1887 return a
1888
1888
1889 def instance(ui, path, create):
1889 def instance(ui, path, create):
1890 return localrepository(ui, util.drop_scheme('file', path), create)
1890 return localrepository(ui, util.drop_scheme('file', path), create)
1891
1891
1892 def islocal(path):
1892 def islocal(path):
1893 return True
1893 return True
@@ -1,1481 +1,1484 b''
1 # revlog.py - storage back-end for mercurial
1 # revlog.py - storage back-end for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """Storage back-end for Mercurial.
8 """Storage back-end for Mercurial.
9
9
10 This provides efficient delta storage with O(1) retrieve and append
10 This provides efficient delta storage with O(1) retrieve and append
11 and O(changes) merge between branches.
11 and O(changes) merge between branches.
12 """
12 """
13
13
14 # import stuff from node for others to import from revlog
14 # import stuff from node for others to import from revlog
15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
16 from i18n import _
16 from i18n import _
17 import changegroup, ancestor, mdiff, parsers, error, util
17 import changegroup, ancestor, mdiff, parsers, error, util
18 import struct, zlib, errno
18 import struct, zlib, errno
19
19
20 _pack = struct.pack
20 _pack = struct.pack
21 _unpack = struct.unpack
21 _unpack = struct.unpack
22 _compress = zlib.compress
22 _compress = zlib.compress
23 _decompress = zlib.decompress
23 _decompress = zlib.decompress
24 _sha = util.sha1
24 _sha = util.sha1
25
25
26 # revlog header flags
26 # revlog header flags
27 REVLOGV0 = 0
27 REVLOGV0 = 0
28 REVLOGNG = 1
28 REVLOGNG = 1
29 REVLOGNGINLINEDATA = (1 << 16)
29 REVLOGNGINLINEDATA = (1 << 16)
30 REVLOGSHALLOW = (1 << 17)
30 REVLOGSHALLOW = (1 << 17)
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGSHALLOW
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGSHALLOW
35
35
36 # revlog index flags
36 # revlog index flags
37 REVIDX_PARENTDELTA = 1
37 REVIDX_PARENTDELTA = 1
38 REVIDX_PUNCHED_FLAG = 2
38 REVIDX_PUNCHED_FLAG = 2
39 REVIDX_KNOWN_FLAGS = REVIDX_PUNCHED_FLAG | REVIDX_PARENTDELTA
39 REVIDX_KNOWN_FLAGS = REVIDX_PUNCHED_FLAG | REVIDX_PARENTDELTA
40
40
41 # amount of data read unconditionally, should be >= 4
41 # amount of data read unconditionally, should be >= 4
42 # when not inline: threshold for using lazy index
42 # when not inline: threshold for using lazy index
43 _prereadsize = 1048576
43 _prereadsize = 1048576
44 # max size of revlog with inline data
44 # max size of revlog with inline data
45 _maxinline = 131072
45 _maxinline = 131072
46
46
47 RevlogError = error.RevlogError
47 RevlogError = error.RevlogError
48 LookupError = error.LookupError
48 LookupError = error.LookupError
49
49
50 def getoffset(q):
50 def getoffset(q):
51 return int(q >> 16)
51 return int(q >> 16)
52
52
53 def gettype(q):
53 def gettype(q):
54 return int(q & 0xFFFF)
54 return int(q & 0xFFFF)
55
55
56 def offset_type(offset, type):
56 def offset_type(offset, type):
57 return long(long(offset) << 16 | type)
57 return long(long(offset) << 16 | type)
58
58
59 nullhash = _sha(nullid)
59 nullhash = _sha(nullid)
60
60
61 def hash(text, p1, p2):
61 def hash(text, p1, p2):
62 """generate a hash from the given text and its parent hashes
62 """generate a hash from the given text and its parent hashes
63
63
64 This hash combines both the current file contents and its history
64 This hash combines both the current file contents and its history
65 in a manner that makes it easy to distinguish nodes with the same
65 in a manner that makes it easy to distinguish nodes with the same
66 content in the revision graph.
66 content in the revision graph.
67 """
67 """
68 # As of now, if one of the parent node is null, p2 is null
68 # As of now, if one of the parent node is null, p2 is null
69 if p2 == nullid:
69 if p2 == nullid:
70 # deep copy of a hash is faster than creating one
70 # deep copy of a hash is faster than creating one
71 s = nullhash.copy()
71 s = nullhash.copy()
72 s.update(p1)
72 s.update(p1)
73 else:
73 else:
74 # none of the parent nodes are nullid
74 # none of the parent nodes are nullid
75 l = [p1, p2]
75 l = [p1, p2]
76 l.sort()
76 l.sort()
77 s = _sha(l[0])
77 s = _sha(l[0])
78 s.update(l[1])
78 s.update(l[1])
79 s.update(text)
79 s.update(text)
80 return s.digest()
80 return s.digest()
81
81
82 def compress(text):
82 def compress(text):
83 """ generate a possibly-compressed representation of text """
83 """ generate a possibly-compressed representation of text """
84 if not text:
84 if not text:
85 return ("", text)
85 return ("", text)
86 l = len(text)
86 l = len(text)
87 bin = None
87 bin = None
88 if l < 44:
88 if l < 44:
89 pass
89 pass
90 elif l > 1000000:
90 elif l > 1000000:
91 # zlib makes an internal copy, thus doubling memory usage for
91 # zlib makes an internal copy, thus doubling memory usage for
92 # large files, so lets do this in pieces
92 # large files, so lets do this in pieces
93 z = zlib.compressobj()
93 z = zlib.compressobj()
94 p = []
94 p = []
95 pos = 0
95 pos = 0
96 while pos < l:
96 while pos < l:
97 pos2 = pos + 2**20
97 pos2 = pos + 2**20
98 p.append(z.compress(text[pos:pos2]))
98 p.append(z.compress(text[pos:pos2]))
99 pos = pos2
99 pos = pos2
100 p.append(z.flush())
100 p.append(z.flush())
101 if sum(map(len, p)) < l:
101 if sum(map(len, p)) < l:
102 bin = "".join(p)
102 bin = "".join(p)
103 else:
103 else:
104 bin = _compress(text)
104 bin = _compress(text)
105 if bin is None or len(bin) > l:
105 if bin is None or len(bin) > l:
106 if text[0] == '\0':
106 if text[0] == '\0':
107 return ("", text)
107 return ("", text)
108 return ('u', text)
108 return ('u', text)
109 return ("", bin)
109 return ("", bin)
110
110
111 def decompress(bin):
111 def decompress(bin):
112 """ decompress the given input """
112 """ decompress the given input """
113 if not bin:
113 if not bin:
114 return bin
114 return bin
115 t = bin[0]
115 t = bin[0]
116 if t == '\0':
116 if t == '\0':
117 return bin
117 return bin
118 if t == 'x':
118 if t == 'x':
119 return _decompress(bin)
119 return _decompress(bin)
120 if t == 'u':
120 if t == 'u':
121 return bin[1:]
121 return bin[1:]
122 raise RevlogError(_("unknown compression type %r") % t)
122 raise RevlogError(_("unknown compression type %r") % t)
123
123
124 class lazyparser(object):
124 class lazyparser(object):
125 """
125 """
126 this class avoids the need to parse the entirety of large indices
126 this class avoids the need to parse the entirety of large indices
127 """
127 """
128
128
129 # lazyparser is not safe to use on windows if win32 extensions not
129 # lazyparser is not safe to use on windows if win32 extensions not
130 # available. it keeps file handle open, which make it not possible
130 # available. it keeps file handle open, which make it not possible
131 # to break hardlinks on local cloned repos.
131 # to break hardlinks on local cloned repos.
132
132
133 def __init__(self, dataf):
133 def __init__(self, dataf):
134 try:
134 try:
135 size = util.fstat(dataf).st_size
135 size = util.fstat(dataf).st_size
136 except AttributeError:
136 except AttributeError:
137 size = 0
137 size = 0
138 self.dataf = dataf
138 self.dataf = dataf
139 self.s = struct.calcsize(indexformatng)
139 self.s = struct.calcsize(indexformatng)
140 self.datasize = size
140 self.datasize = size
141 self.l = size // self.s
141 self.l = size // self.s
142 self.index = [None] * self.l
142 self.index = [None] * self.l
143 self.map = {nullid: nullrev}
143 self.map = {nullid: nullrev}
144 self.allmap = 0
144 self.allmap = 0
145 self.all = 0
145 self.all = 0
146 self.mapfind_count = 0
146 self.mapfind_count = 0
147
147
148 def loadmap(self):
148 def loadmap(self):
149 """
149 """
150 during a commit, we need to make sure the rev being added is
150 during a commit, we need to make sure the rev being added is
151 not a duplicate. This requires loading the entire index,
151 not a duplicate. This requires loading the entire index,
152 which is fairly slow. loadmap can load up just the node map,
152 which is fairly slow. loadmap can load up just the node map,
153 which takes much less time.
153 which takes much less time.
154 """
154 """
155 if self.allmap:
155 if self.allmap:
156 return
156 return
157 end = self.datasize
157 end = self.datasize
158 self.allmap = 1
158 self.allmap = 1
159 cur = 0
159 cur = 0
160 count = 0
160 count = 0
161 blocksize = self.s * 256
161 blocksize = self.s * 256
162 self.dataf.seek(0)
162 self.dataf.seek(0)
163 while cur < end:
163 while cur < end:
164 data = self.dataf.read(blocksize)
164 data = self.dataf.read(blocksize)
165 off = 0
165 off = 0
166 for x in xrange(256):
166 for x in xrange(256):
167 n = data[off + ngshaoffset:off + ngshaoffset + 20]
167 n = data[off + ngshaoffset:off + ngshaoffset + 20]
168 self.map[n] = count
168 self.map[n] = count
169 count += 1
169 count += 1
170 if count >= self.l:
170 if count >= self.l:
171 break
171 break
172 off += self.s
172 off += self.s
173 cur += blocksize
173 cur += blocksize
174
174
175 def loadblock(self, blockstart, blocksize, data=None):
175 def loadblock(self, blockstart, blocksize, data=None):
176 if self.all:
176 if self.all:
177 return
177 return
178 if data is None:
178 if data is None:
179 self.dataf.seek(blockstart)
179 self.dataf.seek(blockstart)
180 if blockstart + blocksize > self.datasize:
180 if blockstart + blocksize > self.datasize:
181 # the revlog may have grown since we've started running,
181 # the revlog may have grown since we've started running,
182 # but we don't have space in self.index for more entries.
182 # but we don't have space in self.index for more entries.
183 # limit blocksize so that we don't get too much data.
183 # limit blocksize so that we don't get too much data.
184 blocksize = max(self.datasize - blockstart, 0)
184 blocksize = max(self.datasize - blockstart, 0)
185 data = self.dataf.read(blocksize)
185 data = self.dataf.read(blocksize)
186 lend = len(data) // self.s
186 lend = len(data) // self.s
187 i = blockstart // self.s
187 i = blockstart // self.s
188 off = 0
188 off = 0
189 # lazyindex supports __delitem__
189 # lazyindex supports __delitem__
190 if lend > len(self.index) - i:
190 if lend > len(self.index) - i:
191 lend = len(self.index) - i
191 lend = len(self.index) - i
192 for x in xrange(lend):
192 for x in xrange(lend):
193 if self.index[i + x] is None:
193 if self.index[i + x] is None:
194 b = data[off : off + self.s]
194 b = data[off : off + self.s]
195 self.index[i + x] = b
195 self.index[i + x] = b
196 n = b[ngshaoffset:ngshaoffset + 20]
196 n = b[ngshaoffset:ngshaoffset + 20]
197 self.map[n] = i + x
197 self.map[n] = i + x
198 off += self.s
198 off += self.s
199
199
200 def findnode(self, node):
200 def findnode(self, node):
201 """search backwards through the index file for a specific node"""
201 """search backwards through the index file for a specific node"""
202 if self.allmap:
202 if self.allmap:
203 return None
203 return None
204
204
205 # hg log will cause many many searches for the manifest
205 # hg log will cause many many searches for the manifest
206 # nodes. After we get called a few times, just load the whole
206 # nodes. After we get called a few times, just load the whole
207 # thing.
207 # thing.
208 if self.mapfind_count > 8:
208 if self.mapfind_count > 8:
209 self.loadmap()
209 self.loadmap()
210 if node in self.map:
210 if node in self.map:
211 return node
211 return node
212 return None
212 return None
213 self.mapfind_count += 1
213 self.mapfind_count += 1
214 last = self.l - 1
214 last = self.l - 1
215 while self.index[last] != None:
215 while self.index[last] != None:
216 if last == 0:
216 if last == 0:
217 self.all = 1
217 self.all = 1
218 self.allmap = 1
218 self.allmap = 1
219 return None
219 return None
220 last -= 1
220 last -= 1
221 end = (last + 1) * self.s
221 end = (last + 1) * self.s
222 blocksize = self.s * 256
222 blocksize = self.s * 256
223 while end >= 0:
223 while end >= 0:
224 start = max(end - blocksize, 0)
224 start = max(end - blocksize, 0)
225 self.dataf.seek(start)
225 self.dataf.seek(start)
226 data = self.dataf.read(end - start)
226 data = self.dataf.read(end - start)
227 findend = end - start
227 findend = end - start
228 while True:
228 while True:
229 # we're searching backwards, so we have to make sure
229 # we're searching backwards, so we have to make sure
230 # we don't find a changeset where this node is a parent
230 # we don't find a changeset where this node is a parent
231 off = data.find(node, 0, findend)
231 off = data.find(node, 0, findend)
232 findend = off
232 findend = off
233 if off >= 0:
233 if off >= 0:
234 i = off / self.s
234 i = off / self.s
235 off = i * self.s
235 off = i * self.s
236 n = data[off + ngshaoffset:off + ngshaoffset + 20]
236 n = data[off + ngshaoffset:off + ngshaoffset + 20]
237 if n == node:
237 if n == node:
238 self.map[n] = i + start / self.s
238 self.map[n] = i + start / self.s
239 return node
239 return node
240 else:
240 else:
241 break
241 break
242 end -= blocksize
242 end -= blocksize
243 return None
243 return None
244
244
245 def loadindex(self, i=None, end=None):
245 def loadindex(self, i=None, end=None):
246 if self.all:
246 if self.all:
247 return
247 return
248 all = False
248 all = False
249 if i is None:
249 if i is None:
250 blockstart = 0
250 blockstart = 0
251 blocksize = (65536 / self.s) * self.s
251 blocksize = (65536 / self.s) * self.s
252 end = self.datasize
252 end = self.datasize
253 all = True
253 all = True
254 else:
254 else:
255 if end:
255 if end:
256 blockstart = i * self.s
256 blockstart = i * self.s
257 end = end * self.s
257 end = end * self.s
258 blocksize = end - blockstart
258 blocksize = end - blockstart
259 else:
259 else:
260 blockstart = (i & ~1023) * self.s
260 blockstart = (i & ~1023) * self.s
261 blocksize = self.s * 1024
261 blocksize = self.s * 1024
262 end = blockstart + blocksize
262 end = blockstart + blocksize
263 while blockstart < end:
263 while blockstart < end:
264 self.loadblock(blockstart, blocksize)
264 self.loadblock(blockstart, blocksize)
265 blockstart += blocksize
265 blockstart += blocksize
266 if all:
266 if all:
267 self.all = True
267 self.all = True
268
268
269 class lazyindex(object):
269 class lazyindex(object):
270 """a lazy version of the index array"""
270 """a lazy version of the index array"""
271 def __init__(self, parser):
271 def __init__(self, parser):
272 self.p = parser
272 self.p = parser
273 def __len__(self):
273 def __len__(self):
274 return len(self.p.index)
274 return len(self.p.index)
275 def load(self, pos):
275 def load(self, pos):
276 if pos < 0:
276 if pos < 0:
277 pos += len(self.p.index)
277 pos += len(self.p.index)
278 self.p.loadindex(pos)
278 self.p.loadindex(pos)
279 return self.p.index[pos]
279 return self.p.index[pos]
280 def __getitem__(self, pos):
280 def __getitem__(self, pos):
281 return _unpack(indexformatng, self.p.index[pos] or self.load(pos))
281 return _unpack(indexformatng, self.p.index[pos] or self.load(pos))
282 def __setitem__(self, pos, item):
282 def __setitem__(self, pos, item):
283 self.p.index[pos] = _pack(indexformatng, *item)
283 self.p.index[pos] = _pack(indexformatng, *item)
284 def __delitem__(self, pos):
284 def __delitem__(self, pos):
285 del self.p.index[pos]
285 del self.p.index[pos]
286 def insert(self, pos, e):
286 def insert(self, pos, e):
287 self.p.index.insert(pos, _pack(indexformatng, *e))
287 self.p.index.insert(pos, _pack(indexformatng, *e))
288 def append(self, e):
288 def append(self, e):
289 self.p.index.append(_pack(indexformatng, *e))
289 self.p.index.append(_pack(indexformatng, *e))
290
290
291 class lazymap(object):
291 class lazymap(object):
292 """a lazy version of the node map"""
292 """a lazy version of the node map"""
293 def __init__(self, parser):
293 def __init__(self, parser):
294 self.p = parser
294 self.p = parser
295 def load(self, key):
295 def load(self, key):
296 n = self.p.findnode(key)
296 n = self.p.findnode(key)
297 if n is None:
297 if n is None:
298 raise KeyError(key)
298 raise KeyError(key)
299 def __contains__(self, key):
299 def __contains__(self, key):
300 if key in self.p.map:
300 if key in self.p.map:
301 return True
301 return True
302 self.p.loadmap()
302 self.p.loadmap()
303 return key in self.p.map
303 return key in self.p.map
304 def __iter__(self):
304 def __iter__(self):
305 yield nullid
305 yield nullid
306 for i, ret in enumerate(self.p.index):
306 for i, ret in enumerate(self.p.index):
307 if not ret:
307 if not ret:
308 self.p.loadindex(i)
308 self.p.loadindex(i)
309 ret = self.p.index[i]
309 ret = self.p.index[i]
310 if isinstance(ret, str):
310 if isinstance(ret, str):
311 ret = _unpack(indexformatng, ret)
311 ret = _unpack(indexformatng, ret)
312 yield ret[7]
312 yield ret[7]
313 def __getitem__(self, key):
313 def __getitem__(self, key):
314 try:
314 try:
315 return self.p.map[key]
315 return self.p.map[key]
316 except KeyError:
316 except KeyError:
317 try:
317 try:
318 self.load(key)
318 self.load(key)
319 return self.p.map[key]
319 return self.p.map[key]
320 except KeyError:
320 except KeyError:
321 raise KeyError("node " + hex(key))
321 raise KeyError("node " + hex(key))
322 def __setitem__(self, key, val):
322 def __setitem__(self, key, val):
323 self.p.map[key] = val
323 self.p.map[key] = val
324 def __delitem__(self, key):
324 def __delitem__(self, key):
325 del self.p.map[key]
325 del self.p.map[key]
326
326
327 indexformatv0 = ">4l20s20s20s"
327 indexformatv0 = ">4l20s20s20s"
328 v0shaoffset = 56
328 v0shaoffset = 56
329
329
330 class revlogoldio(object):
330 class revlogoldio(object):
331 def __init__(self):
331 def __init__(self):
332 self.size = struct.calcsize(indexformatv0)
332 self.size = struct.calcsize(indexformatv0)
333
333
334 def parseindex(self, fp, data, inline):
334 def parseindex(self, fp, data, inline):
335 s = self.size
335 s = self.size
336 index = []
336 index = []
337 nodemap = {nullid: nullrev}
337 nodemap = {nullid: nullrev}
338 n = off = 0
338 n = off = 0
339 if len(data) == _prereadsize:
339 if len(data) == _prereadsize:
340 data += fp.read() # read the rest
340 data += fp.read() # read the rest
341 l = len(data)
341 l = len(data)
342 while off + s <= l:
342 while off + s <= l:
343 cur = data[off:off + s]
343 cur = data[off:off + s]
344 off += s
344 off += s
345 e = _unpack(indexformatv0, cur)
345 e = _unpack(indexformatv0, cur)
346 # transform to revlogv1 format
346 # transform to revlogv1 format
347 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
347 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
348 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
348 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
349 index.append(e2)
349 index.append(e2)
350 nodemap[e[6]] = n
350 nodemap[e[6]] = n
351 n += 1
351 n += 1
352
352
353 return index, nodemap, None
353 return index, nodemap, None
354
354
355 def packentry(self, entry, node, version, rev):
355 def packentry(self, entry, node, version, rev):
356 if gettype(entry[0]):
356 if gettype(entry[0]):
357 raise RevlogError(_("index entry flags need RevlogNG"))
357 raise RevlogError(_("index entry flags need RevlogNG"))
358 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
358 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
359 node(entry[5]), node(entry[6]), entry[7])
359 node(entry[5]), node(entry[6]), entry[7])
360 return _pack(indexformatv0, *e2)
360 return _pack(indexformatv0, *e2)
361
361
362 # index ng:
362 # index ng:
363 # 6 bytes: offset
363 # 6 bytes: offset
364 # 2 bytes: flags
364 # 2 bytes: flags
365 # 4 bytes: compressed length
365 # 4 bytes: compressed length
366 # 4 bytes: uncompressed length
366 # 4 bytes: uncompressed length
367 # 4 bytes: base rev
367 # 4 bytes: base rev
368 # 4 bytes: link rev
368 # 4 bytes: link rev
369 # 4 bytes: parent 1 rev
369 # 4 bytes: parent 1 rev
370 # 4 bytes: parent 2 rev
370 # 4 bytes: parent 2 rev
371 # 32 bytes: nodeid
371 # 32 bytes: nodeid
372 indexformatng = ">Qiiiiii20s12x"
372 indexformatng = ">Qiiiiii20s12x"
373 ngshaoffset = 32
373 ngshaoffset = 32
374 versionformat = ">I"
374 versionformat = ">I"
375
375
376 class revlogio(object):
376 class revlogio(object):
377 def __init__(self):
377 def __init__(self):
378 self.size = struct.calcsize(indexformatng)
378 self.size = struct.calcsize(indexformatng)
379
379
380 def parseindex(self, fp, data, inline):
380 def parseindex(self, fp, data, inline):
381 if len(data) == _prereadsize:
381 if len(data) == _prereadsize:
382 if util.openhardlinks() and not inline:
382 if util.openhardlinks() and not inline:
383 # big index, let's parse it on demand
383 # big index, let's parse it on demand
384 parser = lazyparser(fp)
384 parser = lazyparser(fp)
385 index = lazyindex(parser)
385 index = lazyindex(parser)
386 nodemap = lazymap(parser)
386 nodemap = lazymap(parser)
387 e = list(index[0])
387 e = list(index[0])
388 type = gettype(e[0])
388 type = gettype(e[0])
389 e[0] = offset_type(0, type)
389 e[0] = offset_type(0, type)
390 index[0] = e
390 index[0] = e
391 return index, nodemap, None
391 return index, nodemap, None
392 else:
392 else:
393 data += fp.read()
393 data += fp.read()
394
394
395 # call the C implementation to parse the index data
395 # call the C implementation to parse the index data
396 index, nodemap, cache = parsers.parse_index(data, inline)
396 index, nodemap, cache = parsers.parse_index(data, inline)
397 return index, nodemap, cache
397 return index, nodemap, cache
398
398
399 def packentry(self, entry, node, version, rev):
399 def packentry(self, entry, node, version, rev):
400 p = _pack(indexformatng, *entry)
400 p = _pack(indexformatng, *entry)
401 if rev == 0:
401 if rev == 0:
402 p = _pack(versionformat, version) + p[4:]
402 p = _pack(versionformat, version) + p[4:]
403 return p
403 return p
404
404
405 class revlog(object):
405 class revlog(object):
406 """
406 """
407 the underlying revision storage object
407 the underlying revision storage object
408
408
409 A revlog consists of two parts, an index and the revision data.
409 A revlog consists of two parts, an index and the revision data.
410
410
411 The index is a file with a fixed record size containing
411 The index is a file with a fixed record size containing
412 information on each revision, including its nodeid (hash), the
412 information on each revision, including its nodeid (hash), the
413 nodeids of its parents, the position and offset of its data within
413 nodeids of its parents, the position and offset of its data within
414 the data file, and the revision it's based on. Finally, each entry
414 the data file, and the revision it's based on. Finally, each entry
415 contains a linkrev entry that can serve as a pointer to external
415 contains a linkrev entry that can serve as a pointer to external
416 data.
416 data.
417
417
418 The revision data itself is a linear collection of data chunks.
418 The revision data itself is a linear collection of data chunks.
419 Each chunk represents a revision and is usually represented as a
419 Each chunk represents a revision and is usually represented as a
420 delta against the previous chunk. To bound lookup time, runs of
420 delta against the previous chunk. To bound lookup time, runs of
421 deltas are limited to about 2 times the length of the original
421 deltas are limited to about 2 times the length of the original
422 version data. This makes retrieval of a version proportional to
422 version data. This makes retrieval of a version proportional to
423 its size, or O(1) relative to the number of revisions.
423 its size, or O(1) relative to the number of revisions.
424
424
425 Both pieces of the revlog are written to in an append-only
425 Both pieces of the revlog are written to in an append-only
426 fashion, which means we never need to rewrite a file to insert or
426 fashion, which means we never need to rewrite a file to insert or
427 remove data, and can use some simple techniques to avoid the need
427 remove data, and can use some simple techniques to avoid the need
428 for locking while reading.
428 for locking while reading.
429 """
429 """
430 def __init__(self, opener, indexfile, shallowroot=None):
430 def __init__(self, opener, indexfile, shallowroot=None):
431 """
431 """
432 create a revlog object
432 create a revlog object
433
433
434 opener is a function that abstracts the file opening operation
434 opener is a function that abstracts the file opening operation
435 and can be used to implement COW semantics or the like.
435 and can be used to implement COW semantics or the like.
436 """
436 """
437 self.indexfile = indexfile
437 self.indexfile = indexfile
438 self.datafile = indexfile[:-2] + ".d"
438 self.datafile = indexfile[:-2] + ".d"
439 self.opener = opener
439 self.opener = opener
440 self._cache = None
440 self._cache = None
441 self._chunkcache = (0, '')
441 self._chunkcache = (0, '')
442 self.nodemap = {nullid: nullrev}
442 self.nodemap = {nullid: nullrev}
443 self.index = []
443 self.index = []
444 self._shallowroot = shallowroot
444 self._shallowroot = shallowroot
445 self._parentdelta = 0
445 self._parentdelta = 0
446
446
447 v = REVLOG_DEFAULT_VERSION
447 v = REVLOG_DEFAULT_VERSION
448 if hasattr(opener, 'options') and 'defversion' in opener.options:
448 if hasattr(opener, 'options') and 'defversion' in opener.options:
449 v = opener.options['defversion']
449 v = opener.options['defversion']
450 if v & REVLOGNG:
450 if v & REVLOGNG:
451 v |= REVLOGNGINLINEDATA
451 v |= REVLOGNGINLINEDATA
452 if v & REVLOGNG and 'parentdelta' in opener.options:
452 if v & REVLOGNG and 'parentdelta' in opener.options:
453 self._parentdelta = 1
453 self._parentdelta = 1
454
454
455 if shallowroot:
455 if shallowroot:
456 v |= REVLOGSHALLOW
456 v |= REVLOGSHALLOW
457
457
458 i = ''
458 i = ''
459 try:
459 try:
460 f = self.opener(self.indexfile)
460 f = self.opener(self.indexfile)
461 if "nonlazy" in getattr(self.opener, 'options', {}):
461 if "nonlazy" in getattr(self.opener, 'options', {}):
462 i = f.read()
462 i = f.read()
463 else:
463 else:
464 i = f.read(_prereadsize)
464 i = f.read(_prereadsize)
465 if len(i) > 0:
465 if len(i) > 0:
466 v = struct.unpack(versionformat, i[:4])[0]
466 v = struct.unpack(versionformat, i[:4])[0]
467 except IOError, inst:
467 except IOError, inst:
468 if inst.errno != errno.ENOENT:
468 if inst.errno != errno.ENOENT:
469 raise
469 raise
470
470
471 self.version = v
471 self.version = v
472 self._inline = v & REVLOGNGINLINEDATA
472 self._inline = v & REVLOGNGINLINEDATA
473 self._shallow = v & REVLOGSHALLOW
473 self._shallow = v & REVLOGSHALLOW
474 flags = v & ~0xFFFF
474 flags = v & ~0xFFFF
475 fmt = v & 0xFFFF
475 fmt = v & 0xFFFF
476 if fmt == REVLOGV0 and flags:
476 if fmt == REVLOGV0 and flags:
477 raise RevlogError(_("index %s unknown flags %#04x for format v0")
477 raise RevlogError(_("index %s unknown flags %#04x for format v0")
478 % (self.indexfile, flags >> 16))
478 % (self.indexfile, flags >> 16))
479 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
479 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
480 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
480 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
481 % (self.indexfile, flags >> 16))
481 % (self.indexfile, flags >> 16))
482 elif fmt > REVLOGNG:
482 elif fmt > REVLOGNG:
483 raise RevlogError(_("index %s unknown format %d")
483 raise RevlogError(_("index %s unknown format %d")
484 % (self.indexfile, fmt))
484 % (self.indexfile, fmt))
485
485
486 self._io = revlogio()
486 self._io = revlogio()
487 if self.version == REVLOGV0:
487 if self.version == REVLOGV0:
488 self._io = revlogoldio()
488 self._io = revlogoldio()
489 if i:
489 if i:
490 try:
490 try:
491 d = self._io.parseindex(f, i, self._inline)
491 d = self._io.parseindex(f, i, self._inline)
492 except (ValueError, IndexError):
492 except (ValueError, IndexError):
493 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
493 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
494 self.index, self.nodemap, self._chunkcache = d
494 self.index, self.nodemap, self._chunkcache = d
495 if not self._chunkcache:
495 if not self._chunkcache:
496 self._chunkclear()
496 self._chunkclear()
497
497
498 # add the magic null revision at -1 (if it hasn't been done already)
498 # add the magic null revision at -1 (if it hasn't been done already)
499 if (self.index == [] or isinstance(self.index, lazyindex) or
499 if (self.index == [] or isinstance(self.index, lazyindex) or
500 self.index[-1][7] != nullid) :
500 self.index[-1][7] != nullid) :
501 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
501 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
502
502
503 def _loadindex(self, start, end):
503 def _loadindex(self, start, end):
504 """load a block of indexes all at once from the lazy parser"""
504 """load a block of indexes all at once from the lazy parser"""
505 if isinstance(self.index, lazyindex):
505 if isinstance(self.index, lazyindex):
506 self.index.p.loadindex(start, end)
506 self.index.p.loadindex(start, end)
507
507
508 def _loadindexmap(self):
508 def _loadindexmap(self):
509 """loads both the map and the index from the lazy parser"""
509 """loads both the map and the index from the lazy parser"""
510 if isinstance(self.index, lazyindex):
510 if isinstance(self.index, lazyindex):
511 p = self.index.p
511 p = self.index.p
512 p.loadindex()
512 p.loadindex()
513 self.nodemap = p.map
513 self.nodemap = p.map
514
514
515 def _loadmap(self):
515 def _loadmap(self):
516 """loads the map from the lazy parser"""
516 """loads the map from the lazy parser"""
517 if isinstance(self.nodemap, lazymap):
517 if isinstance(self.nodemap, lazymap):
518 self.nodemap.p.loadmap()
518 self.nodemap.p.loadmap()
519 self.nodemap = self.nodemap.p.map
519 self.nodemap = self.nodemap.p.map
520
520
521 def tip(self):
521 def tip(self):
522 return self.node(len(self.index) - 2)
522 return self.node(len(self.index) - 2)
523 def __len__(self):
523 def __len__(self):
524 return len(self.index) - 1
524 return len(self.index) - 1
525 def __iter__(self):
525 def __iter__(self):
526 for i in xrange(len(self)):
526 for i in xrange(len(self)):
527 yield i
527 yield i
528 def rev(self, node):
528 def rev(self, node):
529 try:
529 try:
530 return self.nodemap[node]
530 return self.nodemap[node]
531 except KeyError:
531 except KeyError:
532 raise LookupError(node, self.indexfile, _('no node'))
532 raise LookupError(node, self.indexfile, _('no node'))
533 def node(self, rev):
533 def node(self, rev):
534 return self.index[rev][7]
534 return self.index[rev][7]
535 def linkrev(self, rev):
535 def linkrev(self, rev):
536 return self.index[rev][4]
536 return self.index[rev][4]
537 def parents(self, node):
537 def parents(self, node):
538 i = self.index
538 i = self.index
539 d = i[self.rev(node)]
539 d = i[self.rev(node)]
540 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
540 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
541 def parentrevs(self, rev):
541 def parentrevs(self, rev):
542 return self.index[rev][5:7]
542 return self.index[rev][5:7]
543 def start(self, rev):
543 def start(self, rev):
544 return int(self.index[rev][0] >> 16)
544 return int(self.index[rev][0] >> 16)
545 def end(self, rev):
545 def end(self, rev):
546 return self.start(rev) + self.length(rev)
546 return self.start(rev) + self.length(rev)
547 def length(self, rev):
547 def length(self, rev):
548 return self.index[rev][1]
548 return self.index[rev][1]
549 def base(self, rev):
549 def base(self, rev):
550 return self.index[rev][3]
550 return self.index[rev][3]
551 def flags(self, rev):
551 def flags(self, rev):
552 return self.index[rev][0] & 0xFFFF
552 return self.index[rev][0] & 0xFFFF
553 def rawsize(self, rev):
553 def rawsize(self, rev):
554 """return the length of the uncompressed text for a given revision"""
554 """return the length of the uncompressed text for a given revision"""
555 l = self.index[rev][2]
555 l = self.index[rev][2]
556 if l >= 0:
556 if l >= 0:
557 return l
557 return l
558
558
559 t = self.revision(self.node(rev))
559 t = self.revision(self.node(rev))
560 return len(t)
560 return len(t)
561 size = rawsize
561 size = rawsize
562
562
563 def reachable(self, node, stop=None):
563 def reachable(self, node, stop=None):
564 """return the set of all nodes ancestral to a given node, including
564 """return the set of all nodes ancestral to a given node, including
565 the node itself, stopping when stop is matched"""
565 the node itself, stopping when stop is matched"""
566 reachable = set((node,))
566 reachable = set((node,))
567 visit = [node]
567 visit = [node]
568 if stop:
568 if stop:
569 stopn = self.rev(stop)
569 stopn = self.rev(stop)
570 else:
570 else:
571 stopn = 0
571 stopn = 0
572 while visit:
572 while visit:
573 n = visit.pop(0)
573 n = visit.pop(0)
574 if n == stop:
574 if n == stop:
575 continue
575 continue
576 if n == nullid:
576 if n == nullid:
577 continue
577 continue
578 for p in self.parents(n):
578 for p in self.parents(n):
579 if self.rev(p) < stopn:
579 if self.rev(p) < stopn:
580 continue
580 continue
581 if p not in reachable:
581 if p not in reachable:
582 reachable.add(p)
582 reachable.add(p)
583 visit.append(p)
583 visit.append(p)
584 return reachable
584 return reachable
585
585
586 def ancestors(self, *revs):
586 def ancestors(self, *revs):
587 """Generate the ancestors of 'revs' in reverse topological order.
587 """Generate the ancestors of 'revs' in reverse topological order.
588
588
589 Yield a sequence of revision numbers starting with the parents
589 Yield a sequence of revision numbers starting with the parents
590 of each revision in revs, i.e., each revision is *not* considered
590 of each revision in revs, i.e., each revision is *not* considered
591 an ancestor of itself. Results are in breadth-first order:
591 an ancestor of itself. Results are in breadth-first order:
592 parents of each rev in revs, then parents of those, etc. Result
592 parents of each rev in revs, then parents of those, etc. Result
593 does not include the null revision."""
593 does not include the null revision."""
594 visit = list(revs)
594 visit = list(revs)
595 seen = set([nullrev])
595 seen = set([nullrev])
596 while visit:
596 while visit:
597 for parent in self.parentrevs(visit.pop(0)):
597 for parent in self.parentrevs(visit.pop(0)):
598 if parent not in seen:
598 if parent not in seen:
599 visit.append(parent)
599 visit.append(parent)
600 seen.add(parent)
600 seen.add(parent)
601 yield parent
601 yield parent
602
602
603 def descendants(self, *revs):
603 def descendants(self, *revs):
604 """Generate the descendants of 'revs' in revision order.
604 """Generate the descendants of 'revs' in revision order.
605
605
606 Yield a sequence of revision numbers starting with a child of
606 Yield a sequence of revision numbers starting with a child of
607 some rev in revs, i.e., each revision is *not* considered a
607 some rev in revs, i.e., each revision is *not* considered a
608 descendant of itself. Results are ordered by revision number (a
608 descendant of itself. Results are ordered by revision number (a
609 topological sort)."""
609 topological sort)."""
610 seen = set(revs)
610 seen = set(revs)
611 for i in xrange(min(revs) + 1, len(self)):
611 for i in xrange(min(revs) + 1, len(self)):
612 for x in self.parentrevs(i):
612 for x in self.parentrevs(i):
613 if x != nullrev and x in seen:
613 if x != nullrev and x in seen:
614 seen.add(i)
614 seen.add(i)
615 yield i
615 yield i
616 break
616 break
617
617
618 def findmissing(self, common=None, heads=None):
618 def findmissing(self, common=None, heads=None):
619 """Return the ancestors of heads that are not ancestors of common.
619 """Return the ancestors of heads that are not ancestors of common.
620
620
621 More specifically, return a list of nodes N such that every N
621 More specifically, return a list of nodes N such that every N
622 satisfies the following constraints:
622 satisfies the following constraints:
623
623
624 1. N is an ancestor of some node in 'heads'
624 1. N is an ancestor of some node in 'heads'
625 2. N is not an ancestor of any node in 'common'
625 2. N is not an ancestor of any node in 'common'
626
626
627 The list is sorted by revision number, meaning it is
627 The list is sorted by revision number, meaning it is
628 topologically sorted.
628 topologically sorted.
629
629
630 'heads' and 'common' are both lists of node IDs. If heads is
630 'heads' and 'common' are both lists of node IDs. If heads is
631 not supplied, uses all of the revlog's heads. If common is not
631 not supplied, uses all of the revlog's heads. If common is not
632 supplied, uses nullid."""
632 supplied, uses nullid."""
633 if common is None:
633 if common is None:
634 common = [nullid]
634 common = [nullid]
635 if heads is None:
635 if heads is None:
636 heads = self.heads()
636 heads = self.heads()
637
637
638 common = [self.rev(n) for n in common]
638 common = [self.rev(n) for n in common]
639 heads = [self.rev(n) for n in heads]
639 heads = [self.rev(n) for n in heads]
640
640
641 # we want the ancestors, but inclusive
641 # we want the ancestors, but inclusive
642 has = set(self.ancestors(*common))
642 has = set(self.ancestors(*common))
643 has.add(nullrev)
643 has.add(nullrev)
644 has.update(common)
644 has.update(common)
645
645
646 # take all ancestors from heads that aren't in has
646 # take all ancestors from heads that aren't in has
647 missing = set()
647 missing = set()
648 visit = [r for r in heads if r not in has]
648 visit = [r for r in heads if r not in has]
649 while visit:
649 while visit:
650 r = visit.pop(0)
650 r = visit.pop(0)
651 if r in missing:
651 if r in missing:
652 continue
652 continue
653 else:
653 else:
654 missing.add(r)
654 missing.add(r)
655 for p in self.parentrevs(r):
655 for p in self.parentrevs(r):
656 if p not in has:
656 if p not in has:
657 visit.append(p)
657 visit.append(p)
658 missing = list(missing)
658 missing = list(missing)
659 missing.sort()
659 missing.sort()
660 return [self.node(r) for r in missing]
660 return [self.node(r) for r in missing]
661
661
662 def nodesbetween(self, roots=None, heads=None):
662 def nodesbetween(self, roots=None, heads=None):
663 """Return a topological path from 'roots' to 'heads'.
663 """Return a topological path from 'roots' to 'heads'.
664
664
665 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
665 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
666 topologically sorted list of all nodes N that satisfy both of
666 topologically sorted list of all nodes N that satisfy both of
667 these constraints:
667 these constraints:
668
668
669 1. N is a descendant of some node in 'roots'
669 1. N is a descendant of some node in 'roots'
670 2. N is an ancestor of some node in 'heads'
670 2. N is an ancestor of some node in 'heads'
671
671
672 Every node is considered to be both a descendant and an ancestor
672 Every node is considered to be both a descendant and an ancestor
673 of itself, so every reachable node in 'roots' and 'heads' will be
673 of itself, so every reachable node in 'roots' and 'heads' will be
674 included in 'nodes'.
674 included in 'nodes'.
675
675
676 'outroots' is the list of reachable nodes in 'roots', i.e., the
676 'outroots' is the list of reachable nodes in 'roots', i.e., the
677 subset of 'roots' that is returned in 'nodes'. Likewise,
677 subset of 'roots' that is returned in 'nodes'. Likewise,
678 'outheads' is the subset of 'heads' that is also in 'nodes'.
678 'outheads' is the subset of 'heads' that is also in 'nodes'.
679
679
680 'roots' and 'heads' are both lists of node IDs. If 'roots' is
680 'roots' and 'heads' are both lists of node IDs. If 'roots' is
681 unspecified, uses nullid as the only root. If 'heads' is
681 unspecified, uses nullid as the only root. If 'heads' is
682 unspecified, uses list of all of the revlog's heads."""
682 unspecified, uses list of all of the revlog's heads."""
683 nonodes = ([], [], [])
683 nonodes = ([], [], [])
684 if roots is not None:
684 if roots is not None:
685 roots = list(roots)
685 roots = list(roots)
686 if not roots:
686 if not roots:
687 return nonodes
687 return nonodes
688 lowestrev = min([self.rev(n) for n in roots])
688 lowestrev = min([self.rev(n) for n in roots])
689 else:
689 else:
690 roots = [nullid] # Everybody's a descendent of nullid
690 roots = [nullid] # Everybody's a descendent of nullid
691 lowestrev = nullrev
691 lowestrev = nullrev
692 if (lowestrev == nullrev) and (heads is None):
692 if (lowestrev == nullrev) and (heads is None):
693 # We want _all_ the nodes!
693 # We want _all_ the nodes!
694 return ([self.node(r) for r in self], [nullid], list(self.heads()))
694 return ([self.node(r) for r in self], [nullid], list(self.heads()))
695 if heads is None:
695 if heads is None:
696 # All nodes are ancestors, so the latest ancestor is the last
696 # All nodes are ancestors, so the latest ancestor is the last
697 # node.
697 # node.
698 highestrev = len(self) - 1
698 highestrev = len(self) - 1
699 # Set ancestors to None to signal that every node is an ancestor.
699 # Set ancestors to None to signal that every node is an ancestor.
700 ancestors = None
700 ancestors = None
701 # Set heads to an empty dictionary for later discovery of heads
701 # Set heads to an empty dictionary for later discovery of heads
702 heads = {}
702 heads = {}
703 else:
703 else:
704 heads = list(heads)
704 heads = list(heads)
705 if not heads:
705 if not heads:
706 return nonodes
706 return nonodes
707 ancestors = set()
707 ancestors = set()
708 # Turn heads into a dictionary so we can remove 'fake' heads.
708 # Turn heads into a dictionary so we can remove 'fake' heads.
709 # Also, later we will be using it to filter out the heads we can't
709 # Also, later we will be using it to filter out the heads we can't
710 # find from roots.
710 # find from roots.
711 heads = dict.fromkeys(heads, 0)
711 heads = dict.fromkeys(heads, 0)
712 # Start at the top and keep marking parents until we're done.
712 # Start at the top and keep marking parents until we're done.
713 nodestotag = set(heads)
713 nodestotag = set(heads)
714 # Remember where the top was so we can use it as a limit later.
714 # Remember where the top was so we can use it as a limit later.
715 highestrev = max([self.rev(n) for n in nodestotag])
715 highestrev = max([self.rev(n) for n in nodestotag])
716 while nodestotag:
716 while nodestotag:
717 # grab a node to tag
717 # grab a node to tag
718 n = nodestotag.pop()
718 n = nodestotag.pop()
719 # Never tag nullid
719 # Never tag nullid
720 if n == nullid:
720 if n == nullid:
721 continue
721 continue
722 # A node's revision number represents its place in a
722 # A node's revision number represents its place in a
723 # topologically sorted list of nodes.
723 # topologically sorted list of nodes.
724 r = self.rev(n)
724 r = self.rev(n)
725 if r >= lowestrev:
725 if r >= lowestrev:
726 if n not in ancestors:
726 if n not in ancestors:
727 # If we are possibly a descendent of one of the roots
727 # If we are possibly a descendent of one of the roots
728 # and we haven't already been marked as an ancestor
728 # and we haven't already been marked as an ancestor
729 ancestors.add(n) # Mark as ancestor
729 ancestors.add(n) # Mark as ancestor
730 # Add non-nullid parents to list of nodes to tag.
730 # Add non-nullid parents to list of nodes to tag.
731 nodestotag.update([p for p in self.parents(n) if
731 nodestotag.update([p for p in self.parents(n) if
732 p != nullid])
732 p != nullid])
733 elif n in heads: # We've seen it before, is it a fake head?
733 elif n in heads: # We've seen it before, is it a fake head?
734 # So it is, real heads should not be the ancestors of
734 # So it is, real heads should not be the ancestors of
735 # any other heads.
735 # any other heads.
736 heads.pop(n)
736 heads.pop(n)
737 if not ancestors:
737 if not ancestors:
738 return nonodes
738 return nonodes
739 # Now that we have our set of ancestors, we want to remove any
739 # Now that we have our set of ancestors, we want to remove any
740 # roots that are not ancestors.
740 # roots that are not ancestors.
741
741
742 # If one of the roots was nullid, everything is included anyway.
742 # If one of the roots was nullid, everything is included anyway.
743 if lowestrev > nullrev:
743 if lowestrev > nullrev:
744 # But, since we weren't, let's recompute the lowest rev to not
744 # But, since we weren't, let's recompute the lowest rev to not
745 # include roots that aren't ancestors.
745 # include roots that aren't ancestors.
746
746
747 # Filter out roots that aren't ancestors of heads
747 # Filter out roots that aren't ancestors of heads
748 roots = [n for n in roots if n in ancestors]
748 roots = [n for n in roots if n in ancestors]
749 # Recompute the lowest revision
749 # Recompute the lowest revision
750 if roots:
750 if roots:
751 lowestrev = min([self.rev(n) for n in roots])
751 lowestrev = min([self.rev(n) for n in roots])
752 else:
752 else:
753 # No more roots? Return empty list
753 # No more roots? Return empty list
754 return nonodes
754 return nonodes
755 else:
755 else:
756 # We are descending from nullid, and don't need to care about
756 # We are descending from nullid, and don't need to care about
757 # any other roots.
757 # any other roots.
758 lowestrev = nullrev
758 lowestrev = nullrev
759 roots = [nullid]
759 roots = [nullid]
760 # Transform our roots list into a set.
760 # Transform our roots list into a set.
761 descendents = set(roots)
761 descendents = set(roots)
762 # Also, keep the original roots so we can filter out roots that aren't
762 # Also, keep the original roots so we can filter out roots that aren't
763 # 'real' roots (i.e. are descended from other roots).
763 # 'real' roots (i.e. are descended from other roots).
764 roots = descendents.copy()
764 roots = descendents.copy()
765 # Our topologically sorted list of output nodes.
765 # Our topologically sorted list of output nodes.
766 orderedout = []
766 orderedout = []
767 # Don't start at nullid since we don't want nullid in our output list,
767 # Don't start at nullid since we don't want nullid in our output list,
768 # and if nullid shows up in descedents, empty parents will look like
768 # and if nullid shows up in descedents, empty parents will look like
769 # they're descendents.
769 # they're descendents.
770 for r in xrange(max(lowestrev, 0), highestrev + 1):
770 for r in xrange(max(lowestrev, 0), highestrev + 1):
771 n = self.node(r)
771 n = self.node(r)
772 isdescendent = False
772 isdescendent = False
773 if lowestrev == nullrev: # Everybody is a descendent of nullid
773 if lowestrev == nullrev: # Everybody is a descendent of nullid
774 isdescendent = True
774 isdescendent = True
775 elif n in descendents:
775 elif n in descendents:
776 # n is already a descendent
776 # n is already a descendent
777 isdescendent = True
777 isdescendent = True
778 # This check only needs to be done here because all the roots
778 # This check only needs to be done here because all the roots
779 # will start being marked is descendents before the loop.
779 # will start being marked is descendents before the loop.
780 if n in roots:
780 if n in roots:
781 # If n was a root, check if it's a 'real' root.
781 # If n was a root, check if it's a 'real' root.
782 p = tuple(self.parents(n))
782 p = tuple(self.parents(n))
783 # If any of its parents are descendents, it's not a root.
783 # If any of its parents are descendents, it's not a root.
784 if (p[0] in descendents) or (p[1] in descendents):
784 if (p[0] in descendents) or (p[1] in descendents):
785 roots.remove(n)
785 roots.remove(n)
786 else:
786 else:
787 p = tuple(self.parents(n))
787 p = tuple(self.parents(n))
788 # A node is a descendent if either of its parents are
788 # A node is a descendent if either of its parents are
789 # descendents. (We seeded the dependents list with the roots
789 # descendents. (We seeded the dependents list with the roots
790 # up there, remember?)
790 # up there, remember?)
791 if (p[0] in descendents) or (p[1] in descendents):
791 if (p[0] in descendents) or (p[1] in descendents):
792 descendents.add(n)
792 descendents.add(n)
793 isdescendent = True
793 isdescendent = True
794 if isdescendent and ((ancestors is None) or (n in ancestors)):
794 if isdescendent and ((ancestors is None) or (n in ancestors)):
795 # Only include nodes that are both descendents and ancestors.
795 # Only include nodes that are both descendents and ancestors.
796 orderedout.append(n)
796 orderedout.append(n)
797 if (ancestors is not None) and (n in heads):
797 if (ancestors is not None) and (n in heads):
798 # We're trying to figure out which heads are reachable
798 # We're trying to figure out which heads are reachable
799 # from roots.
799 # from roots.
800 # Mark this head as having been reached
800 # Mark this head as having been reached
801 heads[n] = 1
801 heads[n] = 1
802 elif ancestors is None:
802 elif ancestors is None:
803 # Otherwise, we're trying to discover the heads.
803 # Otherwise, we're trying to discover the heads.
804 # Assume this is a head because if it isn't, the next step
804 # Assume this is a head because if it isn't, the next step
805 # will eventually remove it.
805 # will eventually remove it.
806 heads[n] = 1
806 heads[n] = 1
807 # But, obviously its parents aren't.
807 # But, obviously its parents aren't.
808 for p in self.parents(n):
808 for p in self.parents(n):
809 heads.pop(p, None)
809 heads.pop(p, None)
810 heads = [n for n in heads.iterkeys() if heads[n] != 0]
810 heads = [n for n in heads.iterkeys() if heads[n] != 0]
811 roots = list(roots)
811 roots = list(roots)
812 assert orderedout
812 assert orderedout
813 assert roots
813 assert roots
814 assert heads
814 assert heads
815 return (orderedout, roots, heads)
815 return (orderedout, roots, heads)
816
816
817 def heads(self, start=None, stop=None):
817 def heads(self, start=None, stop=None):
818 """return the list of all nodes that have no children
818 """return the list of all nodes that have no children
819
819
820 if start is specified, only heads that are descendants of
820 if start is specified, only heads that are descendants of
821 start will be returned
821 start will be returned
822 if stop is specified, it will consider all the revs from stop
822 if stop is specified, it will consider all the revs from stop
823 as if they had no children
823 as if they had no children
824 """
824 """
825 if start is None and stop is None:
825 if start is None and stop is None:
826 count = len(self)
826 count = len(self)
827 if not count:
827 if not count:
828 return [nullid]
828 return [nullid]
829 ishead = [1] * (count + 1)
829 ishead = [1] * (count + 1)
830 index = self.index
830 index = self.index
831 for r in xrange(count):
831 for r in xrange(count):
832 e = index[r]
832 e = index[r]
833 ishead[e[5]] = ishead[e[6]] = 0
833 ishead[e[5]] = ishead[e[6]] = 0
834 return [self.node(r) for r in xrange(count) if ishead[r]]
834 return [self.node(r) for r in xrange(count) if ishead[r]]
835
835
836 if start is None:
836 if start is None:
837 start = nullid
837 start = nullid
838 if stop is None:
838 if stop is None:
839 stop = []
839 stop = []
840 stoprevs = set([self.rev(n) for n in stop])
840 stoprevs = set([self.rev(n) for n in stop])
841 startrev = self.rev(start)
841 startrev = self.rev(start)
842 reachable = set((startrev,))
842 reachable = set((startrev,))
843 heads = set((startrev,))
843 heads = set((startrev,))
844
844
845 parentrevs = self.parentrevs
845 parentrevs = self.parentrevs
846 for r in xrange(startrev + 1, len(self)):
846 for r in xrange(startrev + 1, len(self)):
847 for p in parentrevs(r):
847 for p in parentrevs(r):
848 if p in reachable:
848 if p in reachable:
849 if r not in stoprevs:
849 if r not in stoprevs:
850 reachable.add(r)
850 reachable.add(r)
851 heads.add(r)
851 heads.add(r)
852 if p in heads and p not in stoprevs:
852 if p in heads and p not in stoprevs:
853 heads.remove(p)
853 heads.remove(p)
854
854
855 return [self.node(r) for r in heads]
855 return [self.node(r) for r in heads]
856
856
857 def children(self, node):
857 def children(self, node):
858 """find the children of a given node"""
858 """find the children of a given node"""
859 c = []
859 c = []
860 p = self.rev(node)
860 p = self.rev(node)
861 for r in range(p + 1, len(self)):
861 for r in range(p + 1, len(self)):
862 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
862 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
863 if prevs:
863 if prevs:
864 for pr in prevs:
864 for pr in prevs:
865 if pr == p:
865 if pr == p:
866 c.append(self.node(r))
866 c.append(self.node(r))
867 elif p == nullrev:
867 elif p == nullrev:
868 c.append(self.node(r))
868 c.append(self.node(r))
869 return c
869 return c
870
870
871 def descendant(self, start, end):
871 def descendant(self, start, end):
872 for i in self.descendants(start):
872 for i in self.descendants(start):
873 if i == end:
873 if i == end:
874 return True
874 return True
875 elif i > end:
875 elif i > end:
876 break
876 break
877 return False
877 return False
878
878
879 def ancestor(self, a, b):
879 def ancestor(self, a, b):
880 """calculate the least common ancestor of nodes a and b"""
880 """calculate the least common ancestor of nodes a and b"""
881
881
882 # fast path, check if it is a descendant
882 # fast path, check if it is a descendant
883 a, b = self.rev(a), self.rev(b)
883 a, b = self.rev(a), self.rev(b)
884 start, end = sorted((a, b))
884 start, end = sorted((a, b))
885 if self.descendant(start, end):
885 if self.descendant(start, end):
886 return self.node(start)
886 return self.node(start)
887
887
888 def parents(rev):
888 def parents(rev):
889 return [p for p in self.parentrevs(rev) if p != nullrev]
889 return [p for p in self.parentrevs(rev) if p != nullrev]
890
890
891 c = ancestor.ancestor(a, b, parents)
891 c = ancestor.ancestor(a, b, parents)
892 if c is None:
892 if c is None:
893 return nullid
893 return nullid
894
894
895 return self.node(c)
895 return self.node(c)
896
896
897 def _match(self, id):
897 def _match(self, id):
898 if isinstance(id, (long, int)):
898 if isinstance(id, (long, int)):
899 # rev
899 # rev
900 return self.node(id)
900 return self.node(id)
901 if len(id) == 20:
901 if len(id) == 20:
902 # possibly a binary node
902 # possibly a binary node
903 # odds of a binary node being all hex in ASCII are 1 in 10**25
903 # odds of a binary node being all hex in ASCII are 1 in 10**25
904 try:
904 try:
905 node = id
905 node = id
906 self.rev(node) # quick search the index
906 self.rev(node) # quick search the index
907 return node
907 return node
908 except LookupError:
908 except LookupError:
909 pass # may be partial hex id
909 pass # may be partial hex id
910 try:
910 try:
911 # str(rev)
911 # str(rev)
912 rev = int(id)
912 rev = int(id)
913 if str(rev) != id:
913 if str(rev) != id:
914 raise ValueError
914 raise ValueError
915 if rev < 0:
915 if rev < 0:
916 rev = len(self) + rev
916 rev = len(self) + rev
917 if rev < 0 or rev >= len(self):
917 if rev < 0 or rev >= len(self):
918 raise ValueError
918 raise ValueError
919 return self.node(rev)
919 return self.node(rev)
920 except (ValueError, OverflowError):
920 except (ValueError, OverflowError):
921 pass
921 pass
922 if len(id) == 40:
922 if len(id) == 40:
923 try:
923 try:
924 # a full hex nodeid?
924 # a full hex nodeid?
925 node = bin(id)
925 node = bin(id)
926 self.rev(node)
926 self.rev(node)
927 return node
927 return node
928 except (TypeError, LookupError):
928 except (TypeError, LookupError):
929 pass
929 pass
930
930
931 def _partialmatch(self, id):
931 def _partialmatch(self, id):
932 if len(id) < 40:
932 if len(id) < 40:
933 try:
933 try:
934 # hex(node)[:...]
934 # hex(node)[:...]
935 l = len(id) // 2 # grab an even number of digits
935 l = len(id) // 2 # grab an even number of digits
936 bin_id = bin(id[:l * 2])
936 bin_id = bin(id[:l * 2])
937 nl = [n for n in self.nodemap if n[:l] == bin_id]
937 nl = [n for n in self.nodemap if n[:l] == bin_id]
938 nl = [n for n in nl if hex(n).startswith(id)]
938 nl = [n for n in nl if hex(n).startswith(id)]
939 if len(nl) > 0:
939 if len(nl) > 0:
940 if len(nl) == 1:
940 if len(nl) == 1:
941 return nl[0]
941 return nl[0]
942 raise LookupError(id, self.indexfile,
942 raise LookupError(id, self.indexfile,
943 _('ambiguous identifier'))
943 _('ambiguous identifier'))
944 return None
944 return None
945 except TypeError:
945 except TypeError:
946 pass
946 pass
947
947
948 def lookup(self, id):
948 def lookup(self, id):
949 """locate a node based on:
949 """locate a node based on:
950 - revision number or str(revision number)
950 - revision number or str(revision number)
951 - nodeid or subset of hex nodeid
951 - nodeid or subset of hex nodeid
952 """
952 """
953 n = self._match(id)
953 n = self._match(id)
954 if n is not None:
954 if n is not None:
955 return n
955 return n
956 n = self._partialmatch(id)
956 n = self._partialmatch(id)
957 if n:
957 if n:
958 return n
958 return n
959
959
960 raise LookupError(id, self.indexfile, _('no match found'))
960 raise LookupError(id, self.indexfile, _('no match found'))
961
961
962 def cmp(self, node, text):
962 def cmp(self, node, text):
963 """compare text with a given file revision
963 """compare text with a given file revision
964
964
965 returns True if text is different than what is stored.
965 returns True if text is different than what is stored.
966 """
966 """
967 p1, p2 = self.parents(node)
967 p1, p2 = self.parents(node)
968 return hash(text, p1, p2) != node
968 return hash(text, p1, p2) != node
969
969
970 def _addchunk(self, offset, data):
970 def _addchunk(self, offset, data):
971 o, d = self._chunkcache
971 o, d = self._chunkcache
972 # try to add to existing cache
972 # try to add to existing cache
973 if o + len(d) == offset and len(d) + len(data) < _prereadsize:
973 if o + len(d) == offset and len(d) + len(data) < _prereadsize:
974 self._chunkcache = o, d + data
974 self._chunkcache = o, d + data
975 else:
975 else:
976 self._chunkcache = offset, data
976 self._chunkcache = offset, data
977
977
978 def _loadchunk(self, offset, length):
978 def _loadchunk(self, offset, length):
979 if self._inline:
979 if self._inline:
980 df = self.opener(self.indexfile)
980 df = self.opener(self.indexfile)
981 else:
981 else:
982 df = self.opener(self.datafile)
982 df = self.opener(self.datafile)
983
983
984 readahead = max(65536, length)
984 readahead = max(65536, length)
985 df.seek(offset)
985 df.seek(offset)
986 d = df.read(readahead)
986 d = df.read(readahead)
987 self._addchunk(offset, d)
987 self._addchunk(offset, d)
988 if readahead > length:
988 if readahead > length:
989 return d[:length]
989 return d[:length]
990 return d
990 return d
991
991
992 def _getchunk(self, offset, length):
992 def _getchunk(self, offset, length):
993 o, d = self._chunkcache
993 o, d = self._chunkcache
994 l = len(d)
994 l = len(d)
995
995
996 # is it in the cache?
996 # is it in the cache?
997 cachestart = offset - o
997 cachestart = offset - o
998 cacheend = cachestart + length
998 cacheend = cachestart + length
999 if cachestart >= 0 and cacheend <= l:
999 if cachestart >= 0 and cacheend <= l:
1000 if cachestart == 0 and cacheend == l:
1000 if cachestart == 0 and cacheend == l:
1001 return d # avoid a copy
1001 return d # avoid a copy
1002 return d[cachestart:cacheend]
1002 return d[cachestart:cacheend]
1003
1003
1004 return self._loadchunk(offset, length)
1004 return self._loadchunk(offset, length)
1005
1005
1006 def _chunkraw(self, startrev, endrev):
1006 def _chunkraw(self, startrev, endrev):
1007 start = self.start(startrev)
1007 start = self.start(startrev)
1008 length = self.end(endrev) - start
1008 length = self.end(endrev) - start
1009 if self._inline:
1009 if self._inline:
1010 start += (startrev + 1) * self._io.size
1010 start += (startrev + 1) * self._io.size
1011 return self._getchunk(start, length)
1011 return self._getchunk(start, length)
1012
1012
1013 def _chunk(self, rev):
1013 def _chunk(self, rev):
1014 return decompress(self._chunkraw(rev, rev))
1014 return decompress(self._chunkraw(rev, rev))
1015
1015
1016 def _chunkclear(self):
1016 def _chunkclear(self):
1017 self._chunkcache = (0, '')
1017 self._chunkcache = (0, '')
1018
1018
1019 def deltaparent(self, rev):
1019 def deltaparent(self, rev):
1020 """return previous revision or parentrev according to flags"""
1020 """return previous revision or parentrev according to flags"""
1021 if self.flags(rev) & REVIDX_PARENTDELTA:
1021 if self.flags(rev) & REVIDX_PARENTDELTA:
1022 return self.parentrevs(rev)[0]
1022 return self.parentrevs(rev)[0]
1023 else:
1023 else:
1024 return rev - 1
1024 return rev - 1
1025
1025
1026 def revdiff(self, rev1, rev2):
1026 def revdiff(self, rev1, rev2):
1027 """return or calculate a delta between two revisions"""
1027 """return or calculate a delta between two revisions"""
1028 if self.base(rev2) != rev2 and self.deltaparent(rev2) == rev1:
1028 if self.base(rev2) != rev2 and self.deltaparent(rev2) == rev1:
1029 return self._chunk(rev2)
1029 return self._chunk(rev2)
1030
1030
1031 return mdiff.textdiff(self.revision(self.node(rev1)),
1031 return mdiff.textdiff(self.revision(self.node(rev1)),
1032 self.revision(self.node(rev2)))
1032 self.revision(self.node(rev2)))
1033
1033
1034 def revision(self, node):
1034 def revision(self, node):
1035 """return an uncompressed revision of a given node"""
1035 """return an uncompressed revision of a given node"""
1036 cachedrev = None
1036 cachedrev = None
1037 if node == nullid:
1037 if node == nullid:
1038 return ""
1038 return ""
1039 if self._cache:
1039 if self._cache:
1040 if self._cache[0] == node:
1040 if self._cache[0] == node:
1041 return self._cache[2]
1041 return self._cache[2]
1042 cachedrev = self._cache[1]
1042 cachedrev = self._cache[1]
1043
1043
1044 # look up what we need to read
1044 # look up what we need to read
1045 text = None
1045 text = None
1046 rev = self.rev(node)
1046 rev = self.rev(node)
1047 base = self.base(rev)
1047 base = self.base(rev)
1048
1048
1049 # check rev flags
1049 # check rev flags
1050 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
1050 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
1051 raise RevlogError(_('incompatible revision flag %x') %
1051 raise RevlogError(_('incompatible revision flag %x') %
1052 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
1052 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
1053
1053
1054 # build delta chain
1054 # build delta chain
1055 self._loadindex(base, rev + 1)
1055 self._loadindex(base, rev + 1)
1056 chain = []
1056 chain = []
1057 index = self.index # for performance
1057 index = self.index # for performance
1058 iterrev = rev
1058 iterrev = rev
1059 e = index[iterrev]
1059 e = index[iterrev]
1060 while iterrev != base and iterrev != cachedrev:
1060 while iterrev != base and iterrev != cachedrev:
1061 chain.append(iterrev)
1061 chain.append(iterrev)
1062 if e[0] & REVIDX_PARENTDELTA:
1062 if e[0] & REVIDX_PARENTDELTA:
1063 iterrev = e[5]
1063 iterrev = e[5]
1064 else:
1064 else:
1065 iterrev -= 1
1065 iterrev -= 1
1066 e = index[iterrev]
1066 e = index[iterrev]
1067 chain.reverse()
1067 chain.reverse()
1068 base = iterrev
1068 base = iterrev
1069
1069
1070 if iterrev == cachedrev:
1070 if iterrev == cachedrev:
1071 # cache hit
1071 # cache hit
1072 text = self._cache[2]
1072 text = self._cache[2]
1073
1073
1074 # drop cache to save memory
1074 # drop cache to save memory
1075 self._cache = None
1075 self._cache = None
1076
1076
1077 self._chunkraw(base, rev)
1077 self._chunkraw(base, rev)
1078 if text is None:
1078 if text is None:
1079 text = self._chunk(base)
1079 text = self._chunk(base)
1080
1080
1081 bins = [self._chunk(r) for r in chain]
1081 bins = [self._chunk(r) for r in chain]
1082 text = mdiff.patches(text, bins)
1082 text = mdiff.patches(text, bins)
1083 p1, p2 = self.parents(node)
1083 p1, p2 = self.parents(node)
1084 if (node != hash(text, p1, p2) and
1084 if (node != hash(text, p1, p2) and
1085 not (self.flags(rev) & REVIDX_PUNCHED_FLAG)):
1085 not (self.flags(rev) & REVIDX_PUNCHED_FLAG)):
1086 raise RevlogError(_("integrity check failed on %s:%d")
1086 raise RevlogError(_("integrity check failed on %s:%d")
1087 % (self.indexfile, rev))
1087 % (self.indexfile, rev))
1088
1088
1089 self._cache = (node, rev, text)
1089 self._cache = (node, rev, text)
1090 return text
1090 return text
1091
1091
1092 def checkinlinesize(self, tr, fp=None):
1092 def checkinlinesize(self, tr, fp=None):
1093 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
1093 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
1094 return
1094 return
1095
1095
1096 trinfo = tr.find(self.indexfile)
1096 trinfo = tr.find(self.indexfile)
1097 if trinfo is None:
1097 if trinfo is None:
1098 raise RevlogError(_("%s not found in the transaction")
1098 raise RevlogError(_("%s not found in the transaction")
1099 % self.indexfile)
1099 % self.indexfile)
1100
1100
1101 trindex = trinfo[2]
1101 trindex = trinfo[2]
1102 dataoff = self.start(trindex)
1102 dataoff = self.start(trindex)
1103
1103
1104 tr.add(self.datafile, dataoff)
1104 tr.add(self.datafile, dataoff)
1105
1105
1106 if fp:
1106 if fp:
1107 fp.flush()
1107 fp.flush()
1108 fp.close()
1108 fp.close()
1109
1109
1110 df = self.opener(self.datafile, 'w')
1110 df = self.opener(self.datafile, 'w')
1111 try:
1111 try:
1112 for r in self:
1112 for r in self:
1113 df.write(self._chunkraw(r, r))
1113 df.write(self._chunkraw(r, r))
1114 finally:
1114 finally:
1115 df.close()
1115 df.close()
1116
1116
1117 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1117 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1118 self.version &= ~(REVLOGNGINLINEDATA)
1118 self.version &= ~(REVLOGNGINLINEDATA)
1119 self._inline = False
1119 self._inline = False
1120 for i in self:
1120 for i in self:
1121 e = self._io.packentry(self.index[i], self.node, self.version, i)
1121 e = self._io.packentry(self.index[i], self.node, self.version, i)
1122 fp.write(e)
1122 fp.write(e)
1123
1123
1124 # if we don't call rename, the temp file will never replace the
1124 # if we don't call rename, the temp file will never replace the
1125 # real index
1125 # real index
1126 fp.rename()
1126 fp.rename()
1127
1127
1128 tr.replace(self.indexfile, trindex * self._io.size)
1128 tr.replace(self.indexfile, trindex * self._io.size)
1129 self._chunkclear()
1129 self._chunkclear()
1130
1130
1131 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
1131 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
1132 """add a revision to the log
1132 """add a revision to the log
1133
1133
1134 text - the revision data to add
1134 text - the revision data to add
1135 transaction - the transaction object used for rollback
1135 transaction - the transaction object used for rollback
1136 link - the linkrev data to add
1136 link - the linkrev data to add
1137 p1, p2 - the parent nodeids of the revision
1137 p1, p2 - the parent nodeids of the revision
1138 cachedelta - an optional precomputed delta
1138 cachedelta - an optional precomputed delta
1139 """
1139 """
1140 node = hash(text, p1, p2)
1140 node = hash(text, p1, p2)
1141 if (node in self.nodemap and
1141 if (node in self.nodemap and
1142 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1142 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1143 return node
1143 return node
1144
1144
1145 dfh = None
1145 dfh = None
1146 if not self._inline:
1146 if not self._inline:
1147 dfh = self.opener(self.datafile, "a")
1147 dfh = self.opener(self.datafile, "a")
1148 ifh = self.opener(self.indexfile, "a+")
1148 ifh = self.opener(self.indexfile, "a+")
1149 try:
1149 try:
1150 return self._addrevision(node, text, transaction, link, p1, p2,
1150 return self._addrevision(node, text, transaction, link, p1, p2,
1151 cachedelta, ifh, dfh)
1151 cachedelta, ifh, dfh)
1152 finally:
1152 finally:
1153 if dfh:
1153 if dfh:
1154 dfh.close()
1154 dfh.close()
1155 ifh.close()
1155 ifh.close()
1156
1156
1157 def _addrevision(self, node, text, transaction, link, p1, p2,
1157 def _addrevision(self, node, text, transaction, link, p1, p2,
1158 cachedelta, ifh, dfh):
1158 cachedelta, ifh, dfh):
1159 curr = len(self)
1159 curr = len(self)
1160 prev = curr - 1
1160 prev = curr - 1
1161 base = curr
1161 base = curr
1162 offset = self.end(prev)
1162 offset = self.end(prev)
1163 flags = 0
1163 flags = 0
1164 d = None
1164 d = None
1165
1165
1166 if self._parentdelta:
1166 if self._parentdelta:
1167 deltarev, deltanode = self.rev(p1), p1
1167 deltarev, deltanode = self.rev(p1), p1
1168 flags = REVIDX_PARENTDELTA
1168 flags = REVIDX_PARENTDELTA
1169 else:
1169 else:
1170 deltarev, deltanode = prev, self.node(prev)
1170 deltarev, deltanode = prev, self.node(prev)
1171
1171
1172 # should we try to build a delta?
1172 # should we try to build a delta?
1173 if deltarev != nullrev:
1173 if deltarev != nullrev:
1174 # can we use the cached delta?
1174 # can we use the cached delta?
1175 if cachedelta:
1175 if cachedelta:
1176 cacherev, d = cachedelta
1176 cacherev, d = cachedelta
1177 if cacherev != deltarev:
1177 if cacherev != deltarev:
1178 d = None
1178 d = None
1179 if d is None:
1179 if d is None:
1180 ptext = self.revision(deltanode)
1180 ptext = self.revision(deltanode)
1181 d = mdiff.textdiff(ptext, text)
1181 d = mdiff.textdiff(ptext, text)
1182 data = compress(d)
1182 data = compress(d)
1183 l = len(data[1]) + len(data[0])
1183 l = len(data[1]) + len(data[0])
1184 base = self.base(deltarev)
1184 base = self.base(deltarev)
1185 dist = l + offset - self.start(base)
1185 dist = l + offset - self.start(base)
1186
1186
1187 # full versions are inserted when the needed deltas
1187 # full versions are inserted when the needed deltas
1188 # become comparable to the uncompressed text
1188 # become comparable to the uncompressed text
1189 # or the base revision is punched
1189 # or the base revision is punched
1190 if (d is None or dist > len(text) * 2 or
1190 if (d is None or dist > len(text) * 2 or
1191 (self.flags(base) & REVIDX_PUNCHED_FLAG)):
1191 (self.flags(base) & REVIDX_PUNCHED_FLAG)):
1192 data = compress(text)
1192 data = compress(text)
1193 l = len(data[1]) + len(data[0])
1193 l = len(data[1]) + len(data[0])
1194 base = curr
1194 base = curr
1195
1195
1196 e = (offset_type(offset, flags), l, len(text),
1196 e = (offset_type(offset, flags), l, len(text),
1197 base, link, self.rev(p1), self.rev(p2), node)
1197 base, link, self.rev(p1), self.rev(p2), node)
1198 self.index.insert(-1, e)
1198 self.index.insert(-1, e)
1199 self.nodemap[node] = curr
1199 self.nodemap[node] = curr
1200
1200
1201 entry = self._io.packentry(e, self.node, self.version, curr)
1201 entry = self._io.packentry(e, self.node, self.version, curr)
1202 if not self._inline:
1202 if not self._inline:
1203 transaction.add(self.datafile, offset)
1203 transaction.add(self.datafile, offset)
1204 transaction.add(self.indexfile, curr * len(entry))
1204 transaction.add(self.indexfile, curr * len(entry))
1205 if data[0]:
1205 if data[0]:
1206 dfh.write(data[0])
1206 dfh.write(data[0])
1207 dfh.write(data[1])
1207 dfh.write(data[1])
1208 dfh.flush()
1208 dfh.flush()
1209 ifh.write(entry)
1209 ifh.write(entry)
1210 else:
1210 else:
1211 offset += curr * self._io.size
1211 offset += curr * self._io.size
1212 transaction.add(self.indexfile, offset, curr)
1212 transaction.add(self.indexfile, offset, curr)
1213 ifh.write(entry)
1213 ifh.write(entry)
1214 ifh.write(data[0])
1214 ifh.write(data[0])
1215 ifh.write(data[1])
1215 ifh.write(data[1])
1216 self.checkinlinesize(transaction, ifh)
1216 self.checkinlinesize(transaction, ifh)
1217
1217
1218 if type(text) == str: # only accept immutable objects
1218 if type(text) == str: # only accept immutable objects
1219 self._cache = (node, curr, text)
1219 self._cache = (node, curr, text)
1220 return node
1220 return node
1221
1221
1222 def group(self, nodelist, lookup, infocollect=None, fullrev=False):
1222 def group(self, nodelist, lookup, infocollect=None, fullrev=False):
1223 """Calculate a delta group, yielding a sequence of changegroup chunks
1223 """Calculate a delta group, yielding a sequence of changegroup chunks
1224 (strings).
1224 (strings).
1225
1225
1226 Given a list of changeset revs, return a set of deltas and
1226 Given a list of changeset revs, return a set of deltas and
1227 metadata corresponding to nodes. The first delta is
1227 metadata corresponding to nodes. The first delta is
1228 first parent(nodelist[0]) -> nodelist[0], the receiver is
1228 first parent(nodelist[0]) -> nodelist[0], the receiver is
1229 guaranteed to have this parent as it has all history before
1229 guaranteed to have this parent as it has all history before
1230 these changesets. In the case firstparent is nullrev the
1230 these changesets. In the case firstparent is nullrev the
1231 changegroup starts with a full revision.
1231 changegroup starts with a full revision.
1232 fullrev forces the insertion of the full revision, necessary
1232 fullrev forces the insertion of the full revision, necessary
1233 in the case of shallow clones where the first parent might
1233 in the case of shallow clones where the first parent might
1234 not exist at the reciever.
1234 not exist at the reciever.
1235 """
1235 """
1236
1236
1237 revs = [self.rev(n) for n in nodelist]
1237 revs = [self.rev(n) for n in nodelist]
1238
1238
1239 # if we don't have any revisions touched by these changesets, bail
1239 # if we don't have any revisions touched by these changesets, bail
1240 if not revs:
1240 if not revs:
1241 yield changegroup.closechunk()
1241 yield changegroup.closechunk()
1242 return
1242 return
1243
1243
1244 # add the parent of the first rev
1244 # add the parent of the first rev
1245 p = self.parentrevs(revs[0])[0]
1245 p = self.parentrevs(revs[0])[0]
1246 revs.insert(0, p)
1246 revs.insert(0, p)
1247 if p == nullrev:
1247 if p == nullrev:
1248 fullrev = True
1248 fullrev = True
1249
1249
1250 # build deltas
1250 # build deltas
1251 for d in xrange(len(revs) - 1):
1251 for d in xrange(len(revs) - 1):
1252 a, b = revs[d], revs[d + 1]
1252 a, b = revs[d], revs[d + 1]
1253 nb = self.node(b)
1253 nb = self.node(b)
1254
1254
1255 if infocollect is not None:
1255 if infocollect is not None:
1256 infocollect(nb)
1256 infocollect(nb)
1257
1257
1258 p = self.parents(nb)
1258 p = self.parents(nb)
1259 meta = nb + p[0] + p[1] + lookup(nb)
1259 meta = nb + p[0] + p[1] + lookup(nb)
1260 if fullrev:
1260 if fullrev:
1261 d = self.revision(nb)
1261 d = self.revision(nb)
1262 meta += mdiff.trivialdiffheader(len(d))
1262 meta += mdiff.trivialdiffheader(len(d))
1263 fullrev = False
1263 fullrev = False
1264 else:
1264 else:
1265 d = self.revdiff(a, b)
1265 d = self.revdiff(a, b)
1266 yield changegroup.chunkheader(len(meta) + len(d))
1266 yield changegroup.chunkheader(len(meta) + len(d))
1267 yield meta
1267 yield meta
1268 yield d
1268 yield d
1269
1269
1270 yield changegroup.closechunk()
1270 yield changegroup.closechunk()
1271
1271
1272 def addgroup(self, revs, linkmapper, transaction):
1272 def addgroup(self, bundle, linkmapper, transaction):
1273 """
1273 """
1274 add a delta group
1274 add a delta group
1275
1275
1276 given a set of deltas, add them to the revision log. the
1276 given a set of deltas, add them to the revision log. the
1277 first delta is against its parent, which should be in our
1277 first delta is against its parent, which should be in our
1278 log, the rest are against the previous delta.
1278 log, the rest are against the previous delta.
1279 """
1279 """
1280
1280
1281 #track the base of the current delta log
1281 #track the base of the current delta log
1282 r = len(self)
1282 r = len(self)
1283 t = r - 1
1283 t = r - 1
1284 node = None
1284 node = None
1285
1285
1286 base = prev = nullrev
1286 base = prev = nullrev
1287 start = end = textlen = 0
1287 start = end = textlen = 0
1288 if r:
1288 if r:
1289 end = self.end(t)
1289 end = self.end(t)
1290
1290
1291 ifh = self.opener(self.indexfile, "a+")
1291 ifh = self.opener(self.indexfile, "a+")
1292 isize = r * self._io.size
1292 isize = r * self._io.size
1293 if self._inline:
1293 if self._inline:
1294 transaction.add(self.indexfile, end + isize, r)
1294 transaction.add(self.indexfile, end + isize, r)
1295 dfh = None
1295 dfh = None
1296 else:
1296 else:
1297 transaction.add(self.indexfile, isize, r)
1297 transaction.add(self.indexfile, isize, r)
1298 transaction.add(self.datafile, end)
1298 transaction.add(self.datafile, end)
1299 dfh = self.opener(self.datafile, "a")
1299 dfh = self.opener(self.datafile, "a")
1300
1300
1301 try:
1301 try:
1302 # loop through our set of deltas
1302 # loop through our set of deltas
1303 chain = None
1303 chain = None
1304 for chunk in revs:
1304 while 1:
1305 chunk = bundle.chunk()
1306 if not chunk:
1307 break
1305 node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80])
1308 node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80])
1306 link = linkmapper(cs)
1309 link = linkmapper(cs)
1307 if (node in self.nodemap and
1310 if (node in self.nodemap and
1308 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1311 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1309 # this can happen if two branches make the same change
1312 # this can happen if two branches make the same change
1310 chain = node
1313 chain = node
1311 continue
1314 continue
1312 delta = buffer(chunk, 80)
1315 delta = buffer(chunk, 80)
1313 del chunk
1316 del chunk
1314
1317
1315 for p in (p1, p2):
1318 for p in (p1, p2):
1316 if not p in self.nodemap:
1319 if not p in self.nodemap:
1317 if self._shallow:
1320 if self._shallow:
1318 # add null entries for missing parents
1321 # add null entries for missing parents
1319 if base == nullrev:
1322 if base == nullrev:
1320 base = len(self)
1323 base = len(self)
1321 e = (offset_type(end, REVIDX_PUNCHED_FLAG),
1324 e = (offset_type(end, REVIDX_PUNCHED_FLAG),
1322 0, 0, base, nullrev, nullrev, nullrev, p)
1325 0, 0, base, nullrev, nullrev, nullrev, p)
1323 self.index.insert(-1, e)
1326 self.index.insert(-1, e)
1324 self.nodemap[p] = r
1327 self.nodemap[p] = r
1325 entry = self._io.packentry(e, self.node,
1328 entry = self._io.packentry(e, self.node,
1326 self.version, r)
1329 self.version, r)
1327 ifh.write(entry)
1330 ifh.write(entry)
1328 t, r = r, r + 1
1331 t, r = r, r + 1
1329 else:
1332 else:
1330 raise LookupError(p, self.indexfile,
1333 raise LookupError(p, self.indexfile,
1331 _('unknown parent'))
1334 _('unknown parent'))
1332
1335
1333 if not chain:
1336 if not chain:
1334 # retrieve the parent revision of the delta chain
1337 # retrieve the parent revision of the delta chain
1335 chain = p1
1338 chain = p1
1336 if not chain in self.nodemap:
1339 if not chain in self.nodemap:
1337 raise LookupError(chain, self.indexfile, _('unknown base'))
1340 raise LookupError(chain, self.indexfile, _('unknown base'))
1338
1341
1339 # full versions are inserted when the needed deltas become
1342 # full versions are inserted when the needed deltas become
1340 # comparable to the uncompressed text or when the previous
1343 # comparable to the uncompressed text or when the previous
1341 # version is not the one we have a delta against. We use
1344 # version is not the one we have a delta against. We use
1342 # the size of the previous full rev as a proxy for the
1345 # the size of the previous full rev as a proxy for the
1343 # current size.
1346 # current size.
1344
1347
1345 if chain == prev:
1348 if chain == prev:
1346 cdelta = compress(delta)
1349 cdelta = compress(delta)
1347 cdeltalen = len(cdelta[0]) + len(cdelta[1])
1350 cdeltalen = len(cdelta[0]) + len(cdelta[1])
1348 textlen = mdiff.patchedsize(textlen, delta)
1351 textlen = mdiff.patchedsize(textlen, delta)
1349
1352
1350 if chain != prev or (end - start + cdeltalen) > textlen * 2:
1353 if chain != prev or (end - start + cdeltalen) > textlen * 2:
1351 # flush our writes here so we can read it in revision
1354 # flush our writes here so we can read it in revision
1352 if dfh:
1355 if dfh:
1353 dfh.flush()
1356 dfh.flush()
1354 ifh.flush()
1357 ifh.flush()
1355 text = self.revision(chain)
1358 text = self.revision(chain)
1356 text = mdiff.patch(text, delta)
1359 text = mdiff.patch(text, delta)
1357 del delta
1360 del delta
1358 chk = self._addrevision(node, text, transaction, link,
1361 chk = self._addrevision(node, text, transaction, link,
1359 p1, p2, None, ifh, dfh)
1362 p1, p2, None, ifh, dfh)
1360 if not dfh and not self._inline:
1363 if not dfh and not self._inline:
1361 # addrevision switched from inline to conventional
1364 # addrevision switched from inline to conventional
1362 # reopen the index
1365 # reopen the index
1363 dfh = self.opener(self.datafile, "a")
1366 dfh = self.opener(self.datafile, "a")
1364 ifh = self.opener(self.indexfile, "a")
1367 ifh = self.opener(self.indexfile, "a")
1365 if chk != node:
1368 if chk != node:
1366 raise RevlogError(_("consistency error adding group"))
1369 raise RevlogError(_("consistency error adding group"))
1367 textlen = len(text)
1370 textlen = len(text)
1368 else:
1371 else:
1369 e = (offset_type(end, 0), cdeltalen, textlen, base,
1372 e = (offset_type(end, 0), cdeltalen, textlen, base,
1370 link, self.rev(p1), self.rev(p2), node)
1373 link, self.rev(p1), self.rev(p2), node)
1371 self.index.insert(-1, e)
1374 self.index.insert(-1, e)
1372 self.nodemap[node] = r
1375 self.nodemap[node] = r
1373 entry = self._io.packentry(e, self.node, self.version, r)
1376 entry = self._io.packentry(e, self.node, self.version, r)
1374 if self._inline:
1377 if self._inline:
1375 ifh.write(entry)
1378 ifh.write(entry)
1376 ifh.write(cdelta[0])
1379 ifh.write(cdelta[0])
1377 ifh.write(cdelta[1])
1380 ifh.write(cdelta[1])
1378 self.checkinlinesize(transaction, ifh)
1381 self.checkinlinesize(transaction, ifh)
1379 if not self._inline:
1382 if not self._inline:
1380 dfh = self.opener(self.datafile, "a")
1383 dfh = self.opener(self.datafile, "a")
1381 ifh = self.opener(self.indexfile, "a")
1384 ifh = self.opener(self.indexfile, "a")
1382 else:
1385 else:
1383 dfh.write(cdelta[0])
1386 dfh.write(cdelta[0])
1384 dfh.write(cdelta[1])
1387 dfh.write(cdelta[1])
1385 ifh.write(entry)
1388 ifh.write(entry)
1386
1389
1387 t, r, chain, prev = r, r + 1, node, node
1390 t, r, chain, prev = r, r + 1, node, node
1388 base = self.base(t)
1391 base = self.base(t)
1389 start = self.start(base)
1392 start = self.start(base)
1390 end = self.end(t)
1393 end = self.end(t)
1391 finally:
1394 finally:
1392 if dfh:
1395 if dfh:
1393 dfh.close()
1396 dfh.close()
1394 ifh.close()
1397 ifh.close()
1395
1398
1396 return node
1399 return node
1397
1400
1398 def strip(self, minlink, transaction):
1401 def strip(self, minlink, transaction):
1399 """truncate the revlog on the first revision with a linkrev >= minlink
1402 """truncate the revlog on the first revision with a linkrev >= minlink
1400
1403
1401 This function is called when we're stripping revision minlink and
1404 This function is called when we're stripping revision minlink and
1402 its descendants from the repository.
1405 its descendants from the repository.
1403
1406
1404 We have to remove all revisions with linkrev >= minlink, because
1407 We have to remove all revisions with linkrev >= minlink, because
1405 the equivalent changelog revisions will be renumbered after the
1408 the equivalent changelog revisions will be renumbered after the
1406 strip.
1409 strip.
1407
1410
1408 So we truncate the revlog on the first of these revisions, and
1411 So we truncate the revlog on the first of these revisions, and
1409 trust that the caller has saved the revisions that shouldn't be
1412 trust that the caller has saved the revisions that shouldn't be
1410 removed and that it'll readd them after this truncation.
1413 removed and that it'll readd them after this truncation.
1411 """
1414 """
1412 if len(self) == 0:
1415 if len(self) == 0:
1413 return
1416 return
1414
1417
1415 if isinstance(self.index, lazyindex):
1418 if isinstance(self.index, lazyindex):
1416 self._loadindexmap()
1419 self._loadindexmap()
1417
1420
1418 for rev in self:
1421 for rev in self:
1419 if self.index[rev][4] >= minlink:
1422 if self.index[rev][4] >= minlink:
1420 break
1423 break
1421 else:
1424 else:
1422 return
1425 return
1423
1426
1424 # first truncate the files on disk
1427 # first truncate the files on disk
1425 end = self.start(rev)
1428 end = self.start(rev)
1426 if not self._inline:
1429 if not self._inline:
1427 transaction.add(self.datafile, end)
1430 transaction.add(self.datafile, end)
1428 end = rev * self._io.size
1431 end = rev * self._io.size
1429 else:
1432 else:
1430 end += rev * self._io.size
1433 end += rev * self._io.size
1431
1434
1432 transaction.add(self.indexfile, end)
1435 transaction.add(self.indexfile, end)
1433
1436
1434 # then reset internal state in memory to forget those revisions
1437 # then reset internal state in memory to forget those revisions
1435 self._cache = None
1438 self._cache = None
1436 self._chunkclear()
1439 self._chunkclear()
1437 for x in xrange(rev, len(self)):
1440 for x in xrange(rev, len(self)):
1438 del self.nodemap[self.node(x)]
1441 del self.nodemap[self.node(x)]
1439
1442
1440 del self.index[rev:-1]
1443 del self.index[rev:-1]
1441
1444
1442 def checksize(self):
1445 def checksize(self):
1443 expected = 0
1446 expected = 0
1444 if len(self):
1447 if len(self):
1445 expected = max(0, self.end(len(self) - 1))
1448 expected = max(0, self.end(len(self) - 1))
1446
1449
1447 try:
1450 try:
1448 f = self.opener(self.datafile)
1451 f = self.opener(self.datafile)
1449 f.seek(0, 2)
1452 f.seek(0, 2)
1450 actual = f.tell()
1453 actual = f.tell()
1451 dd = actual - expected
1454 dd = actual - expected
1452 except IOError, inst:
1455 except IOError, inst:
1453 if inst.errno != errno.ENOENT:
1456 if inst.errno != errno.ENOENT:
1454 raise
1457 raise
1455 dd = 0
1458 dd = 0
1456
1459
1457 try:
1460 try:
1458 f = self.opener(self.indexfile)
1461 f = self.opener(self.indexfile)
1459 f.seek(0, 2)
1462 f.seek(0, 2)
1460 actual = f.tell()
1463 actual = f.tell()
1461 s = self._io.size
1464 s = self._io.size
1462 i = max(0, actual // s)
1465 i = max(0, actual // s)
1463 di = actual - (i * s)
1466 di = actual - (i * s)
1464 if self._inline:
1467 if self._inline:
1465 databytes = 0
1468 databytes = 0
1466 for r in self:
1469 for r in self:
1467 databytes += max(0, self.length(r))
1470 databytes += max(0, self.length(r))
1468 dd = 0
1471 dd = 0
1469 di = actual - len(self) * s - databytes
1472 di = actual - len(self) * s - databytes
1470 except IOError, inst:
1473 except IOError, inst:
1471 if inst.errno != errno.ENOENT:
1474 if inst.errno != errno.ENOENT:
1472 raise
1475 raise
1473 di = 0
1476 di = 0
1474
1477
1475 return (dd, di)
1478 return (dd, di)
1476
1479
1477 def files(self):
1480 def files(self):
1478 res = [self.indexfile]
1481 res = [self.indexfile]
1479 if not self._inline:
1482 if not self._inline:
1480 res.append(self.datafile)
1483 res.append(self.datafile)
1481 return res
1484 return res
General Comments 0
You need to be logged in to leave comments. Login now