##// END OF EJS Templates
manifest: change manifestctx to not inherit from manifestdict...
Durham Goode -
r29909:b60a5fe9 default
parent child Browse files
Show More
@@ -1,1977 +1,1977 b''
1 # context.py - changeset and file context objects for mercurial
1 # context.py - changeset and file context objects for mercurial
2 #
2 #
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import os
11 import os
12 import re
12 import re
13 import stat
13 import stat
14
14
15 from .i18n import _
15 from .i18n import _
16 from .node import (
16 from .node import (
17 bin,
17 bin,
18 hex,
18 hex,
19 nullid,
19 nullid,
20 nullrev,
20 nullrev,
21 short,
21 short,
22 wdirid,
22 wdirid,
23 )
23 )
24 from . import (
24 from . import (
25 encoding,
25 encoding,
26 error,
26 error,
27 fileset,
27 fileset,
28 match as matchmod,
28 match as matchmod,
29 mdiff,
29 mdiff,
30 obsolete as obsmod,
30 obsolete as obsmod,
31 patch,
31 patch,
32 phases,
32 phases,
33 repoview,
33 repoview,
34 revlog,
34 revlog,
35 scmutil,
35 scmutil,
36 subrepo,
36 subrepo,
37 util,
37 util,
38 )
38 )
39
39
40 propertycache = util.propertycache
40 propertycache = util.propertycache
41
41
42 # Phony node value to stand-in for new files in some uses of
42 # Phony node value to stand-in for new files in some uses of
43 # manifests. Manifests support 21-byte hashes for nodes which are
43 # manifests. Manifests support 21-byte hashes for nodes which are
44 # dirty in the working copy.
44 # dirty in the working copy.
45 _newnode = '!' * 21
45 _newnode = '!' * 21
46
46
47 nonascii = re.compile(r'[^\x21-\x7f]').search
47 nonascii = re.compile(r'[^\x21-\x7f]').search
48
48
49 class basectx(object):
49 class basectx(object):
50 """A basectx object represents the common logic for its children:
50 """A basectx object represents the common logic for its children:
51 changectx: read-only context that is already present in the repo,
51 changectx: read-only context that is already present in the repo,
52 workingctx: a context that represents the working directory and can
52 workingctx: a context that represents the working directory and can
53 be committed,
53 be committed,
54 memctx: a context that represents changes in-memory and can also
54 memctx: a context that represents changes in-memory and can also
55 be committed."""
55 be committed."""
56 def __new__(cls, repo, changeid='', *args, **kwargs):
56 def __new__(cls, repo, changeid='', *args, **kwargs):
57 if isinstance(changeid, basectx):
57 if isinstance(changeid, basectx):
58 return changeid
58 return changeid
59
59
60 o = super(basectx, cls).__new__(cls)
60 o = super(basectx, cls).__new__(cls)
61
61
62 o._repo = repo
62 o._repo = repo
63 o._rev = nullrev
63 o._rev = nullrev
64 o._node = nullid
64 o._node = nullid
65
65
66 return o
66 return o
67
67
68 def __str__(self):
68 def __str__(self):
69 return short(self.node())
69 return short(self.node())
70
70
71 def __int__(self):
71 def __int__(self):
72 return self.rev()
72 return self.rev()
73
73
74 def __repr__(self):
74 def __repr__(self):
75 return "<%s %s>" % (type(self).__name__, str(self))
75 return "<%s %s>" % (type(self).__name__, str(self))
76
76
77 def __eq__(self, other):
77 def __eq__(self, other):
78 try:
78 try:
79 return type(self) == type(other) and self._rev == other._rev
79 return type(self) == type(other) and self._rev == other._rev
80 except AttributeError:
80 except AttributeError:
81 return False
81 return False
82
82
83 def __ne__(self, other):
83 def __ne__(self, other):
84 return not (self == other)
84 return not (self == other)
85
85
86 def __contains__(self, key):
86 def __contains__(self, key):
87 return key in self._manifest
87 return key in self._manifest
88
88
89 def __getitem__(self, key):
89 def __getitem__(self, key):
90 return self.filectx(key)
90 return self.filectx(key)
91
91
92 def __iter__(self):
92 def __iter__(self):
93 return iter(self._manifest)
93 return iter(self._manifest)
94
94
95 def _manifestmatches(self, match, s):
95 def _manifestmatches(self, match, s):
96 """generate a new manifest filtered by the match argument
96 """generate a new manifest filtered by the match argument
97
97
98 This method is for internal use only and mainly exists to provide an
98 This method is for internal use only and mainly exists to provide an
99 object oriented way for other contexts to customize the manifest
99 object oriented way for other contexts to customize the manifest
100 generation.
100 generation.
101 """
101 """
102 return self.manifest().matches(match)
102 return self.manifest().matches(match)
103
103
104 def _matchstatus(self, other, match):
104 def _matchstatus(self, other, match):
105 """return match.always if match is none
105 """return match.always if match is none
106
106
107 This internal method provides a way for child objects to override the
107 This internal method provides a way for child objects to override the
108 match operator.
108 match operator.
109 """
109 """
110 return match or matchmod.always(self._repo.root, self._repo.getcwd())
110 return match or matchmod.always(self._repo.root, self._repo.getcwd())
111
111
112 def _buildstatus(self, other, s, match, listignored, listclean,
112 def _buildstatus(self, other, s, match, listignored, listclean,
113 listunknown):
113 listunknown):
114 """build a status with respect to another context"""
114 """build a status with respect to another context"""
115 # Load earliest manifest first for caching reasons. More specifically,
115 # Load earliest manifest first for caching reasons. More specifically,
116 # if you have revisions 1000 and 1001, 1001 is probably stored as a
116 # if you have revisions 1000 and 1001, 1001 is probably stored as a
117 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
117 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
118 # 1000 and cache it so that when you read 1001, we just need to apply a
118 # 1000 and cache it so that when you read 1001, we just need to apply a
119 # delta to what's in the cache. So that's one full reconstruction + one
119 # delta to what's in the cache. So that's one full reconstruction + one
120 # delta application.
120 # delta application.
121 if self.rev() is not None and self.rev() < other.rev():
121 if self.rev() is not None and self.rev() < other.rev():
122 self.manifest()
122 self.manifest()
123 mf1 = other._manifestmatches(match, s)
123 mf1 = other._manifestmatches(match, s)
124 mf2 = self._manifestmatches(match, s)
124 mf2 = self._manifestmatches(match, s)
125
125
126 modified, added = [], []
126 modified, added = [], []
127 removed = []
127 removed = []
128 clean = []
128 clean = []
129 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
129 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
130 deletedset = set(deleted)
130 deletedset = set(deleted)
131 d = mf1.diff(mf2, clean=listclean)
131 d = mf1.diff(mf2, clean=listclean)
132 for fn, value in d.iteritems():
132 for fn, value in d.iteritems():
133 if fn in deletedset:
133 if fn in deletedset:
134 continue
134 continue
135 if value is None:
135 if value is None:
136 clean.append(fn)
136 clean.append(fn)
137 continue
137 continue
138 (node1, flag1), (node2, flag2) = value
138 (node1, flag1), (node2, flag2) = value
139 if node1 is None:
139 if node1 is None:
140 added.append(fn)
140 added.append(fn)
141 elif node2 is None:
141 elif node2 is None:
142 removed.append(fn)
142 removed.append(fn)
143 elif flag1 != flag2:
143 elif flag1 != flag2:
144 modified.append(fn)
144 modified.append(fn)
145 elif node2 != _newnode:
145 elif node2 != _newnode:
146 # When comparing files between two commits, we save time by
146 # When comparing files between two commits, we save time by
147 # not comparing the file contents when the nodeids differ.
147 # not comparing the file contents when the nodeids differ.
148 # Note that this means we incorrectly report a reverted change
148 # Note that this means we incorrectly report a reverted change
149 # to a file as a modification.
149 # to a file as a modification.
150 modified.append(fn)
150 modified.append(fn)
151 elif self[fn].cmp(other[fn]):
151 elif self[fn].cmp(other[fn]):
152 modified.append(fn)
152 modified.append(fn)
153 else:
153 else:
154 clean.append(fn)
154 clean.append(fn)
155
155
156 if removed:
156 if removed:
157 # need to filter files if they are already reported as removed
157 # need to filter files if they are already reported as removed
158 unknown = [fn for fn in unknown if fn not in mf1]
158 unknown = [fn for fn in unknown if fn not in mf1]
159 ignored = [fn for fn in ignored if fn not in mf1]
159 ignored = [fn for fn in ignored if fn not in mf1]
160 # if they're deleted, don't report them as removed
160 # if they're deleted, don't report them as removed
161 removed = [fn for fn in removed if fn not in deletedset]
161 removed = [fn for fn in removed if fn not in deletedset]
162
162
163 return scmutil.status(modified, added, removed, deleted, unknown,
163 return scmutil.status(modified, added, removed, deleted, unknown,
164 ignored, clean)
164 ignored, clean)
165
165
166 @propertycache
166 @propertycache
167 def substate(self):
167 def substate(self):
168 return subrepo.state(self, self._repo.ui)
168 return subrepo.state(self, self._repo.ui)
169
169
170 def subrev(self, subpath):
170 def subrev(self, subpath):
171 return self.substate[subpath][1]
171 return self.substate[subpath][1]
172
172
173 def rev(self):
173 def rev(self):
174 return self._rev
174 return self._rev
175 def node(self):
175 def node(self):
176 return self._node
176 return self._node
177 def hex(self):
177 def hex(self):
178 return hex(self.node())
178 return hex(self.node())
179 def manifest(self):
179 def manifest(self):
180 return self._manifest
180 return self._manifest
181 def repo(self):
181 def repo(self):
182 return self._repo
182 return self._repo
183 def phasestr(self):
183 def phasestr(self):
184 return phases.phasenames[self.phase()]
184 return phases.phasenames[self.phase()]
185 def mutable(self):
185 def mutable(self):
186 return self.phase() > phases.public
186 return self.phase() > phases.public
187
187
188 def getfileset(self, expr):
188 def getfileset(self, expr):
189 return fileset.getfileset(self, expr)
189 return fileset.getfileset(self, expr)
190
190
191 def obsolete(self):
191 def obsolete(self):
192 """True if the changeset is obsolete"""
192 """True if the changeset is obsolete"""
193 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
193 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
194
194
195 def extinct(self):
195 def extinct(self):
196 """True if the changeset is extinct"""
196 """True if the changeset is extinct"""
197 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
197 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
198
198
199 def unstable(self):
199 def unstable(self):
200 """True if the changeset is not obsolete but it's ancestor are"""
200 """True if the changeset is not obsolete but it's ancestor are"""
201 return self.rev() in obsmod.getrevs(self._repo, 'unstable')
201 return self.rev() in obsmod.getrevs(self._repo, 'unstable')
202
202
203 def bumped(self):
203 def bumped(self):
204 """True if the changeset try to be a successor of a public changeset
204 """True if the changeset try to be a successor of a public changeset
205
205
206 Only non-public and non-obsolete changesets may be bumped.
206 Only non-public and non-obsolete changesets may be bumped.
207 """
207 """
208 return self.rev() in obsmod.getrevs(self._repo, 'bumped')
208 return self.rev() in obsmod.getrevs(self._repo, 'bumped')
209
209
210 def divergent(self):
210 def divergent(self):
211 """Is a successors of a changeset with multiple possible successors set
211 """Is a successors of a changeset with multiple possible successors set
212
212
213 Only non-public and non-obsolete changesets may be divergent.
213 Only non-public and non-obsolete changesets may be divergent.
214 """
214 """
215 return self.rev() in obsmod.getrevs(self._repo, 'divergent')
215 return self.rev() in obsmod.getrevs(self._repo, 'divergent')
216
216
217 def troubled(self):
217 def troubled(self):
218 """True if the changeset is either unstable, bumped or divergent"""
218 """True if the changeset is either unstable, bumped or divergent"""
219 return self.unstable() or self.bumped() or self.divergent()
219 return self.unstable() or self.bumped() or self.divergent()
220
220
221 def troubles(self):
221 def troubles(self):
222 """return the list of troubles affecting this changesets.
222 """return the list of troubles affecting this changesets.
223
223
224 Troubles are returned as strings. possible values are:
224 Troubles are returned as strings. possible values are:
225 - unstable,
225 - unstable,
226 - bumped,
226 - bumped,
227 - divergent.
227 - divergent.
228 """
228 """
229 troubles = []
229 troubles = []
230 if self.unstable():
230 if self.unstable():
231 troubles.append('unstable')
231 troubles.append('unstable')
232 if self.bumped():
232 if self.bumped():
233 troubles.append('bumped')
233 troubles.append('bumped')
234 if self.divergent():
234 if self.divergent():
235 troubles.append('divergent')
235 troubles.append('divergent')
236 return troubles
236 return troubles
237
237
238 def parents(self):
238 def parents(self):
239 """return contexts for each parent changeset"""
239 """return contexts for each parent changeset"""
240 return self._parents
240 return self._parents
241
241
242 def p1(self):
242 def p1(self):
243 return self._parents[0]
243 return self._parents[0]
244
244
245 def p2(self):
245 def p2(self):
246 parents = self._parents
246 parents = self._parents
247 if len(parents) == 2:
247 if len(parents) == 2:
248 return parents[1]
248 return parents[1]
249 return changectx(self._repo, nullrev)
249 return changectx(self._repo, nullrev)
250
250
251 def _fileinfo(self, path):
251 def _fileinfo(self, path):
252 if '_manifest' in self.__dict__:
252 if '_manifest' in self.__dict__:
253 try:
253 try:
254 return self._manifest[path], self._manifest.flags(path)
254 return self._manifest[path], self._manifest.flags(path)
255 except KeyError:
255 except KeyError:
256 raise error.ManifestLookupError(self._node, path,
256 raise error.ManifestLookupError(self._node, path,
257 _('not found in manifest'))
257 _('not found in manifest'))
258 if '_manifestdelta' in self.__dict__ or path in self.files():
258 if '_manifestdelta' in self.__dict__ or path in self.files():
259 if path in self._manifestdelta:
259 if path in self._manifestdelta:
260 return (self._manifestdelta[path],
260 return (self._manifestdelta[path],
261 self._manifestdelta.flags(path))
261 self._manifestdelta.flags(path))
262 node, flag = self._repo.manifest.find(self._changeset.manifest, path)
262 node, flag = self._repo.manifest.find(self._changeset.manifest, path)
263 if not node:
263 if not node:
264 raise error.ManifestLookupError(self._node, path,
264 raise error.ManifestLookupError(self._node, path,
265 _('not found in manifest'))
265 _('not found in manifest'))
266
266
267 return node, flag
267 return node, flag
268
268
269 def filenode(self, path):
269 def filenode(self, path):
270 return self._fileinfo(path)[0]
270 return self._fileinfo(path)[0]
271
271
272 def flags(self, path):
272 def flags(self, path):
273 try:
273 try:
274 return self._fileinfo(path)[1]
274 return self._fileinfo(path)[1]
275 except error.LookupError:
275 except error.LookupError:
276 return ''
276 return ''
277
277
278 def sub(self, path, allowcreate=True):
278 def sub(self, path, allowcreate=True):
279 '''return a subrepo for the stored revision of path, never wdir()'''
279 '''return a subrepo for the stored revision of path, never wdir()'''
280 return subrepo.subrepo(self, path, allowcreate=allowcreate)
280 return subrepo.subrepo(self, path, allowcreate=allowcreate)
281
281
282 def nullsub(self, path, pctx):
282 def nullsub(self, path, pctx):
283 return subrepo.nullsubrepo(self, path, pctx)
283 return subrepo.nullsubrepo(self, path, pctx)
284
284
285 def workingsub(self, path):
285 def workingsub(self, path):
286 '''return a subrepo for the stored revision, or wdir if this is a wdir
286 '''return a subrepo for the stored revision, or wdir if this is a wdir
287 context.
287 context.
288 '''
288 '''
289 return subrepo.subrepo(self, path, allowwdir=True)
289 return subrepo.subrepo(self, path, allowwdir=True)
290
290
291 def match(self, pats=[], include=None, exclude=None, default='glob',
291 def match(self, pats=[], include=None, exclude=None, default='glob',
292 listsubrepos=False, badfn=None):
292 listsubrepos=False, badfn=None):
293 r = self._repo
293 r = self._repo
294 return matchmod.match(r.root, r.getcwd(), pats,
294 return matchmod.match(r.root, r.getcwd(), pats,
295 include, exclude, default,
295 include, exclude, default,
296 auditor=r.nofsauditor, ctx=self,
296 auditor=r.nofsauditor, ctx=self,
297 listsubrepos=listsubrepos, badfn=badfn)
297 listsubrepos=listsubrepos, badfn=badfn)
298
298
299 def diff(self, ctx2=None, match=None, **opts):
299 def diff(self, ctx2=None, match=None, **opts):
300 """Returns a diff generator for the given contexts and matcher"""
300 """Returns a diff generator for the given contexts and matcher"""
301 if ctx2 is None:
301 if ctx2 is None:
302 ctx2 = self.p1()
302 ctx2 = self.p1()
303 if ctx2 is not None:
303 if ctx2 is not None:
304 ctx2 = self._repo[ctx2]
304 ctx2 = self._repo[ctx2]
305 diffopts = patch.diffopts(self._repo.ui, opts)
305 diffopts = patch.diffopts(self._repo.ui, opts)
306 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
306 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
307
307
308 def dirs(self):
308 def dirs(self):
309 return self._manifest.dirs()
309 return self._manifest.dirs()
310
310
311 def hasdir(self, dir):
311 def hasdir(self, dir):
312 return self._manifest.hasdir(dir)
312 return self._manifest.hasdir(dir)
313
313
314 def dirty(self, missing=False, merge=True, branch=True):
314 def dirty(self, missing=False, merge=True, branch=True):
315 return False
315 return False
316
316
317 def status(self, other=None, match=None, listignored=False,
317 def status(self, other=None, match=None, listignored=False,
318 listclean=False, listunknown=False, listsubrepos=False):
318 listclean=False, listunknown=False, listsubrepos=False):
319 """return status of files between two nodes or node and working
319 """return status of files between two nodes or node and working
320 directory.
320 directory.
321
321
322 If other is None, compare this node with working directory.
322 If other is None, compare this node with working directory.
323
323
324 returns (modified, added, removed, deleted, unknown, ignored, clean)
324 returns (modified, added, removed, deleted, unknown, ignored, clean)
325 """
325 """
326
326
327 ctx1 = self
327 ctx1 = self
328 ctx2 = self._repo[other]
328 ctx2 = self._repo[other]
329
329
330 # This next code block is, admittedly, fragile logic that tests for
330 # This next code block is, admittedly, fragile logic that tests for
331 # reversing the contexts and wouldn't need to exist if it weren't for
331 # reversing the contexts and wouldn't need to exist if it weren't for
332 # the fast (and common) code path of comparing the working directory
332 # the fast (and common) code path of comparing the working directory
333 # with its first parent.
333 # with its first parent.
334 #
334 #
335 # What we're aiming for here is the ability to call:
335 # What we're aiming for here is the ability to call:
336 #
336 #
337 # workingctx.status(parentctx)
337 # workingctx.status(parentctx)
338 #
338 #
339 # If we always built the manifest for each context and compared those,
339 # If we always built the manifest for each context and compared those,
340 # then we'd be done. But the special case of the above call means we
340 # then we'd be done. But the special case of the above call means we
341 # just copy the manifest of the parent.
341 # just copy the manifest of the parent.
342 reversed = False
342 reversed = False
343 if (not isinstance(ctx1, changectx)
343 if (not isinstance(ctx1, changectx)
344 and isinstance(ctx2, changectx)):
344 and isinstance(ctx2, changectx)):
345 reversed = True
345 reversed = True
346 ctx1, ctx2 = ctx2, ctx1
346 ctx1, ctx2 = ctx2, ctx1
347
347
348 match = ctx2._matchstatus(ctx1, match)
348 match = ctx2._matchstatus(ctx1, match)
349 r = scmutil.status([], [], [], [], [], [], [])
349 r = scmutil.status([], [], [], [], [], [], [])
350 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
350 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
351 listunknown)
351 listunknown)
352
352
353 if reversed:
353 if reversed:
354 # Reverse added and removed. Clear deleted, unknown and ignored as
354 # Reverse added and removed. Clear deleted, unknown and ignored as
355 # these make no sense to reverse.
355 # these make no sense to reverse.
356 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
356 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
357 r.clean)
357 r.clean)
358
358
359 if listsubrepos:
359 if listsubrepos:
360 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
360 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
361 try:
361 try:
362 rev2 = ctx2.subrev(subpath)
362 rev2 = ctx2.subrev(subpath)
363 except KeyError:
363 except KeyError:
364 # A subrepo that existed in node1 was deleted between
364 # A subrepo that existed in node1 was deleted between
365 # node1 and node2 (inclusive). Thus, ctx2's substate
365 # node1 and node2 (inclusive). Thus, ctx2's substate
366 # won't contain that subpath. The best we can do ignore it.
366 # won't contain that subpath. The best we can do ignore it.
367 rev2 = None
367 rev2 = None
368 submatch = matchmod.subdirmatcher(subpath, match)
368 submatch = matchmod.subdirmatcher(subpath, match)
369 s = sub.status(rev2, match=submatch, ignored=listignored,
369 s = sub.status(rev2, match=submatch, ignored=listignored,
370 clean=listclean, unknown=listunknown,
370 clean=listclean, unknown=listunknown,
371 listsubrepos=True)
371 listsubrepos=True)
372 for rfiles, sfiles in zip(r, s):
372 for rfiles, sfiles in zip(r, s):
373 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
373 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
374
374
375 for l in r:
375 for l in r:
376 l.sort()
376 l.sort()
377
377
378 return r
378 return r
379
379
380
380
381 def makememctx(repo, parents, text, user, date, branch, files, store,
381 def makememctx(repo, parents, text, user, date, branch, files, store,
382 editor=None, extra=None):
382 editor=None, extra=None):
383 def getfilectx(repo, memctx, path):
383 def getfilectx(repo, memctx, path):
384 data, mode, copied = store.getfile(path)
384 data, mode, copied = store.getfile(path)
385 if data is None:
385 if data is None:
386 return None
386 return None
387 islink, isexec = mode
387 islink, isexec = mode
388 return memfilectx(repo, path, data, islink=islink, isexec=isexec,
388 return memfilectx(repo, path, data, islink=islink, isexec=isexec,
389 copied=copied, memctx=memctx)
389 copied=copied, memctx=memctx)
390 if extra is None:
390 if extra is None:
391 extra = {}
391 extra = {}
392 if branch:
392 if branch:
393 extra['branch'] = encoding.fromlocal(branch)
393 extra['branch'] = encoding.fromlocal(branch)
394 ctx = memctx(repo, parents, text, files, getfilectx, user,
394 ctx = memctx(repo, parents, text, files, getfilectx, user,
395 date, extra, editor)
395 date, extra, editor)
396 return ctx
396 return ctx
397
397
398 class changectx(basectx):
398 class changectx(basectx):
399 """A changecontext object makes access to data related to a particular
399 """A changecontext object makes access to data related to a particular
400 changeset convenient. It represents a read-only context already present in
400 changeset convenient. It represents a read-only context already present in
401 the repo."""
401 the repo."""
402 def __init__(self, repo, changeid=''):
402 def __init__(self, repo, changeid=''):
403 """changeid is a revision number, node, or tag"""
403 """changeid is a revision number, node, or tag"""
404
404
405 # since basectx.__new__ already took care of copying the object, we
405 # since basectx.__new__ already took care of copying the object, we
406 # don't need to do anything in __init__, so we just exit here
406 # don't need to do anything in __init__, so we just exit here
407 if isinstance(changeid, basectx):
407 if isinstance(changeid, basectx):
408 return
408 return
409
409
410 if changeid == '':
410 if changeid == '':
411 changeid = '.'
411 changeid = '.'
412 self._repo = repo
412 self._repo = repo
413
413
414 try:
414 try:
415 if isinstance(changeid, int):
415 if isinstance(changeid, int):
416 self._node = repo.changelog.node(changeid)
416 self._node = repo.changelog.node(changeid)
417 self._rev = changeid
417 self._rev = changeid
418 return
418 return
419 if isinstance(changeid, long):
419 if isinstance(changeid, long):
420 changeid = str(changeid)
420 changeid = str(changeid)
421 if changeid == 'null':
421 if changeid == 'null':
422 self._node = nullid
422 self._node = nullid
423 self._rev = nullrev
423 self._rev = nullrev
424 return
424 return
425 if changeid == 'tip':
425 if changeid == 'tip':
426 self._node = repo.changelog.tip()
426 self._node = repo.changelog.tip()
427 self._rev = repo.changelog.rev(self._node)
427 self._rev = repo.changelog.rev(self._node)
428 return
428 return
429 if changeid == '.' or changeid == repo.dirstate.p1():
429 if changeid == '.' or changeid == repo.dirstate.p1():
430 # this is a hack to delay/avoid loading obsmarkers
430 # this is a hack to delay/avoid loading obsmarkers
431 # when we know that '.' won't be hidden
431 # when we know that '.' won't be hidden
432 self._node = repo.dirstate.p1()
432 self._node = repo.dirstate.p1()
433 self._rev = repo.unfiltered().changelog.rev(self._node)
433 self._rev = repo.unfiltered().changelog.rev(self._node)
434 return
434 return
435 if len(changeid) == 20:
435 if len(changeid) == 20:
436 try:
436 try:
437 self._node = changeid
437 self._node = changeid
438 self._rev = repo.changelog.rev(changeid)
438 self._rev = repo.changelog.rev(changeid)
439 return
439 return
440 except error.FilteredRepoLookupError:
440 except error.FilteredRepoLookupError:
441 raise
441 raise
442 except LookupError:
442 except LookupError:
443 pass
443 pass
444
444
445 try:
445 try:
446 r = int(changeid)
446 r = int(changeid)
447 if str(r) != changeid:
447 if str(r) != changeid:
448 raise ValueError
448 raise ValueError
449 l = len(repo.changelog)
449 l = len(repo.changelog)
450 if r < 0:
450 if r < 0:
451 r += l
451 r += l
452 if r < 0 or r >= l:
452 if r < 0 or r >= l:
453 raise ValueError
453 raise ValueError
454 self._rev = r
454 self._rev = r
455 self._node = repo.changelog.node(r)
455 self._node = repo.changelog.node(r)
456 return
456 return
457 except error.FilteredIndexError:
457 except error.FilteredIndexError:
458 raise
458 raise
459 except (ValueError, OverflowError, IndexError):
459 except (ValueError, OverflowError, IndexError):
460 pass
460 pass
461
461
462 if len(changeid) == 40:
462 if len(changeid) == 40:
463 try:
463 try:
464 self._node = bin(changeid)
464 self._node = bin(changeid)
465 self._rev = repo.changelog.rev(self._node)
465 self._rev = repo.changelog.rev(self._node)
466 return
466 return
467 except error.FilteredLookupError:
467 except error.FilteredLookupError:
468 raise
468 raise
469 except (TypeError, LookupError):
469 except (TypeError, LookupError):
470 pass
470 pass
471
471
472 # lookup bookmarks through the name interface
472 # lookup bookmarks through the name interface
473 try:
473 try:
474 self._node = repo.names.singlenode(repo, changeid)
474 self._node = repo.names.singlenode(repo, changeid)
475 self._rev = repo.changelog.rev(self._node)
475 self._rev = repo.changelog.rev(self._node)
476 return
476 return
477 except KeyError:
477 except KeyError:
478 pass
478 pass
479 except error.FilteredRepoLookupError:
479 except error.FilteredRepoLookupError:
480 raise
480 raise
481 except error.RepoLookupError:
481 except error.RepoLookupError:
482 pass
482 pass
483
483
484 self._node = repo.unfiltered().changelog._partialmatch(changeid)
484 self._node = repo.unfiltered().changelog._partialmatch(changeid)
485 if self._node is not None:
485 if self._node is not None:
486 self._rev = repo.changelog.rev(self._node)
486 self._rev = repo.changelog.rev(self._node)
487 return
487 return
488
488
489 # lookup failed
489 # lookup failed
490 # check if it might have come from damaged dirstate
490 # check if it might have come from damaged dirstate
491 #
491 #
492 # XXX we could avoid the unfiltered if we had a recognizable
492 # XXX we could avoid the unfiltered if we had a recognizable
493 # exception for filtered changeset access
493 # exception for filtered changeset access
494 if changeid in repo.unfiltered().dirstate.parents():
494 if changeid in repo.unfiltered().dirstate.parents():
495 msg = _("working directory has unknown parent '%s'!")
495 msg = _("working directory has unknown parent '%s'!")
496 raise error.Abort(msg % short(changeid))
496 raise error.Abort(msg % short(changeid))
497 try:
497 try:
498 if len(changeid) == 20 and nonascii(changeid):
498 if len(changeid) == 20 and nonascii(changeid):
499 changeid = hex(changeid)
499 changeid = hex(changeid)
500 except TypeError:
500 except TypeError:
501 pass
501 pass
502 except (error.FilteredIndexError, error.FilteredLookupError,
502 except (error.FilteredIndexError, error.FilteredLookupError,
503 error.FilteredRepoLookupError):
503 error.FilteredRepoLookupError):
504 if repo.filtername.startswith('visible'):
504 if repo.filtername.startswith('visible'):
505 msg = _("hidden revision '%s'") % changeid
505 msg = _("hidden revision '%s'") % changeid
506 hint = _('use --hidden to access hidden revisions')
506 hint = _('use --hidden to access hidden revisions')
507 raise error.FilteredRepoLookupError(msg, hint=hint)
507 raise error.FilteredRepoLookupError(msg, hint=hint)
508 msg = _("filtered revision '%s' (not in '%s' subset)")
508 msg = _("filtered revision '%s' (not in '%s' subset)")
509 msg %= (changeid, repo.filtername)
509 msg %= (changeid, repo.filtername)
510 raise error.FilteredRepoLookupError(msg)
510 raise error.FilteredRepoLookupError(msg)
511 except IndexError:
511 except IndexError:
512 pass
512 pass
513 raise error.RepoLookupError(
513 raise error.RepoLookupError(
514 _("unknown revision '%s'") % changeid)
514 _("unknown revision '%s'") % changeid)
515
515
516 def __hash__(self):
516 def __hash__(self):
517 try:
517 try:
518 return hash(self._rev)
518 return hash(self._rev)
519 except AttributeError:
519 except AttributeError:
520 return id(self)
520 return id(self)
521
521
522 def __nonzero__(self):
522 def __nonzero__(self):
523 return self._rev != nullrev
523 return self._rev != nullrev
524
524
525 @propertycache
525 @propertycache
526 def _changeset(self):
526 def _changeset(self):
527 return self._repo.changelog.changelogrevision(self.rev())
527 return self._repo.changelog.changelogrevision(self.rev())
528
528
529 @propertycache
529 @propertycache
530 def _manifest(self):
530 def _manifest(self):
531 return self._repo.manifestlog[self._changeset.manifest]
531 return self._repo.manifestlog[self._changeset.manifest].read()
532
532
533 @propertycache
533 @propertycache
534 def _manifestdelta(self):
534 def _manifestdelta(self):
535 return self._repo.manifest.readdelta(self._changeset.manifest)
535 return self._repo.manifest.readdelta(self._changeset.manifest)
536
536
537 @propertycache
537 @propertycache
538 def _parents(self):
538 def _parents(self):
539 repo = self._repo
539 repo = self._repo
540 p1, p2 = repo.changelog.parentrevs(self._rev)
540 p1, p2 = repo.changelog.parentrevs(self._rev)
541 if p2 == nullrev:
541 if p2 == nullrev:
542 return [changectx(repo, p1)]
542 return [changectx(repo, p1)]
543 return [changectx(repo, p1), changectx(repo, p2)]
543 return [changectx(repo, p1), changectx(repo, p2)]
544
544
545 def changeset(self):
545 def changeset(self):
546 c = self._changeset
546 c = self._changeset
547 return (
547 return (
548 c.manifest,
548 c.manifest,
549 c.user,
549 c.user,
550 c.date,
550 c.date,
551 c.files,
551 c.files,
552 c.description,
552 c.description,
553 c.extra,
553 c.extra,
554 )
554 )
555 def manifestnode(self):
555 def manifestnode(self):
556 return self._changeset.manifest
556 return self._changeset.manifest
557
557
558 def user(self):
558 def user(self):
559 return self._changeset.user
559 return self._changeset.user
560 def date(self):
560 def date(self):
561 return self._changeset.date
561 return self._changeset.date
562 def files(self):
562 def files(self):
563 return self._changeset.files
563 return self._changeset.files
564 def description(self):
564 def description(self):
565 return self._changeset.description
565 return self._changeset.description
566 def branch(self):
566 def branch(self):
567 return encoding.tolocal(self._changeset.extra.get("branch"))
567 return encoding.tolocal(self._changeset.extra.get("branch"))
568 def closesbranch(self):
568 def closesbranch(self):
569 return 'close' in self._changeset.extra
569 return 'close' in self._changeset.extra
570 def extra(self):
570 def extra(self):
571 return self._changeset.extra
571 return self._changeset.extra
572 def tags(self):
572 def tags(self):
573 return self._repo.nodetags(self._node)
573 return self._repo.nodetags(self._node)
574 def bookmarks(self):
574 def bookmarks(self):
575 return self._repo.nodebookmarks(self._node)
575 return self._repo.nodebookmarks(self._node)
576 def phase(self):
576 def phase(self):
577 return self._repo._phasecache.phase(self._repo, self._rev)
577 return self._repo._phasecache.phase(self._repo, self._rev)
578 def hidden(self):
578 def hidden(self):
579 return self._rev in repoview.filterrevs(self._repo, 'visible')
579 return self._rev in repoview.filterrevs(self._repo, 'visible')
580
580
581 def children(self):
581 def children(self):
582 """return contexts for each child changeset"""
582 """return contexts for each child changeset"""
583 c = self._repo.changelog.children(self._node)
583 c = self._repo.changelog.children(self._node)
584 return [changectx(self._repo, x) for x in c]
584 return [changectx(self._repo, x) for x in c]
585
585
586 def ancestors(self):
586 def ancestors(self):
587 for a in self._repo.changelog.ancestors([self._rev]):
587 for a in self._repo.changelog.ancestors([self._rev]):
588 yield changectx(self._repo, a)
588 yield changectx(self._repo, a)
589
589
590 def descendants(self):
590 def descendants(self):
591 for d in self._repo.changelog.descendants([self._rev]):
591 for d in self._repo.changelog.descendants([self._rev]):
592 yield changectx(self._repo, d)
592 yield changectx(self._repo, d)
593
593
594 def filectx(self, path, fileid=None, filelog=None):
594 def filectx(self, path, fileid=None, filelog=None):
595 """get a file context from this changeset"""
595 """get a file context from this changeset"""
596 if fileid is None:
596 if fileid is None:
597 fileid = self.filenode(path)
597 fileid = self.filenode(path)
598 return filectx(self._repo, path, fileid=fileid,
598 return filectx(self._repo, path, fileid=fileid,
599 changectx=self, filelog=filelog)
599 changectx=self, filelog=filelog)
600
600
601 def ancestor(self, c2, warn=False):
601 def ancestor(self, c2, warn=False):
602 """return the "best" ancestor context of self and c2
602 """return the "best" ancestor context of self and c2
603
603
604 If there are multiple candidates, it will show a message and check
604 If there are multiple candidates, it will show a message and check
605 merge.preferancestor configuration before falling back to the
605 merge.preferancestor configuration before falling back to the
606 revlog ancestor."""
606 revlog ancestor."""
607 # deal with workingctxs
607 # deal with workingctxs
608 n2 = c2._node
608 n2 = c2._node
609 if n2 is None:
609 if n2 is None:
610 n2 = c2._parents[0]._node
610 n2 = c2._parents[0]._node
611 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
611 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
612 if not cahs:
612 if not cahs:
613 anc = nullid
613 anc = nullid
614 elif len(cahs) == 1:
614 elif len(cahs) == 1:
615 anc = cahs[0]
615 anc = cahs[0]
616 else:
616 else:
617 # experimental config: merge.preferancestor
617 # experimental config: merge.preferancestor
618 for r in self._repo.ui.configlist('merge', 'preferancestor', ['*']):
618 for r in self._repo.ui.configlist('merge', 'preferancestor', ['*']):
619 try:
619 try:
620 ctx = changectx(self._repo, r)
620 ctx = changectx(self._repo, r)
621 except error.RepoLookupError:
621 except error.RepoLookupError:
622 continue
622 continue
623 anc = ctx.node()
623 anc = ctx.node()
624 if anc in cahs:
624 if anc in cahs:
625 break
625 break
626 else:
626 else:
627 anc = self._repo.changelog.ancestor(self._node, n2)
627 anc = self._repo.changelog.ancestor(self._node, n2)
628 if warn:
628 if warn:
629 self._repo.ui.status(
629 self._repo.ui.status(
630 (_("note: using %s as ancestor of %s and %s\n") %
630 (_("note: using %s as ancestor of %s and %s\n") %
631 (short(anc), short(self._node), short(n2))) +
631 (short(anc), short(self._node), short(n2))) +
632 ''.join(_(" alternatively, use --config "
632 ''.join(_(" alternatively, use --config "
633 "merge.preferancestor=%s\n") %
633 "merge.preferancestor=%s\n") %
634 short(n) for n in sorted(cahs) if n != anc))
634 short(n) for n in sorted(cahs) if n != anc))
635 return changectx(self._repo, anc)
635 return changectx(self._repo, anc)
636
636
637 def descendant(self, other):
637 def descendant(self, other):
638 """True if other is descendant of this changeset"""
638 """True if other is descendant of this changeset"""
639 return self._repo.changelog.descendant(self._rev, other._rev)
639 return self._repo.changelog.descendant(self._rev, other._rev)
640
640
641 def walk(self, match):
641 def walk(self, match):
642 '''Generates matching file names.'''
642 '''Generates matching file names.'''
643
643
644 # Wrap match.bad method to have message with nodeid
644 # Wrap match.bad method to have message with nodeid
645 def bad(fn, msg):
645 def bad(fn, msg):
646 # The manifest doesn't know about subrepos, so don't complain about
646 # The manifest doesn't know about subrepos, so don't complain about
647 # paths into valid subrepos.
647 # paths into valid subrepos.
648 if any(fn == s or fn.startswith(s + '/')
648 if any(fn == s or fn.startswith(s + '/')
649 for s in self.substate):
649 for s in self.substate):
650 return
650 return
651 match.bad(fn, _('no such file in rev %s') % self)
651 match.bad(fn, _('no such file in rev %s') % self)
652
652
653 m = matchmod.badmatch(match, bad)
653 m = matchmod.badmatch(match, bad)
654 return self._manifest.walk(m)
654 return self._manifest.walk(m)
655
655
656 def matches(self, match):
656 def matches(self, match):
657 return self.walk(match)
657 return self.walk(match)
658
658
659 class basefilectx(object):
659 class basefilectx(object):
660 """A filecontext object represents the common logic for its children:
660 """A filecontext object represents the common logic for its children:
661 filectx: read-only access to a filerevision that is already present
661 filectx: read-only access to a filerevision that is already present
662 in the repo,
662 in the repo,
663 workingfilectx: a filecontext that represents files from the working
663 workingfilectx: a filecontext that represents files from the working
664 directory,
664 directory,
665 memfilectx: a filecontext that represents files in-memory."""
665 memfilectx: a filecontext that represents files in-memory."""
666 def __new__(cls, repo, path, *args, **kwargs):
666 def __new__(cls, repo, path, *args, **kwargs):
667 return super(basefilectx, cls).__new__(cls)
667 return super(basefilectx, cls).__new__(cls)
668
668
669 @propertycache
669 @propertycache
670 def _filelog(self):
670 def _filelog(self):
671 return self._repo.file(self._path)
671 return self._repo.file(self._path)
672
672
673 @propertycache
673 @propertycache
674 def _changeid(self):
674 def _changeid(self):
675 if '_changeid' in self.__dict__:
675 if '_changeid' in self.__dict__:
676 return self._changeid
676 return self._changeid
677 elif '_changectx' in self.__dict__:
677 elif '_changectx' in self.__dict__:
678 return self._changectx.rev()
678 return self._changectx.rev()
679 elif '_descendantrev' in self.__dict__:
679 elif '_descendantrev' in self.__dict__:
680 # this file context was created from a revision with a known
680 # this file context was created from a revision with a known
681 # descendant, we can (lazily) correct for linkrev aliases
681 # descendant, we can (lazily) correct for linkrev aliases
682 return self._adjustlinkrev(self._path, self._filelog,
682 return self._adjustlinkrev(self._path, self._filelog,
683 self._filenode, self._descendantrev)
683 self._filenode, self._descendantrev)
684 else:
684 else:
685 return self._filelog.linkrev(self._filerev)
685 return self._filelog.linkrev(self._filerev)
686
686
687 @propertycache
687 @propertycache
688 def _filenode(self):
688 def _filenode(self):
689 if '_fileid' in self.__dict__:
689 if '_fileid' in self.__dict__:
690 return self._filelog.lookup(self._fileid)
690 return self._filelog.lookup(self._fileid)
691 else:
691 else:
692 return self._changectx.filenode(self._path)
692 return self._changectx.filenode(self._path)
693
693
694 @propertycache
694 @propertycache
695 def _filerev(self):
695 def _filerev(self):
696 return self._filelog.rev(self._filenode)
696 return self._filelog.rev(self._filenode)
697
697
698 @propertycache
698 @propertycache
699 def _repopath(self):
699 def _repopath(self):
700 return self._path
700 return self._path
701
701
702 def __nonzero__(self):
702 def __nonzero__(self):
703 try:
703 try:
704 self._filenode
704 self._filenode
705 return True
705 return True
706 except error.LookupError:
706 except error.LookupError:
707 # file is missing
707 # file is missing
708 return False
708 return False
709
709
710 def __str__(self):
710 def __str__(self):
711 return "%s@%s" % (self.path(), self._changectx)
711 return "%s@%s" % (self.path(), self._changectx)
712
712
713 def __repr__(self):
713 def __repr__(self):
714 return "<%s %s>" % (type(self).__name__, str(self))
714 return "<%s %s>" % (type(self).__name__, str(self))
715
715
716 def __hash__(self):
716 def __hash__(self):
717 try:
717 try:
718 return hash((self._path, self._filenode))
718 return hash((self._path, self._filenode))
719 except AttributeError:
719 except AttributeError:
720 return id(self)
720 return id(self)
721
721
722 def __eq__(self, other):
722 def __eq__(self, other):
723 try:
723 try:
724 return (type(self) == type(other) and self._path == other._path
724 return (type(self) == type(other) and self._path == other._path
725 and self._filenode == other._filenode)
725 and self._filenode == other._filenode)
726 except AttributeError:
726 except AttributeError:
727 return False
727 return False
728
728
729 def __ne__(self, other):
729 def __ne__(self, other):
730 return not (self == other)
730 return not (self == other)
731
731
732 def filerev(self):
732 def filerev(self):
733 return self._filerev
733 return self._filerev
734 def filenode(self):
734 def filenode(self):
735 return self._filenode
735 return self._filenode
736 def flags(self):
736 def flags(self):
737 return self._changectx.flags(self._path)
737 return self._changectx.flags(self._path)
738 def filelog(self):
738 def filelog(self):
739 return self._filelog
739 return self._filelog
740 def rev(self):
740 def rev(self):
741 return self._changeid
741 return self._changeid
742 def linkrev(self):
742 def linkrev(self):
743 return self._filelog.linkrev(self._filerev)
743 return self._filelog.linkrev(self._filerev)
744 def node(self):
744 def node(self):
745 return self._changectx.node()
745 return self._changectx.node()
746 def hex(self):
746 def hex(self):
747 return self._changectx.hex()
747 return self._changectx.hex()
748 def user(self):
748 def user(self):
749 return self._changectx.user()
749 return self._changectx.user()
750 def date(self):
750 def date(self):
751 return self._changectx.date()
751 return self._changectx.date()
752 def files(self):
752 def files(self):
753 return self._changectx.files()
753 return self._changectx.files()
754 def description(self):
754 def description(self):
755 return self._changectx.description()
755 return self._changectx.description()
756 def branch(self):
756 def branch(self):
757 return self._changectx.branch()
757 return self._changectx.branch()
758 def extra(self):
758 def extra(self):
759 return self._changectx.extra()
759 return self._changectx.extra()
760 def phase(self):
760 def phase(self):
761 return self._changectx.phase()
761 return self._changectx.phase()
762 def phasestr(self):
762 def phasestr(self):
763 return self._changectx.phasestr()
763 return self._changectx.phasestr()
764 def manifest(self):
764 def manifest(self):
765 return self._changectx.manifest()
765 return self._changectx.manifest()
766 def changectx(self):
766 def changectx(self):
767 return self._changectx
767 return self._changectx
768 def repo(self):
768 def repo(self):
769 return self._repo
769 return self._repo
770
770
771 def path(self):
771 def path(self):
772 return self._path
772 return self._path
773
773
774 def isbinary(self):
774 def isbinary(self):
775 try:
775 try:
776 return util.binary(self.data())
776 return util.binary(self.data())
777 except IOError:
777 except IOError:
778 return False
778 return False
779 def isexec(self):
779 def isexec(self):
780 return 'x' in self.flags()
780 return 'x' in self.flags()
781 def islink(self):
781 def islink(self):
782 return 'l' in self.flags()
782 return 'l' in self.flags()
783
783
784 def isabsent(self):
784 def isabsent(self):
785 """whether this filectx represents a file not in self._changectx
785 """whether this filectx represents a file not in self._changectx
786
786
787 This is mainly for merge code to detect change/delete conflicts. This is
787 This is mainly for merge code to detect change/delete conflicts. This is
788 expected to be True for all subclasses of basectx."""
788 expected to be True for all subclasses of basectx."""
789 return False
789 return False
790
790
791 _customcmp = False
791 _customcmp = False
792 def cmp(self, fctx):
792 def cmp(self, fctx):
793 """compare with other file context
793 """compare with other file context
794
794
795 returns True if different than fctx.
795 returns True if different than fctx.
796 """
796 """
797 if fctx._customcmp:
797 if fctx._customcmp:
798 return fctx.cmp(self)
798 return fctx.cmp(self)
799
799
800 if (fctx._filenode is None
800 if (fctx._filenode is None
801 and (self._repo._encodefilterpats
801 and (self._repo._encodefilterpats
802 # if file data starts with '\1\n', empty metadata block is
802 # if file data starts with '\1\n', empty metadata block is
803 # prepended, which adds 4 bytes to filelog.size().
803 # prepended, which adds 4 bytes to filelog.size().
804 or self.size() - 4 == fctx.size())
804 or self.size() - 4 == fctx.size())
805 or self.size() == fctx.size()):
805 or self.size() == fctx.size()):
806 return self._filelog.cmp(self._filenode, fctx.data())
806 return self._filelog.cmp(self._filenode, fctx.data())
807
807
808 return True
808 return True
809
809
810 def _adjustlinkrev(self, path, filelog, fnode, srcrev, inclusive=False):
810 def _adjustlinkrev(self, path, filelog, fnode, srcrev, inclusive=False):
811 """return the first ancestor of <srcrev> introducing <fnode>
811 """return the first ancestor of <srcrev> introducing <fnode>
812
812
813 If the linkrev of the file revision does not point to an ancestor of
813 If the linkrev of the file revision does not point to an ancestor of
814 srcrev, we'll walk down the ancestors until we find one introducing
814 srcrev, we'll walk down the ancestors until we find one introducing
815 this file revision.
815 this file revision.
816
816
817 :repo: a localrepository object (used to access changelog and manifest)
817 :repo: a localrepository object (used to access changelog and manifest)
818 :path: the file path
818 :path: the file path
819 :fnode: the nodeid of the file revision
819 :fnode: the nodeid of the file revision
820 :filelog: the filelog of this path
820 :filelog: the filelog of this path
821 :srcrev: the changeset revision we search ancestors from
821 :srcrev: the changeset revision we search ancestors from
822 :inclusive: if true, the src revision will also be checked
822 :inclusive: if true, the src revision will also be checked
823 """
823 """
824 repo = self._repo
824 repo = self._repo
825 cl = repo.unfiltered().changelog
825 cl = repo.unfiltered().changelog
826 ma = repo.manifest
826 ma = repo.manifest
827 # fetch the linkrev
827 # fetch the linkrev
828 fr = filelog.rev(fnode)
828 fr = filelog.rev(fnode)
829 lkr = filelog.linkrev(fr)
829 lkr = filelog.linkrev(fr)
830 # hack to reuse ancestor computation when searching for renames
830 # hack to reuse ancestor computation when searching for renames
831 memberanc = getattr(self, '_ancestrycontext', None)
831 memberanc = getattr(self, '_ancestrycontext', None)
832 iteranc = None
832 iteranc = None
833 if srcrev is None:
833 if srcrev is None:
834 # wctx case, used by workingfilectx during mergecopy
834 # wctx case, used by workingfilectx during mergecopy
835 revs = [p.rev() for p in self._repo[None].parents()]
835 revs = [p.rev() for p in self._repo[None].parents()]
836 inclusive = True # we skipped the real (revless) source
836 inclusive = True # we skipped the real (revless) source
837 else:
837 else:
838 revs = [srcrev]
838 revs = [srcrev]
839 if memberanc is None:
839 if memberanc is None:
840 memberanc = iteranc = cl.ancestors(revs, lkr,
840 memberanc = iteranc = cl.ancestors(revs, lkr,
841 inclusive=inclusive)
841 inclusive=inclusive)
842 # check if this linkrev is an ancestor of srcrev
842 # check if this linkrev is an ancestor of srcrev
843 if lkr not in memberanc:
843 if lkr not in memberanc:
844 if iteranc is None:
844 if iteranc is None:
845 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
845 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
846 for a in iteranc:
846 for a in iteranc:
847 ac = cl.read(a) # get changeset data (we avoid object creation)
847 ac = cl.read(a) # get changeset data (we avoid object creation)
848 if path in ac[3]: # checking the 'files' field.
848 if path in ac[3]: # checking the 'files' field.
849 # The file has been touched, check if the content is
849 # The file has been touched, check if the content is
850 # similar to the one we search for.
850 # similar to the one we search for.
851 if fnode == ma.readfast(ac[0]).get(path):
851 if fnode == ma.readfast(ac[0]).get(path):
852 return a
852 return a
853 # In theory, we should never get out of that loop without a result.
853 # In theory, we should never get out of that loop without a result.
854 # But if manifest uses a buggy file revision (not children of the
854 # But if manifest uses a buggy file revision (not children of the
855 # one it replaces) we could. Such a buggy situation will likely
855 # one it replaces) we could. Such a buggy situation will likely
856 # result is crash somewhere else at to some point.
856 # result is crash somewhere else at to some point.
857 return lkr
857 return lkr
858
858
859 def introrev(self):
859 def introrev(self):
860 """return the rev of the changeset which introduced this file revision
860 """return the rev of the changeset which introduced this file revision
861
861
862 This method is different from linkrev because it take into account the
862 This method is different from linkrev because it take into account the
863 changeset the filectx was created from. It ensures the returned
863 changeset the filectx was created from. It ensures the returned
864 revision is one of its ancestors. This prevents bugs from
864 revision is one of its ancestors. This prevents bugs from
865 'linkrev-shadowing' when a file revision is used by multiple
865 'linkrev-shadowing' when a file revision is used by multiple
866 changesets.
866 changesets.
867 """
867 """
868 lkr = self.linkrev()
868 lkr = self.linkrev()
869 attrs = vars(self)
869 attrs = vars(self)
870 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
870 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
871 if noctx or self.rev() == lkr:
871 if noctx or self.rev() == lkr:
872 return self.linkrev()
872 return self.linkrev()
873 return self._adjustlinkrev(self._path, self._filelog, self._filenode,
873 return self._adjustlinkrev(self._path, self._filelog, self._filenode,
874 self.rev(), inclusive=True)
874 self.rev(), inclusive=True)
875
875
876 def _parentfilectx(self, path, fileid, filelog):
876 def _parentfilectx(self, path, fileid, filelog):
877 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
877 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
878 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
878 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
879 if '_changeid' in vars(self) or '_changectx' in vars(self):
879 if '_changeid' in vars(self) or '_changectx' in vars(self):
880 # If self is associated with a changeset (probably explicitly
880 # If self is associated with a changeset (probably explicitly
881 # fed), ensure the created filectx is associated with a
881 # fed), ensure the created filectx is associated with a
882 # changeset that is an ancestor of self.changectx.
882 # changeset that is an ancestor of self.changectx.
883 # This lets us later use _adjustlinkrev to get a correct link.
883 # This lets us later use _adjustlinkrev to get a correct link.
884 fctx._descendantrev = self.rev()
884 fctx._descendantrev = self.rev()
885 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
885 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
886 elif '_descendantrev' in vars(self):
886 elif '_descendantrev' in vars(self):
887 # Otherwise propagate _descendantrev if we have one associated.
887 # Otherwise propagate _descendantrev if we have one associated.
888 fctx._descendantrev = self._descendantrev
888 fctx._descendantrev = self._descendantrev
889 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
889 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
890 return fctx
890 return fctx
891
891
892 def parents(self):
892 def parents(self):
893 _path = self._path
893 _path = self._path
894 fl = self._filelog
894 fl = self._filelog
895 parents = self._filelog.parents(self._filenode)
895 parents = self._filelog.parents(self._filenode)
896 pl = [(_path, node, fl) for node in parents if node != nullid]
896 pl = [(_path, node, fl) for node in parents if node != nullid]
897
897
898 r = fl.renamed(self._filenode)
898 r = fl.renamed(self._filenode)
899 if r:
899 if r:
900 # - In the simple rename case, both parent are nullid, pl is empty.
900 # - In the simple rename case, both parent are nullid, pl is empty.
901 # - In case of merge, only one of the parent is null id and should
901 # - In case of merge, only one of the parent is null id and should
902 # be replaced with the rename information. This parent is -always-
902 # be replaced with the rename information. This parent is -always-
903 # the first one.
903 # the first one.
904 #
904 #
905 # As null id have always been filtered out in the previous list
905 # As null id have always been filtered out in the previous list
906 # comprehension, inserting to 0 will always result in "replacing
906 # comprehension, inserting to 0 will always result in "replacing
907 # first nullid parent with rename information.
907 # first nullid parent with rename information.
908 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
908 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
909
909
910 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
910 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
911
911
912 def p1(self):
912 def p1(self):
913 return self.parents()[0]
913 return self.parents()[0]
914
914
915 def p2(self):
915 def p2(self):
916 p = self.parents()
916 p = self.parents()
917 if len(p) == 2:
917 if len(p) == 2:
918 return p[1]
918 return p[1]
919 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
919 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
920
920
921 def annotate(self, follow=False, linenumber=False, diffopts=None):
921 def annotate(self, follow=False, linenumber=False, diffopts=None):
922 '''returns a list of tuples of ((ctx, number), line) for each line
922 '''returns a list of tuples of ((ctx, number), line) for each line
923 in the file, where ctx is the filectx of the node where
923 in the file, where ctx is the filectx of the node where
924 that line was last changed; if linenumber parameter is true, number is
924 that line was last changed; if linenumber parameter is true, number is
925 the line number at the first appearance in the managed file, otherwise,
925 the line number at the first appearance in the managed file, otherwise,
926 number has a fixed value of False.
926 number has a fixed value of False.
927 '''
927 '''
928
928
929 def lines(text):
929 def lines(text):
930 if text.endswith("\n"):
930 if text.endswith("\n"):
931 return text.count("\n")
931 return text.count("\n")
932 return text.count("\n") + 1
932 return text.count("\n") + 1
933
933
934 if linenumber:
934 if linenumber:
935 def decorate(text, rev):
935 def decorate(text, rev):
936 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
936 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
937 else:
937 else:
938 def decorate(text, rev):
938 def decorate(text, rev):
939 return ([(rev, False)] * lines(text), text)
939 return ([(rev, False)] * lines(text), text)
940
940
941 def pair(parent, child):
941 def pair(parent, child):
942 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts,
942 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts,
943 refine=True)
943 refine=True)
944 for (a1, a2, b1, b2), t in blocks:
944 for (a1, a2, b1, b2), t in blocks:
945 # Changed blocks ('!') or blocks made only of blank lines ('~')
945 # Changed blocks ('!') or blocks made only of blank lines ('~')
946 # belong to the child.
946 # belong to the child.
947 if t == '=':
947 if t == '=':
948 child[0][b1:b2] = parent[0][a1:a2]
948 child[0][b1:b2] = parent[0][a1:a2]
949 return child
949 return child
950
950
951 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
951 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
952
952
953 def parents(f):
953 def parents(f):
954 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
954 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
955 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
955 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
956 # from the topmost introrev (= srcrev) down to p.linkrev() if it
956 # from the topmost introrev (= srcrev) down to p.linkrev() if it
957 # isn't an ancestor of the srcrev.
957 # isn't an ancestor of the srcrev.
958 f._changeid
958 f._changeid
959 pl = f.parents()
959 pl = f.parents()
960
960
961 # Don't return renamed parents if we aren't following.
961 # Don't return renamed parents if we aren't following.
962 if not follow:
962 if not follow:
963 pl = [p for p in pl if p.path() == f.path()]
963 pl = [p for p in pl if p.path() == f.path()]
964
964
965 # renamed filectx won't have a filelog yet, so set it
965 # renamed filectx won't have a filelog yet, so set it
966 # from the cache to save time
966 # from the cache to save time
967 for p in pl:
967 for p in pl:
968 if not '_filelog' in p.__dict__:
968 if not '_filelog' in p.__dict__:
969 p._filelog = getlog(p.path())
969 p._filelog = getlog(p.path())
970
970
971 return pl
971 return pl
972
972
973 # use linkrev to find the first changeset where self appeared
973 # use linkrev to find the first changeset where self appeared
974 base = self
974 base = self
975 introrev = self.introrev()
975 introrev = self.introrev()
976 if self.rev() != introrev:
976 if self.rev() != introrev:
977 base = self.filectx(self.filenode(), changeid=introrev)
977 base = self.filectx(self.filenode(), changeid=introrev)
978 if getattr(base, '_ancestrycontext', None) is None:
978 if getattr(base, '_ancestrycontext', None) is None:
979 cl = self._repo.changelog
979 cl = self._repo.changelog
980 if introrev is None:
980 if introrev is None:
981 # wctx is not inclusive, but works because _ancestrycontext
981 # wctx is not inclusive, but works because _ancestrycontext
982 # is used to test filelog revisions
982 # is used to test filelog revisions
983 ac = cl.ancestors([p.rev() for p in base.parents()],
983 ac = cl.ancestors([p.rev() for p in base.parents()],
984 inclusive=True)
984 inclusive=True)
985 else:
985 else:
986 ac = cl.ancestors([introrev], inclusive=True)
986 ac = cl.ancestors([introrev], inclusive=True)
987 base._ancestrycontext = ac
987 base._ancestrycontext = ac
988
988
989 # This algorithm would prefer to be recursive, but Python is a
989 # This algorithm would prefer to be recursive, but Python is a
990 # bit recursion-hostile. Instead we do an iterative
990 # bit recursion-hostile. Instead we do an iterative
991 # depth-first search.
991 # depth-first search.
992
992
993 visit = [base]
993 visit = [base]
994 hist = {}
994 hist = {}
995 pcache = {}
995 pcache = {}
996 needed = {base: 1}
996 needed = {base: 1}
997 while visit:
997 while visit:
998 f = visit[-1]
998 f = visit[-1]
999 pcached = f in pcache
999 pcached = f in pcache
1000 if not pcached:
1000 if not pcached:
1001 pcache[f] = parents(f)
1001 pcache[f] = parents(f)
1002
1002
1003 ready = True
1003 ready = True
1004 pl = pcache[f]
1004 pl = pcache[f]
1005 for p in pl:
1005 for p in pl:
1006 if p not in hist:
1006 if p not in hist:
1007 ready = False
1007 ready = False
1008 visit.append(p)
1008 visit.append(p)
1009 if not pcached:
1009 if not pcached:
1010 needed[p] = needed.get(p, 0) + 1
1010 needed[p] = needed.get(p, 0) + 1
1011 if ready:
1011 if ready:
1012 visit.pop()
1012 visit.pop()
1013 reusable = f in hist
1013 reusable = f in hist
1014 if reusable:
1014 if reusable:
1015 curr = hist[f]
1015 curr = hist[f]
1016 else:
1016 else:
1017 curr = decorate(f.data(), f)
1017 curr = decorate(f.data(), f)
1018 for p in pl:
1018 for p in pl:
1019 if not reusable:
1019 if not reusable:
1020 curr = pair(hist[p], curr)
1020 curr = pair(hist[p], curr)
1021 if needed[p] == 1:
1021 if needed[p] == 1:
1022 del hist[p]
1022 del hist[p]
1023 del needed[p]
1023 del needed[p]
1024 else:
1024 else:
1025 needed[p] -= 1
1025 needed[p] -= 1
1026
1026
1027 hist[f] = curr
1027 hist[f] = curr
1028 pcache[f] = []
1028 pcache[f] = []
1029
1029
1030 return zip(hist[base][0], hist[base][1].splitlines(True))
1030 return zip(hist[base][0], hist[base][1].splitlines(True))
1031
1031
1032 def ancestors(self, followfirst=False):
1032 def ancestors(self, followfirst=False):
1033 visit = {}
1033 visit = {}
1034 c = self
1034 c = self
1035 if followfirst:
1035 if followfirst:
1036 cut = 1
1036 cut = 1
1037 else:
1037 else:
1038 cut = None
1038 cut = None
1039
1039
1040 while True:
1040 while True:
1041 for parent in c.parents()[:cut]:
1041 for parent in c.parents()[:cut]:
1042 visit[(parent.linkrev(), parent.filenode())] = parent
1042 visit[(parent.linkrev(), parent.filenode())] = parent
1043 if not visit:
1043 if not visit:
1044 break
1044 break
1045 c = visit.pop(max(visit))
1045 c = visit.pop(max(visit))
1046 yield c
1046 yield c
1047
1047
1048 class filectx(basefilectx):
1048 class filectx(basefilectx):
1049 """A filecontext object makes access to data related to a particular
1049 """A filecontext object makes access to data related to a particular
1050 filerevision convenient."""
1050 filerevision convenient."""
1051 def __init__(self, repo, path, changeid=None, fileid=None,
1051 def __init__(self, repo, path, changeid=None, fileid=None,
1052 filelog=None, changectx=None):
1052 filelog=None, changectx=None):
1053 """changeid can be a changeset revision, node, or tag.
1053 """changeid can be a changeset revision, node, or tag.
1054 fileid can be a file revision or node."""
1054 fileid can be a file revision or node."""
1055 self._repo = repo
1055 self._repo = repo
1056 self._path = path
1056 self._path = path
1057
1057
1058 assert (changeid is not None
1058 assert (changeid is not None
1059 or fileid is not None
1059 or fileid is not None
1060 or changectx is not None), \
1060 or changectx is not None), \
1061 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1061 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1062 % (changeid, fileid, changectx))
1062 % (changeid, fileid, changectx))
1063
1063
1064 if filelog is not None:
1064 if filelog is not None:
1065 self._filelog = filelog
1065 self._filelog = filelog
1066
1066
1067 if changeid is not None:
1067 if changeid is not None:
1068 self._changeid = changeid
1068 self._changeid = changeid
1069 if changectx is not None:
1069 if changectx is not None:
1070 self._changectx = changectx
1070 self._changectx = changectx
1071 if fileid is not None:
1071 if fileid is not None:
1072 self._fileid = fileid
1072 self._fileid = fileid
1073
1073
1074 @propertycache
1074 @propertycache
1075 def _changectx(self):
1075 def _changectx(self):
1076 try:
1076 try:
1077 return changectx(self._repo, self._changeid)
1077 return changectx(self._repo, self._changeid)
1078 except error.FilteredRepoLookupError:
1078 except error.FilteredRepoLookupError:
1079 # Linkrev may point to any revision in the repository. When the
1079 # Linkrev may point to any revision in the repository. When the
1080 # repository is filtered this may lead to `filectx` trying to build
1080 # repository is filtered this may lead to `filectx` trying to build
1081 # `changectx` for filtered revision. In such case we fallback to
1081 # `changectx` for filtered revision. In such case we fallback to
1082 # creating `changectx` on the unfiltered version of the reposition.
1082 # creating `changectx` on the unfiltered version of the reposition.
1083 # This fallback should not be an issue because `changectx` from
1083 # This fallback should not be an issue because `changectx` from
1084 # `filectx` are not used in complex operations that care about
1084 # `filectx` are not used in complex operations that care about
1085 # filtering.
1085 # filtering.
1086 #
1086 #
1087 # This fallback is a cheap and dirty fix that prevent several
1087 # This fallback is a cheap and dirty fix that prevent several
1088 # crashes. It does not ensure the behavior is correct. However the
1088 # crashes. It does not ensure the behavior is correct. However the
1089 # behavior was not correct before filtering either and "incorrect
1089 # behavior was not correct before filtering either and "incorrect
1090 # behavior" is seen as better as "crash"
1090 # behavior" is seen as better as "crash"
1091 #
1091 #
1092 # Linkrevs have several serious troubles with filtering that are
1092 # Linkrevs have several serious troubles with filtering that are
1093 # complicated to solve. Proper handling of the issue here should be
1093 # complicated to solve. Proper handling of the issue here should be
1094 # considered when solving linkrev issue are on the table.
1094 # considered when solving linkrev issue are on the table.
1095 return changectx(self._repo.unfiltered(), self._changeid)
1095 return changectx(self._repo.unfiltered(), self._changeid)
1096
1096
1097 def filectx(self, fileid, changeid=None):
1097 def filectx(self, fileid, changeid=None):
1098 '''opens an arbitrary revision of the file without
1098 '''opens an arbitrary revision of the file without
1099 opening a new filelog'''
1099 opening a new filelog'''
1100 return filectx(self._repo, self._path, fileid=fileid,
1100 return filectx(self._repo, self._path, fileid=fileid,
1101 filelog=self._filelog, changeid=changeid)
1101 filelog=self._filelog, changeid=changeid)
1102
1102
1103 def data(self):
1103 def data(self):
1104 try:
1104 try:
1105 return self._filelog.read(self._filenode)
1105 return self._filelog.read(self._filenode)
1106 except error.CensoredNodeError:
1106 except error.CensoredNodeError:
1107 if self._repo.ui.config("censor", "policy", "abort") == "ignore":
1107 if self._repo.ui.config("censor", "policy", "abort") == "ignore":
1108 return ""
1108 return ""
1109 raise error.Abort(_("censored node: %s") % short(self._filenode),
1109 raise error.Abort(_("censored node: %s") % short(self._filenode),
1110 hint=_("set censor.policy to ignore errors"))
1110 hint=_("set censor.policy to ignore errors"))
1111
1111
1112 def size(self):
1112 def size(self):
1113 return self._filelog.size(self._filerev)
1113 return self._filelog.size(self._filerev)
1114
1114
1115 def renamed(self):
1115 def renamed(self):
1116 """check if file was actually renamed in this changeset revision
1116 """check if file was actually renamed in this changeset revision
1117
1117
1118 If rename logged in file revision, we report copy for changeset only
1118 If rename logged in file revision, we report copy for changeset only
1119 if file revisions linkrev points back to the changeset in question
1119 if file revisions linkrev points back to the changeset in question
1120 or both changeset parents contain different file revisions.
1120 or both changeset parents contain different file revisions.
1121 """
1121 """
1122
1122
1123 renamed = self._filelog.renamed(self._filenode)
1123 renamed = self._filelog.renamed(self._filenode)
1124 if not renamed:
1124 if not renamed:
1125 return renamed
1125 return renamed
1126
1126
1127 if self.rev() == self.linkrev():
1127 if self.rev() == self.linkrev():
1128 return renamed
1128 return renamed
1129
1129
1130 name = self.path()
1130 name = self.path()
1131 fnode = self._filenode
1131 fnode = self._filenode
1132 for p in self._changectx.parents():
1132 for p in self._changectx.parents():
1133 try:
1133 try:
1134 if fnode == p.filenode(name):
1134 if fnode == p.filenode(name):
1135 return None
1135 return None
1136 except error.LookupError:
1136 except error.LookupError:
1137 pass
1137 pass
1138 return renamed
1138 return renamed
1139
1139
1140 def children(self):
1140 def children(self):
1141 # hard for renames
1141 # hard for renames
1142 c = self._filelog.children(self._filenode)
1142 c = self._filelog.children(self._filenode)
1143 return [filectx(self._repo, self._path, fileid=x,
1143 return [filectx(self._repo, self._path, fileid=x,
1144 filelog=self._filelog) for x in c]
1144 filelog=self._filelog) for x in c]
1145
1145
1146 class committablectx(basectx):
1146 class committablectx(basectx):
1147 """A committablectx object provides common functionality for a context that
1147 """A committablectx object provides common functionality for a context that
1148 wants the ability to commit, e.g. workingctx or memctx."""
1148 wants the ability to commit, e.g. workingctx or memctx."""
1149 def __init__(self, repo, text="", user=None, date=None, extra=None,
1149 def __init__(self, repo, text="", user=None, date=None, extra=None,
1150 changes=None):
1150 changes=None):
1151 self._repo = repo
1151 self._repo = repo
1152 self._rev = None
1152 self._rev = None
1153 self._node = None
1153 self._node = None
1154 self._text = text
1154 self._text = text
1155 if date:
1155 if date:
1156 self._date = util.parsedate(date)
1156 self._date = util.parsedate(date)
1157 if user:
1157 if user:
1158 self._user = user
1158 self._user = user
1159 if changes:
1159 if changes:
1160 self._status = changes
1160 self._status = changes
1161
1161
1162 self._extra = {}
1162 self._extra = {}
1163 if extra:
1163 if extra:
1164 self._extra = extra.copy()
1164 self._extra = extra.copy()
1165 if 'branch' not in self._extra:
1165 if 'branch' not in self._extra:
1166 try:
1166 try:
1167 branch = encoding.fromlocal(self._repo.dirstate.branch())
1167 branch = encoding.fromlocal(self._repo.dirstate.branch())
1168 except UnicodeDecodeError:
1168 except UnicodeDecodeError:
1169 raise error.Abort(_('branch name not in UTF-8!'))
1169 raise error.Abort(_('branch name not in UTF-8!'))
1170 self._extra['branch'] = branch
1170 self._extra['branch'] = branch
1171 if self._extra['branch'] == '':
1171 if self._extra['branch'] == '':
1172 self._extra['branch'] = 'default'
1172 self._extra['branch'] = 'default'
1173
1173
1174 def __str__(self):
1174 def __str__(self):
1175 return str(self._parents[0]) + "+"
1175 return str(self._parents[0]) + "+"
1176
1176
1177 def __nonzero__(self):
1177 def __nonzero__(self):
1178 return True
1178 return True
1179
1179
1180 def _buildflagfunc(self):
1180 def _buildflagfunc(self):
1181 # Create a fallback function for getting file flags when the
1181 # Create a fallback function for getting file flags when the
1182 # filesystem doesn't support them
1182 # filesystem doesn't support them
1183
1183
1184 copiesget = self._repo.dirstate.copies().get
1184 copiesget = self._repo.dirstate.copies().get
1185 parents = self.parents()
1185 parents = self.parents()
1186 if len(parents) < 2:
1186 if len(parents) < 2:
1187 # when we have one parent, it's easy: copy from parent
1187 # when we have one parent, it's easy: copy from parent
1188 man = parents[0].manifest()
1188 man = parents[0].manifest()
1189 def func(f):
1189 def func(f):
1190 f = copiesget(f, f)
1190 f = copiesget(f, f)
1191 return man.flags(f)
1191 return man.flags(f)
1192 else:
1192 else:
1193 # merges are tricky: we try to reconstruct the unstored
1193 # merges are tricky: we try to reconstruct the unstored
1194 # result from the merge (issue1802)
1194 # result from the merge (issue1802)
1195 p1, p2 = parents
1195 p1, p2 = parents
1196 pa = p1.ancestor(p2)
1196 pa = p1.ancestor(p2)
1197 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1197 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1198
1198
1199 def func(f):
1199 def func(f):
1200 f = copiesget(f, f) # may be wrong for merges with copies
1200 f = copiesget(f, f) # may be wrong for merges with copies
1201 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1201 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1202 if fl1 == fl2:
1202 if fl1 == fl2:
1203 return fl1
1203 return fl1
1204 if fl1 == fla:
1204 if fl1 == fla:
1205 return fl2
1205 return fl2
1206 if fl2 == fla:
1206 if fl2 == fla:
1207 return fl1
1207 return fl1
1208 return '' # punt for conflicts
1208 return '' # punt for conflicts
1209
1209
1210 return func
1210 return func
1211
1211
1212 @propertycache
1212 @propertycache
1213 def _flagfunc(self):
1213 def _flagfunc(self):
1214 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1214 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1215
1215
1216 @propertycache
1216 @propertycache
1217 def _manifest(self):
1217 def _manifest(self):
1218 """generate a manifest corresponding to the values in self._status
1218 """generate a manifest corresponding to the values in self._status
1219
1219
1220 This reuse the file nodeid from parent, but we append an extra letter
1220 This reuse the file nodeid from parent, but we append an extra letter
1221 when modified. Modified files get an extra 'm' while added files get
1221 when modified. Modified files get an extra 'm' while added files get
1222 an extra 'a'. This is used by manifests merge to see that files
1222 an extra 'a'. This is used by manifests merge to see that files
1223 are different and by update logic to avoid deleting newly added files.
1223 are different and by update logic to avoid deleting newly added files.
1224 """
1224 """
1225 parents = self.parents()
1225 parents = self.parents()
1226
1226
1227 man1 = parents[0].manifest()
1227 man1 = parents[0].manifest()
1228 man = man1.copy()
1228 man = man1.copy()
1229 if len(parents) > 1:
1229 if len(parents) > 1:
1230 man2 = self.p2().manifest()
1230 man2 = self.p2().manifest()
1231 def getman(f):
1231 def getman(f):
1232 if f in man1:
1232 if f in man1:
1233 return man1
1233 return man1
1234 return man2
1234 return man2
1235 else:
1235 else:
1236 getman = lambda f: man1
1236 getman = lambda f: man1
1237
1237
1238 copied = self._repo.dirstate.copies()
1238 copied = self._repo.dirstate.copies()
1239 ff = self._flagfunc
1239 ff = self._flagfunc
1240 for i, l in (("a", self._status.added), ("m", self._status.modified)):
1240 for i, l in (("a", self._status.added), ("m", self._status.modified)):
1241 for f in l:
1241 for f in l:
1242 orig = copied.get(f, f)
1242 orig = copied.get(f, f)
1243 man[f] = getman(orig).get(orig, nullid) + i
1243 man[f] = getman(orig).get(orig, nullid) + i
1244 try:
1244 try:
1245 man.setflag(f, ff(f))
1245 man.setflag(f, ff(f))
1246 except OSError:
1246 except OSError:
1247 pass
1247 pass
1248
1248
1249 for f in self._status.deleted + self._status.removed:
1249 for f in self._status.deleted + self._status.removed:
1250 if f in man:
1250 if f in man:
1251 del man[f]
1251 del man[f]
1252
1252
1253 return man
1253 return man
1254
1254
1255 @propertycache
1255 @propertycache
1256 def _status(self):
1256 def _status(self):
1257 return self._repo.status()
1257 return self._repo.status()
1258
1258
1259 @propertycache
1259 @propertycache
1260 def _user(self):
1260 def _user(self):
1261 return self._repo.ui.username()
1261 return self._repo.ui.username()
1262
1262
1263 @propertycache
1263 @propertycache
1264 def _date(self):
1264 def _date(self):
1265 return util.makedate()
1265 return util.makedate()
1266
1266
1267 def subrev(self, subpath):
1267 def subrev(self, subpath):
1268 return None
1268 return None
1269
1269
1270 def manifestnode(self):
1270 def manifestnode(self):
1271 return None
1271 return None
1272 def user(self):
1272 def user(self):
1273 return self._user or self._repo.ui.username()
1273 return self._user or self._repo.ui.username()
1274 def date(self):
1274 def date(self):
1275 return self._date
1275 return self._date
1276 def description(self):
1276 def description(self):
1277 return self._text
1277 return self._text
1278 def files(self):
1278 def files(self):
1279 return sorted(self._status.modified + self._status.added +
1279 return sorted(self._status.modified + self._status.added +
1280 self._status.removed)
1280 self._status.removed)
1281
1281
1282 def modified(self):
1282 def modified(self):
1283 return self._status.modified
1283 return self._status.modified
1284 def added(self):
1284 def added(self):
1285 return self._status.added
1285 return self._status.added
1286 def removed(self):
1286 def removed(self):
1287 return self._status.removed
1287 return self._status.removed
1288 def deleted(self):
1288 def deleted(self):
1289 return self._status.deleted
1289 return self._status.deleted
1290 def branch(self):
1290 def branch(self):
1291 return encoding.tolocal(self._extra['branch'])
1291 return encoding.tolocal(self._extra['branch'])
1292 def closesbranch(self):
1292 def closesbranch(self):
1293 return 'close' in self._extra
1293 return 'close' in self._extra
1294 def extra(self):
1294 def extra(self):
1295 return self._extra
1295 return self._extra
1296
1296
1297 def tags(self):
1297 def tags(self):
1298 return []
1298 return []
1299
1299
1300 def bookmarks(self):
1300 def bookmarks(self):
1301 b = []
1301 b = []
1302 for p in self.parents():
1302 for p in self.parents():
1303 b.extend(p.bookmarks())
1303 b.extend(p.bookmarks())
1304 return b
1304 return b
1305
1305
1306 def phase(self):
1306 def phase(self):
1307 phase = phases.draft # default phase to draft
1307 phase = phases.draft # default phase to draft
1308 for p in self.parents():
1308 for p in self.parents():
1309 phase = max(phase, p.phase())
1309 phase = max(phase, p.phase())
1310 return phase
1310 return phase
1311
1311
1312 def hidden(self):
1312 def hidden(self):
1313 return False
1313 return False
1314
1314
1315 def children(self):
1315 def children(self):
1316 return []
1316 return []
1317
1317
1318 def flags(self, path):
1318 def flags(self, path):
1319 if '_manifest' in self.__dict__:
1319 if '_manifest' in self.__dict__:
1320 try:
1320 try:
1321 return self._manifest.flags(path)
1321 return self._manifest.flags(path)
1322 except KeyError:
1322 except KeyError:
1323 return ''
1323 return ''
1324
1324
1325 try:
1325 try:
1326 return self._flagfunc(path)
1326 return self._flagfunc(path)
1327 except OSError:
1327 except OSError:
1328 return ''
1328 return ''
1329
1329
1330 def ancestor(self, c2):
1330 def ancestor(self, c2):
1331 """return the "best" ancestor context of self and c2"""
1331 """return the "best" ancestor context of self and c2"""
1332 return self._parents[0].ancestor(c2) # punt on two parents for now
1332 return self._parents[0].ancestor(c2) # punt on two parents for now
1333
1333
1334 def walk(self, match):
1334 def walk(self, match):
1335 '''Generates matching file names.'''
1335 '''Generates matching file names.'''
1336 return sorted(self._repo.dirstate.walk(match, sorted(self.substate),
1336 return sorted(self._repo.dirstate.walk(match, sorted(self.substate),
1337 True, False))
1337 True, False))
1338
1338
1339 def matches(self, match):
1339 def matches(self, match):
1340 return sorted(self._repo.dirstate.matches(match))
1340 return sorted(self._repo.dirstate.matches(match))
1341
1341
1342 def ancestors(self):
1342 def ancestors(self):
1343 for p in self._parents:
1343 for p in self._parents:
1344 yield p
1344 yield p
1345 for a in self._repo.changelog.ancestors(
1345 for a in self._repo.changelog.ancestors(
1346 [p.rev() for p in self._parents]):
1346 [p.rev() for p in self._parents]):
1347 yield changectx(self._repo, a)
1347 yield changectx(self._repo, a)
1348
1348
1349 def markcommitted(self, node):
1349 def markcommitted(self, node):
1350 """Perform post-commit cleanup necessary after committing this ctx
1350 """Perform post-commit cleanup necessary after committing this ctx
1351
1351
1352 Specifically, this updates backing stores this working context
1352 Specifically, this updates backing stores this working context
1353 wraps to reflect the fact that the changes reflected by this
1353 wraps to reflect the fact that the changes reflected by this
1354 workingctx have been committed. For example, it marks
1354 workingctx have been committed. For example, it marks
1355 modified and added files as normal in the dirstate.
1355 modified and added files as normal in the dirstate.
1356
1356
1357 """
1357 """
1358
1358
1359 self._repo.dirstate.beginparentchange()
1359 self._repo.dirstate.beginparentchange()
1360 for f in self.modified() + self.added():
1360 for f in self.modified() + self.added():
1361 self._repo.dirstate.normal(f)
1361 self._repo.dirstate.normal(f)
1362 for f in self.removed():
1362 for f in self.removed():
1363 self._repo.dirstate.drop(f)
1363 self._repo.dirstate.drop(f)
1364 self._repo.dirstate.setparents(node)
1364 self._repo.dirstate.setparents(node)
1365 self._repo.dirstate.endparentchange()
1365 self._repo.dirstate.endparentchange()
1366
1366
1367 # write changes out explicitly, because nesting wlock at
1367 # write changes out explicitly, because nesting wlock at
1368 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1368 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1369 # from immediately doing so for subsequent changing files
1369 # from immediately doing so for subsequent changing files
1370 self._repo.dirstate.write(self._repo.currenttransaction())
1370 self._repo.dirstate.write(self._repo.currenttransaction())
1371
1371
1372 class workingctx(committablectx):
1372 class workingctx(committablectx):
1373 """A workingctx object makes access to data related to
1373 """A workingctx object makes access to data related to
1374 the current working directory convenient.
1374 the current working directory convenient.
1375 date - any valid date string or (unixtime, offset), or None.
1375 date - any valid date string or (unixtime, offset), or None.
1376 user - username string, or None.
1376 user - username string, or None.
1377 extra - a dictionary of extra values, or None.
1377 extra - a dictionary of extra values, or None.
1378 changes - a list of file lists as returned by localrepo.status()
1378 changes - a list of file lists as returned by localrepo.status()
1379 or None to use the repository status.
1379 or None to use the repository status.
1380 """
1380 """
1381 def __init__(self, repo, text="", user=None, date=None, extra=None,
1381 def __init__(self, repo, text="", user=None, date=None, extra=None,
1382 changes=None):
1382 changes=None):
1383 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1383 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1384
1384
1385 def __iter__(self):
1385 def __iter__(self):
1386 d = self._repo.dirstate
1386 d = self._repo.dirstate
1387 for f in d:
1387 for f in d:
1388 if d[f] != 'r':
1388 if d[f] != 'r':
1389 yield f
1389 yield f
1390
1390
1391 def __contains__(self, key):
1391 def __contains__(self, key):
1392 return self._repo.dirstate[key] not in "?r"
1392 return self._repo.dirstate[key] not in "?r"
1393
1393
1394 def hex(self):
1394 def hex(self):
1395 return hex(wdirid)
1395 return hex(wdirid)
1396
1396
1397 @propertycache
1397 @propertycache
1398 def _parents(self):
1398 def _parents(self):
1399 p = self._repo.dirstate.parents()
1399 p = self._repo.dirstate.parents()
1400 if p[1] == nullid:
1400 if p[1] == nullid:
1401 p = p[:-1]
1401 p = p[:-1]
1402 return [changectx(self._repo, x) for x in p]
1402 return [changectx(self._repo, x) for x in p]
1403
1403
1404 def filectx(self, path, filelog=None):
1404 def filectx(self, path, filelog=None):
1405 """get a file context from the working directory"""
1405 """get a file context from the working directory"""
1406 return workingfilectx(self._repo, path, workingctx=self,
1406 return workingfilectx(self._repo, path, workingctx=self,
1407 filelog=filelog)
1407 filelog=filelog)
1408
1408
1409 def dirty(self, missing=False, merge=True, branch=True):
1409 def dirty(self, missing=False, merge=True, branch=True):
1410 "check whether a working directory is modified"
1410 "check whether a working directory is modified"
1411 # check subrepos first
1411 # check subrepos first
1412 for s in sorted(self.substate):
1412 for s in sorted(self.substate):
1413 if self.sub(s).dirty():
1413 if self.sub(s).dirty():
1414 return True
1414 return True
1415 # check current working dir
1415 # check current working dir
1416 return ((merge and self.p2()) or
1416 return ((merge and self.p2()) or
1417 (branch and self.branch() != self.p1().branch()) or
1417 (branch and self.branch() != self.p1().branch()) or
1418 self.modified() or self.added() or self.removed() or
1418 self.modified() or self.added() or self.removed() or
1419 (missing and self.deleted()))
1419 (missing and self.deleted()))
1420
1420
1421 def add(self, list, prefix=""):
1421 def add(self, list, prefix=""):
1422 join = lambda f: os.path.join(prefix, f)
1422 join = lambda f: os.path.join(prefix, f)
1423 with self._repo.wlock():
1423 with self._repo.wlock():
1424 ui, ds = self._repo.ui, self._repo.dirstate
1424 ui, ds = self._repo.ui, self._repo.dirstate
1425 rejected = []
1425 rejected = []
1426 lstat = self._repo.wvfs.lstat
1426 lstat = self._repo.wvfs.lstat
1427 for f in list:
1427 for f in list:
1428 scmutil.checkportable(ui, join(f))
1428 scmutil.checkportable(ui, join(f))
1429 try:
1429 try:
1430 st = lstat(f)
1430 st = lstat(f)
1431 except OSError:
1431 except OSError:
1432 ui.warn(_("%s does not exist!\n") % join(f))
1432 ui.warn(_("%s does not exist!\n") % join(f))
1433 rejected.append(f)
1433 rejected.append(f)
1434 continue
1434 continue
1435 if st.st_size > 10000000:
1435 if st.st_size > 10000000:
1436 ui.warn(_("%s: up to %d MB of RAM may be required "
1436 ui.warn(_("%s: up to %d MB of RAM may be required "
1437 "to manage this file\n"
1437 "to manage this file\n"
1438 "(use 'hg revert %s' to cancel the "
1438 "(use 'hg revert %s' to cancel the "
1439 "pending addition)\n")
1439 "pending addition)\n")
1440 % (f, 3 * st.st_size // 1000000, join(f)))
1440 % (f, 3 * st.st_size // 1000000, join(f)))
1441 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1441 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1442 ui.warn(_("%s not added: only files and symlinks "
1442 ui.warn(_("%s not added: only files and symlinks "
1443 "supported currently\n") % join(f))
1443 "supported currently\n") % join(f))
1444 rejected.append(f)
1444 rejected.append(f)
1445 elif ds[f] in 'amn':
1445 elif ds[f] in 'amn':
1446 ui.warn(_("%s already tracked!\n") % join(f))
1446 ui.warn(_("%s already tracked!\n") % join(f))
1447 elif ds[f] == 'r':
1447 elif ds[f] == 'r':
1448 ds.normallookup(f)
1448 ds.normallookup(f)
1449 else:
1449 else:
1450 ds.add(f)
1450 ds.add(f)
1451 return rejected
1451 return rejected
1452
1452
1453 def forget(self, files, prefix=""):
1453 def forget(self, files, prefix=""):
1454 join = lambda f: os.path.join(prefix, f)
1454 join = lambda f: os.path.join(prefix, f)
1455 with self._repo.wlock():
1455 with self._repo.wlock():
1456 rejected = []
1456 rejected = []
1457 for f in files:
1457 for f in files:
1458 if f not in self._repo.dirstate:
1458 if f not in self._repo.dirstate:
1459 self._repo.ui.warn(_("%s not tracked!\n") % join(f))
1459 self._repo.ui.warn(_("%s not tracked!\n") % join(f))
1460 rejected.append(f)
1460 rejected.append(f)
1461 elif self._repo.dirstate[f] != 'a':
1461 elif self._repo.dirstate[f] != 'a':
1462 self._repo.dirstate.remove(f)
1462 self._repo.dirstate.remove(f)
1463 else:
1463 else:
1464 self._repo.dirstate.drop(f)
1464 self._repo.dirstate.drop(f)
1465 return rejected
1465 return rejected
1466
1466
1467 def undelete(self, list):
1467 def undelete(self, list):
1468 pctxs = self.parents()
1468 pctxs = self.parents()
1469 with self._repo.wlock():
1469 with self._repo.wlock():
1470 for f in list:
1470 for f in list:
1471 if self._repo.dirstate[f] != 'r':
1471 if self._repo.dirstate[f] != 'r':
1472 self._repo.ui.warn(_("%s not removed!\n") % f)
1472 self._repo.ui.warn(_("%s not removed!\n") % f)
1473 else:
1473 else:
1474 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1474 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1475 t = fctx.data()
1475 t = fctx.data()
1476 self._repo.wwrite(f, t, fctx.flags())
1476 self._repo.wwrite(f, t, fctx.flags())
1477 self._repo.dirstate.normal(f)
1477 self._repo.dirstate.normal(f)
1478
1478
1479 def copy(self, source, dest):
1479 def copy(self, source, dest):
1480 try:
1480 try:
1481 st = self._repo.wvfs.lstat(dest)
1481 st = self._repo.wvfs.lstat(dest)
1482 except OSError as err:
1482 except OSError as err:
1483 if err.errno != errno.ENOENT:
1483 if err.errno != errno.ENOENT:
1484 raise
1484 raise
1485 self._repo.ui.warn(_("%s does not exist!\n") % dest)
1485 self._repo.ui.warn(_("%s does not exist!\n") % dest)
1486 return
1486 return
1487 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1487 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1488 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1488 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1489 "symbolic link\n") % dest)
1489 "symbolic link\n") % dest)
1490 else:
1490 else:
1491 with self._repo.wlock():
1491 with self._repo.wlock():
1492 if self._repo.dirstate[dest] in '?':
1492 if self._repo.dirstate[dest] in '?':
1493 self._repo.dirstate.add(dest)
1493 self._repo.dirstate.add(dest)
1494 elif self._repo.dirstate[dest] in 'r':
1494 elif self._repo.dirstate[dest] in 'r':
1495 self._repo.dirstate.normallookup(dest)
1495 self._repo.dirstate.normallookup(dest)
1496 self._repo.dirstate.copy(source, dest)
1496 self._repo.dirstate.copy(source, dest)
1497
1497
1498 def match(self, pats=[], include=None, exclude=None, default='glob',
1498 def match(self, pats=[], include=None, exclude=None, default='glob',
1499 listsubrepos=False, badfn=None):
1499 listsubrepos=False, badfn=None):
1500 r = self._repo
1500 r = self._repo
1501
1501
1502 # Only a case insensitive filesystem needs magic to translate user input
1502 # Only a case insensitive filesystem needs magic to translate user input
1503 # to actual case in the filesystem.
1503 # to actual case in the filesystem.
1504 if not util.fscasesensitive(r.root):
1504 if not util.fscasesensitive(r.root):
1505 return matchmod.icasefsmatcher(r.root, r.getcwd(), pats, include,
1505 return matchmod.icasefsmatcher(r.root, r.getcwd(), pats, include,
1506 exclude, default, r.auditor, self,
1506 exclude, default, r.auditor, self,
1507 listsubrepos=listsubrepos,
1507 listsubrepos=listsubrepos,
1508 badfn=badfn)
1508 badfn=badfn)
1509 return matchmod.match(r.root, r.getcwd(), pats,
1509 return matchmod.match(r.root, r.getcwd(), pats,
1510 include, exclude, default,
1510 include, exclude, default,
1511 auditor=r.auditor, ctx=self,
1511 auditor=r.auditor, ctx=self,
1512 listsubrepos=listsubrepos, badfn=badfn)
1512 listsubrepos=listsubrepos, badfn=badfn)
1513
1513
1514 def _filtersuspectsymlink(self, files):
1514 def _filtersuspectsymlink(self, files):
1515 if not files or self._repo.dirstate._checklink:
1515 if not files or self._repo.dirstate._checklink:
1516 return files
1516 return files
1517
1517
1518 # Symlink placeholders may get non-symlink-like contents
1518 # Symlink placeholders may get non-symlink-like contents
1519 # via user error or dereferencing by NFS or Samba servers,
1519 # via user error or dereferencing by NFS or Samba servers,
1520 # so we filter out any placeholders that don't look like a
1520 # so we filter out any placeholders that don't look like a
1521 # symlink
1521 # symlink
1522 sane = []
1522 sane = []
1523 for f in files:
1523 for f in files:
1524 if self.flags(f) == 'l':
1524 if self.flags(f) == 'l':
1525 d = self[f].data()
1525 d = self[f].data()
1526 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1526 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1527 self._repo.ui.debug('ignoring suspect symlink placeholder'
1527 self._repo.ui.debug('ignoring suspect symlink placeholder'
1528 ' "%s"\n' % f)
1528 ' "%s"\n' % f)
1529 continue
1529 continue
1530 sane.append(f)
1530 sane.append(f)
1531 return sane
1531 return sane
1532
1532
1533 def _checklookup(self, files):
1533 def _checklookup(self, files):
1534 # check for any possibly clean files
1534 # check for any possibly clean files
1535 if not files:
1535 if not files:
1536 return [], []
1536 return [], []
1537
1537
1538 modified = []
1538 modified = []
1539 fixup = []
1539 fixup = []
1540 pctx = self._parents[0]
1540 pctx = self._parents[0]
1541 # do a full compare of any files that might have changed
1541 # do a full compare of any files that might have changed
1542 for f in sorted(files):
1542 for f in sorted(files):
1543 if (f not in pctx or self.flags(f) != pctx.flags(f)
1543 if (f not in pctx or self.flags(f) != pctx.flags(f)
1544 or pctx[f].cmp(self[f])):
1544 or pctx[f].cmp(self[f])):
1545 modified.append(f)
1545 modified.append(f)
1546 else:
1546 else:
1547 fixup.append(f)
1547 fixup.append(f)
1548
1548
1549 # update dirstate for files that are actually clean
1549 # update dirstate for files that are actually clean
1550 if fixup:
1550 if fixup:
1551 try:
1551 try:
1552 # updating the dirstate is optional
1552 # updating the dirstate is optional
1553 # so we don't wait on the lock
1553 # so we don't wait on the lock
1554 # wlock can invalidate the dirstate, so cache normal _after_
1554 # wlock can invalidate the dirstate, so cache normal _after_
1555 # taking the lock
1555 # taking the lock
1556 with self._repo.wlock(False):
1556 with self._repo.wlock(False):
1557 normal = self._repo.dirstate.normal
1557 normal = self._repo.dirstate.normal
1558 for f in fixup:
1558 for f in fixup:
1559 normal(f)
1559 normal(f)
1560 # write changes out explicitly, because nesting
1560 # write changes out explicitly, because nesting
1561 # wlock at runtime may prevent 'wlock.release()'
1561 # wlock at runtime may prevent 'wlock.release()'
1562 # after this block from doing so for subsequent
1562 # after this block from doing so for subsequent
1563 # changing files
1563 # changing files
1564 self._repo.dirstate.write(self._repo.currenttransaction())
1564 self._repo.dirstate.write(self._repo.currenttransaction())
1565 except error.LockError:
1565 except error.LockError:
1566 pass
1566 pass
1567 return modified, fixup
1567 return modified, fixup
1568
1568
1569 def _manifestmatches(self, match, s):
1569 def _manifestmatches(self, match, s):
1570 """Slow path for workingctx
1570 """Slow path for workingctx
1571
1571
1572 The fast path is when we compare the working directory to its parent
1572 The fast path is when we compare the working directory to its parent
1573 which means this function is comparing with a non-parent; therefore we
1573 which means this function is comparing with a non-parent; therefore we
1574 need to build a manifest and return what matches.
1574 need to build a manifest and return what matches.
1575 """
1575 """
1576 mf = self._repo['.']._manifestmatches(match, s)
1576 mf = self._repo['.']._manifestmatches(match, s)
1577 for f in s.modified + s.added:
1577 for f in s.modified + s.added:
1578 mf[f] = _newnode
1578 mf[f] = _newnode
1579 mf.setflag(f, self.flags(f))
1579 mf.setflag(f, self.flags(f))
1580 for f in s.removed:
1580 for f in s.removed:
1581 if f in mf:
1581 if f in mf:
1582 del mf[f]
1582 del mf[f]
1583 return mf
1583 return mf
1584
1584
1585 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1585 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1586 unknown=False):
1586 unknown=False):
1587 '''Gets the status from the dirstate -- internal use only.'''
1587 '''Gets the status from the dirstate -- internal use only.'''
1588 listignored, listclean, listunknown = ignored, clean, unknown
1588 listignored, listclean, listunknown = ignored, clean, unknown
1589 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1589 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1590 subrepos = []
1590 subrepos = []
1591 if '.hgsub' in self:
1591 if '.hgsub' in self:
1592 subrepos = sorted(self.substate)
1592 subrepos = sorted(self.substate)
1593 cmp, s = self._repo.dirstate.status(match, subrepos, listignored,
1593 cmp, s = self._repo.dirstate.status(match, subrepos, listignored,
1594 listclean, listunknown)
1594 listclean, listunknown)
1595
1595
1596 # check for any possibly clean files
1596 # check for any possibly clean files
1597 if cmp:
1597 if cmp:
1598 modified2, fixup = self._checklookup(cmp)
1598 modified2, fixup = self._checklookup(cmp)
1599 s.modified.extend(modified2)
1599 s.modified.extend(modified2)
1600
1600
1601 # update dirstate for files that are actually clean
1601 # update dirstate for files that are actually clean
1602 if fixup and listclean:
1602 if fixup and listclean:
1603 s.clean.extend(fixup)
1603 s.clean.extend(fixup)
1604
1604
1605 if match.always():
1605 if match.always():
1606 # cache for performance
1606 # cache for performance
1607 if s.unknown or s.ignored or s.clean:
1607 if s.unknown or s.ignored or s.clean:
1608 # "_status" is cached with list*=False in the normal route
1608 # "_status" is cached with list*=False in the normal route
1609 self._status = scmutil.status(s.modified, s.added, s.removed,
1609 self._status = scmutil.status(s.modified, s.added, s.removed,
1610 s.deleted, [], [], [])
1610 s.deleted, [], [], [])
1611 else:
1611 else:
1612 self._status = s
1612 self._status = s
1613
1613
1614 return s
1614 return s
1615
1615
1616 def _buildstatus(self, other, s, match, listignored, listclean,
1616 def _buildstatus(self, other, s, match, listignored, listclean,
1617 listunknown):
1617 listunknown):
1618 """build a status with respect to another context
1618 """build a status with respect to another context
1619
1619
1620 This includes logic for maintaining the fast path of status when
1620 This includes logic for maintaining the fast path of status when
1621 comparing the working directory against its parent, which is to skip
1621 comparing the working directory against its parent, which is to skip
1622 building a new manifest if self (working directory) is not comparing
1622 building a new manifest if self (working directory) is not comparing
1623 against its parent (repo['.']).
1623 against its parent (repo['.']).
1624 """
1624 """
1625 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1625 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1626 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1626 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1627 # might have accidentally ended up with the entire contents of the file
1627 # might have accidentally ended up with the entire contents of the file
1628 # they are supposed to be linking to.
1628 # they are supposed to be linking to.
1629 s.modified[:] = self._filtersuspectsymlink(s.modified)
1629 s.modified[:] = self._filtersuspectsymlink(s.modified)
1630 if other != self._repo['.']:
1630 if other != self._repo['.']:
1631 s = super(workingctx, self)._buildstatus(other, s, match,
1631 s = super(workingctx, self)._buildstatus(other, s, match,
1632 listignored, listclean,
1632 listignored, listclean,
1633 listunknown)
1633 listunknown)
1634 return s
1634 return s
1635
1635
1636 def _matchstatus(self, other, match):
1636 def _matchstatus(self, other, match):
1637 """override the match method with a filter for directory patterns
1637 """override the match method with a filter for directory patterns
1638
1638
1639 We use inheritance to customize the match.bad method only in cases of
1639 We use inheritance to customize the match.bad method only in cases of
1640 workingctx since it belongs only to the working directory when
1640 workingctx since it belongs only to the working directory when
1641 comparing against the parent changeset.
1641 comparing against the parent changeset.
1642
1642
1643 If we aren't comparing against the working directory's parent, then we
1643 If we aren't comparing against the working directory's parent, then we
1644 just use the default match object sent to us.
1644 just use the default match object sent to us.
1645 """
1645 """
1646 superself = super(workingctx, self)
1646 superself = super(workingctx, self)
1647 match = superself._matchstatus(other, match)
1647 match = superself._matchstatus(other, match)
1648 if other != self._repo['.']:
1648 if other != self._repo['.']:
1649 def bad(f, msg):
1649 def bad(f, msg):
1650 # 'f' may be a directory pattern from 'match.files()',
1650 # 'f' may be a directory pattern from 'match.files()',
1651 # so 'f not in ctx1' is not enough
1651 # so 'f not in ctx1' is not enough
1652 if f not in other and not other.hasdir(f):
1652 if f not in other and not other.hasdir(f):
1653 self._repo.ui.warn('%s: %s\n' %
1653 self._repo.ui.warn('%s: %s\n' %
1654 (self._repo.dirstate.pathto(f), msg))
1654 (self._repo.dirstate.pathto(f), msg))
1655 match.bad = bad
1655 match.bad = bad
1656 return match
1656 return match
1657
1657
1658 class committablefilectx(basefilectx):
1658 class committablefilectx(basefilectx):
1659 """A committablefilectx provides common functionality for a file context
1659 """A committablefilectx provides common functionality for a file context
1660 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1660 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1661 def __init__(self, repo, path, filelog=None, ctx=None):
1661 def __init__(self, repo, path, filelog=None, ctx=None):
1662 self._repo = repo
1662 self._repo = repo
1663 self._path = path
1663 self._path = path
1664 self._changeid = None
1664 self._changeid = None
1665 self._filerev = self._filenode = None
1665 self._filerev = self._filenode = None
1666
1666
1667 if filelog is not None:
1667 if filelog is not None:
1668 self._filelog = filelog
1668 self._filelog = filelog
1669 if ctx:
1669 if ctx:
1670 self._changectx = ctx
1670 self._changectx = ctx
1671
1671
1672 def __nonzero__(self):
1672 def __nonzero__(self):
1673 return True
1673 return True
1674
1674
1675 def linkrev(self):
1675 def linkrev(self):
1676 # linked to self._changectx no matter if file is modified or not
1676 # linked to self._changectx no matter if file is modified or not
1677 return self.rev()
1677 return self.rev()
1678
1678
1679 def parents(self):
1679 def parents(self):
1680 '''return parent filectxs, following copies if necessary'''
1680 '''return parent filectxs, following copies if necessary'''
1681 def filenode(ctx, path):
1681 def filenode(ctx, path):
1682 return ctx._manifest.get(path, nullid)
1682 return ctx._manifest.get(path, nullid)
1683
1683
1684 path = self._path
1684 path = self._path
1685 fl = self._filelog
1685 fl = self._filelog
1686 pcl = self._changectx._parents
1686 pcl = self._changectx._parents
1687 renamed = self.renamed()
1687 renamed = self.renamed()
1688
1688
1689 if renamed:
1689 if renamed:
1690 pl = [renamed + (None,)]
1690 pl = [renamed + (None,)]
1691 else:
1691 else:
1692 pl = [(path, filenode(pcl[0], path), fl)]
1692 pl = [(path, filenode(pcl[0], path), fl)]
1693
1693
1694 for pc in pcl[1:]:
1694 for pc in pcl[1:]:
1695 pl.append((path, filenode(pc, path), fl))
1695 pl.append((path, filenode(pc, path), fl))
1696
1696
1697 return [self._parentfilectx(p, fileid=n, filelog=l)
1697 return [self._parentfilectx(p, fileid=n, filelog=l)
1698 for p, n, l in pl if n != nullid]
1698 for p, n, l in pl if n != nullid]
1699
1699
1700 def children(self):
1700 def children(self):
1701 return []
1701 return []
1702
1702
1703 class workingfilectx(committablefilectx):
1703 class workingfilectx(committablefilectx):
1704 """A workingfilectx object makes access to data related to a particular
1704 """A workingfilectx object makes access to data related to a particular
1705 file in the working directory convenient."""
1705 file in the working directory convenient."""
1706 def __init__(self, repo, path, filelog=None, workingctx=None):
1706 def __init__(self, repo, path, filelog=None, workingctx=None):
1707 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1707 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1708
1708
1709 @propertycache
1709 @propertycache
1710 def _changectx(self):
1710 def _changectx(self):
1711 return workingctx(self._repo)
1711 return workingctx(self._repo)
1712
1712
1713 def data(self):
1713 def data(self):
1714 return self._repo.wread(self._path)
1714 return self._repo.wread(self._path)
1715 def renamed(self):
1715 def renamed(self):
1716 rp = self._repo.dirstate.copied(self._path)
1716 rp = self._repo.dirstate.copied(self._path)
1717 if not rp:
1717 if not rp:
1718 return None
1718 return None
1719 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1719 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1720
1720
1721 def size(self):
1721 def size(self):
1722 return self._repo.wvfs.lstat(self._path).st_size
1722 return self._repo.wvfs.lstat(self._path).st_size
1723 def date(self):
1723 def date(self):
1724 t, tz = self._changectx.date()
1724 t, tz = self._changectx.date()
1725 try:
1725 try:
1726 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1726 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1727 except OSError as err:
1727 except OSError as err:
1728 if err.errno != errno.ENOENT:
1728 if err.errno != errno.ENOENT:
1729 raise
1729 raise
1730 return (t, tz)
1730 return (t, tz)
1731
1731
1732 def cmp(self, fctx):
1732 def cmp(self, fctx):
1733 """compare with other file context
1733 """compare with other file context
1734
1734
1735 returns True if different than fctx.
1735 returns True if different than fctx.
1736 """
1736 """
1737 # fctx should be a filectx (not a workingfilectx)
1737 # fctx should be a filectx (not a workingfilectx)
1738 # invert comparison to reuse the same code path
1738 # invert comparison to reuse the same code path
1739 return fctx.cmp(self)
1739 return fctx.cmp(self)
1740
1740
1741 def remove(self, ignoremissing=False):
1741 def remove(self, ignoremissing=False):
1742 """wraps unlink for a repo's working directory"""
1742 """wraps unlink for a repo's working directory"""
1743 util.unlinkpath(self._repo.wjoin(self._path), ignoremissing)
1743 util.unlinkpath(self._repo.wjoin(self._path), ignoremissing)
1744
1744
1745 def write(self, data, flags):
1745 def write(self, data, flags):
1746 """wraps repo.wwrite"""
1746 """wraps repo.wwrite"""
1747 self._repo.wwrite(self._path, data, flags)
1747 self._repo.wwrite(self._path, data, flags)
1748
1748
1749 class workingcommitctx(workingctx):
1749 class workingcommitctx(workingctx):
1750 """A workingcommitctx object makes access to data related to
1750 """A workingcommitctx object makes access to data related to
1751 the revision being committed convenient.
1751 the revision being committed convenient.
1752
1752
1753 This hides changes in the working directory, if they aren't
1753 This hides changes in the working directory, if they aren't
1754 committed in this context.
1754 committed in this context.
1755 """
1755 """
1756 def __init__(self, repo, changes,
1756 def __init__(self, repo, changes,
1757 text="", user=None, date=None, extra=None):
1757 text="", user=None, date=None, extra=None):
1758 super(workingctx, self).__init__(repo, text, user, date, extra,
1758 super(workingctx, self).__init__(repo, text, user, date, extra,
1759 changes)
1759 changes)
1760
1760
1761 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1761 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1762 unknown=False):
1762 unknown=False):
1763 """Return matched files only in ``self._status``
1763 """Return matched files only in ``self._status``
1764
1764
1765 Uncommitted files appear "clean" via this context, even if
1765 Uncommitted files appear "clean" via this context, even if
1766 they aren't actually so in the working directory.
1766 they aren't actually so in the working directory.
1767 """
1767 """
1768 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1768 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1769 if clean:
1769 if clean:
1770 clean = [f for f in self._manifest if f not in self._changedset]
1770 clean = [f for f in self._manifest if f not in self._changedset]
1771 else:
1771 else:
1772 clean = []
1772 clean = []
1773 return scmutil.status([f for f in self._status.modified if match(f)],
1773 return scmutil.status([f for f in self._status.modified if match(f)],
1774 [f for f in self._status.added if match(f)],
1774 [f for f in self._status.added if match(f)],
1775 [f for f in self._status.removed if match(f)],
1775 [f for f in self._status.removed if match(f)],
1776 [], [], [], clean)
1776 [], [], [], clean)
1777
1777
1778 @propertycache
1778 @propertycache
1779 def _changedset(self):
1779 def _changedset(self):
1780 """Return the set of files changed in this context
1780 """Return the set of files changed in this context
1781 """
1781 """
1782 changed = set(self._status.modified)
1782 changed = set(self._status.modified)
1783 changed.update(self._status.added)
1783 changed.update(self._status.added)
1784 changed.update(self._status.removed)
1784 changed.update(self._status.removed)
1785 return changed
1785 return changed
1786
1786
1787 def makecachingfilectxfn(func):
1787 def makecachingfilectxfn(func):
1788 """Create a filectxfn that caches based on the path.
1788 """Create a filectxfn that caches based on the path.
1789
1789
1790 We can't use util.cachefunc because it uses all arguments as the cache
1790 We can't use util.cachefunc because it uses all arguments as the cache
1791 key and this creates a cycle since the arguments include the repo and
1791 key and this creates a cycle since the arguments include the repo and
1792 memctx.
1792 memctx.
1793 """
1793 """
1794 cache = {}
1794 cache = {}
1795
1795
1796 def getfilectx(repo, memctx, path):
1796 def getfilectx(repo, memctx, path):
1797 if path not in cache:
1797 if path not in cache:
1798 cache[path] = func(repo, memctx, path)
1798 cache[path] = func(repo, memctx, path)
1799 return cache[path]
1799 return cache[path]
1800
1800
1801 return getfilectx
1801 return getfilectx
1802
1802
1803 class memctx(committablectx):
1803 class memctx(committablectx):
1804 """Use memctx to perform in-memory commits via localrepo.commitctx().
1804 """Use memctx to perform in-memory commits via localrepo.commitctx().
1805
1805
1806 Revision information is supplied at initialization time while
1806 Revision information is supplied at initialization time while
1807 related files data and is made available through a callback
1807 related files data and is made available through a callback
1808 mechanism. 'repo' is the current localrepo, 'parents' is a
1808 mechanism. 'repo' is the current localrepo, 'parents' is a
1809 sequence of two parent revisions identifiers (pass None for every
1809 sequence of two parent revisions identifiers (pass None for every
1810 missing parent), 'text' is the commit message and 'files' lists
1810 missing parent), 'text' is the commit message and 'files' lists
1811 names of files touched by the revision (normalized and relative to
1811 names of files touched by the revision (normalized and relative to
1812 repository root).
1812 repository root).
1813
1813
1814 filectxfn(repo, memctx, path) is a callable receiving the
1814 filectxfn(repo, memctx, path) is a callable receiving the
1815 repository, the current memctx object and the normalized path of
1815 repository, the current memctx object and the normalized path of
1816 requested file, relative to repository root. It is fired by the
1816 requested file, relative to repository root. It is fired by the
1817 commit function for every file in 'files', but calls order is
1817 commit function for every file in 'files', but calls order is
1818 undefined. If the file is available in the revision being
1818 undefined. If the file is available in the revision being
1819 committed (updated or added), filectxfn returns a memfilectx
1819 committed (updated or added), filectxfn returns a memfilectx
1820 object. If the file was removed, filectxfn raises an
1820 object. If the file was removed, filectxfn raises an
1821 IOError. Moved files are represented by marking the source file
1821 IOError. Moved files are represented by marking the source file
1822 removed and the new file added with copy information (see
1822 removed and the new file added with copy information (see
1823 memfilectx).
1823 memfilectx).
1824
1824
1825 user receives the committer name and defaults to current
1825 user receives the committer name and defaults to current
1826 repository username, date is the commit date in any format
1826 repository username, date is the commit date in any format
1827 supported by util.parsedate() and defaults to current date, extra
1827 supported by util.parsedate() and defaults to current date, extra
1828 is a dictionary of metadata or is left empty.
1828 is a dictionary of metadata or is left empty.
1829 """
1829 """
1830
1830
1831 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
1831 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
1832 # Extensions that need to retain compatibility across Mercurial 3.1 can use
1832 # Extensions that need to retain compatibility across Mercurial 3.1 can use
1833 # this field to determine what to do in filectxfn.
1833 # this field to determine what to do in filectxfn.
1834 _returnnoneformissingfiles = True
1834 _returnnoneformissingfiles = True
1835
1835
1836 def __init__(self, repo, parents, text, files, filectxfn, user=None,
1836 def __init__(self, repo, parents, text, files, filectxfn, user=None,
1837 date=None, extra=None, editor=False):
1837 date=None, extra=None, editor=False):
1838 super(memctx, self).__init__(repo, text, user, date, extra)
1838 super(memctx, self).__init__(repo, text, user, date, extra)
1839 self._rev = None
1839 self._rev = None
1840 self._node = None
1840 self._node = None
1841 parents = [(p or nullid) for p in parents]
1841 parents = [(p or nullid) for p in parents]
1842 p1, p2 = parents
1842 p1, p2 = parents
1843 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
1843 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
1844 files = sorted(set(files))
1844 files = sorted(set(files))
1845 self._files = files
1845 self._files = files
1846 self.substate = {}
1846 self.substate = {}
1847
1847
1848 # if store is not callable, wrap it in a function
1848 # if store is not callable, wrap it in a function
1849 if not callable(filectxfn):
1849 if not callable(filectxfn):
1850 def getfilectx(repo, memctx, path):
1850 def getfilectx(repo, memctx, path):
1851 fctx = filectxfn[path]
1851 fctx = filectxfn[path]
1852 # this is weird but apparently we only keep track of one parent
1852 # this is weird but apparently we only keep track of one parent
1853 # (why not only store that instead of a tuple?)
1853 # (why not only store that instead of a tuple?)
1854 copied = fctx.renamed()
1854 copied = fctx.renamed()
1855 if copied:
1855 if copied:
1856 copied = copied[0]
1856 copied = copied[0]
1857 return memfilectx(repo, path, fctx.data(),
1857 return memfilectx(repo, path, fctx.data(),
1858 islink=fctx.islink(), isexec=fctx.isexec(),
1858 islink=fctx.islink(), isexec=fctx.isexec(),
1859 copied=copied, memctx=memctx)
1859 copied=copied, memctx=memctx)
1860 self._filectxfn = getfilectx
1860 self._filectxfn = getfilectx
1861 else:
1861 else:
1862 # memoizing increases performance for e.g. vcs convert scenarios.
1862 # memoizing increases performance for e.g. vcs convert scenarios.
1863 self._filectxfn = makecachingfilectxfn(filectxfn)
1863 self._filectxfn = makecachingfilectxfn(filectxfn)
1864
1864
1865 if extra:
1865 if extra:
1866 self._extra = extra.copy()
1866 self._extra = extra.copy()
1867 else:
1867 else:
1868 self._extra = {}
1868 self._extra = {}
1869
1869
1870 if self._extra.get('branch', '') == '':
1870 if self._extra.get('branch', '') == '':
1871 self._extra['branch'] = 'default'
1871 self._extra['branch'] = 'default'
1872
1872
1873 if editor:
1873 if editor:
1874 self._text = editor(self._repo, self, [])
1874 self._text = editor(self._repo, self, [])
1875 self._repo.savecommitmessage(self._text)
1875 self._repo.savecommitmessage(self._text)
1876
1876
1877 def filectx(self, path, filelog=None):
1877 def filectx(self, path, filelog=None):
1878 """get a file context from the working directory
1878 """get a file context from the working directory
1879
1879
1880 Returns None if file doesn't exist and should be removed."""
1880 Returns None if file doesn't exist and should be removed."""
1881 return self._filectxfn(self._repo, self, path)
1881 return self._filectxfn(self._repo, self, path)
1882
1882
1883 def commit(self):
1883 def commit(self):
1884 """commit context to the repo"""
1884 """commit context to the repo"""
1885 return self._repo.commitctx(self)
1885 return self._repo.commitctx(self)
1886
1886
1887 @propertycache
1887 @propertycache
1888 def _manifest(self):
1888 def _manifest(self):
1889 """generate a manifest based on the return values of filectxfn"""
1889 """generate a manifest based on the return values of filectxfn"""
1890
1890
1891 # keep this simple for now; just worry about p1
1891 # keep this simple for now; just worry about p1
1892 pctx = self._parents[0]
1892 pctx = self._parents[0]
1893 man = pctx.manifest().copy()
1893 man = pctx.manifest().copy()
1894
1894
1895 for f in self._status.modified:
1895 for f in self._status.modified:
1896 p1node = nullid
1896 p1node = nullid
1897 p2node = nullid
1897 p2node = nullid
1898 p = pctx[f].parents() # if file isn't in pctx, check p2?
1898 p = pctx[f].parents() # if file isn't in pctx, check p2?
1899 if len(p) > 0:
1899 if len(p) > 0:
1900 p1node = p[0].filenode()
1900 p1node = p[0].filenode()
1901 if len(p) > 1:
1901 if len(p) > 1:
1902 p2node = p[1].filenode()
1902 p2node = p[1].filenode()
1903 man[f] = revlog.hash(self[f].data(), p1node, p2node)
1903 man[f] = revlog.hash(self[f].data(), p1node, p2node)
1904
1904
1905 for f in self._status.added:
1905 for f in self._status.added:
1906 man[f] = revlog.hash(self[f].data(), nullid, nullid)
1906 man[f] = revlog.hash(self[f].data(), nullid, nullid)
1907
1907
1908 for f in self._status.removed:
1908 for f in self._status.removed:
1909 if f in man:
1909 if f in man:
1910 del man[f]
1910 del man[f]
1911
1911
1912 return man
1912 return man
1913
1913
1914 @propertycache
1914 @propertycache
1915 def _status(self):
1915 def _status(self):
1916 """Calculate exact status from ``files`` specified at construction
1916 """Calculate exact status from ``files`` specified at construction
1917 """
1917 """
1918 man1 = self.p1().manifest()
1918 man1 = self.p1().manifest()
1919 p2 = self._parents[1]
1919 p2 = self._parents[1]
1920 # "1 < len(self._parents)" can't be used for checking
1920 # "1 < len(self._parents)" can't be used for checking
1921 # existence of the 2nd parent, because "memctx._parents" is
1921 # existence of the 2nd parent, because "memctx._parents" is
1922 # explicitly initialized by the list, of which length is 2.
1922 # explicitly initialized by the list, of which length is 2.
1923 if p2.node() != nullid:
1923 if p2.node() != nullid:
1924 man2 = p2.manifest()
1924 man2 = p2.manifest()
1925 managing = lambda f: f in man1 or f in man2
1925 managing = lambda f: f in man1 or f in man2
1926 else:
1926 else:
1927 managing = lambda f: f in man1
1927 managing = lambda f: f in man1
1928
1928
1929 modified, added, removed = [], [], []
1929 modified, added, removed = [], [], []
1930 for f in self._files:
1930 for f in self._files:
1931 if not managing(f):
1931 if not managing(f):
1932 added.append(f)
1932 added.append(f)
1933 elif self[f]:
1933 elif self[f]:
1934 modified.append(f)
1934 modified.append(f)
1935 else:
1935 else:
1936 removed.append(f)
1936 removed.append(f)
1937
1937
1938 return scmutil.status(modified, added, removed, [], [], [], [])
1938 return scmutil.status(modified, added, removed, [], [], [], [])
1939
1939
1940 class memfilectx(committablefilectx):
1940 class memfilectx(committablefilectx):
1941 """memfilectx represents an in-memory file to commit.
1941 """memfilectx represents an in-memory file to commit.
1942
1942
1943 See memctx and committablefilectx for more details.
1943 See memctx and committablefilectx for more details.
1944 """
1944 """
1945 def __init__(self, repo, path, data, islink=False,
1945 def __init__(self, repo, path, data, islink=False,
1946 isexec=False, copied=None, memctx=None):
1946 isexec=False, copied=None, memctx=None):
1947 """
1947 """
1948 path is the normalized file path relative to repository root.
1948 path is the normalized file path relative to repository root.
1949 data is the file content as a string.
1949 data is the file content as a string.
1950 islink is True if the file is a symbolic link.
1950 islink is True if the file is a symbolic link.
1951 isexec is True if the file is executable.
1951 isexec is True if the file is executable.
1952 copied is the source file path if current file was copied in the
1952 copied is the source file path if current file was copied in the
1953 revision being committed, or None."""
1953 revision being committed, or None."""
1954 super(memfilectx, self).__init__(repo, path, None, memctx)
1954 super(memfilectx, self).__init__(repo, path, None, memctx)
1955 self._data = data
1955 self._data = data
1956 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1956 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1957 self._copied = None
1957 self._copied = None
1958 if copied:
1958 if copied:
1959 self._copied = (copied, nullid)
1959 self._copied = (copied, nullid)
1960
1960
1961 def data(self):
1961 def data(self):
1962 return self._data
1962 return self._data
1963 def size(self):
1963 def size(self):
1964 return len(self.data())
1964 return len(self.data())
1965 def flags(self):
1965 def flags(self):
1966 return self._flags
1966 return self._flags
1967 def renamed(self):
1967 def renamed(self):
1968 return self._copied
1968 return self._copied
1969
1969
1970 def remove(self, ignoremissing=False):
1970 def remove(self, ignoremissing=False):
1971 """wraps unlink for a repo's working directory"""
1971 """wraps unlink for a repo's working directory"""
1972 # need to figure out what to do here
1972 # need to figure out what to do here
1973 del self._changectx[self._path]
1973 del self._changectx[self._path]
1974
1974
1975 def write(self, data, flags):
1975 def write(self, data, flags):
1976 """wraps repo.wwrite"""
1976 """wraps repo.wwrite"""
1977 self._data = data
1977 self._data = data
@@ -1,1232 +1,1245 b''
1 # manifest.py - manifest revision class for mercurial
1 # manifest.py - manifest revision class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import array
10 import array
11 import heapq
11 import heapq
12 import os
12 import os
13 import struct
13 import struct
14
14
15 from .i18n import _
15 from .i18n import _
16 from . import (
16 from . import (
17 error,
17 error,
18 mdiff,
18 mdiff,
19 parsers,
19 parsers,
20 revlog,
20 revlog,
21 util,
21 util,
22 )
22 )
23
23
24 propertycache = util.propertycache
24 propertycache = util.propertycache
25
25
26 def _parsev1(data):
26 def _parsev1(data):
27 # This method does a little bit of excessive-looking
27 # This method does a little bit of excessive-looking
28 # precondition checking. This is so that the behavior of this
28 # precondition checking. This is so that the behavior of this
29 # class exactly matches its C counterpart to try and help
29 # class exactly matches its C counterpart to try and help
30 # prevent surprise breakage for anyone that develops against
30 # prevent surprise breakage for anyone that develops against
31 # the pure version.
31 # the pure version.
32 if data and data[-1] != '\n':
32 if data and data[-1] != '\n':
33 raise ValueError('Manifest did not end in a newline.')
33 raise ValueError('Manifest did not end in a newline.')
34 prev = None
34 prev = None
35 for l in data.splitlines():
35 for l in data.splitlines():
36 if prev is not None and prev > l:
36 if prev is not None and prev > l:
37 raise ValueError('Manifest lines not in sorted order.')
37 raise ValueError('Manifest lines not in sorted order.')
38 prev = l
38 prev = l
39 f, n = l.split('\0')
39 f, n = l.split('\0')
40 if len(n) > 40:
40 if len(n) > 40:
41 yield f, revlog.bin(n[:40]), n[40:]
41 yield f, revlog.bin(n[:40]), n[40:]
42 else:
42 else:
43 yield f, revlog.bin(n), ''
43 yield f, revlog.bin(n), ''
44
44
45 def _parsev2(data):
45 def _parsev2(data):
46 metadataend = data.find('\n')
46 metadataend = data.find('\n')
47 # Just ignore metadata for now
47 # Just ignore metadata for now
48 pos = metadataend + 1
48 pos = metadataend + 1
49 prevf = ''
49 prevf = ''
50 while pos < len(data):
50 while pos < len(data):
51 end = data.find('\n', pos + 1) # +1 to skip stem length byte
51 end = data.find('\n', pos + 1) # +1 to skip stem length byte
52 if end == -1:
52 if end == -1:
53 raise ValueError('Manifest ended with incomplete file entry.')
53 raise ValueError('Manifest ended with incomplete file entry.')
54 stemlen = ord(data[pos])
54 stemlen = ord(data[pos])
55 items = data[pos + 1:end].split('\0')
55 items = data[pos + 1:end].split('\0')
56 f = prevf[:stemlen] + items[0]
56 f = prevf[:stemlen] + items[0]
57 if prevf > f:
57 if prevf > f:
58 raise ValueError('Manifest entries not in sorted order.')
58 raise ValueError('Manifest entries not in sorted order.')
59 fl = items[1]
59 fl = items[1]
60 # Just ignore metadata (items[2:] for now)
60 # Just ignore metadata (items[2:] for now)
61 n = data[end + 1:end + 21]
61 n = data[end + 1:end + 21]
62 yield f, n, fl
62 yield f, n, fl
63 pos = end + 22
63 pos = end + 22
64 prevf = f
64 prevf = f
65
65
66 def _parse(data):
66 def _parse(data):
67 """Generates (path, node, flags) tuples from a manifest text"""
67 """Generates (path, node, flags) tuples from a manifest text"""
68 if data.startswith('\0'):
68 if data.startswith('\0'):
69 return iter(_parsev2(data))
69 return iter(_parsev2(data))
70 else:
70 else:
71 return iter(_parsev1(data))
71 return iter(_parsev1(data))
72
72
73 def _text(it, usemanifestv2):
73 def _text(it, usemanifestv2):
74 """Given an iterator over (path, node, flags) tuples, returns a manifest
74 """Given an iterator over (path, node, flags) tuples, returns a manifest
75 text"""
75 text"""
76 if usemanifestv2:
76 if usemanifestv2:
77 return _textv2(it)
77 return _textv2(it)
78 else:
78 else:
79 return _textv1(it)
79 return _textv1(it)
80
80
81 def _textv1(it):
81 def _textv1(it):
82 files = []
82 files = []
83 lines = []
83 lines = []
84 _hex = revlog.hex
84 _hex = revlog.hex
85 for f, n, fl in it:
85 for f, n, fl in it:
86 files.append(f)
86 files.append(f)
87 # if this is changed to support newlines in filenames,
87 # if this is changed to support newlines in filenames,
88 # be sure to check the templates/ dir again (especially *-raw.tmpl)
88 # be sure to check the templates/ dir again (especially *-raw.tmpl)
89 lines.append("%s\0%s%s\n" % (f, _hex(n), fl))
89 lines.append("%s\0%s%s\n" % (f, _hex(n), fl))
90
90
91 _checkforbidden(files)
91 _checkforbidden(files)
92 return ''.join(lines)
92 return ''.join(lines)
93
93
94 def _textv2(it):
94 def _textv2(it):
95 files = []
95 files = []
96 lines = ['\0\n']
96 lines = ['\0\n']
97 prevf = ''
97 prevf = ''
98 for f, n, fl in it:
98 for f, n, fl in it:
99 files.append(f)
99 files.append(f)
100 stem = os.path.commonprefix([prevf, f])
100 stem = os.path.commonprefix([prevf, f])
101 stemlen = min(len(stem), 255)
101 stemlen = min(len(stem), 255)
102 lines.append("%c%s\0%s\n%s\n" % (stemlen, f[stemlen:], fl, n))
102 lines.append("%c%s\0%s\n%s\n" % (stemlen, f[stemlen:], fl, n))
103 prevf = f
103 prevf = f
104 _checkforbidden(files)
104 _checkforbidden(files)
105 return ''.join(lines)
105 return ''.join(lines)
106
106
107 class _lazymanifest(dict):
107 class _lazymanifest(dict):
108 """This is the pure implementation of lazymanifest.
108 """This is the pure implementation of lazymanifest.
109
109
110 It has not been optimized *at all* and is not lazy.
110 It has not been optimized *at all* and is not lazy.
111 """
111 """
112
112
113 def __init__(self, data):
113 def __init__(self, data):
114 dict.__init__(self)
114 dict.__init__(self)
115 for f, n, fl in _parse(data):
115 for f, n, fl in _parse(data):
116 self[f] = n, fl
116 self[f] = n, fl
117
117
118 def __setitem__(self, k, v):
118 def __setitem__(self, k, v):
119 node, flag = v
119 node, flag = v
120 assert node is not None
120 assert node is not None
121 if len(node) > 21:
121 if len(node) > 21:
122 node = node[:21] # match c implementation behavior
122 node = node[:21] # match c implementation behavior
123 dict.__setitem__(self, k, (node, flag))
123 dict.__setitem__(self, k, (node, flag))
124
124
125 def __iter__(self):
125 def __iter__(self):
126 return iter(sorted(dict.keys(self)))
126 return iter(sorted(dict.keys(self)))
127
127
128 def iterkeys(self):
128 def iterkeys(self):
129 return iter(sorted(dict.keys(self)))
129 return iter(sorted(dict.keys(self)))
130
130
131 def iterentries(self):
131 def iterentries(self):
132 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
132 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
133
133
134 def copy(self):
134 def copy(self):
135 c = _lazymanifest('')
135 c = _lazymanifest('')
136 c.update(self)
136 c.update(self)
137 return c
137 return c
138
138
139 def diff(self, m2, clean=False):
139 def diff(self, m2, clean=False):
140 '''Finds changes between the current manifest and m2.'''
140 '''Finds changes between the current manifest and m2.'''
141 diff = {}
141 diff = {}
142
142
143 for fn, e1 in self.iteritems():
143 for fn, e1 in self.iteritems():
144 if fn not in m2:
144 if fn not in m2:
145 diff[fn] = e1, (None, '')
145 diff[fn] = e1, (None, '')
146 else:
146 else:
147 e2 = m2[fn]
147 e2 = m2[fn]
148 if e1 != e2:
148 if e1 != e2:
149 diff[fn] = e1, e2
149 diff[fn] = e1, e2
150 elif clean:
150 elif clean:
151 diff[fn] = None
151 diff[fn] = None
152
152
153 for fn, e2 in m2.iteritems():
153 for fn, e2 in m2.iteritems():
154 if fn not in self:
154 if fn not in self:
155 diff[fn] = (None, ''), e2
155 diff[fn] = (None, ''), e2
156
156
157 return diff
157 return diff
158
158
159 def filtercopy(self, filterfn):
159 def filtercopy(self, filterfn):
160 c = _lazymanifest('')
160 c = _lazymanifest('')
161 for f, n, fl in self.iterentries():
161 for f, n, fl in self.iterentries():
162 if filterfn(f):
162 if filterfn(f):
163 c[f] = n, fl
163 c[f] = n, fl
164 return c
164 return c
165
165
166 def text(self):
166 def text(self):
167 """Get the full data of this manifest as a bytestring."""
167 """Get the full data of this manifest as a bytestring."""
168 return _textv1(self.iterentries())
168 return _textv1(self.iterentries())
169
169
170 try:
170 try:
171 _lazymanifest = parsers.lazymanifest
171 _lazymanifest = parsers.lazymanifest
172 except AttributeError:
172 except AttributeError:
173 pass
173 pass
174
174
175 class manifestdict(object):
175 class manifestdict(object):
176 def __init__(self, data=''):
176 def __init__(self, data=''):
177 if data.startswith('\0'):
177 if data.startswith('\0'):
178 #_lazymanifest can not parse v2
178 #_lazymanifest can not parse v2
179 self._lm = _lazymanifest('')
179 self._lm = _lazymanifest('')
180 for f, n, fl in _parsev2(data):
180 for f, n, fl in _parsev2(data):
181 self._lm[f] = n, fl
181 self._lm[f] = n, fl
182 else:
182 else:
183 self._lm = _lazymanifest(data)
183 self._lm = _lazymanifest(data)
184
184
185 def __getitem__(self, key):
185 def __getitem__(self, key):
186 return self._lm[key][0]
186 return self._lm[key][0]
187
187
188 def find(self, key):
188 def find(self, key):
189 return self._lm[key]
189 return self._lm[key]
190
190
191 def __len__(self):
191 def __len__(self):
192 return len(self._lm)
192 return len(self._lm)
193
193
194 def __setitem__(self, key, node):
194 def __setitem__(self, key, node):
195 self._lm[key] = node, self.flags(key, '')
195 self._lm[key] = node, self.flags(key, '')
196
196
197 def __contains__(self, key):
197 def __contains__(self, key):
198 return key in self._lm
198 return key in self._lm
199
199
200 def __delitem__(self, key):
200 def __delitem__(self, key):
201 del self._lm[key]
201 del self._lm[key]
202
202
203 def __iter__(self):
203 def __iter__(self):
204 return self._lm.__iter__()
204 return self._lm.__iter__()
205
205
206 def iterkeys(self):
206 def iterkeys(self):
207 return self._lm.iterkeys()
207 return self._lm.iterkeys()
208
208
209 def keys(self):
209 def keys(self):
210 return list(self.iterkeys())
210 return list(self.iterkeys())
211
211
212 def filesnotin(self, m2):
212 def filesnotin(self, m2):
213 '''Set of files in this manifest that are not in the other'''
213 '''Set of files in this manifest that are not in the other'''
214 diff = self.diff(m2)
214 diff = self.diff(m2)
215 files = set(filepath
215 files = set(filepath
216 for filepath, hashflags in diff.iteritems()
216 for filepath, hashflags in diff.iteritems()
217 if hashflags[1][0] is None)
217 if hashflags[1][0] is None)
218 return files
218 return files
219
219
220 @propertycache
220 @propertycache
221 def _dirs(self):
221 def _dirs(self):
222 return util.dirs(self)
222 return util.dirs(self)
223
223
224 def dirs(self):
224 def dirs(self):
225 return self._dirs
225 return self._dirs
226
226
227 def hasdir(self, dir):
227 def hasdir(self, dir):
228 return dir in self._dirs
228 return dir in self._dirs
229
229
230 def _filesfastpath(self, match):
230 def _filesfastpath(self, match):
231 '''Checks whether we can correctly and quickly iterate over matcher
231 '''Checks whether we can correctly and quickly iterate over matcher
232 files instead of over manifest files.'''
232 files instead of over manifest files.'''
233 files = match.files()
233 files = match.files()
234 return (len(files) < 100 and (match.isexact() or
234 return (len(files) < 100 and (match.isexact() or
235 (match.prefix() and all(fn in self for fn in files))))
235 (match.prefix() and all(fn in self for fn in files))))
236
236
237 def walk(self, match):
237 def walk(self, match):
238 '''Generates matching file names.
238 '''Generates matching file names.
239
239
240 Equivalent to manifest.matches(match).iterkeys(), but without creating
240 Equivalent to manifest.matches(match).iterkeys(), but without creating
241 an entirely new manifest.
241 an entirely new manifest.
242
242
243 It also reports nonexistent files by marking them bad with match.bad().
243 It also reports nonexistent files by marking them bad with match.bad().
244 '''
244 '''
245 if match.always():
245 if match.always():
246 for f in iter(self):
246 for f in iter(self):
247 yield f
247 yield f
248 return
248 return
249
249
250 fset = set(match.files())
250 fset = set(match.files())
251
251
252 # avoid the entire walk if we're only looking for specific files
252 # avoid the entire walk if we're only looking for specific files
253 if self._filesfastpath(match):
253 if self._filesfastpath(match):
254 for fn in sorted(fset):
254 for fn in sorted(fset):
255 yield fn
255 yield fn
256 return
256 return
257
257
258 for fn in self:
258 for fn in self:
259 if fn in fset:
259 if fn in fset:
260 # specified pattern is the exact name
260 # specified pattern is the exact name
261 fset.remove(fn)
261 fset.remove(fn)
262 if match(fn):
262 if match(fn):
263 yield fn
263 yield fn
264
264
265 # for dirstate.walk, files=['.'] means "walk the whole tree".
265 # for dirstate.walk, files=['.'] means "walk the whole tree".
266 # follow that here, too
266 # follow that here, too
267 fset.discard('.')
267 fset.discard('.')
268
268
269 for fn in sorted(fset):
269 for fn in sorted(fset):
270 if not self.hasdir(fn):
270 if not self.hasdir(fn):
271 match.bad(fn, None)
271 match.bad(fn, None)
272
272
273 def matches(self, match):
273 def matches(self, match):
274 '''generate a new manifest filtered by the match argument'''
274 '''generate a new manifest filtered by the match argument'''
275 if match.always():
275 if match.always():
276 return self.copy()
276 return self.copy()
277
277
278 if self._filesfastpath(match):
278 if self._filesfastpath(match):
279 m = manifestdict()
279 m = manifestdict()
280 lm = self._lm
280 lm = self._lm
281 for fn in match.files():
281 for fn in match.files():
282 if fn in lm:
282 if fn in lm:
283 m._lm[fn] = lm[fn]
283 m._lm[fn] = lm[fn]
284 return m
284 return m
285
285
286 m = manifestdict()
286 m = manifestdict()
287 m._lm = self._lm.filtercopy(match)
287 m._lm = self._lm.filtercopy(match)
288 return m
288 return m
289
289
290 def diff(self, m2, clean=False):
290 def diff(self, m2, clean=False):
291 '''Finds changes between the current manifest and m2.
291 '''Finds changes between the current manifest and m2.
292
292
293 Args:
293 Args:
294 m2: the manifest to which this manifest should be compared.
294 m2: the manifest to which this manifest should be compared.
295 clean: if true, include files unchanged between these manifests
295 clean: if true, include files unchanged between these manifests
296 with a None value in the returned dictionary.
296 with a None value in the returned dictionary.
297
297
298 The result is returned as a dict with filename as key and
298 The result is returned as a dict with filename as key and
299 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
299 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
300 nodeid in the current/other manifest and fl1/fl2 is the flag
300 nodeid in the current/other manifest and fl1/fl2 is the flag
301 in the current/other manifest. Where the file does not exist,
301 in the current/other manifest. Where the file does not exist,
302 the nodeid will be None and the flags will be the empty
302 the nodeid will be None and the flags will be the empty
303 string.
303 string.
304 '''
304 '''
305 return self._lm.diff(m2._lm, clean)
305 return self._lm.diff(m2._lm, clean)
306
306
307 def setflag(self, key, flag):
307 def setflag(self, key, flag):
308 self._lm[key] = self[key], flag
308 self._lm[key] = self[key], flag
309
309
310 def get(self, key, default=None):
310 def get(self, key, default=None):
311 try:
311 try:
312 return self._lm[key][0]
312 return self._lm[key][0]
313 except KeyError:
313 except KeyError:
314 return default
314 return default
315
315
316 def flags(self, key, default=''):
316 def flags(self, key, default=''):
317 try:
317 try:
318 return self._lm[key][1]
318 return self._lm[key][1]
319 except KeyError:
319 except KeyError:
320 return default
320 return default
321
321
322 def copy(self):
322 def copy(self):
323 c = manifestdict()
323 c = manifestdict()
324 c._lm = self._lm.copy()
324 c._lm = self._lm.copy()
325 return c
325 return c
326
326
327 def iteritems(self):
327 def iteritems(self):
328 return (x[:2] for x in self._lm.iterentries())
328 return (x[:2] for x in self._lm.iterentries())
329
329
330 def iterentries(self):
330 def iterentries(self):
331 return self._lm.iterentries()
331 return self._lm.iterentries()
332
332
333 def text(self, usemanifestv2=False):
333 def text(self, usemanifestv2=False):
334 if usemanifestv2:
334 if usemanifestv2:
335 return _textv2(self._lm.iterentries())
335 return _textv2(self._lm.iterentries())
336 else:
336 else:
337 # use (probably) native version for v1
337 # use (probably) native version for v1
338 return self._lm.text()
338 return self._lm.text()
339
339
340 def fastdelta(self, base, changes):
340 def fastdelta(self, base, changes):
341 """Given a base manifest text as an array.array and a list of changes
341 """Given a base manifest text as an array.array and a list of changes
342 relative to that text, compute a delta that can be used by revlog.
342 relative to that text, compute a delta that can be used by revlog.
343 """
343 """
344 delta = []
344 delta = []
345 dstart = None
345 dstart = None
346 dend = None
346 dend = None
347 dline = [""]
347 dline = [""]
348 start = 0
348 start = 0
349 # zero copy representation of base as a buffer
349 # zero copy representation of base as a buffer
350 addbuf = util.buffer(base)
350 addbuf = util.buffer(base)
351
351
352 changes = list(changes)
352 changes = list(changes)
353 if len(changes) < 1000:
353 if len(changes) < 1000:
354 # start with a readonly loop that finds the offset of
354 # start with a readonly loop that finds the offset of
355 # each line and creates the deltas
355 # each line and creates the deltas
356 for f, todelete in changes:
356 for f, todelete in changes:
357 # bs will either be the index of the item or the insert point
357 # bs will either be the index of the item or the insert point
358 start, end = _msearch(addbuf, f, start)
358 start, end = _msearch(addbuf, f, start)
359 if not todelete:
359 if not todelete:
360 h, fl = self._lm[f]
360 h, fl = self._lm[f]
361 l = "%s\0%s%s\n" % (f, revlog.hex(h), fl)
361 l = "%s\0%s%s\n" % (f, revlog.hex(h), fl)
362 else:
362 else:
363 if start == end:
363 if start == end:
364 # item we want to delete was not found, error out
364 # item we want to delete was not found, error out
365 raise AssertionError(
365 raise AssertionError(
366 _("failed to remove %s from manifest") % f)
366 _("failed to remove %s from manifest") % f)
367 l = ""
367 l = ""
368 if dstart is not None and dstart <= start and dend >= start:
368 if dstart is not None and dstart <= start and dend >= start:
369 if dend < end:
369 if dend < end:
370 dend = end
370 dend = end
371 if l:
371 if l:
372 dline.append(l)
372 dline.append(l)
373 else:
373 else:
374 if dstart is not None:
374 if dstart is not None:
375 delta.append([dstart, dend, "".join(dline)])
375 delta.append([dstart, dend, "".join(dline)])
376 dstart = start
376 dstart = start
377 dend = end
377 dend = end
378 dline = [l]
378 dline = [l]
379
379
380 if dstart is not None:
380 if dstart is not None:
381 delta.append([dstart, dend, "".join(dline)])
381 delta.append([dstart, dend, "".join(dline)])
382 # apply the delta to the base, and get a delta for addrevision
382 # apply the delta to the base, and get a delta for addrevision
383 deltatext, arraytext = _addlistdelta(base, delta)
383 deltatext, arraytext = _addlistdelta(base, delta)
384 else:
384 else:
385 # For large changes, it's much cheaper to just build the text and
385 # For large changes, it's much cheaper to just build the text and
386 # diff it.
386 # diff it.
387 arraytext = array.array('c', self.text())
387 arraytext = array.array('c', self.text())
388 deltatext = mdiff.textdiff(base, arraytext)
388 deltatext = mdiff.textdiff(base, arraytext)
389
389
390 return arraytext, deltatext
390 return arraytext, deltatext
391
391
392 def _msearch(m, s, lo=0, hi=None):
392 def _msearch(m, s, lo=0, hi=None):
393 '''return a tuple (start, end) that says where to find s within m.
393 '''return a tuple (start, end) that says where to find s within m.
394
394
395 If the string is found m[start:end] are the line containing
395 If the string is found m[start:end] are the line containing
396 that string. If start == end the string was not found and
396 that string. If start == end the string was not found and
397 they indicate the proper sorted insertion point.
397 they indicate the proper sorted insertion point.
398
398
399 m should be a buffer or a string
399 m should be a buffer or a string
400 s is a string'''
400 s is a string'''
401 def advance(i, c):
401 def advance(i, c):
402 while i < lenm and m[i] != c:
402 while i < lenm and m[i] != c:
403 i += 1
403 i += 1
404 return i
404 return i
405 if not s:
405 if not s:
406 return (lo, lo)
406 return (lo, lo)
407 lenm = len(m)
407 lenm = len(m)
408 if not hi:
408 if not hi:
409 hi = lenm
409 hi = lenm
410 while lo < hi:
410 while lo < hi:
411 mid = (lo + hi) // 2
411 mid = (lo + hi) // 2
412 start = mid
412 start = mid
413 while start > 0 and m[start - 1] != '\n':
413 while start > 0 and m[start - 1] != '\n':
414 start -= 1
414 start -= 1
415 end = advance(start, '\0')
415 end = advance(start, '\0')
416 if m[start:end] < s:
416 if m[start:end] < s:
417 # we know that after the null there are 40 bytes of sha1
417 # we know that after the null there are 40 bytes of sha1
418 # this translates to the bisect lo = mid + 1
418 # this translates to the bisect lo = mid + 1
419 lo = advance(end + 40, '\n') + 1
419 lo = advance(end + 40, '\n') + 1
420 else:
420 else:
421 # this translates to the bisect hi = mid
421 # this translates to the bisect hi = mid
422 hi = start
422 hi = start
423 end = advance(lo, '\0')
423 end = advance(lo, '\0')
424 found = m[lo:end]
424 found = m[lo:end]
425 if s == found:
425 if s == found:
426 # we know that after the null there are 40 bytes of sha1
426 # we know that after the null there are 40 bytes of sha1
427 end = advance(end + 40, '\n')
427 end = advance(end + 40, '\n')
428 return (lo, end + 1)
428 return (lo, end + 1)
429 else:
429 else:
430 return (lo, lo)
430 return (lo, lo)
431
431
432 def _checkforbidden(l):
432 def _checkforbidden(l):
433 """Check filenames for illegal characters."""
433 """Check filenames for illegal characters."""
434 for f in l:
434 for f in l:
435 if '\n' in f or '\r' in f:
435 if '\n' in f or '\r' in f:
436 raise error.RevlogError(
436 raise error.RevlogError(
437 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
437 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
438
438
439
439
440 # apply the changes collected during the bisect loop to our addlist
440 # apply the changes collected during the bisect loop to our addlist
441 # return a delta suitable for addrevision
441 # return a delta suitable for addrevision
442 def _addlistdelta(addlist, x):
442 def _addlistdelta(addlist, x):
443 # for large addlist arrays, building a new array is cheaper
443 # for large addlist arrays, building a new array is cheaper
444 # than repeatedly modifying the existing one
444 # than repeatedly modifying the existing one
445 currentposition = 0
445 currentposition = 0
446 newaddlist = array.array('c')
446 newaddlist = array.array('c')
447
447
448 for start, end, content in x:
448 for start, end, content in x:
449 newaddlist += addlist[currentposition:start]
449 newaddlist += addlist[currentposition:start]
450 if content:
450 if content:
451 newaddlist += array.array('c', content)
451 newaddlist += array.array('c', content)
452
452
453 currentposition = end
453 currentposition = end
454
454
455 newaddlist += addlist[currentposition:]
455 newaddlist += addlist[currentposition:]
456
456
457 deltatext = "".join(struct.pack(">lll", start, end, len(content))
457 deltatext = "".join(struct.pack(">lll", start, end, len(content))
458 + content for start, end, content in x)
458 + content for start, end, content in x)
459 return deltatext, newaddlist
459 return deltatext, newaddlist
460
460
461 def _splittopdir(f):
461 def _splittopdir(f):
462 if '/' in f:
462 if '/' in f:
463 dir, subpath = f.split('/', 1)
463 dir, subpath = f.split('/', 1)
464 return dir + '/', subpath
464 return dir + '/', subpath
465 else:
465 else:
466 return '', f
466 return '', f
467
467
468 _noop = lambda s: None
468 _noop = lambda s: None
469
469
470 class treemanifest(object):
470 class treemanifest(object):
471 def __init__(self, dir='', text=''):
471 def __init__(self, dir='', text=''):
472 self._dir = dir
472 self._dir = dir
473 self._node = revlog.nullid
473 self._node = revlog.nullid
474 self._loadfunc = _noop
474 self._loadfunc = _noop
475 self._copyfunc = _noop
475 self._copyfunc = _noop
476 self._dirty = False
476 self._dirty = False
477 self._dirs = {}
477 self._dirs = {}
478 # Using _lazymanifest here is a little slower than plain old dicts
478 # Using _lazymanifest here is a little slower than plain old dicts
479 self._files = {}
479 self._files = {}
480 self._flags = {}
480 self._flags = {}
481 if text:
481 if text:
482 def readsubtree(subdir, subm):
482 def readsubtree(subdir, subm):
483 raise AssertionError('treemanifest constructor only accepts '
483 raise AssertionError('treemanifest constructor only accepts '
484 'flat manifests')
484 'flat manifests')
485 self.parse(text, readsubtree)
485 self.parse(text, readsubtree)
486 self._dirty = True # Mark flat manifest dirty after parsing
486 self._dirty = True # Mark flat manifest dirty after parsing
487
487
488 def _subpath(self, path):
488 def _subpath(self, path):
489 return self._dir + path
489 return self._dir + path
490
490
491 def __len__(self):
491 def __len__(self):
492 self._load()
492 self._load()
493 size = len(self._files)
493 size = len(self._files)
494 for m in self._dirs.values():
494 for m in self._dirs.values():
495 size += m.__len__()
495 size += m.__len__()
496 return size
496 return size
497
497
498 def _isempty(self):
498 def _isempty(self):
499 self._load() # for consistency; already loaded by all callers
499 self._load() # for consistency; already loaded by all callers
500 return (not self._files and (not self._dirs or
500 return (not self._files and (not self._dirs or
501 all(m._isempty() for m in self._dirs.values())))
501 all(m._isempty() for m in self._dirs.values())))
502
502
503 def __repr__(self):
503 def __repr__(self):
504 return ('<treemanifest dir=%s, node=%s, loaded=%s, dirty=%s at 0x%x>' %
504 return ('<treemanifest dir=%s, node=%s, loaded=%s, dirty=%s at 0x%x>' %
505 (self._dir, revlog.hex(self._node),
505 (self._dir, revlog.hex(self._node),
506 bool(self._loadfunc is _noop),
506 bool(self._loadfunc is _noop),
507 self._dirty, id(self)))
507 self._dirty, id(self)))
508
508
509 def dir(self):
509 def dir(self):
510 '''The directory that this tree manifest represents, including a
510 '''The directory that this tree manifest represents, including a
511 trailing '/'. Empty string for the repo root directory.'''
511 trailing '/'. Empty string for the repo root directory.'''
512 return self._dir
512 return self._dir
513
513
514 def node(self):
514 def node(self):
515 '''This node of this instance. nullid for unsaved instances. Should
515 '''This node of this instance. nullid for unsaved instances. Should
516 be updated when the instance is read or written from a revlog.
516 be updated when the instance is read or written from a revlog.
517 '''
517 '''
518 assert not self._dirty
518 assert not self._dirty
519 return self._node
519 return self._node
520
520
521 def setnode(self, node):
521 def setnode(self, node):
522 self._node = node
522 self._node = node
523 self._dirty = False
523 self._dirty = False
524
524
525 def iterentries(self):
525 def iterentries(self):
526 self._load()
526 self._load()
527 for p, n in sorted(self._dirs.items() + self._files.items()):
527 for p, n in sorted(self._dirs.items() + self._files.items()):
528 if p in self._files:
528 if p in self._files:
529 yield self._subpath(p), n, self._flags.get(p, '')
529 yield self._subpath(p), n, self._flags.get(p, '')
530 else:
530 else:
531 for x in n.iterentries():
531 for x in n.iterentries():
532 yield x
532 yield x
533
533
534 def iteritems(self):
534 def iteritems(self):
535 self._load()
535 self._load()
536 for p, n in sorted(self._dirs.items() + self._files.items()):
536 for p, n in sorted(self._dirs.items() + self._files.items()):
537 if p in self._files:
537 if p in self._files:
538 yield self._subpath(p), n
538 yield self._subpath(p), n
539 else:
539 else:
540 for f, sn in n.iteritems():
540 for f, sn in n.iteritems():
541 yield f, sn
541 yield f, sn
542
542
543 def iterkeys(self):
543 def iterkeys(self):
544 self._load()
544 self._load()
545 for p in sorted(self._dirs.keys() + self._files.keys()):
545 for p in sorted(self._dirs.keys() + self._files.keys()):
546 if p in self._files:
546 if p in self._files:
547 yield self._subpath(p)
547 yield self._subpath(p)
548 else:
548 else:
549 for f in self._dirs[p].iterkeys():
549 for f in self._dirs[p].iterkeys():
550 yield f
550 yield f
551
551
552 def keys(self):
552 def keys(self):
553 return list(self.iterkeys())
553 return list(self.iterkeys())
554
554
555 def __iter__(self):
555 def __iter__(self):
556 return self.iterkeys()
556 return self.iterkeys()
557
557
558 def __contains__(self, f):
558 def __contains__(self, f):
559 if f is None:
559 if f is None:
560 return False
560 return False
561 self._load()
561 self._load()
562 dir, subpath = _splittopdir(f)
562 dir, subpath = _splittopdir(f)
563 if dir:
563 if dir:
564 if dir not in self._dirs:
564 if dir not in self._dirs:
565 return False
565 return False
566 return self._dirs[dir].__contains__(subpath)
566 return self._dirs[dir].__contains__(subpath)
567 else:
567 else:
568 return f in self._files
568 return f in self._files
569
569
570 def get(self, f, default=None):
570 def get(self, f, default=None):
571 self._load()
571 self._load()
572 dir, subpath = _splittopdir(f)
572 dir, subpath = _splittopdir(f)
573 if dir:
573 if dir:
574 if dir not in self._dirs:
574 if dir not in self._dirs:
575 return default
575 return default
576 return self._dirs[dir].get(subpath, default)
576 return self._dirs[dir].get(subpath, default)
577 else:
577 else:
578 return self._files.get(f, default)
578 return self._files.get(f, default)
579
579
580 def __getitem__(self, f):
580 def __getitem__(self, f):
581 self._load()
581 self._load()
582 dir, subpath = _splittopdir(f)
582 dir, subpath = _splittopdir(f)
583 if dir:
583 if dir:
584 return self._dirs[dir].__getitem__(subpath)
584 return self._dirs[dir].__getitem__(subpath)
585 else:
585 else:
586 return self._files[f]
586 return self._files[f]
587
587
588 def flags(self, f):
588 def flags(self, f):
589 self._load()
589 self._load()
590 dir, subpath = _splittopdir(f)
590 dir, subpath = _splittopdir(f)
591 if dir:
591 if dir:
592 if dir not in self._dirs:
592 if dir not in self._dirs:
593 return ''
593 return ''
594 return self._dirs[dir].flags(subpath)
594 return self._dirs[dir].flags(subpath)
595 else:
595 else:
596 if f in self._dirs:
596 if f in self._dirs:
597 return ''
597 return ''
598 return self._flags.get(f, '')
598 return self._flags.get(f, '')
599
599
600 def find(self, f):
600 def find(self, f):
601 self._load()
601 self._load()
602 dir, subpath = _splittopdir(f)
602 dir, subpath = _splittopdir(f)
603 if dir:
603 if dir:
604 return self._dirs[dir].find(subpath)
604 return self._dirs[dir].find(subpath)
605 else:
605 else:
606 return self._files[f], self._flags.get(f, '')
606 return self._files[f], self._flags.get(f, '')
607
607
608 def __delitem__(self, f):
608 def __delitem__(self, f):
609 self._load()
609 self._load()
610 dir, subpath = _splittopdir(f)
610 dir, subpath = _splittopdir(f)
611 if dir:
611 if dir:
612 self._dirs[dir].__delitem__(subpath)
612 self._dirs[dir].__delitem__(subpath)
613 # If the directory is now empty, remove it
613 # If the directory is now empty, remove it
614 if self._dirs[dir]._isempty():
614 if self._dirs[dir]._isempty():
615 del self._dirs[dir]
615 del self._dirs[dir]
616 else:
616 else:
617 del self._files[f]
617 del self._files[f]
618 if f in self._flags:
618 if f in self._flags:
619 del self._flags[f]
619 del self._flags[f]
620 self._dirty = True
620 self._dirty = True
621
621
622 def __setitem__(self, f, n):
622 def __setitem__(self, f, n):
623 assert n is not None
623 assert n is not None
624 self._load()
624 self._load()
625 dir, subpath = _splittopdir(f)
625 dir, subpath = _splittopdir(f)
626 if dir:
626 if dir:
627 if dir not in self._dirs:
627 if dir not in self._dirs:
628 self._dirs[dir] = treemanifest(self._subpath(dir))
628 self._dirs[dir] = treemanifest(self._subpath(dir))
629 self._dirs[dir].__setitem__(subpath, n)
629 self._dirs[dir].__setitem__(subpath, n)
630 else:
630 else:
631 self._files[f] = n[:21] # to match manifestdict's behavior
631 self._files[f] = n[:21] # to match manifestdict's behavior
632 self._dirty = True
632 self._dirty = True
633
633
634 def _load(self):
634 def _load(self):
635 if self._loadfunc is not _noop:
635 if self._loadfunc is not _noop:
636 lf, self._loadfunc = self._loadfunc, _noop
636 lf, self._loadfunc = self._loadfunc, _noop
637 lf(self)
637 lf(self)
638 elif self._copyfunc is not _noop:
638 elif self._copyfunc is not _noop:
639 cf, self._copyfunc = self._copyfunc, _noop
639 cf, self._copyfunc = self._copyfunc, _noop
640 cf(self)
640 cf(self)
641
641
642 def setflag(self, f, flags):
642 def setflag(self, f, flags):
643 """Set the flags (symlink, executable) for path f."""
643 """Set the flags (symlink, executable) for path f."""
644 self._load()
644 self._load()
645 dir, subpath = _splittopdir(f)
645 dir, subpath = _splittopdir(f)
646 if dir:
646 if dir:
647 if dir not in self._dirs:
647 if dir not in self._dirs:
648 self._dirs[dir] = treemanifest(self._subpath(dir))
648 self._dirs[dir] = treemanifest(self._subpath(dir))
649 self._dirs[dir].setflag(subpath, flags)
649 self._dirs[dir].setflag(subpath, flags)
650 else:
650 else:
651 self._flags[f] = flags
651 self._flags[f] = flags
652 self._dirty = True
652 self._dirty = True
653
653
654 def copy(self):
654 def copy(self):
655 copy = treemanifest(self._dir)
655 copy = treemanifest(self._dir)
656 copy._node = self._node
656 copy._node = self._node
657 copy._dirty = self._dirty
657 copy._dirty = self._dirty
658 if self._copyfunc is _noop:
658 if self._copyfunc is _noop:
659 def _copyfunc(s):
659 def _copyfunc(s):
660 self._load()
660 self._load()
661 for d in self._dirs:
661 for d in self._dirs:
662 s._dirs[d] = self._dirs[d].copy()
662 s._dirs[d] = self._dirs[d].copy()
663 s._files = dict.copy(self._files)
663 s._files = dict.copy(self._files)
664 s._flags = dict.copy(self._flags)
664 s._flags = dict.copy(self._flags)
665 if self._loadfunc is _noop:
665 if self._loadfunc is _noop:
666 _copyfunc(copy)
666 _copyfunc(copy)
667 else:
667 else:
668 copy._copyfunc = _copyfunc
668 copy._copyfunc = _copyfunc
669 else:
669 else:
670 copy._copyfunc = self._copyfunc
670 copy._copyfunc = self._copyfunc
671 return copy
671 return copy
672
672
673 def filesnotin(self, m2):
673 def filesnotin(self, m2):
674 '''Set of files in this manifest that are not in the other'''
674 '''Set of files in this manifest that are not in the other'''
675 files = set()
675 files = set()
676 def _filesnotin(t1, t2):
676 def _filesnotin(t1, t2):
677 if t1._node == t2._node and not t1._dirty and not t2._dirty:
677 if t1._node == t2._node and not t1._dirty and not t2._dirty:
678 return
678 return
679 t1._load()
679 t1._load()
680 t2._load()
680 t2._load()
681 for d, m1 in t1._dirs.iteritems():
681 for d, m1 in t1._dirs.iteritems():
682 if d in t2._dirs:
682 if d in t2._dirs:
683 m2 = t2._dirs[d]
683 m2 = t2._dirs[d]
684 _filesnotin(m1, m2)
684 _filesnotin(m1, m2)
685 else:
685 else:
686 files.update(m1.iterkeys())
686 files.update(m1.iterkeys())
687
687
688 for fn in t1._files.iterkeys():
688 for fn in t1._files.iterkeys():
689 if fn not in t2._files:
689 if fn not in t2._files:
690 files.add(t1._subpath(fn))
690 files.add(t1._subpath(fn))
691
691
692 _filesnotin(self, m2)
692 _filesnotin(self, m2)
693 return files
693 return files
694
694
695 @propertycache
695 @propertycache
696 def _alldirs(self):
696 def _alldirs(self):
697 return util.dirs(self)
697 return util.dirs(self)
698
698
699 def dirs(self):
699 def dirs(self):
700 return self._alldirs
700 return self._alldirs
701
701
702 def hasdir(self, dir):
702 def hasdir(self, dir):
703 self._load()
703 self._load()
704 topdir, subdir = _splittopdir(dir)
704 topdir, subdir = _splittopdir(dir)
705 if topdir:
705 if topdir:
706 if topdir in self._dirs:
706 if topdir in self._dirs:
707 return self._dirs[topdir].hasdir(subdir)
707 return self._dirs[topdir].hasdir(subdir)
708 return False
708 return False
709 return (dir + '/') in self._dirs
709 return (dir + '/') in self._dirs
710
710
711 def walk(self, match):
711 def walk(self, match):
712 '''Generates matching file names.
712 '''Generates matching file names.
713
713
714 Equivalent to manifest.matches(match).iterkeys(), but without creating
714 Equivalent to manifest.matches(match).iterkeys(), but without creating
715 an entirely new manifest.
715 an entirely new manifest.
716
716
717 It also reports nonexistent files by marking them bad with match.bad().
717 It also reports nonexistent files by marking them bad with match.bad().
718 '''
718 '''
719 if match.always():
719 if match.always():
720 for f in iter(self):
720 for f in iter(self):
721 yield f
721 yield f
722 return
722 return
723
723
724 fset = set(match.files())
724 fset = set(match.files())
725
725
726 for fn in self._walk(match):
726 for fn in self._walk(match):
727 if fn in fset:
727 if fn in fset:
728 # specified pattern is the exact name
728 # specified pattern is the exact name
729 fset.remove(fn)
729 fset.remove(fn)
730 yield fn
730 yield fn
731
731
732 # for dirstate.walk, files=['.'] means "walk the whole tree".
732 # for dirstate.walk, files=['.'] means "walk the whole tree".
733 # follow that here, too
733 # follow that here, too
734 fset.discard('.')
734 fset.discard('.')
735
735
736 for fn in sorted(fset):
736 for fn in sorted(fset):
737 if not self.hasdir(fn):
737 if not self.hasdir(fn):
738 match.bad(fn, None)
738 match.bad(fn, None)
739
739
740 def _walk(self, match):
740 def _walk(self, match):
741 '''Recursively generates matching file names for walk().'''
741 '''Recursively generates matching file names for walk().'''
742 if not match.visitdir(self._dir[:-1] or '.'):
742 if not match.visitdir(self._dir[:-1] or '.'):
743 return
743 return
744
744
745 # yield this dir's files and walk its submanifests
745 # yield this dir's files and walk its submanifests
746 self._load()
746 self._load()
747 for p in sorted(self._dirs.keys() + self._files.keys()):
747 for p in sorted(self._dirs.keys() + self._files.keys()):
748 if p in self._files:
748 if p in self._files:
749 fullp = self._subpath(p)
749 fullp = self._subpath(p)
750 if match(fullp):
750 if match(fullp):
751 yield fullp
751 yield fullp
752 else:
752 else:
753 for f in self._dirs[p]._walk(match):
753 for f in self._dirs[p]._walk(match):
754 yield f
754 yield f
755
755
756 def matches(self, match):
756 def matches(self, match):
757 '''generate a new manifest filtered by the match argument'''
757 '''generate a new manifest filtered by the match argument'''
758 if match.always():
758 if match.always():
759 return self.copy()
759 return self.copy()
760
760
761 return self._matches(match)
761 return self._matches(match)
762
762
763 def _matches(self, match):
763 def _matches(self, match):
764 '''recursively generate a new manifest filtered by the match argument.
764 '''recursively generate a new manifest filtered by the match argument.
765 '''
765 '''
766
766
767 visit = match.visitdir(self._dir[:-1] or '.')
767 visit = match.visitdir(self._dir[:-1] or '.')
768 if visit == 'all':
768 if visit == 'all':
769 return self.copy()
769 return self.copy()
770 ret = treemanifest(self._dir)
770 ret = treemanifest(self._dir)
771 if not visit:
771 if not visit:
772 return ret
772 return ret
773
773
774 self._load()
774 self._load()
775 for fn in self._files:
775 for fn in self._files:
776 fullp = self._subpath(fn)
776 fullp = self._subpath(fn)
777 if not match(fullp):
777 if not match(fullp):
778 continue
778 continue
779 ret._files[fn] = self._files[fn]
779 ret._files[fn] = self._files[fn]
780 if fn in self._flags:
780 if fn in self._flags:
781 ret._flags[fn] = self._flags[fn]
781 ret._flags[fn] = self._flags[fn]
782
782
783 for dir, subm in self._dirs.iteritems():
783 for dir, subm in self._dirs.iteritems():
784 m = subm._matches(match)
784 m = subm._matches(match)
785 if not m._isempty():
785 if not m._isempty():
786 ret._dirs[dir] = m
786 ret._dirs[dir] = m
787
787
788 if not ret._isempty():
788 if not ret._isempty():
789 ret._dirty = True
789 ret._dirty = True
790 return ret
790 return ret
791
791
792 def diff(self, m2, clean=False):
792 def diff(self, m2, clean=False):
793 '''Finds changes between the current manifest and m2.
793 '''Finds changes between the current manifest and m2.
794
794
795 Args:
795 Args:
796 m2: the manifest to which this manifest should be compared.
796 m2: the manifest to which this manifest should be compared.
797 clean: if true, include files unchanged between these manifests
797 clean: if true, include files unchanged between these manifests
798 with a None value in the returned dictionary.
798 with a None value in the returned dictionary.
799
799
800 The result is returned as a dict with filename as key and
800 The result is returned as a dict with filename as key and
801 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
801 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
802 nodeid in the current/other manifest and fl1/fl2 is the flag
802 nodeid in the current/other manifest and fl1/fl2 is the flag
803 in the current/other manifest. Where the file does not exist,
803 in the current/other manifest. Where the file does not exist,
804 the nodeid will be None and the flags will be the empty
804 the nodeid will be None and the flags will be the empty
805 string.
805 string.
806 '''
806 '''
807 result = {}
807 result = {}
808 emptytree = treemanifest()
808 emptytree = treemanifest()
809 def _diff(t1, t2):
809 def _diff(t1, t2):
810 if t1._node == t2._node and not t1._dirty and not t2._dirty:
810 if t1._node == t2._node and not t1._dirty and not t2._dirty:
811 return
811 return
812 t1._load()
812 t1._load()
813 t2._load()
813 t2._load()
814 for d, m1 in t1._dirs.iteritems():
814 for d, m1 in t1._dirs.iteritems():
815 m2 = t2._dirs.get(d, emptytree)
815 m2 = t2._dirs.get(d, emptytree)
816 _diff(m1, m2)
816 _diff(m1, m2)
817
817
818 for d, m2 in t2._dirs.iteritems():
818 for d, m2 in t2._dirs.iteritems():
819 if d not in t1._dirs:
819 if d not in t1._dirs:
820 _diff(emptytree, m2)
820 _diff(emptytree, m2)
821
821
822 for fn, n1 in t1._files.iteritems():
822 for fn, n1 in t1._files.iteritems():
823 fl1 = t1._flags.get(fn, '')
823 fl1 = t1._flags.get(fn, '')
824 n2 = t2._files.get(fn, None)
824 n2 = t2._files.get(fn, None)
825 fl2 = t2._flags.get(fn, '')
825 fl2 = t2._flags.get(fn, '')
826 if n1 != n2 or fl1 != fl2:
826 if n1 != n2 or fl1 != fl2:
827 result[t1._subpath(fn)] = ((n1, fl1), (n2, fl2))
827 result[t1._subpath(fn)] = ((n1, fl1), (n2, fl2))
828 elif clean:
828 elif clean:
829 result[t1._subpath(fn)] = None
829 result[t1._subpath(fn)] = None
830
830
831 for fn, n2 in t2._files.iteritems():
831 for fn, n2 in t2._files.iteritems():
832 if fn not in t1._files:
832 if fn not in t1._files:
833 fl2 = t2._flags.get(fn, '')
833 fl2 = t2._flags.get(fn, '')
834 result[t2._subpath(fn)] = ((None, ''), (n2, fl2))
834 result[t2._subpath(fn)] = ((None, ''), (n2, fl2))
835
835
836 _diff(self, m2)
836 _diff(self, m2)
837 return result
837 return result
838
838
839 def unmodifiedsince(self, m2):
839 def unmodifiedsince(self, m2):
840 return not self._dirty and not m2._dirty and self._node == m2._node
840 return not self._dirty and not m2._dirty and self._node == m2._node
841
841
842 def parse(self, text, readsubtree):
842 def parse(self, text, readsubtree):
843 for f, n, fl in _parse(text):
843 for f, n, fl in _parse(text):
844 if fl == 't':
844 if fl == 't':
845 f = f + '/'
845 f = f + '/'
846 self._dirs[f] = readsubtree(self._subpath(f), n)
846 self._dirs[f] = readsubtree(self._subpath(f), n)
847 elif '/' in f:
847 elif '/' in f:
848 # This is a flat manifest, so use __setitem__ and setflag rather
848 # This is a flat manifest, so use __setitem__ and setflag rather
849 # than assigning directly to _files and _flags, so we can
849 # than assigning directly to _files and _flags, so we can
850 # assign a path in a subdirectory, and to mark dirty (compared
850 # assign a path in a subdirectory, and to mark dirty (compared
851 # to nullid).
851 # to nullid).
852 self[f] = n
852 self[f] = n
853 if fl:
853 if fl:
854 self.setflag(f, fl)
854 self.setflag(f, fl)
855 else:
855 else:
856 # Assigning to _files and _flags avoids marking as dirty,
856 # Assigning to _files and _flags avoids marking as dirty,
857 # and should be a little faster.
857 # and should be a little faster.
858 self._files[f] = n
858 self._files[f] = n
859 if fl:
859 if fl:
860 self._flags[f] = fl
860 self._flags[f] = fl
861
861
862 def text(self, usemanifestv2=False):
862 def text(self, usemanifestv2=False):
863 """Get the full data of this manifest as a bytestring."""
863 """Get the full data of this manifest as a bytestring."""
864 self._load()
864 self._load()
865 return _text(self.iterentries(), usemanifestv2)
865 return _text(self.iterentries(), usemanifestv2)
866
866
867 def dirtext(self, usemanifestv2=False):
867 def dirtext(self, usemanifestv2=False):
868 """Get the full data of this directory as a bytestring. Make sure that
868 """Get the full data of this directory as a bytestring. Make sure that
869 any submanifests have been written first, so their nodeids are correct.
869 any submanifests have been written first, so their nodeids are correct.
870 """
870 """
871 self._load()
871 self._load()
872 flags = self.flags
872 flags = self.flags
873 dirs = [(d[:-1], self._dirs[d]._node, 't') for d in self._dirs]
873 dirs = [(d[:-1], self._dirs[d]._node, 't') for d in self._dirs]
874 files = [(f, self._files[f], flags(f)) for f in self._files]
874 files = [(f, self._files[f], flags(f)) for f in self._files]
875 return _text(sorted(dirs + files), usemanifestv2)
875 return _text(sorted(dirs + files), usemanifestv2)
876
876
877 def read(self, gettext, readsubtree):
877 def read(self, gettext, readsubtree):
878 def _load_for_read(s):
878 def _load_for_read(s):
879 s.parse(gettext(), readsubtree)
879 s.parse(gettext(), readsubtree)
880 s._dirty = False
880 s._dirty = False
881 self._loadfunc = _load_for_read
881 self._loadfunc = _load_for_read
882
882
883 def writesubtrees(self, m1, m2, writesubtree):
883 def writesubtrees(self, m1, m2, writesubtree):
884 self._load() # for consistency; should never have any effect here
884 self._load() # for consistency; should never have any effect here
885 m1._load()
885 m1._load()
886 m2._load()
886 m2._load()
887 emptytree = treemanifest()
887 emptytree = treemanifest()
888 for d, subm in self._dirs.iteritems():
888 for d, subm in self._dirs.iteritems():
889 subp1 = m1._dirs.get(d, emptytree)._node
889 subp1 = m1._dirs.get(d, emptytree)._node
890 subp2 = m2._dirs.get(d, emptytree)._node
890 subp2 = m2._dirs.get(d, emptytree)._node
891 if subp1 == revlog.nullid:
891 if subp1 == revlog.nullid:
892 subp1, subp2 = subp2, subp1
892 subp1, subp2 = subp2, subp1
893 writesubtree(subm, subp1, subp2)
893 writesubtree(subm, subp1, subp2)
894
894
895 class manifestrevlog(revlog.revlog):
895 class manifestrevlog(revlog.revlog):
896 '''A revlog that stores manifest texts. This is responsible for caching the
896 '''A revlog that stores manifest texts. This is responsible for caching the
897 full-text manifest contents.
897 full-text manifest contents.
898 '''
898 '''
899 def __init__(self, opener, indexfile):
899 def __init__(self, opener, indexfile):
900 super(manifestrevlog, self).__init__(opener, indexfile)
900 super(manifestrevlog, self).__init__(opener, indexfile)
901
901
902 # During normal operations, we expect to deal with not more than four
902 # During normal operations, we expect to deal with not more than four
903 # revs at a time (such as during commit --amend). When rebasing large
903 # revs at a time (such as during commit --amend). When rebasing large
904 # stacks of commits, the number can go up, hence the config knob below.
904 # stacks of commits, the number can go up, hence the config knob below.
905 cachesize = 4
905 cachesize = 4
906 opts = getattr(opener, 'options', None)
906 opts = getattr(opener, 'options', None)
907 if opts is not None:
907 if opts is not None:
908 cachesize = opts.get('manifestcachesize', cachesize)
908 cachesize = opts.get('manifestcachesize', cachesize)
909 self._fulltextcache = util.lrucachedict(cachesize)
909 self._fulltextcache = util.lrucachedict(cachesize)
910
910
911 @property
911 @property
912 def fulltextcache(self):
912 def fulltextcache(self):
913 return self._fulltextcache
913 return self._fulltextcache
914
914
915 def clearcaches(self):
915 def clearcaches(self):
916 super(manifestrevlog, self).clearcaches()
916 super(manifestrevlog, self).clearcaches()
917 self._fulltextcache.clear()
917 self._fulltextcache.clear()
918
918
919 class manifestlog(object):
919 class manifestlog(object):
920 """A collection class representing the collection of manifest snapshots
920 """A collection class representing the collection of manifest snapshots
921 referenced by commits in the repository.
921 referenced by commits in the repository.
922
922
923 In this situation, 'manifest' refers to the abstract concept of a snapshot
923 In this situation, 'manifest' refers to the abstract concept of a snapshot
924 of the list of files in the given commit. Consumers of the output of this
924 of the list of files in the given commit. Consumers of the output of this
925 class do not care about the implementation details of the actual manifests
925 class do not care about the implementation details of the actual manifests
926 they receive (i.e. tree or flat or lazily loaded, etc)."""
926 they receive (i.e. tree or flat or lazily loaded, etc)."""
927 def __init__(self, opener, repo):
927 def __init__(self, opener, repo):
928 self._repo = repo
928 self._repo = repo
929
929
930 # We'll separate this into it's own cache once oldmanifest is no longer
930 # We'll separate this into it's own cache once oldmanifest is no longer
931 # used
931 # used
932 self._mancache = repo.manifest._mancache
932 self._mancache = repo.manifest._mancache
933
933
934 @property
934 @property
935 def _revlog(self):
935 def _revlog(self):
936 return self._repo.manifest
936 return self._repo.manifest
937
937
938 @property
938 @property
939 def _oldmanifest(self):
939 def _oldmanifest(self):
940 # _revlog is the same as _oldmanifest right now, but we eventually want
940 # _revlog is the same as _oldmanifest right now, but we eventually want
941 # to delete _oldmanifest while still allowing manifestlog to access the
941 # to delete _oldmanifest while still allowing manifestlog to access the
942 # revlog specific apis.
942 # revlog specific apis.
943 return self._repo.manifest
943 return self._repo.manifest
944
944
945 def __getitem__(self, node):
945 def __getitem__(self, node):
946 """Retrieves the manifest instance for the given node. Throws a KeyError
946 """Retrieves the manifest instance for the given node. Throws a KeyError
947 if not found.
947 if not found.
948 """
948 """
949 if node in self._mancache:
949 if node in self._mancache:
950 cachemf = self._mancache[node]
950 cachemf = self._mancache[node]
951 # The old manifest may put non-ctx manifests in the cache, so skip
951 # The old manifest may put non-ctx manifests in the cache, so skip
952 # those since they don't implement the full api.
952 # those since they don't implement the full api.
953 if (isinstance(cachemf, manifestctx) or
953 if (isinstance(cachemf, manifestctx) or
954 isinstance(cachemf, treemanifestctx)):
954 isinstance(cachemf, treemanifestctx)):
955 return cachemf
955 return cachemf
956
956
957 if self._oldmanifest._treeinmem:
957 if self._oldmanifest._treeinmem:
958 m = treemanifestctx(self._revlog, '', node)
958 m = treemanifestctx(self._revlog, '', node)
959 else:
959 else:
960 m = manifestctx(self._revlog, node)
960 m = manifestctx(self._revlog, node)
961 if node != revlog.nullid:
961 if node != revlog.nullid:
962 self._mancache[node] = m
962 self._mancache[node] = m
963 return m
963 return m
964
964
965 class manifestctx(manifestdict):
965 class manifestctx(object):
966 """A class representing a single revision of a manifest, including its
966 """A class representing a single revision of a manifest, including its
967 contents, its parent revs, and its linkrev.
967 contents, its parent revs, and its linkrev.
968 """
968 """
969 def __init__(self, revlog, node):
969 def __init__(self, revlog, node):
970 self._revlog = revlog
970 self._revlog = revlog
971 self._data = None
971
972
972 self._node = node
973 self._node = node
973
974
974 # TODO: We eventually want p1, p2, and linkrev exposed on this class,
975 # TODO: We eventually want p1, p2, and linkrev exposed on this class,
975 # but let's add it later when something needs it and we can load it
976 # but let's add it later when something needs it and we can load it
976 # lazily.
977 # lazily.
977 #self.p1, self.p2 = revlog.parents(node)
978 #self.p1, self.p2 = revlog.parents(node)
978 #rev = revlog.rev(node)
979 #rev = revlog.rev(node)
979 #self.linkrev = revlog.linkrev(rev)
980 #self.linkrev = revlog.linkrev(rev)
980
981
981 # This should eventually be made lazy loaded, so consumers can access
982 # the node/p1/linkrev data without having to parse the whole manifest.
983 data = revlog.revision(node)
984 arraytext = array.array('c', data)
985 revlog._fulltextcache[node] = arraytext
986 super(manifestctx, self).__init__(data)
987
988 def node(self):
982 def node(self):
989 return self._node
983 return self._node
990
984
991 class treemanifestctx(treemanifest):
985 def read(self):
986 if not self._data:
987 if self._node == revlog.nullid:
988 self._data = manifestdict()
989 else:
990 text = self._revlog.revision(self._node)
991 arraytext = array.array('c', text)
992 self._revlog._fulltextcache[self._node] = arraytext
993 self._data = manifestdict(text)
994 return self._data
995
996 class treemanifestctx(object):
992 def __init__(self, revlog, dir, node):
997 def __init__(self, revlog, dir, node):
993 revlog = revlog.dirlog(dir)
998 revlog = revlog.dirlog(dir)
994 self._revlog = revlog
999 self._revlog = revlog
995 self._dir = dir
1000 self._dir = dir
1001 self._data = None
996
1002
997 self._node = node
1003 self._node = node
998
1004
999 # TODO: Load p1/p2/linkrev lazily. They need to be lazily loaded so that
1005 # TODO: Load p1/p2/linkrev lazily. They need to be lazily loaded so that
1000 # we can instantiate treemanifestctx objects for directories we don't
1006 # we can instantiate treemanifestctx objects for directories we don't
1001 # have on disk.
1007 # have on disk.
1002 #self.p1, self.p2 = revlog.parents(node)
1008 #self.p1, self.p2 = revlog.parents(node)
1003 #rev = revlog.rev(node)
1009 #rev = revlog.rev(node)
1004 #self.linkrev = revlog.linkrev(rev)
1010 #self.linkrev = revlog.linkrev(rev)
1005
1011
1006 if revlog._treeondisk:
1012 def read(self):
1007 super(treemanifestctx, self).__init__(dir=dir)
1013 if not self._data:
1014 if self._node == revlog.nullid:
1015 self._data = treemanifest()
1016 elif self._revlog._treeondisk:
1017 m = treemanifest(dir=self._dir)
1008 def gettext():
1018 def gettext():
1009 return revlog.revision(node)
1019 return self._revlog.revision(self._node)
1010 def readsubtree(dir, subm):
1020 def readsubtree(dir, subm):
1011 return revlog.dirlog(dir).read(subm)
1021 return treemanifestctx(self._revlog, dir, subm).read()
1012 self.read(gettext, readsubtree)
1022 m.read(gettext, readsubtree)
1013 self.setnode(node)
1023 m.setnode(self._node)
1024 self._data = m
1014 else:
1025 else:
1015 text = revlog.revision(node)
1026 text = self._revlog.revision(self._node)
1016 arraytext = array.array('c', text)
1027 arraytext = array.array('c', text)
1017 revlog.fulltextcache[node] = arraytext
1028 self._revlog.fulltextcache[self._node] = arraytext
1018 super(treemanifestctx, self).__init__(dir=dir, text=text)
1029 self._data = treemanifest(dir=self._dir, text=text)
1030
1031 return self._data
1019
1032
1020 def node(self):
1033 def node(self):
1021 return self._node
1034 return self._node
1022
1035
1023 class manifest(manifestrevlog):
1036 class manifest(manifestrevlog):
1024 def __init__(self, opener, dir='', dirlogcache=None):
1037 def __init__(self, opener, dir='', dirlogcache=None):
1025 '''The 'dir' and 'dirlogcache' arguments are for internal use by
1038 '''The 'dir' and 'dirlogcache' arguments are for internal use by
1026 manifest.manifest only. External users should create a root manifest
1039 manifest.manifest only. External users should create a root manifest
1027 log with manifest.manifest(opener) and call dirlog() on it.
1040 log with manifest.manifest(opener) and call dirlog() on it.
1028 '''
1041 '''
1029 # During normal operations, we expect to deal with not more than four
1042 # During normal operations, we expect to deal with not more than four
1030 # revs at a time (such as during commit --amend). When rebasing large
1043 # revs at a time (such as during commit --amend). When rebasing large
1031 # stacks of commits, the number can go up, hence the config knob below.
1044 # stacks of commits, the number can go up, hence the config knob below.
1032 cachesize = 4
1045 cachesize = 4
1033 usetreemanifest = False
1046 usetreemanifest = False
1034 usemanifestv2 = False
1047 usemanifestv2 = False
1035 opts = getattr(opener, 'options', None)
1048 opts = getattr(opener, 'options', None)
1036 if opts is not None:
1049 if opts is not None:
1037 cachesize = opts.get('manifestcachesize', cachesize)
1050 cachesize = opts.get('manifestcachesize', cachesize)
1038 usetreemanifest = opts.get('treemanifest', usetreemanifest)
1051 usetreemanifest = opts.get('treemanifest', usetreemanifest)
1039 usemanifestv2 = opts.get('manifestv2', usemanifestv2)
1052 usemanifestv2 = opts.get('manifestv2', usemanifestv2)
1040 self._mancache = util.lrucachedict(cachesize)
1053 self._mancache = util.lrucachedict(cachesize)
1041 self._treeinmem = usetreemanifest
1054 self._treeinmem = usetreemanifest
1042 self._treeondisk = usetreemanifest
1055 self._treeondisk = usetreemanifest
1043 self._usemanifestv2 = usemanifestv2
1056 self._usemanifestv2 = usemanifestv2
1044 indexfile = "00manifest.i"
1057 indexfile = "00manifest.i"
1045 if dir:
1058 if dir:
1046 assert self._treeondisk, 'opts is %r' % opts
1059 assert self._treeondisk, 'opts is %r' % opts
1047 if not dir.endswith('/'):
1060 if not dir.endswith('/'):
1048 dir = dir + '/'
1061 dir = dir + '/'
1049 indexfile = "meta/" + dir + "00manifest.i"
1062 indexfile = "meta/" + dir + "00manifest.i"
1050 super(manifest, self).__init__(opener, indexfile)
1063 super(manifest, self).__init__(opener, indexfile)
1051 self._dir = dir
1064 self._dir = dir
1052 # The dirlogcache is kept on the root manifest log
1065 # The dirlogcache is kept on the root manifest log
1053 if dir:
1066 if dir:
1054 self._dirlogcache = dirlogcache
1067 self._dirlogcache = dirlogcache
1055 else:
1068 else:
1056 self._dirlogcache = {'': self}
1069 self._dirlogcache = {'': self}
1057
1070
1058 def _newmanifest(self, data=''):
1071 def _newmanifest(self, data=''):
1059 if self._treeinmem:
1072 if self._treeinmem:
1060 return treemanifest(self._dir, data)
1073 return treemanifest(self._dir, data)
1061 return manifestdict(data)
1074 return manifestdict(data)
1062
1075
1063 def dirlog(self, dir):
1076 def dirlog(self, dir):
1064 if dir:
1077 if dir:
1065 assert self._treeondisk
1078 assert self._treeondisk
1066 if dir not in self._dirlogcache:
1079 if dir not in self._dirlogcache:
1067 self._dirlogcache[dir] = manifest(self.opener, dir,
1080 self._dirlogcache[dir] = manifest(self.opener, dir,
1068 self._dirlogcache)
1081 self._dirlogcache)
1069 return self._dirlogcache[dir]
1082 return self._dirlogcache[dir]
1070
1083
1071 def _slowreaddelta(self, node):
1084 def _slowreaddelta(self, node):
1072 r0 = self.deltaparent(self.rev(node))
1085 r0 = self.deltaparent(self.rev(node))
1073 m0 = self.read(self.node(r0))
1086 m0 = self.read(self.node(r0))
1074 m1 = self.read(node)
1087 m1 = self.read(node)
1075 md = self._newmanifest()
1088 md = self._newmanifest()
1076 for f, ((n0, fl0), (n1, fl1)) in m0.diff(m1).iteritems():
1089 for f, ((n0, fl0), (n1, fl1)) in m0.diff(m1).iteritems():
1077 if n1:
1090 if n1:
1078 md[f] = n1
1091 md[f] = n1
1079 if fl1:
1092 if fl1:
1080 md.setflag(f, fl1)
1093 md.setflag(f, fl1)
1081 return md
1094 return md
1082
1095
1083 def readdelta(self, node):
1096 def readdelta(self, node):
1084 if self._usemanifestv2 or self._treeondisk:
1097 if self._usemanifestv2 or self._treeondisk:
1085 return self._slowreaddelta(node)
1098 return self._slowreaddelta(node)
1086 r = self.rev(node)
1099 r = self.rev(node)
1087 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1100 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1088 return self._newmanifest(d)
1101 return self._newmanifest(d)
1089
1102
1090 def readshallowdelta(self, node):
1103 def readshallowdelta(self, node):
1091 '''For flat manifests, this is the same as readdelta(). For
1104 '''For flat manifests, this is the same as readdelta(). For
1092 treemanifests, this will read the delta for this revlog's directory,
1105 treemanifests, this will read the delta for this revlog's directory,
1093 without recursively reading subdirectory manifests. Instead, any
1106 without recursively reading subdirectory manifests. Instead, any
1094 subdirectory entry will be reported as it appears in the manifests, i.e.
1107 subdirectory entry will be reported as it appears in the manifests, i.e.
1095 the subdirectory will be reported among files and distinguished only by
1108 the subdirectory will be reported among files and distinguished only by
1096 its 't' flag.'''
1109 its 't' flag.'''
1097 if not self._treeondisk:
1110 if not self._treeondisk:
1098 return self.readdelta(node)
1111 return self.readdelta(node)
1099 if self._usemanifestv2:
1112 if self._usemanifestv2:
1100 raise error.Abort(
1113 raise error.Abort(
1101 _("readshallowdelta() not implemented for manifestv2"))
1114 _("readshallowdelta() not implemented for manifestv2"))
1102 r = self.rev(node)
1115 r = self.rev(node)
1103 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1116 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1104 return manifestdict(d)
1117 return manifestdict(d)
1105
1118
1106 def readfast(self, node):
1119 def readfast(self, node):
1107 '''use the faster of readdelta or read
1120 '''use the faster of readdelta or read
1108
1121
1109 This will return a manifest which is either only the files
1122 This will return a manifest which is either only the files
1110 added/modified relative to p1, or all files in the
1123 added/modified relative to p1, or all files in the
1111 manifest. Which one is returned depends on the codepath used
1124 manifest. Which one is returned depends on the codepath used
1112 to retrieve the data.
1125 to retrieve the data.
1113 '''
1126 '''
1114 r = self.rev(node)
1127 r = self.rev(node)
1115 deltaparent = self.deltaparent(r)
1128 deltaparent = self.deltaparent(r)
1116 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1129 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1117 return self.readdelta(node)
1130 return self.readdelta(node)
1118 return self.read(node)
1131 return self.read(node)
1119
1132
1120 def readshallowfast(self, node):
1133 def readshallowfast(self, node):
1121 '''like readfast(), but calls readshallowdelta() instead of readdelta()
1134 '''like readfast(), but calls readshallowdelta() instead of readdelta()
1122 '''
1135 '''
1123 r = self.rev(node)
1136 r = self.rev(node)
1124 deltaparent = self.deltaparent(r)
1137 deltaparent = self.deltaparent(r)
1125 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1138 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1126 return self.readshallowdelta(node)
1139 return self.readshallowdelta(node)
1127 return self.readshallow(node)
1140 return self.readshallow(node)
1128
1141
1129 def read(self, node):
1142 def read(self, node):
1130 if node == revlog.nullid:
1143 if node == revlog.nullid:
1131 return self._newmanifest() # don't upset local cache
1144 return self._newmanifest() # don't upset local cache
1132 if node in self._mancache:
1145 if node in self._mancache:
1133 cached = self._mancache[node]
1146 cached = self._mancache[node]
1134 if (isinstance(cached, manifestctx) or
1147 if (isinstance(cached, manifestctx) or
1135 isinstance(cached, treemanifestctx)):
1148 isinstance(cached, treemanifestctx)):
1136 cached = cached.read()
1149 cached = cached.read()
1137 return cached
1150 return cached
1138 if self._treeondisk:
1151 if self._treeondisk:
1139 def gettext():
1152 def gettext():
1140 return self.revision(node)
1153 return self.revision(node)
1141 def readsubtree(dir, subm):
1154 def readsubtree(dir, subm):
1142 return self.dirlog(dir).read(subm)
1155 return self.dirlog(dir).read(subm)
1143 m = self._newmanifest()
1156 m = self._newmanifest()
1144 m.read(gettext, readsubtree)
1157 m.read(gettext, readsubtree)
1145 m.setnode(node)
1158 m.setnode(node)
1146 arraytext = None
1159 arraytext = None
1147 else:
1160 else:
1148 text = self.revision(node)
1161 text = self.revision(node)
1149 m = self._newmanifest(text)
1162 m = self._newmanifest(text)
1150 arraytext = array.array('c', text)
1163 arraytext = array.array('c', text)
1151 self._mancache[node] = m
1164 self._mancache[node] = m
1152 self.fulltextcache[node] = arraytext
1165 self.fulltextcache[node] = arraytext
1153 return m
1166 return m
1154
1167
1155 def readshallow(self, node):
1168 def readshallow(self, node):
1156 '''Reads the manifest in this directory. When using flat manifests,
1169 '''Reads the manifest in this directory. When using flat manifests,
1157 this manifest will generally have files in subdirectories in it. Does
1170 this manifest will generally have files in subdirectories in it. Does
1158 not cache the manifest as the callers generally do not read the same
1171 not cache the manifest as the callers generally do not read the same
1159 version twice.'''
1172 version twice.'''
1160 return manifestdict(self.revision(node))
1173 return manifestdict(self.revision(node))
1161
1174
1162 def find(self, node, f):
1175 def find(self, node, f):
1163 '''look up entry for a single file efficiently.
1176 '''look up entry for a single file efficiently.
1164 return (node, flags) pair if found, (None, None) if not.'''
1177 return (node, flags) pair if found, (None, None) if not.'''
1165 m = self.read(node)
1178 m = self.read(node)
1166 try:
1179 try:
1167 return m.find(f)
1180 return m.find(f)
1168 except KeyError:
1181 except KeyError:
1169 return None, None
1182 return None, None
1170
1183
1171 def add(self, m, transaction, link, p1, p2, added, removed):
1184 def add(self, m, transaction, link, p1, p2, added, removed):
1172 if (p1 in self.fulltextcache and not self._treeinmem
1185 if (p1 in self.fulltextcache and not self._treeinmem
1173 and not self._usemanifestv2):
1186 and not self._usemanifestv2):
1174 # If our first parent is in the manifest cache, we can
1187 # If our first parent is in the manifest cache, we can
1175 # compute a delta here using properties we know about the
1188 # compute a delta here using properties we know about the
1176 # manifest up-front, which may save time later for the
1189 # manifest up-front, which may save time later for the
1177 # revlog layer.
1190 # revlog layer.
1178
1191
1179 _checkforbidden(added)
1192 _checkforbidden(added)
1180 # combine the changed lists into one sorted iterator
1193 # combine the changed lists into one sorted iterator
1181 work = heapq.merge([(x, False) for x in added],
1194 work = heapq.merge([(x, False) for x in added],
1182 [(x, True) for x in removed])
1195 [(x, True) for x in removed])
1183
1196
1184 arraytext, deltatext = m.fastdelta(self.fulltextcache[p1], work)
1197 arraytext, deltatext = m.fastdelta(self.fulltextcache[p1], work)
1185 cachedelta = self.rev(p1), deltatext
1198 cachedelta = self.rev(p1), deltatext
1186 text = util.buffer(arraytext)
1199 text = util.buffer(arraytext)
1187 n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
1200 n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
1188 else:
1201 else:
1189 # The first parent manifest isn't already loaded, so we'll
1202 # The first parent manifest isn't already loaded, so we'll
1190 # just encode a fulltext of the manifest and pass that
1203 # just encode a fulltext of the manifest and pass that
1191 # through to the revlog layer, and let it handle the delta
1204 # through to the revlog layer, and let it handle the delta
1192 # process.
1205 # process.
1193 if self._treeondisk:
1206 if self._treeondisk:
1194 m1 = self.read(p1)
1207 m1 = self.read(p1)
1195 m2 = self.read(p2)
1208 m2 = self.read(p2)
1196 n = self._addtree(m, transaction, link, m1, m2)
1209 n = self._addtree(m, transaction, link, m1, m2)
1197 arraytext = None
1210 arraytext = None
1198 else:
1211 else:
1199 text = m.text(self._usemanifestv2)
1212 text = m.text(self._usemanifestv2)
1200 n = self.addrevision(text, transaction, link, p1, p2)
1213 n = self.addrevision(text, transaction, link, p1, p2)
1201 arraytext = array.array('c', text)
1214 arraytext = array.array('c', text)
1202
1215
1203 self._mancache[n] = m
1216 self._mancache[n] = m
1204 self.fulltextcache[n] = arraytext
1217 self.fulltextcache[n] = arraytext
1205
1218
1206 return n
1219 return n
1207
1220
1208 def _addtree(self, m, transaction, link, m1, m2):
1221 def _addtree(self, m, transaction, link, m1, m2):
1209 # If the manifest is unchanged compared to one parent,
1222 # If the manifest is unchanged compared to one parent,
1210 # don't write a new revision
1223 # don't write a new revision
1211 if m.unmodifiedsince(m1) or m.unmodifiedsince(m2):
1224 if m.unmodifiedsince(m1) or m.unmodifiedsince(m2):
1212 return m.node()
1225 return m.node()
1213 def writesubtree(subm, subp1, subp2):
1226 def writesubtree(subm, subp1, subp2):
1214 sublog = self.dirlog(subm.dir())
1227 sublog = self.dirlog(subm.dir())
1215 sublog.add(subm, transaction, link, subp1, subp2, None, None)
1228 sublog.add(subm, transaction, link, subp1, subp2, None, None)
1216 m.writesubtrees(m1, m2, writesubtree)
1229 m.writesubtrees(m1, m2, writesubtree)
1217 text = m.dirtext(self._usemanifestv2)
1230 text = m.dirtext(self._usemanifestv2)
1218 # Double-check whether contents are unchanged to one parent
1231 # Double-check whether contents are unchanged to one parent
1219 if text == m1.dirtext(self._usemanifestv2):
1232 if text == m1.dirtext(self._usemanifestv2):
1220 n = m1.node()
1233 n = m1.node()
1221 elif text == m2.dirtext(self._usemanifestv2):
1234 elif text == m2.dirtext(self._usemanifestv2):
1222 n = m2.node()
1235 n = m2.node()
1223 else:
1236 else:
1224 n = self.addrevision(text, transaction, link, m1.node(), m2.node())
1237 n = self.addrevision(text, transaction, link, m1.node(), m2.node())
1225 # Save nodeid so parent manifest can calculate its nodeid
1238 # Save nodeid so parent manifest can calculate its nodeid
1226 m.setnode(n)
1239 m.setnode(n)
1227 return n
1240 return n
1228
1241
1229 def clearcaches(self):
1242 def clearcaches(self):
1230 super(manifest, self).clearcaches()
1243 super(manifest, self).clearcaches()
1231 self._mancache.clear()
1244 self._mancache.clear()
1232 self._dirlogcache = {'': self}
1245 self._dirlogcache = {'': self}
General Comments 0
You need to be logged in to leave comments. Login now