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