##// END OF EJS Templates
context: drop deprecated methods (API)...
Matt Harbison -
r35908:44bc37d2 default
parent child Browse files
Show More
@@ -1,2790 +1,2749 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 filecmp
11 import filecmp
12 import os
12 import os
13 import re
13 import re
14 import stat
14 import stat
15
15
16 from .i18n import _
16 from .i18n import _
17 from .node import (
17 from .node import (
18 addednodeid,
18 addednodeid,
19 bin,
19 bin,
20 hex,
20 hex,
21 modifiednodeid,
21 modifiednodeid,
22 nullid,
22 nullid,
23 nullrev,
23 nullrev,
24 short,
24 short,
25 wdirid,
25 wdirid,
26 wdirnodes,
26 wdirnodes,
27 wdirrev,
27 wdirrev,
28 )
28 )
29 from .thirdparty import (
29 from .thirdparty import (
30 attr,
30 attr,
31 )
31 )
32 from . import (
32 from . import (
33 encoding,
33 encoding,
34 error,
34 error,
35 fileset,
35 fileset,
36 match as matchmod,
36 match as matchmod,
37 mdiff,
37 mdiff,
38 obsolete as obsmod,
38 obsolete as obsmod,
39 obsutil,
39 obsutil,
40 patch,
40 patch,
41 pathutil,
41 pathutil,
42 phases,
42 phases,
43 pycompat,
43 pycompat,
44 repoview,
44 repoview,
45 revlog,
45 revlog,
46 scmutil,
46 scmutil,
47 sparse,
47 sparse,
48 subrepo,
48 subrepo,
49 util,
49 util,
50 )
50 )
51
51
52 propertycache = util.propertycache
52 propertycache = util.propertycache
53
53
54 nonascii = re.compile(r'[^\x21-\x7f]').search
54 nonascii = re.compile(r'[^\x21-\x7f]').search
55
55
56 class basectx(object):
56 class basectx(object):
57 """A basectx object represents the common logic for its children:
57 """A basectx object represents the common logic for its children:
58 changectx: read-only context that is already present in the repo,
58 changectx: read-only context that is already present in the repo,
59 workingctx: a context that represents the working directory and can
59 workingctx: a context that represents the working directory and can
60 be committed,
60 be committed,
61 memctx: a context that represents changes in-memory and can also
61 memctx: a context that represents changes in-memory and can also
62 be committed."""
62 be committed."""
63 def __new__(cls, repo, changeid='', *args, **kwargs):
63 def __new__(cls, repo, changeid='', *args, **kwargs):
64 if isinstance(changeid, basectx):
64 if isinstance(changeid, basectx):
65 return changeid
65 return changeid
66
66
67 o = super(basectx, cls).__new__(cls)
67 o = super(basectx, cls).__new__(cls)
68
68
69 o._repo = repo
69 o._repo = repo
70 o._rev = nullrev
70 o._rev = nullrev
71 o._node = nullid
71 o._node = nullid
72
72
73 return o
73 return o
74
74
75 def __bytes__(self):
75 def __bytes__(self):
76 return short(self.node())
76 return short(self.node())
77
77
78 __str__ = encoding.strmethod(__bytes__)
78 __str__ = encoding.strmethod(__bytes__)
79
79
80 def __int__(self):
80 def __int__(self):
81 return self.rev()
81 return self.rev()
82
82
83 def __repr__(self):
83 def __repr__(self):
84 return r"<%s %s>" % (type(self).__name__, str(self))
84 return r"<%s %s>" % (type(self).__name__, str(self))
85
85
86 def __eq__(self, other):
86 def __eq__(self, other):
87 try:
87 try:
88 return type(self) == type(other) and self._rev == other._rev
88 return type(self) == type(other) and self._rev == other._rev
89 except AttributeError:
89 except AttributeError:
90 return False
90 return False
91
91
92 def __ne__(self, other):
92 def __ne__(self, other):
93 return not (self == other)
93 return not (self == other)
94
94
95 def __contains__(self, key):
95 def __contains__(self, key):
96 return key in self._manifest
96 return key in self._manifest
97
97
98 def __getitem__(self, key):
98 def __getitem__(self, key):
99 return self.filectx(key)
99 return self.filectx(key)
100
100
101 def __iter__(self):
101 def __iter__(self):
102 return iter(self._manifest)
102 return iter(self._manifest)
103
103
104 def _buildstatusmanifest(self, status):
104 def _buildstatusmanifest(self, status):
105 """Builds a manifest that includes the given status results, if this is
105 """Builds a manifest that includes the given status results, if this is
106 a working copy context. For non-working copy contexts, it just returns
106 a working copy context. For non-working copy contexts, it just returns
107 the normal manifest."""
107 the normal manifest."""
108 return self.manifest()
108 return self.manifest()
109
109
110 def _matchstatus(self, other, match):
110 def _matchstatus(self, other, match):
111 """This internal method provides a way for child objects to override the
111 """This internal method provides a way for child objects to override the
112 match operator.
112 match operator.
113 """
113 """
114 return match
114 return match
115
115
116 def _buildstatus(self, other, s, match, listignored, listclean,
116 def _buildstatus(self, other, s, match, listignored, listclean,
117 listunknown):
117 listunknown):
118 """build a status with respect to another context"""
118 """build a status with respect to another context"""
119 # Load earliest manifest first for caching reasons. More specifically,
119 # Load earliest manifest first for caching reasons. More specifically,
120 # if you have revisions 1000 and 1001, 1001 is probably stored as a
120 # if you have revisions 1000 and 1001, 1001 is probably stored as a
121 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
121 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
122 # 1000 and cache it so that when you read 1001, we just need to apply a
122 # 1000 and cache it so that when you read 1001, we just need to apply a
123 # delta to what's in the cache. So that's one full reconstruction + one
123 # delta to what's in the cache. So that's one full reconstruction + one
124 # delta application.
124 # delta application.
125 mf2 = None
125 mf2 = None
126 if self.rev() is not None and self.rev() < other.rev():
126 if self.rev() is not None and self.rev() < other.rev():
127 mf2 = self._buildstatusmanifest(s)
127 mf2 = self._buildstatusmanifest(s)
128 mf1 = other._buildstatusmanifest(s)
128 mf1 = other._buildstatusmanifest(s)
129 if mf2 is None:
129 if mf2 is None:
130 mf2 = self._buildstatusmanifest(s)
130 mf2 = self._buildstatusmanifest(s)
131
131
132 modified, added = [], []
132 modified, added = [], []
133 removed = []
133 removed = []
134 clean = []
134 clean = []
135 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
135 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
136 deletedset = set(deleted)
136 deletedset = set(deleted)
137 d = mf1.diff(mf2, match=match, clean=listclean)
137 d = mf1.diff(mf2, match=match, clean=listclean)
138 for fn, value in d.iteritems():
138 for fn, value in d.iteritems():
139 if fn in deletedset:
139 if fn in deletedset:
140 continue
140 continue
141 if value is None:
141 if value is None:
142 clean.append(fn)
142 clean.append(fn)
143 continue
143 continue
144 (node1, flag1), (node2, flag2) = value
144 (node1, flag1), (node2, flag2) = value
145 if node1 is None:
145 if node1 is None:
146 added.append(fn)
146 added.append(fn)
147 elif node2 is None:
147 elif node2 is None:
148 removed.append(fn)
148 removed.append(fn)
149 elif flag1 != flag2:
149 elif flag1 != flag2:
150 modified.append(fn)
150 modified.append(fn)
151 elif node2 not in wdirnodes:
151 elif node2 not in wdirnodes:
152 # When comparing files between two commits, we save time by
152 # When comparing files between two commits, we save time by
153 # not comparing the file contents when the nodeids differ.
153 # not comparing the file contents when the nodeids differ.
154 # Note that this means we incorrectly report a reverted change
154 # Note that this means we incorrectly report a reverted change
155 # to a file as a modification.
155 # to a file as a modification.
156 modified.append(fn)
156 modified.append(fn)
157 elif self[fn].cmp(other[fn]):
157 elif self[fn].cmp(other[fn]):
158 modified.append(fn)
158 modified.append(fn)
159 else:
159 else:
160 clean.append(fn)
160 clean.append(fn)
161
161
162 if removed:
162 if removed:
163 # need to filter files if they are already reported as removed
163 # need to filter files if they are already reported as removed
164 unknown = [fn for fn in unknown if fn not in mf1 and
164 unknown = [fn for fn in unknown if fn not in mf1 and
165 (not match or match(fn))]
165 (not match or match(fn))]
166 ignored = [fn for fn in ignored if fn not in mf1 and
166 ignored = [fn for fn in ignored if fn not in mf1 and
167 (not match or match(fn))]
167 (not match or match(fn))]
168 # if they're deleted, don't report them as removed
168 # if they're deleted, don't report them as removed
169 removed = [fn for fn in removed if fn not in deletedset]
169 removed = [fn for fn in removed if fn not in deletedset]
170
170
171 return scmutil.status(modified, added, removed, deleted, unknown,
171 return scmutil.status(modified, added, removed, deleted, unknown,
172 ignored, clean)
172 ignored, clean)
173
173
174 @propertycache
174 @propertycache
175 def substate(self):
175 def substate(self):
176 return subrepo.state(self, self._repo.ui)
176 return subrepo.state(self, self._repo.ui)
177
177
178 def subrev(self, subpath):
178 def subrev(self, subpath):
179 return self.substate[subpath][1]
179 return self.substate[subpath][1]
180
180
181 def rev(self):
181 def rev(self):
182 return self._rev
182 return self._rev
183 def node(self):
183 def node(self):
184 return self._node
184 return self._node
185 def hex(self):
185 def hex(self):
186 return hex(self.node())
186 return hex(self.node())
187 def manifest(self):
187 def manifest(self):
188 return self._manifest
188 return self._manifest
189 def manifestctx(self):
189 def manifestctx(self):
190 return self._manifestctx
190 return self._manifestctx
191 def repo(self):
191 def repo(self):
192 return self._repo
192 return self._repo
193 def phasestr(self):
193 def phasestr(self):
194 return phases.phasenames[self.phase()]
194 return phases.phasenames[self.phase()]
195 def mutable(self):
195 def mutable(self):
196 return self.phase() > phases.public
196 return self.phase() > phases.public
197
197
198 def getfileset(self, expr):
198 def getfileset(self, expr):
199 return fileset.getfileset(self, expr)
199 return fileset.getfileset(self, expr)
200
200
201 def obsolete(self):
201 def obsolete(self):
202 """True if the changeset is obsolete"""
202 """True if the changeset is obsolete"""
203 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
203 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
204
204
205 def extinct(self):
205 def extinct(self):
206 """True if the changeset is extinct"""
206 """True if the changeset is extinct"""
207 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
207 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
208
208
209 def unstable(self):
210 msg = ("'context.unstable' is deprecated, "
211 "use 'context.orphan'")
212 self._repo.ui.deprecwarn(msg, '4.4')
213 return self.orphan()
214
215 def orphan(self):
209 def orphan(self):
216 """True if the changeset is not obsolete but it's ancestor are"""
210 """True if the changeset is not obsolete but it's ancestor are"""
217 return self.rev() in obsmod.getrevs(self._repo, 'orphan')
211 return self.rev() in obsmod.getrevs(self._repo, 'orphan')
218
212
219 def bumped(self):
220 msg = ("'context.bumped' is deprecated, "
221 "use 'context.phasedivergent'")
222 self._repo.ui.deprecwarn(msg, '4.4')
223 return self.phasedivergent()
224
225 def phasedivergent(self):
213 def phasedivergent(self):
226 """True if the changeset try to be a successor of a public changeset
214 """True if the changeset try to be a successor of a public changeset
227
215
228 Only non-public and non-obsolete changesets may be bumped.
216 Only non-public and non-obsolete changesets may be bumped.
229 """
217 """
230 return self.rev() in obsmod.getrevs(self._repo, 'phasedivergent')
218 return self.rev() in obsmod.getrevs(self._repo, 'phasedivergent')
231
219
232 def divergent(self):
233 msg = ("'context.divergent' is deprecated, "
234 "use 'context.contentdivergent'")
235 self._repo.ui.deprecwarn(msg, '4.4')
236 return self.contentdivergent()
237
238 def contentdivergent(self):
220 def contentdivergent(self):
239 """Is a successors of a changeset with multiple possible successors set
221 """Is a successors of a changeset with multiple possible successors set
240
222
241 Only non-public and non-obsolete changesets may be divergent.
223 Only non-public and non-obsolete changesets may be divergent.
242 """
224 """
243 return self.rev() in obsmod.getrevs(self._repo, 'contentdivergent')
225 return self.rev() in obsmod.getrevs(self._repo, 'contentdivergent')
244
226
245 def troubled(self):
246 msg = ("'context.troubled' is deprecated, "
247 "use 'context.isunstable'")
248 self._repo.ui.deprecwarn(msg, '4.4')
249 return self.isunstable()
250
251 def isunstable(self):
227 def isunstable(self):
252 """True if the changeset is either unstable, bumped or divergent"""
228 """True if the changeset is either unstable, bumped or divergent"""
253 return self.orphan() or self.phasedivergent() or self.contentdivergent()
229 return self.orphan() or self.phasedivergent() or self.contentdivergent()
254
230
255 def troubles(self):
256 """Keep the old version around in order to avoid breaking extensions
257 about different return values.
258 """
259 msg = ("'context.troubles' is deprecated, "
260 "use 'context.instabilities'")
261 self._repo.ui.deprecwarn(msg, '4.4')
262
263 troubles = []
264 if self.orphan():
265 troubles.append('orphan')
266 if self.phasedivergent():
267 troubles.append('bumped')
268 if self.contentdivergent():
269 troubles.append('divergent')
270 return troubles
271
272 def instabilities(self):
231 def instabilities(self):
273 """return the list of instabilities affecting this changeset.
232 """return the list of instabilities affecting this changeset.
274
233
275 Instabilities are returned as strings. possible values are:
234 Instabilities are returned as strings. possible values are:
276 - orphan,
235 - orphan,
277 - phase-divergent,
236 - phase-divergent,
278 - content-divergent.
237 - content-divergent.
279 """
238 """
280 instabilities = []
239 instabilities = []
281 if self.orphan():
240 if self.orphan():
282 instabilities.append('orphan')
241 instabilities.append('orphan')
283 if self.phasedivergent():
242 if self.phasedivergent():
284 instabilities.append('phase-divergent')
243 instabilities.append('phase-divergent')
285 if self.contentdivergent():
244 if self.contentdivergent():
286 instabilities.append('content-divergent')
245 instabilities.append('content-divergent')
287 return instabilities
246 return instabilities
288
247
289 def parents(self):
248 def parents(self):
290 """return contexts for each parent changeset"""
249 """return contexts for each parent changeset"""
291 return self._parents
250 return self._parents
292
251
293 def p1(self):
252 def p1(self):
294 return self._parents[0]
253 return self._parents[0]
295
254
296 def p2(self):
255 def p2(self):
297 parents = self._parents
256 parents = self._parents
298 if len(parents) == 2:
257 if len(parents) == 2:
299 return parents[1]
258 return parents[1]
300 return changectx(self._repo, nullrev)
259 return changectx(self._repo, nullrev)
301
260
302 def _fileinfo(self, path):
261 def _fileinfo(self, path):
303 if r'_manifest' in self.__dict__:
262 if r'_manifest' in self.__dict__:
304 try:
263 try:
305 return self._manifest[path], self._manifest.flags(path)
264 return self._manifest[path], self._manifest.flags(path)
306 except KeyError:
265 except KeyError:
307 raise error.ManifestLookupError(self._node, path,
266 raise error.ManifestLookupError(self._node, path,
308 _('not found in manifest'))
267 _('not found in manifest'))
309 if r'_manifestdelta' in self.__dict__ or path in self.files():
268 if r'_manifestdelta' in self.__dict__ or path in self.files():
310 if path in self._manifestdelta:
269 if path in self._manifestdelta:
311 return (self._manifestdelta[path],
270 return (self._manifestdelta[path],
312 self._manifestdelta.flags(path))
271 self._manifestdelta.flags(path))
313 mfl = self._repo.manifestlog
272 mfl = self._repo.manifestlog
314 try:
273 try:
315 node, flag = mfl[self._changeset.manifest].find(path)
274 node, flag = mfl[self._changeset.manifest].find(path)
316 except KeyError:
275 except KeyError:
317 raise error.ManifestLookupError(self._node, path,
276 raise error.ManifestLookupError(self._node, path,
318 _('not found in manifest'))
277 _('not found in manifest'))
319
278
320 return node, flag
279 return node, flag
321
280
322 def filenode(self, path):
281 def filenode(self, path):
323 return self._fileinfo(path)[0]
282 return self._fileinfo(path)[0]
324
283
325 def flags(self, path):
284 def flags(self, path):
326 try:
285 try:
327 return self._fileinfo(path)[1]
286 return self._fileinfo(path)[1]
328 except error.LookupError:
287 except error.LookupError:
329 return ''
288 return ''
330
289
331 def sub(self, path, allowcreate=True):
290 def sub(self, path, allowcreate=True):
332 '''return a subrepo for the stored revision of path, never wdir()'''
291 '''return a subrepo for the stored revision of path, never wdir()'''
333 return subrepo.subrepo(self, path, allowcreate=allowcreate)
292 return subrepo.subrepo(self, path, allowcreate=allowcreate)
334
293
335 def nullsub(self, path, pctx):
294 def nullsub(self, path, pctx):
336 return subrepo.nullsubrepo(self, path, pctx)
295 return subrepo.nullsubrepo(self, path, pctx)
337
296
338 def workingsub(self, path):
297 def workingsub(self, path):
339 '''return a subrepo for the stored revision, or wdir if this is a wdir
298 '''return a subrepo for the stored revision, or wdir if this is a wdir
340 context.
299 context.
341 '''
300 '''
342 return subrepo.subrepo(self, path, allowwdir=True)
301 return subrepo.subrepo(self, path, allowwdir=True)
343
302
344 def match(self, pats=None, include=None, exclude=None, default='glob',
303 def match(self, pats=None, include=None, exclude=None, default='glob',
345 listsubrepos=False, badfn=None):
304 listsubrepos=False, badfn=None):
346 r = self._repo
305 r = self._repo
347 return matchmod.match(r.root, r.getcwd(), pats,
306 return matchmod.match(r.root, r.getcwd(), pats,
348 include, exclude, default,
307 include, exclude, default,
349 auditor=r.nofsauditor, ctx=self,
308 auditor=r.nofsauditor, ctx=self,
350 listsubrepos=listsubrepos, badfn=badfn)
309 listsubrepos=listsubrepos, badfn=badfn)
351
310
352 def diff(self, ctx2=None, match=None, **opts):
311 def diff(self, ctx2=None, match=None, **opts):
353 """Returns a diff generator for the given contexts and matcher"""
312 """Returns a diff generator for the given contexts and matcher"""
354 if ctx2 is None:
313 if ctx2 is None:
355 ctx2 = self.p1()
314 ctx2 = self.p1()
356 if ctx2 is not None:
315 if ctx2 is not None:
357 ctx2 = self._repo[ctx2]
316 ctx2 = self._repo[ctx2]
358 diffopts = patch.diffopts(self._repo.ui, pycompat.byteskwargs(opts))
317 diffopts = patch.diffopts(self._repo.ui, pycompat.byteskwargs(opts))
359 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
318 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
360
319
361 def dirs(self):
320 def dirs(self):
362 return self._manifest.dirs()
321 return self._manifest.dirs()
363
322
364 def hasdir(self, dir):
323 def hasdir(self, dir):
365 return self._manifest.hasdir(dir)
324 return self._manifest.hasdir(dir)
366
325
367 def status(self, other=None, match=None, listignored=False,
326 def status(self, other=None, match=None, listignored=False,
368 listclean=False, listunknown=False, listsubrepos=False):
327 listclean=False, listunknown=False, listsubrepos=False):
369 """return status of files between two nodes or node and working
328 """return status of files between two nodes or node and working
370 directory.
329 directory.
371
330
372 If other is None, compare this node with working directory.
331 If other is None, compare this node with working directory.
373
332
374 returns (modified, added, removed, deleted, unknown, ignored, clean)
333 returns (modified, added, removed, deleted, unknown, ignored, clean)
375 """
334 """
376
335
377 ctx1 = self
336 ctx1 = self
378 ctx2 = self._repo[other]
337 ctx2 = self._repo[other]
379
338
380 # This next code block is, admittedly, fragile logic that tests for
339 # This next code block is, admittedly, fragile logic that tests for
381 # reversing the contexts and wouldn't need to exist if it weren't for
340 # reversing the contexts and wouldn't need to exist if it weren't for
382 # the fast (and common) code path of comparing the working directory
341 # the fast (and common) code path of comparing the working directory
383 # with its first parent.
342 # with its first parent.
384 #
343 #
385 # What we're aiming for here is the ability to call:
344 # What we're aiming for here is the ability to call:
386 #
345 #
387 # workingctx.status(parentctx)
346 # workingctx.status(parentctx)
388 #
347 #
389 # If we always built the manifest for each context and compared those,
348 # If we always built the manifest for each context and compared those,
390 # then we'd be done. But the special case of the above call means we
349 # then we'd be done. But the special case of the above call means we
391 # just copy the manifest of the parent.
350 # just copy the manifest of the parent.
392 reversed = False
351 reversed = False
393 if (not isinstance(ctx1, changectx)
352 if (not isinstance(ctx1, changectx)
394 and isinstance(ctx2, changectx)):
353 and isinstance(ctx2, changectx)):
395 reversed = True
354 reversed = True
396 ctx1, ctx2 = ctx2, ctx1
355 ctx1, ctx2 = ctx2, ctx1
397
356
398 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
357 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
399 match = ctx2._matchstatus(ctx1, match)
358 match = ctx2._matchstatus(ctx1, match)
400 r = scmutil.status([], [], [], [], [], [], [])
359 r = scmutil.status([], [], [], [], [], [], [])
401 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
360 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
402 listunknown)
361 listunknown)
403
362
404 if reversed:
363 if reversed:
405 # Reverse added and removed. Clear deleted, unknown and ignored as
364 # Reverse added and removed. Clear deleted, unknown and ignored as
406 # these make no sense to reverse.
365 # these make no sense to reverse.
407 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
366 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
408 r.clean)
367 r.clean)
409
368
410 if listsubrepos:
369 if listsubrepos:
411 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
370 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
412 try:
371 try:
413 rev2 = ctx2.subrev(subpath)
372 rev2 = ctx2.subrev(subpath)
414 except KeyError:
373 except KeyError:
415 # A subrepo that existed in node1 was deleted between
374 # A subrepo that existed in node1 was deleted between
416 # node1 and node2 (inclusive). Thus, ctx2's substate
375 # node1 and node2 (inclusive). Thus, ctx2's substate
417 # won't contain that subpath. The best we can do ignore it.
376 # won't contain that subpath. The best we can do ignore it.
418 rev2 = None
377 rev2 = None
419 submatch = matchmod.subdirmatcher(subpath, match)
378 submatch = matchmod.subdirmatcher(subpath, match)
420 s = sub.status(rev2, match=submatch, ignored=listignored,
379 s = sub.status(rev2, match=submatch, ignored=listignored,
421 clean=listclean, unknown=listunknown,
380 clean=listclean, unknown=listunknown,
422 listsubrepos=True)
381 listsubrepos=True)
423 for rfiles, sfiles in zip(r, s):
382 for rfiles, sfiles in zip(r, s):
424 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
383 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
425
384
426 for l in r:
385 for l in r:
427 l.sort()
386 l.sort()
428
387
429 return r
388 return r
430
389
431 def _filterederror(repo, changeid):
390 def _filterederror(repo, changeid):
432 """build an exception to be raised about a filtered changeid
391 """build an exception to be raised about a filtered changeid
433
392
434 This is extracted in a function to help extensions (eg: evolve) to
393 This is extracted in a function to help extensions (eg: evolve) to
435 experiment with various message variants."""
394 experiment with various message variants."""
436 if repo.filtername.startswith('visible'):
395 if repo.filtername.startswith('visible'):
437
396
438 # Check if the changeset is obsolete
397 # Check if the changeset is obsolete
439 unfilteredrepo = repo.unfiltered()
398 unfilteredrepo = repo.unfiltered()
440 ctx = unfilteredrepo[changeid]
399 ctx = unfilteredrepo[changeid]
441
400
442 # If the changeset is obsolete, enrich the message with the reason
401 # If the changeset is obsolete, enrich the message with the reason
443 # that made this changeset not visible
402 # that made this changeset not visible
444 if ctx.obsolete():
403 if ctx.obsolete():
445 msg = obsutil._getfilteredreason(repo, changeid, ctx)
404 msg = obsutil._getfilteredreason(repo, changeid, ctx)
446 else:
405 else:
447 msg = _("hidden revision '%s'") % changeid
406 msg = _("hidden revision '%s'") % changeid
448
407
449 hint = _('use --hidden to access hidden revisions')
408 hint = _('use --hidden to access hidden revisions')
450
409
451 return error.FilteredRepoLookupError(msg, hint=hint)
410 return error.FilteredRepoLookupError(msg, hint=hint)
452 msg = _("filtered revision '%s' (not in '%s' subset)")
411 msg = _("filtered revision '%s' (not in '%s' subset)")
453 msg %= (changeid, repo.filtername)
412 msg %= (changeid, repo.filtername)
454 return error.FilteredRepoLookupError(msg)
413 return error.FilteredRepoLookupError(msg)
455
414
456 class changectx(basectx):
415 class changectx(basectx):
457 """A changecontext object makes access to data related to a particular
416 """A changecontext object makes access to data related to a particular
458 changeset convenient. It represents a read-only context already present in
417 changeset convenient. It represents a read-only context already present in
459 the repo."""
418 the repo."""
460 def __init__(self, repo, changeid=''):
419 def __init__(self, repo, changeid=''):
461 """changeid is a revision number, node, or tag"""
420 """changeid is a revision number, node, or tag"""
462
421
463 # since basectx.__new__ already took care of copying the object, we
422 # since basectx.__new__ already took care of copying the object, we
464 # don't need to do anything in __init__, so we just exit here
423 # don't need to do anything in __init__, so we just exit here
465 if isinstance(changeid, basectx):
424 if isinstance(changeid, basectx):
466 return
425 return
467
426
468 if changeid == '':
427 if changeid == '':
469 changeid = '.'
428 changeid = '.'
470 self._repo = repo
429 self._repo = repo
471
430
472 try:
431 try:
473 if isinstance(changeid, int):
432 if isinstance(changeid, int):
474 self._node = repo.changelog.node(changeid)
433 self._node = repo.changelog.node(changeid)
475 self._rev = changeid
434 self._rev = changeid
476 return
435 return
477 if not pycompat.ispy3 and isinstance(changeid, long):
436 if not pycompat.ispy3 and isinstance(changeid, long):
478 changeid = str(changeid)
437 changeid = str(changeid)
479 if changeid == 'null':
438 if changeid == 'null':
480 self._node = nullid
439 self._node = nullid
481 self._rev = nullrev
440 self._rev = nullrev
482 return
441 return
483 if changeid == 'tip':
442 if changeid == 'tip':
484 self._node = repo.changelog.tip()
443 self._node = repo.changelog.tip()
485 self._rev = repo.changelog.rev(self._node)
444 self._rev = repo.changelog.rev(self._node)
486 return
445 return
487 if (changeid == '.'
446 if (changeid == '.'
488 or repo.local() and changeid == repo.dirstate.p1()):
447 or repo.local() and changeid == repo.dirstate.p1()):
489 # this is a hack to delay/avoid loading obsmarkers
448 # this is a hack to delay/avoid loading obsmarkers
490 # when we know that '.' won't be hidden
449 # when we know that '.' won't be hidden
491 self._node = repo.dirstate.p1()
450 self._node = repo.dirstate.p1()
492 self._rev = repo.unfiltered().changelog.rev(self._node)
451 self._rev = repo.unfiltered().changelog.rev(self._node)
493 return
452 return
494 if len(changeid) == 20:
453 if len(changeid) == 20:
495 try:
454 try:
496 self._node = changeid
455 self._node = changeid
497 self._rev = repo.changelog.rev(changeid)
456 self._rev = repo.changelog.rev(changeid)
498 return
457 return
499 except error.FilteredRepoLookupError:
458 except error.FilteredRepoLookupError:
500 raise
459 raise
501 except LookupError:
460 except LookupError:
502 pass
461 pass
503
462
504 try:
463 try:
505 r = int(changeid)
464 r = int(changeid)
506 if '%d' % r != changeid:
465 if '%d' % r != changeid:
507 raise ValueError
466 raise ValueError
508 l = len(repo.changelog)
467 l = len(repo.changelog)
509 if r < 0:
468 if r < 0:
510 r += l
469 r += l
511 if r < 0 or r >= l and r != wdirrev:
470 if r < 0 or r >= l and r != wdirrev:
512 raise ValueError
471 raise ValueError
513 self._rev = r
472 self._rev = r
514 self._node = repo.changelog.node(r)
473 self._node = repo.changelog.node(r)
515 return
474 return
516 except error.FilteredIndexError:
475 except error.FilteredIndexError:
517 raise
476 raise
518 except (ValueError, OverflowError, IndexError):
477 except (ValueError, OverflowError, IndexError):
519 pass
478 pass
520
479
521 if len(changeid) == 40:
480 if len(changeid) == 40:
522 try:
481 try:
523 self._node = bin(changeid)
482 self._node = bin(changeid)
524 self._rev = repo.changelog.rev(self._node)
483 self._rev = repo.changelog.rev(self._node)
525 return
484 return
526 except error.FilteredLookupError:
485 except error.FilteredLookupError:
527 raise
486 raise
528 except (TypeError, LookupError):
487 except (TypeError, LookupError):
529 pass
488 pass
530
489
531 # lookup bookmarks through the name interface
490 # lookup bookmarks through the name interface
532 try:
491 try:
533 self._node = repo.names.singlenode(repo, changeid)
492 self._node = repo.names.singlenode(repo, changeid)
534 self._rev = repo.changelog.rev(self._node)
493 self._rev = repo.changelog.rev(self._node)
535 return
494 return
536 except KeyError:
495 except KeyError:
537 pass
496 pass
538 except error.FilteredRepoLookupError:
497 except error.FilteredRepoLookupError:
539 raise
498 raise
540 except error.RepoLookupError:
499 except error.RepoLookupError:
541 pass
500 pass
542
501
543 self._node = repo.unfiltered().changelog._partialmatch(changeid)
502 self._node = repo.unfiltered().changelog._partialmatch(changeid)
544 if self._node is not None:
503 if self._node is not None:
545 self._rev = repo.changelog.rev(self._node)
504 self._rev = repo.changelog.rev(self._node)
546 return
505 return
547
506
548 # lookup failed
507 # lookup failed
549 # check if it might have come from damaged dirstate
508 # check if it might have come from damaged dirstate
550 #
509 #
551 # XXX we could avoid the unfiltered if we had a recognizable
510 # XXX we could avoid the unfiltered if we had a recognizable
552 # exception for filtered changeset access
511 # exception for filtered changeset access
553 if (repo.local()
512 if (repo.local()
554 and changeid in repo.unfiltered().dirstate.parents()):
513 and changeid in repo.unfiltered().dirstate.parents()):
555 msg = _("working directory has unknown parent '%s'!")
514 msg = _("working directory has unknown parent '%s'!")
556 raise error.Abort(msg % short(changeid))
515 raise error.Abort(msg % short(changeid))
557 try:
516 try:
558 if len(changeid) == 20 and nonascii(changeid):
517 if len(changeid) == 20 and nonascii(changeid):
559 changeid = hex(changeid)
518 changeid = hex(changeid)
560 except TypeError:
519 except TypeError:
561 pass
520 pass
562 except (error.FilteredIndexError, error.FilteredLookupError,
521 except (error.FilteredIndexError, error.FilteredLookupError,
563 error.FilteredRepoLookupError):
522 error.FilteredRepoLookupError):
564 raise _filterederror(repo, changeid)
523 raise _filterederror(repo, changeid)
565 except IndexError:
524 except IndexError:
566 pass
525 pass
567 raise error.RepoLookupError(
526 raise error.RepoLookupError(
568 _("unknown revision '%s'") % changeid)
527 _("unknown revision '%s'") % changeid)
569
528
570 def __hash__(self):
529 def __hash__(self):
571 try:
530 try:
572 return hash(self._rev)
531 return hash(self._rev)
573 except AttributeError:
532 except AttributeError:
574 return id(self)
533 return id(self)
575
534
576 def __nonzero__(self):
535 def __nonzero__(self):
577 return self._rev != nullrev
536 return self._rev != nullrev
578
537
579 __bool__ = __nonzero__
538 __bool__ = __nonzero__
580
539
581 @propertycache
540 @propertycache
582 def _changeset(self):
541 def _changeset(self):
583 return self._repo.changelog.changelogrevision(self.rev())
542 return self._repo.changelog.changelogrevision(self.rev())
584
543
585 @propertycache
544 @propertycache
586 def _manifest(self):
545 def _manifest(self):
587 return self._manifestctx.read()
546 return self._manifestctx.read()
588
547
589 @property
548 @property
590 def _manifestctx(self):
549 def _manifestctx(self):
591 return self._repo.manifestlog[self._changeset.manifest]
550 return self._repo.manifestlog[self._changeset.manifest]
592
551
593 @propertycache
552 @propertycache
594 def _manifestdelta(self):
553 def _manifestdelta(self):
595 return self._manifestctx.readdelta()
554 return self._manifestctx.readdelta()
596
555
597 @propertycache
556 @propertycache
598 def _parents(self):
557 def _parents(self):
599 repo = self._repo
558 repo = self._repo
600 p1, p2 = repo.changelog.parentrevs(self._rev)
559 p1, p2 = repo.changelog.parentrevs(self._rev)
601 if p2 == nullrev:
560 if p2 == nullrev:
602 return [changectx(repo, p1)]
561 return [changectx(repo, p1)]
603 return [changectx(repo, p1), changectx(repo, p2)]
562 return [changectx(repo, p1), changectx(repo, p2)]
604
563
605 def changeset(self):
564 def changeset(self):
606 c = self._changeset
565 c = self._changeset
607 return (
566 return (
608 c.manifest,
567 c.manifest,
609 c.user,
568 c.user,
610 c.date,
569 c.date,
611 c.files,
570 c.files,
612 c.description,
571 c.description,
613 c.extra,
572 c.extra,
614 )
573 )
615 def manifestnode(self):
574 def manifestnode(self):
616 return self._changeset.manifest
575 return self._changeset.manifest
617
576
618 def user(self):
577 def user(self):
619 return self._changeset.user
578 return self._changeset.user
620 def date(self):
579 def date(self):
621 return self._changeset.date
580 return self._changeset.date
622 def files(self):
581 def files(self):
623 return self._changeset.files
582 return self._changeset.files
624 def description(self):
583 def description(self):
625 return self._changeset.description
584 return self._changeset.description
626 def branch(self):
585 def branch(self):
627 return encoding.tolocal(self._changeset.extra.get("branch"))
586 return encoding.tolocal(self._changeset.extra.get("branch"))
628 def closesbranch(self):
587 def closesbranch(self):
629 return 'close' in self._changeset.extra
588 return 'close' in self._changeset.extra
630 def extra(self):
589 def extra(self):
631 """Return a dict of extra information."""
590 """Return a dict of extra information."""
632 return self._changeset.extra
591 return self._changeset.extra
633 def tags(self):
592 def tags(self):
634 """Return a list of byte tag names"""
593 """Return a list of byte tag names"""
635 return self._repo.nodetags(self._node)
594 return self._repo.nodetags(self._node)
636 def bookmarks(self):
595 def bookmarks(self):
637 """Return a list of byte bookmark names."""
596 """Return a list of byte bookmark names."""
638 return self._repo.nodebookmarks(self._node)
597 return self._repo.nodebookmarks(self._node)
639 def phase(self):
598 def phase(self):
640 return self._repo._phasecache.phase(self._repo, self._rev)
599 return self._repo._phasecache.phase(self._repo, self._rev)
641 def hidden(self):
600 def hidden(self):
642 return self._rev in repoview.filterrevs(self._repo, 'visible')
601 return self._rev in repoview.filterrevs(self._repo, 'visible')
643
602
644 def isinmemory(self):
603 def isinmemory(self):
645 return False
604 return False
646
605
647 def children(self):
606 def children(self):
648 """return list of changectx contexts for each child changeset.
607 """return list of changectx contexts for each child changeset.
649
608
650 This returns only the immediate child changesets. Use descendants() to
609 This returns only the immediate child changesets. Use descendants() to
651 recursively walk children.
610 recursively walk children.
652 """
611 """
653 c = self._repo.changelog.children(self._node)
612 c = self._repo.changelog.children(self._node)
654 return [changectx(self._repo, x) for x in c]
613 return [changectx(self._repo, x) for x in c]
655
614
656 def ancestors(self):
615 def ancestors(self):
657 for a in self._repo.changelog.ancestors([self._rev]):
616 for a in self._repo.changelog.ancestors([self._rev]):
658 yield changectx(self._repo, a)
617 yield changectx(self._repo, a)
659
618
660 def descendants(self):
619 def descendants(self):
661 """Recursively yield all children of the changeset.
620 """Recursively yield all children of the changeset.
662
621
663 For just the immediate children, use children()
622 For just the immediate children, use children()
664 """
623 """
665 for d in self._repo.changelog.descendants([self._rev]):
624 for d in self._repo.changelog.descendants([self._rev]):
666 yield changectx(self._repo, d)
625 yield changectx(self._repo, d)
667
626
668 def filectx(self, path, fileid=None, filelog=None):
627 def filectx(self, path, fileid=None, filelog=None):
669 """get a file context from this changeset"""
628 """get a file context from this changeset"""
670 if fileid is None:
629 if fileid is None:
671 fileid = self.filenode(path)
630 fileid = self.filenode(path)
672 return filectx(self._repo, path, fileid=fileid,
631 return filectx(self._repo, path, fileid=fileid,
673 changectx=self, filelog=filelog)
632 changectx=self, filelog=filelog)
674
633
675 def ancestor(self, c2, warn=False):
634 def ancestor(self, c2, warn=False):
676 """return the "best" ancestor context of self and c2
635 """return the "best" ancestor context of self and c2
677
636
678 If there are multiple candidates, it will show a message and check
637 If there are multiple candidates, it will show a message and check
679 merge.preferancestor configuration before falling back to the
638 merge.preferancestor configuration before falling back to the
680 revlog ancestor."""
639 revlog ancestor."""
681 # deal with workingctxs
640 # deal with workingctxs
682 n2 = c2._node
641 n2 = c2._node
683 if n2 is None:
642 if n2 is None:
684 n2 = c2._parents[0]._node
643 n2 = c2._parents[0]._node
685 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
644 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
686 if not cahs:
645 if not cahs:
687 anc = nullid
646 anc = nullid
688 elif len(cahs) == 1:
647 elif len(cahs) == 1:
689 anc = cahs[0]
648 anc = cahs[0]
690 else:
649 else:
691 # experimental config: merge.preferancestor
650 # experimental config: merge.preferancestor
692 for r in self._repo.ui.configlist('merge', 'preferancestor'):
651 for r in self._repo.ui.configlist('merge', 'preferancestor'):
693 try:
652 try:
694 ctx = changectx(self._repo, r)
653 ctx = changectx(self._repo, r)
695 except error.RepoLookupError:
654 except error.RepoLookupError:
696 continue
655 continue
697 anc = ctx.node()
656 anc = ctx.node()
698 if anc in cahs:
657 if anc in cahs:
699 break
658 break
700 else:
659 else:
701 anc = self._repo.changelog.ancestor(self._node, n2)
660 anc = self._repo.changelog.ancestor(self._node, n2)
702 if warn:
661 if warn:
703 self._repo.ui.status(
662 self._repo.ui.status(
704 (_("note: using %s as ancestor of %s and %s\n") %
663 (_("note: using %s as ancestor of %s and %s\n") %
705 (short(anc), short(self._node), short(n2))) +
664 (short(anc), short(self._node), short(n2))) +
706 ''.join(_(" alternatively, use --config "
665 ''.join(_(" alternatively, use --config "
707 "merge.preferancestor=%s\n") %
666 "merge.preferancestor=%s\n") %
708 short(n) for n in sorted(cahs) if n != anc))
667 short(n) for n in sorted(cahs) if n != anc))
709 return changectx(self._repo, anc)
668 return changectx(self._repo, anc)
710
669
711 def descendant(self, other):
670 def descendant(self, other):
712 """True if other is descendant of this changeset"""
671 """True if other is descendant of this changeset"""
713 return self._repo.changelog.descendant(self._rev, other._rev)
672 return self._repo.changelog.descendant(self._rev, other._rev)
714
673
715 def walk(self, match):
674 def walk(self, match):
716 '''Generates matching file names.'''
675 '''Generates matching file names.'''
717
676
718 # Wrap match.bad method to have message with nodeid
677 # Wrap match.bad method to have message with nodeid
719 def bad(fn, msg):
678 def bad(fn, msg):
720 # The manifest doesn't know about subrepos, so don't complain about
679 # The manifest doesn't know about subrepos, so don't complain about
721 # paths into valid subrepos.
680 # paths into valid subrepos.
722 if any(fn == s or fn.startswith(s + '/')
681 if any(fn == s or fn.startswith(s + '/')
723 for s in self.substate):
682 for s in self.substate):
724 return
683 return
725 match.bad(fn, _('no such file in rev %s') % self)
684 match.bad(fn, _('no such file in rev %s') % self)
726
685
727 m = matchmod.badmatch(match, bad)
686 m = matchmod.badmatch(match, bad)
728 return self._manifest.walk(m)
687 return self._manifest.walk(m)
729
688
730 def matches(self, match):
689 def matches(self, match):
731 return self.walk(match)
690 return self.walk(match)
732
691
733 class basefilectx(object):
692 class basefilectx(object):
734 """A filecontext object represents the common logic for its children:
693 """A filecontext object represents the common logic for its children:
735 filectx: read-only access to a filerevision that is already present
694 filectx: read-only access to a filerevision that is already present
736 in the repo,
695 in the repo,
737 workingfilectx: a filecontext that represents files from the working
696 workingfilectx: a filecontext that represents files from the working
738 directory,
697 directory,
739 memfilectx: a filecontext that represents files in-memory,
698 memfilectx: a filecontext that represents files in-memory,
740 overlayfilectx: duplicate another filecontext with some fields overridden.
699 overlayfilectx: duplicate another filecontext with some fields overridden.
741 """
700 """
742 @propertycache
701 @propertycache
743 def _filelog(self):
702 def _filelog(self):
744 return self._repo.file(self._path)
703 return self._repo.file(self._path)
745
704
746 @propertycache
705 @propertycache
747 def _changeid(self):
706 def _changeid(self):
748 if r'_changeid' in self.__dict__:
707 if r'_changeid' in self.__dict__:
749 return self._changeid
708 return self._changeid
750 elif r'_changectx' in self.__dict__:
709 elif r'_changectx' in self.__dict__:
751 return self._changectx.rev()
710 return self._changectx.rev()
752 elif r'_descendantrev' in self.__dict__:
711 elif r'_descendantrev' in self.__dict__:
753 # this file context was created from a revision with a known
712 # this file context was created from a revision with a known
754 # descendant, we can (lazily) correct for linkrev aliases
713 # descendant, we can (lazily) correct for linkrev aliases
755 return self._adjustlinkrev(self._descendantrev)
714 return self._adjustlinkrev(self._descendantrev)
756 else:
715 else:
757 return self._filelog.linkrev(self._filerev)
716 return self._filelog.linkrev(self._filerev)
758
717
759 @propertycache
718 @propertycache
760 def _filenode(self):
719 def _filenode(self):
761 if r'_fileid' in self.__dict__:
720 if r'_fileid' in self.__dict__:
762 return self._filelog.lookup(self._fileid)
721 return self._filelog.lookup(self._fileid)
763 else:
722 else:
764 return self._changectx.filenode(self._path)
723 return self._changectx.filenode(self._path)
765
724
766 @propertycache
725 @propertycache
767 def _filerev(self):
726 def _filerev(self):
768 return self._filelog.rev(self._filenode)
727 return self._filelog.rev(self._filenode)
769
728
770 @propertycache
729 @propertycache
771 def _repopath(self):
730 def _repopath(self):
772 return self._path
731 return self._path
773
732
774 def __nonzero__(self):
733 def __nonzero__(self):
775 try:
734 try:
776 self._filenode
735 self._filenode
777 return True
736 return True
778 except error.LookupError:
737 except error.LookupError:
779 # file is missing
738 # file is missing
780 return False
739 return False
781
740
782 __bool__ = __nonzero__
741 __bool__ = __nonzero__
783
742
784 def __bytes__(self):
743 def __bytes__(self):
785 try:
744 try:
786 return "%s@%s" % (self.path(), self._changectx)
745 return "%s@%s" % (self.path(), self._changectx)
787 except error.LookupError:
746 except error.LookupError:
788 return "%s@???" % self.path()
747 return "%s@???" % self.path()
789
748
790 __str__ = encoding.strmethod(__bytes__)
749 __str__ = encoding.strmethod(__bytes__)
791
750
792 def __repr__(self):
751 def __repr__(self):
793 return "<%s %s>" % (type(self).__name__, str(self))
752 return "<%s %s>" % (type(self).__name__, str(self))
794
753
795 def __hash__(self):
754 def __hash__(self):
796 try:
755 try:
797 return hash((self._path, self._filenode))
756 return hash((self._path, self._filenode))
798 except AttributeError:
757 except AttributeError:
799 return id(self)
758 return id(self)
800
759
801 def __eq__(self, other):
760 def __eq__(self, other):
802 try:
761 try:
803 return (type(self) == type(other) and self._path == other._path
762 return (type(self) == type(other) and self._path == other._path
804 and self._filenode == other._filenode)
763 and self._filenode == other._filenode)
805 except AttributeError:
764 except AttributeError:
806 return False
765 return False
807
766
808 def __ne__(self, other):
767 def __ne__(self, other):
809 return not (self == other)
768 return not (self == other)
810
769
811 def filerev(self):
770 def filerev(self):
812 return self._filerev
771 return self._filerev
813 def filenode(self):
772 def filenode(self):
814 return self._filenode
773 return self._filenode
815 @propertycache
774 @propertycache
816 def _flags(self):
775 def _flags(self):
817 return self._changectx.flags(self._path)
776 return self._changectx.flags(self._path)
818 def flags(self):
777 def flags(self):
819 return self._flags
778 return self._flags
820 def filelog(self):
779 def filelog(self):
821 return self._filelog
780 return self._filelog
822 def rev(self):
781 def rev(self):
823 return self._changeid
782 return self._changeid
824 def linkrev(self):
783 def linkrev(self):
825 return self._filelog.linkrev(self._filerev)
784 return self._filelog.linkrev(self._filerev)
826 def node(self):
785 def node(self):
827 return self._changectx.node()
786 return self._changectx.node()
828 def hex(self):
787 def hex(self):
829 return self._changectx.hex()
788 return self._changectx.hex()
830 def user(self):
789 def user(self):
831 return self._changectx.user()
790 return self._changectx.user()
832 def date(self):
791 def date(self):
833 return self._changectx.date()
792 return self._changectx.date()
834 def files(self):
793 def files(self):
835 return self._changectx.files()
794 return self._changectx.files()
836 def description(self):
795 def description(self):
837 return self._changectx.description()
796 return self._changectx.description()
838 def branch(self):
797 def branch(self):
839 return self._changectx.branch()
798 return self._changectx.branch()
840 def extra(self):
799 def extra(self):
841 return self._changectx.extra()
800 return self._changectx.extra()
842 def phase(self):
801 def phase(self):
843 return self._changectx.phase()
802 return self._changectx.phase()
844 def phasestr(self):
803 def phasestr(self):
845 return self._changectx.phasestr()
804 return self._changectx.phasestr()
846 def obsolete(self):
805 def obsolete(self):
847 return self._changectx.obsolete()
806 return self._changectx.obsolete()
848 def instabilities(self):
807 def instabilities(self):
849 return self._changectx.instabilities()
808 return self._changectx.instabilities()
850 def manifest(self):
809 def manifest(self):
851 return self._changectx.manifest()
810 return self._changectx.manifest()
852 def changectx(self):
811 def changectx(self):
853 return self._changectx
812 return self._changectx
854 def renamed(self):
813 def renamed(self):
855 return self._copied
814 return self._copied
856 def repo(self):
815 def repo(self):
857 return self._repo
816 return self._repo
858 def size(self):
817 def size(self):
859 return len(self.data())
818 return len(self.data())
860
819
861 def path(self):
820 def path(self):
862 return self._path
821 return self._path
863
822
864 def isbinary(self):
823 def isbinary(self):
865 try:
824 try:
866 return util.binary(self.data())
825 return util.binary(self.data())
867 except IOError:
826 except IOError:
868 return False
827 return False
869 def isexec(self):
828 def isexec(self):
870 return 'x' in self.flags()
829 return 'x' in self.flags()
871 def islink(self):
830 def islink(self):
872 return 'l' in self.flags()
831 return 'l' in self.flags()
873
832
874 def isabsent(self):
833 def isabsent(self):
875 """whether this filectx represents a file not in self._changectx
834 """whether this filectx represents a file not in self._changectx
876
835
877 This is mainly for merge code to detect change/delete conflicts. This is
836 This is mainly for merge code to detect change/delete conflicts. This is
878 expected to be True for all subclasses of basectx."""
837 expected to be True for all subclasses of basectx."""
879 return False
838 return False
880
839
881 _customcmp = False
840 _customcmp = False
882 def cmp(self, fctx):
841 def cmp(self, fctx):
883 """compare with other file context
842 """compare with other file context
884
843
885 returns True if different than fctx.
844 returns True if different than fctx.
886 """
845 """
887 if fctx._customcmp:
846 if fctx._customcmp:
888 return fctx.cmp(self)
847 return fctx.cmp(self)
889
848
890 if (fctx._filenode is None
849 if (fctx._filenode is None
891 and (self._repo._encodefilterpats
850 and (self._repo._encodefilterpats
892 # if file data starts with '\1\n', empty metadata block is
851 # if file data starts with '\1\n', empty metadata block is
893 # prepended, which adds 4 bytes to filelog.size().
852 # prepended, which adds 4 bytes to filelog.size().
894 or self.size() - 4 == fctx.size())
853 or self.size() - 4 == fctx.size())
895 or self.size() == fctx.size()):
854 or self.size() == fctx.size()):
896 return self._filelog.cmp(self._filenode, fctx.data())
855 return self._filelog.cmp(self._filenode, fctx.data())
897
856
898 return True
857 return True
899
858
900 def _adjustlinkrev(self, srcrev, inclusive=False):
859 def _adjustlinkrev(self, srcrev, inclusive=False):
901 """return the first ancestor of <srcrev> introducing <fnode>
860 """return the first ancestor of <srcrev> introducing <fnode>
902
861
903 If the linkrev of the file revision does not point to an ancestor of
862 If the linkrev of the file revision does not point to an ancestor of
904 srcrev, we'll walk down the ancestors until we find one introducing
863 srcrev, we'll walk down the ancestors until we find one introducing
905 this file revision.
864 this file revision.
906
865
907 :srcrev: the changeset revision we search ancestors from
866 :srcrev: the changeset revision we search ancestors from
908 :inclusive: if true, the src revision will also be checked
867 :inclusive: if true, the src revision will also be checked
909 """
868 """
910 repo = self._repo
869 repo = self._repo
911 cl = repo.unfiltered().changelog
870 cl = repo.unfiltered().changelog
912 mfl = repo.manifestlog
871 mfl = repo.manifestlog
913 # fetch the linkrev
872 # fetch the linkrev
914 lkr = self.linkrev()
873 lkr = self.linkrev()
915 # hack to reuse ancestor computation when searching for renames
874 # hack to reuse ancestor computation when searching for renames
916 memberanc = getattr(self, '_ancestrycontext', None)
875 memberanc = getattr(self, '_ancestrycontext', None)
917 iteranc = None
876 iteranc = None
918 if srcrev is None:
877 if srcrev is None:
919 # wctx case, used by workingfilectx during mergecopy
878 # wctx case, used by workingfilectx during mergecopy
920 revs = [p.rev() for p in self._repo[None].parents()]
879 revs = [p.rev() for p in self._repo[None].parents()]
921 inclusive = True # we skipped the real (revless) source
880 inclusive = True # we skipped the real (revless) source
922 else:
881 else:
923 revs = [srcrev]
882 revs = [srcrev]
924 if memberanc is None:
883 if memberanc is None:
925 memberanc = iteranc = cl.ancestors(revs, lkr,
884 memberanc = iteranc = cl.ancestors(revs, lkr,
926 inclusive=inclusive)
885 inclusive=inclusive)
927 # check if this linkrev is an ancestor of srcrev
886 # check if this linkrev is an ancestor of srcrev
928 if lkr not in memberanc:
887 if lkr not in memberanc:
929 if iteranc is None:
888 if iteranc is None:
930 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
889 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
931 fnode = self._filenode
890 fnode = self._filenode
932 path = self._path
891 path = self._path
933 for a in iteranc:
892 for a in iteranc:
934 ac = cl.read(a) # get changeset data (we avoid object creation)
893 ac = cl.read(a) # get changeset data (we avoid object creation)
935 if path in ac[3]: # checking the 'files' field.
894 if path in ac[3]: # checking the 'files' field.
936 # The file has been touched, check if the content is
895 # The file has been touched, check if the content is
937 # similar to the one we search for.
896 # similar to the one we search for.
938 if fnode == mfl[ac[0]].readfast().get(path):
897 if fnode == mfl[ac[0]].readfast().get(path):
939 return a
898 return a
940 # In theory, we should never get out of that loop without a result.
899 # In theory, we should never get out of that loop without a result.
941 # But if manifest uses a buggy file revision (not children of the
900 # But if manifest uses a buggy file revision (not children of the
942 # one it replaces) we could. Such a buggy situation will likely
901 # one it replaces) we could. Such a buggy situation will likely
943 # result is crash somewhere else at to some point.
902 # result is crash somewhere else at to some point.
944 return lkr
903 return lkr
945
904
946 def introrev(self):
905 def introrev(self):
947 """return the rev of the changeset which introduced this file revision
906 """return the rev of the changeset which introduced this file revision
948
907
949 This method is different from linkrev because it take into account the
908 This method is different from linkrev because it take into account the
950 changeset the filectx was created from. It ensures the returned
909 changeset the filectx was created from. It ensures the returned
951 revision is one of its ancestors. This prevents bugs from
910 revision is one of its ancestors. This prevents bugs from
952 'linkrev-shadowing' when a file revision is used by multiple
911 'linkrev-shadowing' when a file revision is used by multiple
953 changesets.
912 changesets.
954 """
913 """
955 lkr = self.linkrev()
914 lkr = self.linkrev()
956 attrs = vars(self)
915 attrs = vars(self)
957 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
916 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
958 if noctx or self.rev() == lkr:
917 if noctx or self.rev() == lkr:
959 return self.linkrev()
918 return self.linkrev()
960 return self._adjustlinkrev(self.rev(), inclusive=True)
919 return self._adjustlinkrev(self.rev(), inclusive=True)
961
920
962 def introfilectx(self):
921 def introfilectx(self):
963 """Return filectx having identical contents, but pointing to the
922 """Return filectx having identical contents, but pointing to the
964 changeset revision where this filectx was introduced"""
923 changeset revision where this filectx was introduced"""
965 introrev = self.introrev()
924 introrev = self.introrev()
966 if self.rev() == introrev:
925 if self.rev() == introrev:
967 return self
926 return self
968 return self.filectx(self.filenode(), changeid=introrev)
927 return self.filectx(self.filenode(), changeid=introrev)
969
928
970 def _parentfilectx(self, path, fileid, filelog):
929 def _parentfilectx(self, path, fileid, filelog):
971 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
930 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
972 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
931 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
973 if '_changeid' in vars(self) or '_changectx' in vars(self):
932 if '_changeid' in vars(self) or '_changectx' in vars(self):
974 # If self is associated with a changeset (probably explicitly
933 # If self is associated with a changeset (probably explicitly
975 # fed), ensure the created filectx is associated with a
934 # fed), ensure the created filectx is associated with a
976 # changeset that is an ancestor of self.changectx.
935 # changeset that is an ancestor of self.changectx.
977 # This lets us later use _adjustlinkrev to get a correct link.
936 # This lets us later use _adjustlinkrev to get a correct link.
978 fctx._descendantrev = self.rev()
937 fctx._descendantrev = self.rev()
979 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
938 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
980 elif '_descendantrev' in vars(self):
939 elif '_descendantrev' in vars(self):
981 # Otherwise propagate _descendantrev if we have one associated.
940 # Otherwise propagate _descendantrev if we have one associated.
982 fctx._descendantrev = self._descendantrev
941 fctx._descendantrev = self._descendantrev
983 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
942 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
984 return fctx
943 return fctx
985
944
986 def parents(self):
945 def parents(self):
987 _path = self._path
946 _path = self._path
988 fl = self._filelog
947 fl = self._filelog
989 parents = self._filelog.parents(self._filenode)
948 parents = self._filelog.parents(self._filenode)
990 pl = [(_path, node, fl) for node in parents if node != nullid]
949 pl = [(_path, node, fl) for node in parents if node != nullid]
991
950
992 r = fl.renamed(self._filenode)
951 r = fl.renamed(self._filenode)
993 if r:
952 if r:
994 # - In the simple rename case, both parent are nullid, pl is empty.
953 # - In the simple rename case, both parent are nullid, pl is empty.
995 # - In case of merge, only one of the parent is null id and should
954 # - In case of merge, only one of the parent is null id and should
996 # be replaced with the rename information. This parent is -always-
955 # be replaced with the rename information. This parent is -always-
997 # the first one.
956 # the first one.
998 #
957 #
999 # As null id have always been filtered out in the previous list
958 # As null id have always been filtered out in the previous list
1000 # comprehension, inserting to 0 will always result in "replacing
959 # comprehension, inserting to 0 will always result in "replacing
1001 # first nullid parent with rename information.
960 # first nullid parent with rename information.
1002 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
961 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
1003
962
1004 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
963 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
1005
964
1006 def p1(self):
965 def p1(self):
1007 return self.parents()[0]
966 return self.parents()[0]
1008
967
1009 def p2(self):
968 def p2(self):
1010 p = self.parents()
969 p = self.parents()
1011 if len(p) == 2:
970 if len(p) == 2:
1012 return p[1]
971 return p[1]
1013 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
972 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
1014
973
1015 def annotate(self, follow=False, linenumber=False, skiprevs=None,
974 def annotate(self, follow=False, linenumber=False, skiprevs=None,
1016 diffopts=None):
975 diffopts=None):
1017 '''returns a list of tuples of ((ctx, number), line) for each line
976 '''returns a list of tuples of ((ctx, number), line) for each line
1018 in the file, where ctx is the filectx of the node where
977 in the file, where ctx is the filectx of the node where
1019 that line was last changed; if linenumber parameter is true, number is
978 that line was last changed; if linenumber parameter is true, number is
1020 the line number at the first appearance in the managed file, otherwise,
979 the line number at the first appearance in the managed file, otherwise,
1021 number has a fixed value of False.
980 number has a fixed value of False.
1022 '''
981 '''
1023
982
1024 def lines(text):
983 def lines(text):
1025 if text.endswith("\n"):
984 if text.endswith("\n"):
1026 return text.count("\n")
985 return text.count("\n")
1027 return text.count("\n") + int(bool(text))
986 return text.count("\n") + int(bool(text))
1028
987
1029 if linenumber:
988 if linenumber:
1030 def decorate(text, rev):
989 def decorate(text, rev):
1031 return ([annotateline(fctx=rev, lineno=i)
990 return ([annotateline(fctx=rev, lineno=i)
1032 for i in xrange(1, lines(text) + 1)], text)
991 for i in xrange(1, lines(text) + 1)], text)
1033 else:
992 else:
1034 def decorate(text, rev):
993 def decorate(text, rev):
1035 return ([annotateline(fctx=rev)] * lines(text), text)
994 return ([annotateline(fctx=rev)] * lines(text), text)
1036
995
1037 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
996 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
1038
997
1039 def parents(f):
998 def parents(f):
1040 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
999 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
1041 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
1000 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
1042 # from the topmost introrev (= srcrev) down to p.linkrev() if it
1001 # from the topmost introrev (= srcrev) down to p.linkrev() if it
1043 # isn't an ancestor of the srcrev.
1002 # isn't an ancestor of the srcrev.
1044 f._changeid
1003 f._changeid
1045 pl = f.parents()
1004 pl = f.parents()
1046
1005
1047 # Don't return renamed parents if we aren't following.
1006 # Don't return renamed parents if we aren't following.
1048 if not follow:
1007 if not follow:
1049 pl = [p for p in pl if p.path() == f.path()]
1008 pl = [p for p in pl if p.path() == f.path()]
1050
1009
1051 # renamed filectx won't have a filelog yet, so set it
1010 # renamed filectx won't have a filelog yet, so set it
1052 # from the cache to save time
1011 # from the cache to save time
1053 for p in pl:
1012 for p in pl:
1054 if not r'_filelog' in p.__dict__:
1013 if not r'_filelog' in p.__dict__:
1055 p._filelog = getlog(p.path())
1014 p._filelog = getlog(p.path())
1056
1015
1057 return pl
1016 return pl
1058
1017
1059 # use linkrev to find the first changeset where self appeared
1018 # use linkrev to find the first changeset where self appeared
1060 base = self.introfilectx()
1019 base = self.introfilectx()
1061 if getattr(base, '_ancestrycontext', None) is None:
1020 if getattr(base, '_ancestrycontext', None) is None:
1062 cl = self._repo.changelog
1021 cl = self._repo.changelog
1063 if base.rev() is None:
1022 if base.rev() is None:
1064 # wctx is not inclusive, but works because _ancestrycontext
1023 # wctx is not inclusive, but works because _ancestrycontext
1065 # is used to test filelog revisions
1024 # is used to test filelog revisions
1066 ac = cl.ancestors([p.rev() for p in base.parents()],
1025 ac = cl.ancestors([p.rev() for p in base.parents()],
1067 inclusive=True)
1026 inclusive=True)
1068 else:
1027 else:
1069 ac = cl.ancestors([base.rev()], inclusive=True)
1028 ac = cl.ancestors([base.rev()], inclusive=True)
1070 base._ancestrycontext = ac
1029 base._ancestrycontext = ac
1071
1030
1072 # This algorithm would prefer to be recursive, but Python is a
1031 # This algorithm would prefer to be recursive, but Python is a
1073 # bit recursion-hostile. Instead we do an iterative
1032 # bit recursion-hostile. Instead we do an iterative
1074 # depth-first search.
1033 # depth-first search.
1075
1034
1076 # 1st DFS pre-calculates pcache and needed
1035 # 1st DFS pre-calculates pcache and needed
1077 visit = [base]
1036 visit = [base]
1078 pcache = {}
1037 pcache = {}
1079 needed = {base: 1}
1038 needed = {base: 1}
1080 while visit:
1039 while visit:
1081 f = visit.pop()
1040 f = visit.pop()
1082 if f in pcache:
1041 if f in pcache:
1083 continue
1042 continue
1084 pl = parents(f)
1043 pl = parents(f)
1085 pcache[f] = pl
1044 pcache[f] = pl
1086 for p in pl:
1045 for p in pl:
1087 needed[p] = needed.get(p, 0) + 1
1046 needed[p] = needed.get(p, 0) + 1
1088 if p not in pcache:
1047 if p not in pcache:
1089 visit.append(p)
1048 visit.append(p)
1090
1049
1091 # 2nd DFS does the actual annotate
1050 # 2nd DFS does the actual annotate
1092 visit[:] = [base]
1051 visit[:] = [base]
1093 hist = {}
1052 hist = {}
1094 while visit:
1053 while visit:
1095 f = visit[-1]
1054 f = visit[-1]
1096 if f in hist:
1055 if f in hist:
1097 visit.pop()
1056 visit.pop()
1098 continue
1057 continue
1099
1058
1100 ready = True
1059 ready = True
1101 pl = pcache[f]
1060 pl = pcache[f]
1102 for p in pl:
1061 for p in pl:
1103 if p not in hist:
1062 if p not in hist:
1104 ready = False
1063 ready = False
1105 visit.append(p)
1064 visit.append(p)
1106 if ready:
1065 if ready:
1107 visit.pop()
1066 visit.pop()
1108 curr = decorate(f.data(), f)
1067 curr = decorate(f.data(), f)
1109 skipchild = False
1068 skipchild = False
1110 if skiprevs is not None:
1069 if skiprevs is not None:
1111 skipchild = f._changeid in skiprevs
1070 skipchild = f._changeid in skiprevs
1112 curr = _annotatepair([hist[p] for p in pl], f, curr, skipchild,
1071 curr = _annotatepair([hist[p] for p in pl], f, curr, skipchild,
1113 diffopts)
1072 diffopts)
1114 for p in pl:
1073 for p in pl:
1115 if needed[p] == 1:
1074 if needed[p] == 1:
1116 del hist[p]
1075 del hist[p]
1117 del needed[p]
1076 del needed[p]
1118 else:
1077 else:
1119 needed[p] -= 1
1078 needed[p] -= 1
1120
1079
1121 hist[f] = curr
1080 hist[f] = curr
1122 del pcache[f]
1081 del pcache[f]
1123
1082
1124 return pycompat.ziplist(hist[base][0], hist[base][1].splitlines(True))
1083 return pycompat.ziplist(hist[base][0], hist[base][1].splitlines(True))
1125
1084
1126 def ancestors(self, followfirst=False):
1085 def ancestors(self, followfirst=False):
1127 visit = {}
1086 visit = {}
1128 c = self
1087 c = self
1129 if followfirst:
1088 if followfirst:
1130 cut = 1
1089 cut = 1
1131 else:
1090 else:
1132 cut = None
1091 cut = None
1133
1092
1134 while True:
1093 while True:
1135 for parent in c.parents()[:cut]:
1094 for parent in c.parents()[:cut]:
1136 visit[(parent.linkrev(), parent.filenode())] = parent
1095 visit[(parent.linkrev(), parent.filenode())] = parent
1137 if not visit:
1096 if not visit:
1138 break
1097 break
1139 c = visit.pop(max(visit))
1098 c = visit.pop(max(visit))
1140 yield c
1099 yield c
1141
1100
1142 def decodeddata(self):
1101 def decodeddata(self):
1143 """Returns `data()` after running repository decoding filters.
1102 """Returns `data()` after running repository decoding filters.
1144
1103
1145 This is often equivalent to how the data would be expressed on disk.
1104 This is often equivalent to how the data would be expressed on disk.
1146 """
1105 """
1147 return self._repo.wwritedata(self.path(), self.data())
1106 return self._repo.wwritedata(self.path(), self.data())
1148
1107
1149 @attr.s(slots=True, frozen=True)
1108 @attr.s(slots=True, frozen=True)
1150 class annotateline(object):
1109 class annotateline(object):
1151 fctx = attr.ib()
1110 fctx = attr.ib()
1152 lineno = attr.ib(default=False)
1111 lineno = attr.ib(default=False)
1153 # Whether this annotation was the result of a skip-annotate.
1112 # Whether this annotation was the result of a skip-annotate.
1154 skip = attr.ib(default=False)
1113 skip = attr.ib(default=False)
1155
1114
1156 def _annotatepair(parents, childfctx, child, skipchild, diffopts):
1115 def _annotatepair(parents, childfctx, child, skipchild, diffopts):
1157 r'''
1116 r'''
1158 Given parent and child fctxes and annotate data for parents, for all lines
1117 Given parent and child fctxes and annotate data for parents, for all lines
1159 in either parent that match the child, annotate the child with the parent's
1118 in either parent that match the child, annotate the child with the parent's
1160 data.
1119 data.
1161
1120
1162 Additionally, if `skipchild` is True, replace all other lines with parent
1121 Additionally, if `skipchild` is True, replace all other lines with parent
1163 annotate data as well such that child is never blamed for any lines.
1122 annotate data as well such that child is never blamed for any lines.
1164
1123
1165 See test-annotate.py for unit tests.
1124 See test-annotate.py for unit tests.
1166 '''
1125 '''
1167 pblocks = [(parent, mdiff.allblocks(parent[1], child[1], opts=diffopts))
1126 pblocks = [(parent, mdiff.allblocks(parent[1], child[1], opts=diffopts))
1168 for parent in parents]
1127 for parent in parents]
1169
1128
1170 if skipchild:
1129 if skipchild:
1171 # Need to iterate over the blocks twice -- make it a list
1130 # Need to iterate over the blocks twice -- make it a list
1172 pblocks = [(p, list(blocks)) for (p, blocks) in pblocks]
1131 pblocks = [(p, list(blocks)) for (p, blocks) in pblocks]
1173 # Mercurial currently prefers p2 over p1 for annotate.
1132 # Mercurial currently prefers p2 over p1 for annotate.
1174 # TODO: change this?
1133 # TODO: change this?
1175 for parent, blocks in pblocks:
1134 for parent, blocks in pblocks:
1176 for (a1, a2, b1, b2), t in blocks:
1135 for (a1, a2, b1, b2), t in blocks:
1177 # Changed blocks ('!') or blocks made only of blank lines ('~')
1136 # Changed blocks ('!') or blocks made only of blank lines ('~')
1178 # belong to the child.
1137 # belong to the child.
1179 if t == '=':
1138 if t == '=':
1180 child[0][b1:b2] = parent[0][a1:a2]
1139 child[0][b1:b2] = parent[0][a1:a2]
1181
1140
1182 if skipchild:
1141 if skipchild:
1183 # Now try and match up anything that couldn't be matched,
1142 # Now try and match up anything that couldn't be matched,
1184 # Reversing pblocks maintains bias towards p2, matching above
1143 # Reversing pblocks maintains bias towards p2, matching above
1185 # behavior.
1144 # behavior.
1186 pblocks.reverse()
1145 pblocks.reverse()
1187
1146
1188 # The heuristics are:
1147 # The heuristics are:
1189 # * Work on blocks of changed lines (effectively diff hunks with -U0).
1148 # * Work on blocks of changed lines (effectively diff hunks with -U0).
1190 # This could potentially be smarter but works well enough.
1149 # This could potentially be smarter but works well enough.
1191 # * For a non-matching section, do a best-effort fit. Match lines in
1150 # * For a non-matching section, do a best-effort fit. Match lines in
1192 # diff hunks 1:1, dropping lines as necessary.
1151 # diff hunks 1:1, dropping lines as necessary.
1193 # * Repeat the last line as a last resort.
1152 # * Repeat the last line as a last resort.
1194
1153
1195 # First, replace as much as possible without repeating the last line.
1154 # First, replace as much as possible without repeating the last line.
1196 remaining = [(parent, []) for parent, _blocks in pblocks]
1155 remaining = [(parent, []) for parent, _blocks in pblocks]
1197 for idx, (parent, blocks) in enumerate(pblocks):
1156 for idx, (parent, blocks) in enumerate(pblocks):
1198 for (a1, a2, b1, b2), _t in blocks:
1157 for (a1, a2, b1, b2), _t in blocks:
1199 if a2 - a1 >= b2 - b1:
1158 if a2 - a1 >= b2 - b1:
1200 for bk in xrange(b1, b2):
1159 for bk in xrange(b1, b2):
1201 if child[0][bk].fctx == childfctx:
1160 if child[0][bk].fctx == childfctx:
1202 ak = min(a1 + (bk - b1), a2 - 1)
1161 ak = min(a1 + (bk - b1), a2 - 1)
1203 child[0][bk] = attr.evolve(parent[0][ak], skip=True)
1162 child[0][bk] = attr.evolve(parent[0][ak], skip=True)
1204 else:
1163 else:
1205 remaining[idx][1].append((a1, a2, b1, b2))
1164 remaining[idx][1].append((a1, a2, b1, b2))
1206
1165
1207 # Then, look at anything left, which might involve repeating the last
1166 # Then, look at anything left, which might involve repeating the last
1208 # line.
1167 # line.
1209 for parent, blocks in remaining:
1168 for parent, blocks in remaining:
1210 for a1, a2, b1, b2 in blocks:
1169 for a1, a2, b1, b2 in blocks:
1211 for bk in xrange(b1, b2):
1170 for bk in xrange(b1, b2):
1212 if child[0][bk].fctx == childfctx:
1171 if child[0][bk].fctx == childfctx:
1213 ak = min(a1 + (bk - b1), a2 - 1)
1172 ak = min(a1 + (bk - b1), a2 - 1)
1214 child[0][bk] = attr.evolve(parent[0][ak], skip=True)
1173 child[0][bk] = attr.evolve(parent[0][ak], skip=True)
1215 return child
1174 return child
1216
1175
1217 class filectx(basefilectx):
1176 class filectx(basefilectx):
1218 """A filecontext object makes access to data related to a particular
1177 """A filecontext object makes access to data related to a particular
1219 filerevision convenient."""
1178 filerevision convenient."""
1220 def __init__(self, repo, path, changeid=None, fileid=None,
1179 def __init__(self, repo, path, changeid=None, fileid=None,
1221 filelog=None, changectx=None):
1180 filelog=None, changectx=None):
1222 """changeid can be a changeset revision, node, or tag.
1181 """changeid can be a changeset revision, node, or tag.
1223 fileid can be a file revision or node."""
1182 fileid can be a file revision or node."""
1224 self._repo = repo
1183 self._repo = repo
1225 self._path = path
1184 self._path = path
1226
1185
1227 assert (changeid is not None
1186 assert (changeid is not None
1228 or fileid is not None
1187 or fileid is not None
1229 or changectx is not None), \
1188 or changectx is not None), \
1230 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1189 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1231 % (changeid, fileid, changectx))
1190 % (changeid, fileid, changectx))
1232
1191
1233 if filelog is not None:
1192 if filelog is not None:
1234 self._filelog = filelog
1193 self._filelog = filelog
1235
1194
1236 if changeid is not None:
1195 if changeid is not None:
1237 self._changeid = changeid
1196 self._changeid = changeid
1238 if changectx is not None:
1197 if changectx is not None:
1239 self._changectx = changectx
1198 self._changectx = changectx
1240 if fileid is not None:
1199 if fileid is not None:
1241 self._fileid = fileid
1200 self._fileid = fileid
1242
1201
1243 @propertycache
1202 @propertycache
1244 def _changectx(self):
1203 def _changectx(self):
1245 try:
1204 try:
1246 return changectx(self._repo, self._changeid)
1205 return changectx(self._repo, self._changeid)
1247 except error.FilteredRepoLookupError:
1206 except error.FilteredRepoLookupError:
1248 # Linkrev may point to any revision in the repository. When the
1207 # Linkrev may point to any revision in the repository. When the
1249 # repository is filtered this may lead to `filectx` trying to build
1208 # repository is filtered this may lead to `filectx` trying to build
1250 # `changectx` for filtered revision. In such case we fallback to
1209 # `changectx` for filtered revision. In such case we fallback to
1251 # creating `changectx` on the unfiltered version of the reposition.
1210 # creating `changectx` on the unfiltered version of the reposition.
1252 # This fallback should not be an issue because `changectx` from
1211 # This fallback should not be an issue because `changectx` from
1253 # `filectx` are not used in complex operations that care about
1212 # `filectx` are not used in complex operations that care about
1254 # filtering.
1213 # filtering.
1255 #
1214 #
1256 # This fallback is a cheap and dirty fix that prevent several
1215 # This fallback is a cheap and dirty fix that prevent several
1257 # crashes. It does not ensure the behavior is correct. However the
1216 # crashes. It does not ensure the behavior is correct. However the
1258 # behavior was not correct before filtering either and "incorrect
1217 # behavior was not correct before filtering either and "incorrect
1259 # behavior" is seen as better as "crash"
1218 # behavior" is seen as better as "crash"
1260 #
1219 #
1261 # Linkrevs have several serious troubles with filtering that are
1220 # Linkrevs have several serious troubles with filtering that are
1262 # complicated to solve. Proper handling of the issue here should be
1221 # complicated to solve. Proper handling of the issue here should be
1263 # considered when solving linkrev issue are on the table.
1222 # considered when solving linkrev issue are on the table.
1264 return changectx(self._repo.unfiltered(), self._changeid)
1223 return changectx(self._repo.unfiltered(), self._changeid)
1265
1224
1266 def filectx(self, fileid, changeid=None):
1225 def filectx(self, fileid, changeid=None):
1267 '''opens an arbitrary revision of the file without
1226 '''opens an arbitrary revision of the file without
1268 opening a new filelog'''
1227 opening a new filelog'''
1269 return filectx(self._repo, self._path, fileid=fileid,
1228 return filectx(self._repo, self._path, fileid=fileid,
1270 filelog=self._filelog, changeid=changeid)
1229 filelog=self._filelog, changeid=changeid)
1271
1230
1272 def rawdata(self):
1231 def rawdata(self):
1273 return self._filelog.revision(self._filenode, raw=True)
1232 return self._filelog.revision(self._filenode, raw=True)
1274
1233
1275 def rawflags(self):
1234 def rawflags(self):
1276 """low-level revlog flags"""
1235 """low-level revlog flags"""
1277 return self._filelog.flags(self._filerev)
1236 return self._filelog.flags(self._filerev)
1278
1237
1279 def data(self):
1238 def data(self):
1280 try:
1239 try:
1281 return self._filelog.read(self._filenode)
1240 return self._filelog.read(self._filenode)
1282 except error.CensoredNodeError:
1241 except error.CensoredNodeError:
1283 if self._repo.ui.config("censor", "policy") == "ignore":
1242 if self._repo.ui.config("censor", "policy") == "ignore":
1284 return ""
1243 return ""
1285 raise error.Abort(_("censored node: %s") % short(self._filenode),
1244 raise error.Abort(_("censored node: %s") % short(self._filenode),
1286 hint=_("set censor.policy to ignore errors"))
1245 hint=_("set censor.policy to ignore errors"))
1287
1246
1288 def size(self):
1247 def size(self):
1289 return self._filelog.size(self._filerev)
1248 return self._filelog.size(self._filerev)
1290
1249
1291 @propertycache
1250 @propertycache
1292 def _copied(self):
1251 def _copied(self):
1293 """check if file was actually renamed in this changeset revision
1252 """check if file was actually renamed in this changeset revision
1294
1253
1295 If rename logged in file revision, we report copy for changeset only
1254 If rename logged in file revision, we report copy for changeset only
1296 if file revisions linkrev points back to the changeset in question
1255 if file revisions linkrev points back to the changeset in question
1297 or both changeset parents contain different file revisions.
1256 or both changeset parents contain different file revisions.
1298 """
1257 """
1299
1258
1300 renamed = self._filelog.renamed(self._filenode)
1259 renamed = self._filelog.renamed(self._filenode)
1301 if not renamed:
1260 if not renamed:
1302 return renamed
1261 return renamed
1303
1262
1304 if self.rev() == self.linkrev():
1263 if self.rev() == self.linkrev():
1305 return renamed
1264 return renamed
1306
1265
1307 name = self.path()
1266 name = self.path()
1308 fnode = self._filenode
1267 fnode = self._filenode
1309 for p in self._changectx.parents():
1268 for p in self._changectx.parents():
1310 try:
1269 try:
1311 if fnode == p.filenode(name):
1270 if fnode == p.filenode(name):
1312 return None
1271 return None
1313 except error.LookupError:
1272 except error.LookupError:
1314 pass
1273 pass
1315 return renamed
1274 return renamed
1316
1275
1317 def children(self):
1276 def children(self):
1318 # hard for renames
1277 # hard for renames
1319 c = self._filelog.children(self._filenode)
1278 c = self._filelog.children(self._filenode)
1320 return [filectx(self._repo, self._path, fileid=x,
1279 return [filectx(self._repo, self._path, fileid=x,
1321 filelog=self._filelog) for x in c]
1280 filelog=self._filelog) for x in c]
1322
1281
1323 class committablectx(basectx):
1282 class committablectx(basectx):
1324 """A committablectx object provides common functionality for a context that
1283 """A committablectx object provides common functionality for a context that
1325 wants the ability to commit, e.g. workingctx or memctx."""
1284 wants the ability to commit, e.g. workingctx or memctx."""
1326 def __init__(self, repo, text="", user=None, date=None, extra=None,
1285 def __init__(self, repo, text="", user=None, date=None, extra=None,
1327 changes=None):
1286 changes=None):
1328 self._repo = repo
1287 self._repo = repo
1329 self._rev = None
1288 self._rev = None
1330 self._node = None
1289 self._node = None
1331 self._text = text
1290 self._text = text
1332 if date:
1291 if date:
1333 self._date = util.parsedate(date)
1292 self._date = util.parsedate(date)
1334 if user:
1293 if user:
1335 self._user = user
1294 self._user = user
1336 if changes:
1295 if changes:
1337 self._status = changes
1296 self._status = changes
1338
1297
1339 self._extra = {}
1298 self._extra = {}
1340 if extra:
1299 if extra:
1341 self._extra = extra.copy()
1300 self._extra = extra.copy()
1342 if 'branch' not in self._extra:
1301 if 'branch' not in self._extra:
1343 try:
1302 try:
1344 branch = encoding.fromlocal(self._repo.dirstate.branch())
1303 branch = encoding.fromlocal(self._repo.dirstate.branch())
1345 except UnicodeDecodeError:
1304 except UnicodeDecodeError:
1346 raise error.Abort(_('branch name not in UTF-8!'))
1305 raise error.Abort(_('branch name not in UTF-8!'))
1347 self._extra['branch'] = branch
1306 self._extra['branch'] = branch
1348 if self._extra['branch'] == '':
1307 if self._extra['branch'] == '':
1349 self._extra['branch'] = 'default'
1308 self._extra['branch'] = 'default'
1350
1309
1351 def __bytes__(self):
1310 def __bytes__(self):
1352 return bytes(self._parents[0]) + "+"
1311 return bytes(self._parents[0]) + "+"
1353
1312
1354 __str__ = encoding.strmethod(__bytes__)
1313 __str__ = encoding.strmethod(__bytes__)
1355
1314
1356 def __nonzero__(self):
1315 def __nonzero__(self):
1357 return True
1316 return True
1358
1317
1359 __bool__ = __nonzero__
1318 __bool__ = __nonzero__
1360
1319
1361 def _buildflagfunc(self):
1320 def _buildflagfunc(self):
1362 # Create a fallback function for getting file flags when the
1321 # Create a fallback function for getting file flags when the
1363 # filesystem doesn't support them
1322 # filesystem doesn't support them
1364
1323
1365 copiesget = self._repo.dirstate.copies().get
1324 copiesget = self._repo.dirstate.copies().get
1366 parents = self.parents()
1325 parents = self.parents()
1367 if len(parents) < 2:
1326 if len(parents) < 2:
1368 # when we have one parent, it's easy: copy from parent
1327 # when we have one parent, it's easy: copy from parent
1369 man = parents[0].manifest()
1328 man = parents[0].manifest()
1370 def func(f):
1329 def func(f):
1371 f = copiesget(f, f)
1330 f = copiesget(f, f)
1372 return man.flags(f)
1331 return man.flags(f)
1373 else:
1332 else:
1374 # merges are tricky: we try to reconstruct the unstored
1333 # merges are tricky: we try to reconstruct the unstored
1375 # result from the merge (issue1802)
1334 # result from the merge (issue1802)
1376 p1, p2 = parents
1335 p1, p2 = parents
1377 pa = p1.ancestor(p2)
1336 pa = p1.ancestor(p2)
1378 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1337 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1379
1338
1380 def func(f):
1339 def func(f):
1381 f = copiesget(f, f) # may be wrong for merges with copies
1340 f = copiesget(f, f) # may be wrong for merges with copies
1382 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1341 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1383 if fl1 == fl2:
1342 if fl1 == fl2:
1384 return fl1
1343 return fl1
1385 if fl1 == fla:
1344 if fl1 == fla:
1386 return fl2
1345 return fl2
1387 if fl2 == fla:
1346 if fl2 == fla:
1388 return fl1
1347 return fl1
1389 return '' # punt for conflicts
1348 return '' # punt for conflicts
1390
1349
1391 return func
1350 return func
1392
1351
1393 @propertycache
1352 @propertycache
1394 def _flagfunc(self):
1353 def _flagfunc(self):
1395 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1354 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1396
1355
1397 @propertycache
1356 @propertycache
1398 def _status(self):
1357 def _status(self):
1399 return self._repo.status()
1358 return self._repo.status()
1400
1359
1401 @propertycache
1360 @propertycache
1402 def _user(self):
1361 def _user(self):
1403 return self._repo.ui.username()
1362 return self._repo.ui.username()
1404
1363
1405 @propertycache
1364 @propertycache
1406 def _date(self):
1365 def _date(self):
1407 ui = self._repo.ui
1366 ui = self._repo.ui
1408 date = ui.configdate('devel', 'default-date')
1367 date = ui.configdate('devel', 'default-date')
1409 if date is None:
1368 if date is None:
1410 date = util.makedate()
1369 date = util.makedate()
1411 return date
1370 return date
1412
1371
1413 def subrev(self, subpath):
1372 def subrev(self, subpath):
1414 return None
1373 return None
1415
1374
1416 def manifestnode(self):
1375 def manifestnode(self):
1417 return None
1376 return None
1418 def user(self):
1377 def user(self):
1419 return self._user or self._repo.ui.username()
1378 return self._user or self._repo.ui.username()
1420 def date(self):
1379 def date(self):
1421 return self._date
1380 return self._date
1422 def description(self):
1381 def description(self):
1423 return self._text
1382 return self._text
1424 def files(self):
1383 def files(self):
1425 return sorted(self._status.modified + self._status.added +
1384 return sorted(self._status.modified + self._status.added +
1426 self._status.removed)
1385 self._status.removed)
1427
1386
1428 def modified(self):
1387 def modified(self):
1429 return self._status.modified
1388 return self._status.modified
1430 def added(self):
1389 def added(self):
1431 return self._status.added
1390 return self._status.added
1432 def removed(self):
1391 def removed(self):
1433 return self._status.removed
1392 return self._status.removed
1434 def deleted(self):
1393 def deleted(self):
1435 return self._status.deleted
1394 return self._status.deleted
1436 def branch(self):
1395 def branch(self):
1437 return encoding.tolocal(self._extra['branch'])
1396 return encoding.tolocal(self._extra['branch'])
1438 def closesbranch(self):
1397 def closesbranch(self):
1439 return 'close' in self._extra
1398 return 'close' in self._extra
1440 def extra(self):
1399 def extra(self):
1441 return self._extra
1400 return self._extra
1442
1401
1443 def isinmemory(self):
1402 def isinmemory(self):
1444 return False
1403 return False
1445
1404
1446 def tags(self):
1405 def tags(self):
1447 return []
1406 return []
1448
1407
1449 def bookmarks(self):
1408 def bookmarks(self):
1450 b = []
1409 b = []
1451 for p in self.parents():
1410 for p in self.parents():
1452 b.extend(p.bookmarks())
1411 b.extend(p.bookmarks())
1453 return b
1412 return b
1454
1413
1455 def phase(self):
1414 def phase(self):
1456 phase = phases.draft # default phase to draft
1415 phase = phases.draft # default phase to draft
1457 for p in self.parents():
1416 for p in self.parents():
1458 phase = max(phase, p.phase())
1417 phase = max(phase, p.phase())
1459 return phase
1418 return phase
1460
1419
1461 def hidden(self):
1420 def hidden(self):
1462 return False
1421 return False
1463
1422
1464 def children(self):
1423 def children(self):
1465 return []
1424 return []
1466
1425
1467 def flags(self, path):
1426 def flags(self, path):
1468 if r'_manifest' in self.__dict__:
1427 if r'_manifest' in self.__dict__:
1469 try:
1428 try:
1470 return self._manifest.flags(path)
1429 return self._manifest.flags(path)
1471 except KeyError:
1430 except KeyError:
1472 return ''
1431 return ''
1473
1432
1474 try:
1433 try:
1475 return self._flagfunc(path)
1434 return self._flagfunc(path)
1476 except OSError:
1435 except OSError:
1477 return ''
1436 return ''
1478
1437
1479 def ancestor(self, c2):
1438 def ancestor(self, c2):
1480 """return the "best" ancestor context of self and c2"""
1439 """return the "best" ancestor context of self and c2"""
1481 return self._parents[0].ancestor(c2) # punt on two parents for now
1440 return self._parents[0].ancestor(c2) # punt on two parents for now
1482
1441
1483 def walk(self, match):
1442 def walk(self, match):
1484 '''Generates matching file names.'''
1443 '''Generates matching file names.'''
1485 return sorted(self._repo.dirstate.walk(match,
1444 return sorted(self._repo.dirstate.walk(match,
1486 subrepos=sorted(self.substate),
1445 subrepos=sorted(self.substate),
1487 unknown=True, ignored=False))
1446 unknown=True, ignored=False))
1488
1447
1489 def matches(self, match):
1448 def matches(self, match):
1490 return sorted(self._repo.dirstate.matches(match))
1449 return sorted(self._repo.dirstate.matches(match))
1491
1450
1492 def ancestors(self):
1451 def ancestors(self):
1493 for p in self._parents:
1452 for p in self._parents:
1494 yield p
1453 yield p
1495 for a in self._repo.changelog.ancestors(
1454 for a in self._repo.changelog.ancestors(
1496 [p.rev() for p in self._parents]):
1455 [p.rev() for p in self._parents]):
1497 yield changectx(self._repo, a)
1456 yield changectx(self._repo, a)
1498
1457
1499 def markcommitted(self, node):
1458 def markcommitted(self, node):
1500 """Perform post-commit cleanup necessary after committing this ctx
1459 """Perform post-commit cleanup necessary after committing this ctx
1501
1460
1502 Specifically, this updates backing stores this working context
1461 Specifically, this updates backing stores this working context
1503 wraps to reflect the fact that the changes reflected by this
1462 wraps to reflect the fact that the changes reflected by this
1504 workingctx have been committed. For example, it marks
1463 workingctx have been committed. For example, it marks
1505 modified and added files as normal in the dirstate.
1464 modified and added files as normal in the dirstate.
1506
1465
1507 """
1466 """
1508
1467
1509 with self._repo.dirstate.parentchange():
1468 with self._repo.dirstate.parentchange():
1510 for f in self.modified() + self.added():
1469 for f in self.modified() + self.added():
1511 self._repo.dirstate.normal(f)
1470 self._repo.dirstate.normal(f)
1512 for f in self.removed():
1471 for f in self.removed():
1513 self._repo.dirstate.drop(f)
1472 self._repo.dirstate.drop(f)
1514 self._repo.dirstate.setparents(node)
1473 self._repo.dirstate.setparents(node)
1515
1474
1516 # write changes out explicitly, because nesting wlock at
1475 # write changes out explicitly, because nesting wlock at
1517 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1476 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1518 # from immediately doing so for subsequent changing files
1477 # from immediately doing so for subsequent changing files
1519 self._repo.dirstate.write(self._repo.currenttransaction())
1478 self._repo.dirstate.write(self._repo.currenttransaction())
1520
1479
1521 def dirty(self, missing=False, merge=True, branch=True):
1480 def dirty(self, missing=False, merge=True, branch=True):
1522 return False
1481 return False
1523
1482
1524 class workingctx(committablectx):
1483 class workingctx(committablectx):
1525 """A workingctx object makes access to data related to
1484 """A workingctx object makes access to data related to
1526 the current working directory convenient.
1485 the current working directory convenient.
1527 date - any valid date string or (unixtime, offset), or None.
1486 date - any valid date string or (unixtime, offset), or None.
1528 user - username string, or None.
1487 user - username string, or None.
1529 extra - a dictionary of extra values, or None.
1488 extra - a dictionary of extra values, or None.
1530 changes - a list of file lists as returned by localrepo.status()
1489 changes - a list of file lists as returned by localrepo.status()
1531 or None to use the repository status.
1490 or None to use the repository status.
1532 """
1491 """
1533 def __init__(self, repo, text="", user=None, date=None, extra=None,
1492 def __init__(self, repo, text="", user=None, date=None, extra=None,
1534 changes=None):
1493 changes=None):
1535 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1494 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1536
1495
1537 def __iter__(self):
1496 def __iter__(self):
1538 d = self._repo.dirstate
1497 d = self._repo.dirstate
1539 for f in d:
1498 for f in d:
1540 if d[f] != 'r':
1499 if d[f] != 'r':
1541 yield f
1500 yield f
1542
1501
1543 def __contains__(self, key):
1502 def __contains__(self, key):
1544 return self._repo.dirstate[key] not in "?r"
1503 return self._repo.dirstate[key] not in "?r"
1545
1504
1546 def hex(self):
1505 def hex(self):
1547 return hex(wdirid)
1506 return hex(wdirid)
1548
1507
1549 @propertycache
1508 @propertycache
1550 def _parents(self):
1509 def _parents(self):
1551 p = self._repo.dirstate.parents()
1510 p = self._repo.dirstate.parents()
1552 if p[1] == nullid:
1511 if p[1] == nullid:
1553 p = p[:-1]
1512 p = p[:-1]
1554 return [changectx(self._repo, x) for x in p]
1513 return [changectx(self._repo, x) for x in p]
1555
1514
1556 def filectx(self, path, filelog=None):
1515 def filectx(self, path, filelog=None):
1557 """get a file context from the working directory"""
1516 """get a file context from the working directory"""
1558 return workingfilectx(self._repo, path, workingctx=self,
1517 return workingfilectx(self._repo, path, workingctx=self,
1559 filelog=filelog)
1518 filelog=filelog)
1560
1519
1561 def dirty(self, missing=False, merge=True, branch=True):
1520 def dirty(self, missing=False, merge=True, branch=True):
1562 "check whether a working directory is modified"
1521 "check whether a working directory is modified"
1563 # check subrepos first
1522 # check subrepos first
1564 for s in sorted(self.substate):
1523 for s in sorted(self.substate):
1565 if self.sub(s).dirty(missing=missing):
1524 if self.sub(s).dirty(missing=missing):
1566 return True
1525 return True
1567 # check current working dir
1526 # check current working dir
1568 return ((merge and self.p2()) or
1527 return ((merge and self.p2()) or
1569 (branch and self.branch() != self.p1().branch()) or
1528 (branch and self.branch() != self.p1().branch()) or
1570 self.modified() or self.added() or self.removed() or
1529 self.modified() or self.added() or self.removed() or
1571 (missing and self.deleted()))
1530 (missing and self.deleted()))
1572
1531
1573 def add(self, list, prefix=""):
1532 def add(self, list, prefix=""):
1574 with self._repo.wlock():
1533 with self._repo.wlock():
1575 ui, ds = self._repo.ui, self._repo.dirstate
1534 ui, ds = self._repo.ui, self._repo.dirstate
1576 uipath = lambda f: ds.pathto(pathutil.join(prefix, f))
1535 uipath = lambda f: ds.pathto(pathutil.join(prefix, f))
1577 rejected = []
1536 rejected = []
1578 lstat = self._repo.wvfs.lstat
1537 lstat = self._repo.wvfs.lstat
1579 for f in list:
1538 for f in list:
1580 # ds.pathto() returns an absolute file when this is invoked from
1539 # ds.pathto() returns an absolute file when this is invoked from
1581 # the keyword extension. That gets flagged as non-portable on
1540 # the keyword extension. That gets flagged as non-portable on
1582 # Windows, since it contains the drive letter and colon.
1541 # Windows, since it contains the drive letter and colon.
1583 scmutil.checkportable(ui, os.path.join(prefix, f))
1542 scmutil.checkportable(ui, os.path.join(prefix, f))
1584 try:
1543 try:
1585 st = lstat(f)
1544 st = lstat(f)
1586 except OSError:
1545 except OSError:
1587 ui.warn(_("%s does not exist!\n") % uipath(f))
1546 ui.warn(_("%s does not exist!\n") % uipath(f))
1588 rejected.append(f)
1547 rejected.append(f)
1589 continue
1548 continue
1590 if st.st_size > 10000000:
1549 if st.st_size > 10000000:
1591 ui.warn(_("%s: up to %d MB of RAM may be required "
1550 ui.warn(_("%s: up to %d MB of RAM may be required "
1592 "to manage this file\n"
1551 "to manage this file\n"
1593 "(use 'hg revert %s' to cancel the "
1552 "(use 'hg revert %s' to cancel the "
1594 "pending addition)\n")
1553 "pending addition)\n")
1595 % (f, 3 * st.st_size // 1000000, uipath(f)))
1554 % (f, 3 * st.st_size // 1000000, uipath(f)))
1596 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1555 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1597 ui.warn(_("%s not added: only files and symlinks "
1556 ui.warn(_("%s not added: only files and symlinks "
1598 "supported currently\n") % uipath(f))
1557 "supported currently\n") % uipath(f))
1599 rejected.append(f)
1558 rejected.append(f)
1600 elif ds[f] in 'amn':
1559 elif ds[f] in 'amn':
1601 ui.warn(_("%s already tracked!\n") % uipath(f))
1560 ui.warn(_("%s already tracked!\n") % uipath(f))
1602 elif ds[f] == 'r':
1561 elif ds[f] == 'r':
1603 ds.normallookup(f)
1562 ds.normallookup(f)
1604 else:
1563 else:
1605 ds.add(f)
1564 ds.add(f)
1606 return rejected
1565 return rejected
1607
1566
1608 def forget(self, files, prefix=""):
1567 def forget(self, files, prefix=""):
1609 with self._repo.wlock():
1568 with self._repo.wlock():
1610 ds = self._repo.dirstate
1569 ds = self._repo.dirstate
1611 uipath = lambda f: ds.pathto(pathutil.join(prefix, f))
1570 uipath = lambda f: ds.pathto(pathutil.join(prefix, f))
1612 rejected = []
1571 rejected = []
1613 for f in files:
1572 for f in files:
1614 if f not in self._repo.dirstate:
1573 if f not in self._repo.dirstate:
1615 self._repo.ui.warn(_("%s not tracked!\n") % uipath(f))
1574 self._repo.ui.warn(_("%s not tracked!\n") % uipath(f))
1616 rejected.append(f)
1575 rejected.append(f)
1617 elif self._repo.dirstate[f] != 'a':
1576 elif self._repo.dirstate[f] != 'a':
1618 self._repo.dirstate.remove(f)
1577 self._repo.dirstate.remove(f)
1619 else:
1578 else:
1620 self._repo.dirstate.drop(f)
1579 self._repo.dirstate.drop(f)
1621 return rejected
1580 return rejected
1622
1581
1623 def undelete(self, list):
1582 def undelete(self, list):
1624 pctxs = self.parents()
1583 pctxs = self.parents()
1625 with self._repo.wlock():
1584 with self._repo.wlock():
1626 ds = self._repo.dirstate
1585 ds = self._repo.dirstate
1627 for f in list:
1586 for f in list:
1628 if self._repo.dirstate[f] != 'r':
1587 if self._repo.dirstate[f] != 'r':
1629 self._repo.ui.warn(_("%s not removed!\n") % ds.pathto(f))
1588 self._repo.ui.warn(_("%s not removed!\n") % ds.pathto(f))
1630 else:
1589 else:
1631 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1590 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1632 t = fctx.data()
1591 t = fctx.data()
1633 self._repo.wwrite(f, t, fctx.flags())
1592 self._repo.wwrite(f, t, fctx.flags())
1634 self._repo.dirstate.normal(f)
1593 self._repo.dirstate.normal(f)
1635
1594
1636 def copy(self, source, dest):
1595 def copy(self, source, dest):
1637 try:
1596 try:
1638 st = self._repo.wvfs.lstat(dest)
1597 st = self._repo.wvfs.lstat(dest)
1639 except OSError as err:
1598 except OSError as err:
1640 if err.errno != errno.ENOENT:
1599 if err.errno != errno.ENOENT:
1641 raise
1600 raise
1642 self._repo.ui.warn(_("%s does not exist!\n")
1601 self._repo.ui.warn(_("%s does not exist!\n")
1643 % self._repo.dirstate.pathto(dest))
1602 % self._repo.dirstate.pathto(dest))
1644 return
1603 return
1645 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1604 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1646 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1605 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1647 "symbolic link\n")
1606 "symbolic link\n")
1648 % self._repo.dirstate.pathto(dest))
1607 % self._repo.dirstate.pathto(dest))
1649 else:
1608 else:
1650 with self._repo.wlock():
1609 with self._repo.wlock():
1651 if self._repo.dirstate[dest] in '?':
1610 if self._repo.dirstate[dest] in '?':
1652 self._repo.dirstate.add(dest)
1611 self._repo.dirstate.add(dest)
1653 elif self._repo.dirstate[dest] in 'r':
1612 elif self._repo.dirstate[dest] in 'r':
1654 self._repo.dirstate.normallookup(dest)
1613 self._repo.dirstate.normallookup(dest)
1655 self._repo.dirstate.copy(source, dest)
1614 self._repo.dirstate.copy(source, dest)
1656
1615
1657 def match(self, pats=None, include=None, exclude=None, default='glob',
1616 def match(self, pats=None, include=None, exclude=None, default='glob',
1658 listsubrepos=False, badfn=None):
1617 listsubrepos=False, badfn=None):
1659 r = self._repo
1618 r = self._repo
1660
1619
1661 # Only a case insensitive filesystem needs magic to translate user input
1620 # Only a case insensitive filesystem needs magic to translate user input
1662 # to actual case in the filesystem.
1621 # to actual case in the filesystem.
1663 icasefs = not util.fscasesensitive(r.root)
1622 icasefs = not util.fscasesensitive(r.root)
1664 return matchmod.match(r.root, r.getcwd(), pats, include, exclude,
1623 return matchmod.match(r.root, r.getcwd(), pats, include, exclude,
1665 default, auditor=r.auditor, ctx=self,
1624 default, auditor=r.auditor, ctx=self,
1666 listsubrepos=listsubrepos, badfn=badfn,
1625 listsubrepos=listsubrepos, badfn=badfn,
1667 icasefs=icasefs)
1626 icasefs=icasefs)
1668
1627
1669 def _filtersuspectsymlink(self, files):
1628 def _filtersuspectsymlink(self, files):
1670 if not files or self._repo.dirstate._checklink:
1629 if not files or self._repo.dirstate._checklink:
1671 return files
1630 return files
1672
1631
1673 # Symlink placeholders may get non-symlink-like contents
1632 # Symlink placeholders may get non-symlink-like contents
1674 # via user error or dereferencing by NFS or Samba servers,
1633 # via user error or dereferencing by NFS or Samba servers,
1675 # so we filter out any placeholders that don't look like a
1634 # so we filter out any placeholders that don't look like a
1676 # symlink
1635 # symlink
1677 sane = []
1636 sane = []
1678 for f in files:
1637 for f in files:
1679 if self.flags(f) == 'l':
1638 if self.flags(f) == 'l':
1680 d = self[f].data()
1639 d = self[f].data()
1681 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1640 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1682 self._repo.ui.debug('ignoring suspect symlink placeholder'
1641 self._repo.ui.debug('ignoring suspect symlink placeholder'
1683 ' "%s"\n' % f)
1642 ' "%s"\n' % f)
1684 continue
1643 continue
1685 sane.append(f)
1644 sane.append(f)
1686 return sane
1645 return sane
1687
1646
1688 def _checklookup(self, files):
1647 def _checklookup(self, files):
1689 # check for any possibly clean files
1648 # check for any possibly clean files
1690 if not files:
1649 if not files:
1691 return [], [], []
1650 return [], [], []
1692
1651
1693 modified = []
1652 modified = []
1694 deleted = []
1653 deleted = []
1695 fixup = []
1654 fixup = []
1696 pctx = self._parents[0]
1655 pctx = self._parents[0]
1697 # do a full compare of any files that might have changed
1656 # do a full compare of any files that might have changed
1698 for f in sorted(files):
1657 for f in sorted(files):
1699 try:
1658 try:
1700 # This will return True for a file that got replaced by a
1659 # This will return True for a file that got replaced by a
1701 # directory in the interim, but fixing that is pretty hard.
1660 # directory in the interim, but fixing that is pretty hard.
1702 if (f not in pctx or self.flags(f) != pctx.flags(f)
1661 if (f not in pctx or self.flags(f) != pctx.flags(f)
1703 or pctx[f].cmp(self[f])):
1662 or pctx[f].cmp(self[f])):
1704 modified.append(f)
1663 modified.append(f)
1705 else:
1664 else:
1706 fixup.append(f)
1665 fixup.append(f)
1707 except (IOError, OSError):
1666 except (IOError, OSError):
1708 # A file become inaccessible in between? Mark it as deleted,
1667 # A file become inaccessible in between? Mark it as deleted,
1709 # matching dirstate behavior (issue5584).
1668 # matching dirstate behavior (issue5584).
1710 # The dirstate has more complex behavior around whether a
1669 # The dirstate has more complex behavior around whether a
1711 # missing file matches a directory, etc, but we don't need to
1670 # missing file matches a directory, etc, but we don't need to
1712 # bother with that: if f has made it to this point, we're sure
1671 # bother with that: if f has made it to this point, we're sure
1713 # it's in the dirstate.
1672 # it's in the dirstate.
1714 deleted.append(f)
1673 deleted.append(f)
1715
1674
1716 return modified, deleted, fixup
1675 return modified, deleted, fixup
1717
1676
1718 def _poststatusfixup(self, status, fixup):
1677 def _poststatusfixup(self, status, fixup):
1719 """update dirstate for files that are actually clean"""
1678 """update dirstate for files that are actually clean"""
1720 poststatus = self._repo.postdsstatus()
1679 poststatus = self._repo.postdsstatus()
1721 if fixup or poststatus:
1680 if fixup or poststatus:
1722 try:
1681 try:
1723 oldid = self._repo.dirstate.identity()
1682 oldid = self._repo.dirstate.identity()
1724
1683
1725 # updating the dirstate is optional
1684 # updating the dirstate is optional
1726 # so we don't wait on the lock
1685 # so we don't wait on the lock
1727 # wlock can invalidate the dirstate, so cache normal _after_
1686 # wlock can invalidate the dirstate, so cache normal _after_
1728 # taking the lock
1687 # taking the lock
1729 with self._repo.wlock(False):
1688 with self._repo.wlock(False):
1730 if self._repo.dirstate.identity() == oldid:
1689 if self._repo.dirstate.identity() == oldid:
1731 if fixup:
1690 if fixup:
1732 normal = self._repo.dirstate.normal
1691 normal = self._repo.dirstate.normal
1733 for f in fixup:
1692 for f in fixup:
1734 normal(f)
1693 normal(f)
1735 # write changes out explicitly, because nesting
1694 # write changes out explicitly, because nesting
1736 # wlock at runtime may prevent 'wlock.release()'
1695 # wlock at runtime may prevent 'wlock.release()'
1737 # after this block from doing so for subsequent
1696 # after this block from doing so for subsequent
1738 # changing files
1697 # changing files
1739 tr = self._repo.currenttransaction()
1698 tr = self._repo.currenttransaction()
1740 self._repo.dirstate.write(tr)
1699 self._repo.dirstate.write(tr)
1741
1700
1742 if poststatus:
1701 if poststatus:
1743 for ps in poststatus:
1702 for ps in poststatus:
1744 ps(self, status)
1703 ps(self, status)
1745 else:
1704 else:
1746 # in this case, writing changes out breaks
1705 # in this case, writing changes out breaks
1747 # consistency, because .hg/dirstate was
1706 # consistency, because .hg/dirstate was
1748 # already changed simultaneously after last
1707 # already changed simultaneously after last
1749 # caching (see also issue5584 for detail)
1708 # caching (see also issue5584 for detail)
1750 self._repo.ui.debug('skip updating dirstate: '
1709 self._repo.ui.debug('skip updating dirstate: '
1751 'identity mismatch\n')
1710 'identity mismatch\n')
1752 except error.LockError:
1711 except error.LockError:
1753 pass
1712 pass
1754 finally:
1713 finally:
1755 # Even if the wlock couldn't be grabbed, clear out the list.
1714 # Even if the wlock couldn't be grabbed, clear out the list.
1756 self._repo.clearpostdsstatus()
1715 self._repo.clearpostdsstatus()
1757
1716
1758 def _dirstatestatus(self, match, ignored=False, clean=False, unknown=False):
1717 def _dirstatestatus(self, match, ignored=False, clean=False, unknown=False):
1759 '''Gets the status from the dirstate -- internal use only.'''
1718 '''Gets the status from the dirstate -- internal use only.'''
1760 subrepos = []
1719 subrepos = []
1761 if '.hgsub' in self:
1720 if '.hgsub' in self:
1762 subrepos = sorted(self.substate)
1721 subrepos = sorted(self.substate)
1763 cmp, s = self._repo.dirstate.status(match, subrepos, ignored=ignored,
1722 cmp, s = self._repo.dirstate.status(match, subrepos, ignored=ignored,
1764 clean=clean, unknown=unknown)
1723 clean=clean, unknown=unknown)
1765
1724
1766 # check for any possibly clean files
1725 # check for any possibly clean files
1767 fixup = []
1726 fixup = []
1768 if cmp:
1727 if cmp:
1769 modified2, deleted2, fixup = self._checklookup(cmp)
1728 modified2, deleted2, fixup = self._checklookup(cmp)
1770 s.modified.extend(modified2)
1729 s.modified.extend(modified2)
1771 s.deleted.extend(deleted2)
1730 s.deleted.extend(deleted2)
1772
1731
1773 if fixup and clean:
1732 if fixup and clean:
1774 s.clean.extend(fixup)
1733 s.clean.extend(fixup)
1775
1734
1776 self._poststatusfixup(s, fixup)
1735 self._poststatusfixup(s, fixup)
1777
1736
1778 if match.always():
1737 if match.always():
1779 # cache for performance
1738 # cache for performance
1780 if s.unknown or s.ignored or s.clean:
1739 if s.unknown or s.ignored or s.clean:
1781 # "_status" is cached with list*=False in the normal route
1740 # "_status" is cached with list*=False in the normal route
1782 self._status = scmutil.status(s.modified, s.added, s.removed,
1741 self._status = scmutil.status(s.modified, s.added, s.removed,
1783 s.deleted, [], [], [])
1742 s.deleted, [], [], [])
1784 else:
1743 else:
1785 self._status = s
1744 self._status = s
1786
1745
1787 return s
1746 return s
1788
1747
1789 @propertycache
1748 @propertycache
1790 def _manifest(self):
1749 def _manifest(self):
1791 """generate a manifest corresponding to the values in self._status
1750 """generate a manifest corresponding to the values in self._status
1792
1751
1793 This reuse the file nodeid from parent, but we use special node
1752 This reuse the file nodeid from parent, but we use special node
1794 identifiers for added and modified files. This is used by manifests
1753 identifiers for added and modified files. This is used by manifests
1795 merge to see that files are different and by update logic to avoid
1754 merge to see that files are different and by update logic to avoid
1796 deleting newly added files.
1755 deleting newly added files.
1797 """
1756 """
1798 return self._buildstatusmanifest(self._status)
1757 return self._buildstatusmanifest(self._status)
1799
1758
1800 def _buildstatusmanifest(self, status):
1759 def _buildstatusmanifest(self, status):
1801 """Builds a manifest that includes the given status results."""
1760 """Builds a manifest that includes the given status results."""
1802 parents = self.parents()
1761 parents = self.parents()
1803
1762
1804 man = parents[0].manifest().copy()
1763 man = parents[0].manifest().copy()
1805
1764
1806 ff = self._flagfunc
1765 ff = self._flagfunc
1807 for i, l in ((addednodeid, status.added),
1766 for i, l in ((addednodeid, status.added),
1808 (modifiednodeid, status.modified)):
1767 (modifiednodeid, status.modified)):
1809 for f in l:
1768 for f in l:
1810 man[f] = i
1769 man[f] = i
1811 try:
1770 try:
1812 man.setflag(f, ff(f))
1771 man.setflag(f, ff(f))
1813 except OSError:
1772 except OSError:
1814 pass
1773 pass
1815
1774
1816 for f in status.deleted + status.removed:
1775 for f in status.deleted + status.removed:
1817 if f in man:
1776 if f in man:
1818 del man[f]
1777 del man[f]
1819
1778
1820 return man
1779 return man
1821
1780
1822 def _buildstatus(self, other, s, match, listignored, listclean,
1781 def _buildstatus(self, other, s, match, listignored, listclean,
1823 listunknown):
1782 listunknown):
1824 """build a status with respect to another context
1783 """build a status with respect to another context
1825
1784
1826 This includes logic for maintaining the fast path of status when
1785 This includes logic for maintaining the fast path of status when
1827 comparing the working directory against its parent, which is to skip
1786 comparing the working directory against its parent, which is to skip
1828 building a new manifest if self (working directory) is not comparing
1787 building a new manifest if self (working directory) is not comparing
1829 against its parent (repo['.']).
1788 against its parent (repo['.']).
1830 """
1789 """
1831 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1790 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1832 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1791 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1833 # might have accidentally ended up with the entire contents of the file
1792 # might have accidentally ended up with the entire contents of the file
1834 # they are supposed to be linking to.
1793 # they are supposed to be linking to.
1835 s.modified[:] = self._filtersuspectsymlink(s.modified)
1794 s.modified[:] = self._filtersuspectsymlink(s.modified)
1836 if other != self._repo['.']:
1795 if other != self._repo['.']:
1837 s = super(workingctx, self)._buildstatus(other, s, match,
1796 s = super(workingctx, self)._buildstatus(other, s, match,
1838 listignored, listclean,
1797 listignored, listclean,
1839 listunknown)
1798 listunknown)
1840 return s
1799 return s
1841
1800
1842 def _matchstatus(self, other, match):
1801 def _matchstatus(self, other, match):
1843 """override the match method with a filter for directory patterns
1802 """override the match method with a filter for directory patterns
1844
1803
1845 We use inheritance to customize the match.bad method only in cases of
1804 We use inheritance to customize the match.bad method only in cases of
1846 workingctx since it belongs only to the working directory when
1805 workingctx since it belongs only to the working directory when
1847 comparing against the parent changeset.
1806 comparing against the parent changeset.
1848
1807
1849 If we aren't comparing against the working directory's parent, then we
1808 If we aren't comparing against the working directory's parent, then we
1850 just use the default match object sent to us.
1809 just use the default match object sent to us.
1851 """
1810 """
1852 if other != self._repo['.']:
1811 if other != self._repo['.']:
1853 def bad(f, msg):
1812 def bad(f, msg):
1854 # 'f' may be a directory pattern from 'match.files()',
1813 # 'f' may be a directory pattern from 'match.files()',
1855 # so 'f not in ctx1' is not enough
1814 # so 'f not in ctx1' is not enough
1856 if f not in other and not other.hasdir(f):
1815 if f not in other and not other.hasdir(f):
1857 self._repo.ui.warn('%s: %s\n' %
1816 self._repo.ui.warn('%s: %s\n' %
1858 (self._repo.dirstate.pathto(f), msg))
1817 (self._repo.dirstate.pathto(f), msg))
1859 match.bad = bad
1818 match.bad = bad
1860 return match
1819 return match
1861
1820
1862 def markcommitted(self, node):
1821 def markcommitted(self, node):
1863 super(workingctx, self).markcommitted(node)
1822 super(workingctx, self).markcommitted(node)
1864
1823
1865 sparse.aftercommit(self._repo, node)
1824 sparse.aftercommit(self._repo, node)
1866
1825
1867 class committablefilectx(basefilectx):
1826 class committablefilectx(basefilectx):
1868 """A committablefilectx provides common functionality for a file context
1827 """A committablefilectx provides common functionality for a file context
1869 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1828 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1870 def __init__(self, repo, path, filelog=None, ctx=None):
1829 def __init__(self, repo, path, filelog=None, ctx=None):
1871 self._repo = repo
1830 self._repo = repo
1872 self._path = path
1831 self._path = path
1873 self._changeid = None
1832 self._changeid = None
1874 self._filerev = self._filenode = None
1833 self._filerev = self._filenode = None
1875
1834
1876 if filelog is not None:
1835 if filelog is not None:
1877 self._filelog = filelog
1836 self._filelog = filelog
1878 if ctx:
1837 if ctx:
1879 self._changectx = ctx
1838 self._changectx = ctx
1880
1839
1881 def __nonzero__(self):
1840 def __nonzero__(self):
1882 return True
1841 return True
1883
1842
1884 __bool__ = __nonzero__
1843 __bool__ = __nonzero__
1885
1844
1886 def linkrev(self):
1845 def linkrev(self):
1887 # linked to self._changectx no matter if file is modified or not
1846 # linked to self._changectx no matter if file is modified or not
1888 return self.rev()
1847 return self.rev()
1889
1848
1890 def parents(self):
1849 def parents(self):
1891 '''return parent filectxs, following copies if necessary'''
1850 '''return parent filectxs, following copies if necessary'''
1892 def filenode(ctx, path):
1851 def filenode(ctx, path):
1893 return ctx._manifest.get(path, nullid)
1852 return ctx._manifest.get(path, nullid)
1894
1853
1895 path = self._path
1854 path = self._path
1896 fl = self._filelog
1855 fl = self._filelog
1897 pcl = self._changectx._parents
1856 pcl = self._changectx._parents
1898 renamed = self.renamed()
1857 renamed = self.renamed()
1899
1858
1900 if renamed:
1859 if renamed:
1901 pl = [renamed + (None,)]
1860 pl = [renamed + (None,)]
1902 else:
1861 else:
1903 pl = [(path, filenode(pcl[0], path), fl)]
1862 pl = [(path, filenode(pcl[0], path), fl)]
1904
1863
1905 for pc in pcl[1:]:
1864 for pc in pcl[1:]:
1906 pl.append((path, filenode(pc, path), fl))
1865 pl.append((path, filenode(pc, path), fl))
1907
1866
1908 return [self._parentfilectx(p, fileid=n, filelog=l)
1867 return [self._parentfilectx(p, fileid=n, filelog=l)
1909 for p, n, l in pl if n != nullid]
1868 for p, n, l in pl if n != nullid]
1910
1869
1911 def children(self):
1870 def children(self):
1912 return []
1871 return []
1913
1872
1914 class workingfilectx(committablefilectx):
1873 class workingfilectx(committablefilectx):
1915 """A workingfilectx object makes access to data related to a particular
1874 """A workingfilectx object makes access to data related to a particular
1916 file in the working directory convenient."""
1875 file in the working directory convenient."""
1917 def __init__(self, repo, path, filelog=None, workingctx=None):
1876 def __init__(self, repo, path, filelog=None, workingctx=None):
1918 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1877 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1919
1878
1920 @propertycache
1879 @propertycache
1921 def _changectx(self):
1880 def _changectx(self):
1922 return workingctx(self._repo)
1881 return workingctx(self._repo)
1923
1882
1924 def data(self):
1883 def data(self):
1925 return self._repo.wread(self._path)
1884 return self._repo.wread(self._path)
1926 def renamed(self):
1885 def renamed(self):
1927 rp = self._repo.dirstate.copied(self._path)
1886 rp = self._repo.dirstate.copied(self._path)
1928 if not rp:
1887 if not rp:
1929 return None
1888 return None
1930 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1889 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1931
1890
1932 def size(self):
1891 def size(self):
1933 return self._repo.wvfs.lstat(self._path).st_size
1892 return self._repo.wvfs.lstat(self._path).st_size
1934 def date(self):
1893 def date(self):
1935 t, tz = self._changectx.date()
1894 t, tz = self._changectx.date()
1936 try:
1895 try:
1937 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1896 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1938 except OSError as err:
1897 except OSError as err:
1939 if err.errno != errno.ENOENT:
1898 if err.errno != errno.ENOENT:
1940 raise
1899 raise
1941 return (t, tz)
1900 return (t, tz)
1942
1901
1943 def exists(self):
1902 def exists(self):
1944 return self._repo.wvfs.exists(self._path)
1903 return self._repo.wvfs.exists(self._path)
1945
1904
1946 def lexists(self):
1905 def lexists(self):
1947 return self._repo.wvfs.lexists(self._path)
1906 return self._repo.wvfs.lexists(self._path)
1948
1907
1949 def audit(self):
1908 def audit(self):
1950 return self._repo.wvfs.audit(self._path)
1909 return self._repo.wvfs.audit(self._path)
1951
1910
1952 def cmp(self, fctx):
1911 def cmp(self, fctx):
1953 """compare with other file context
1912 """compare with other file context
1954
1913
1955 returns True if different than fctx.
1914 returns True if different than fctx.
1956 """
1915 """
1957 # fctx should be a filectx (not a workingfilectx)
1916 # fctx should be a filectx (not a workingfilectx)
1958 # invert comparison to reuse the same code path
1917 # invert comparison to reuse the same code path
1959 return fctx.cmp(self)
1918 return fctx.cmp(self)
1960
1919
1961 def remove(self, ignoremissing=False):
1920 def remove(self, ignoremissing=False):
1962 """wraps unlink for a repo's working directory"""
1921 """wraps unlink for a repo's working directory"""
1963 self._repo.wvfs.unlinkpath(self._path, ignoremissing=ignoremissing)
1922 self._repo.wvfs.unlinkpath(self._path, ignoremissing=ignoremissing)
1964
1923
1965 def write(self, data, flags, backgroundclose=False, **kwargs):
1924 def write(self, data, flags, backgroundclose=False, **kwargs):
1966 """wraps repo.wwrite"""
1925 """wraps repo.wwrite"""
1967 self._repo.wwrite(self._path, data, flags,
1926 self._repo.wwrite(self._path, data, flags,
1968 backgroundclose=backgroundclose,
1927 backgroundclose=backgroundclose,
1969 **kwargs)
1928 **kwargs)
1970
1929
1971 def markcopied(self, src):
1930 def markcopied(self, src):
1972 """marks this file a copy of `src`"""
1931 """marks this file a copy of `src`"""
1973 if self._repo.dirstate[self._path] in "nma":
1932 if self._repo.dirstate[self._path] in "nma":
1974 self._repo.dirstate.copy(src, self._path)
1933 self._repo.dirstate.copy(src, self._path)
1975
1934
1976 def clearunknown(self):
1935 def clearunknown(self):
1977 """Removes conflicting items in the working directory so that
1936 """Removes conflicting items in the working directory so that
1978 ``write()`` can be called successfully.
1937 ``write()`` can be called successfully.
1979 """
1938 """
1980 wvfs = self._repo.wvfs
1939 wvfs = self._repo.wvfs
1981 f = self._path
1940 f = self._path
1982 wvfs.audit(f)
1941 wvfs.audit(f)
1983 if wvfs.isdir(f) and not wvfs.islink(f):
1942 if wvfs.isdir(f) and not wvfs.islink(f):
1984 wvfs.rmtree(f, forcibly=True)
1943 wvfs.rmtree(f, forcibly=True)
1985 for p in reversed(list(util.finddirs(f))):
1944 for p in reversed(list(util.finddirs(f))):
1986 if wvfs.isfileorlink(p):
1945 if wvfs.isfileorlink(p):
1987 wvfs.unlink(p)
1946 wvfs.unlink(p)
1988 break
1947 break
1989
1948
1990 def setflags(self, l, x):
1949 def setflags(self, l, x):
1991 self._repo.wvfs.setflags(self._path, l, x)
1950 self._repo.wvfs.setflags(self._path, l, x)
1992
1951
1993 class overlayworkingctx(committablectx):
1952 class overlayworkingctx(committablectx):
1994 """Wraps another mutable context with a write-back cache that can be
1953 """Wraps another mutable context with a write-back cache that can be
1995 converted into a commit context.
1954 converted into a commit context.
1996
1955
1997 self._cache[path] maps to a dict with keys: {
1956 self._cache[path] maps to a dict with keys: {
1998 'exists': bool?
1957 'exists': bool?
1999 'date': date?
1958 'date': date?
2000 'data': str?
1959 'data': str?
2001 'flags': str?
1960 'flags': str?
2002 'copied': str? (path or None)
1961 'copied': str? (path or None)
2003 }
1962 }
2004 If `exists` is True, `flags` must be non-None and 'date' is non-None. If it
1963 If `exists` is True, `flags` must be non-None and 'date' is non-None. If it
2005 is `False`, the file was deleted.
1964 is `False`, the file was deleted.
2006 """
1965 """
2007
1966
2008 def __init__(self, repo):
1967 def __init__(self, repo):
2009 super(overlayworkingctx, self).__init__(repo)
1968 super(overlayworkingctx, self).__init__(repo)
2010 self._repo = repo
1969 self._repo = repo
2011 self.clean()
1970 self.clean()
2012
1971
2013 def setbase(self, wrappedctx):
1972 def setbase(self, wrappedctx):
2014 self._wrappedctx = wrappedctx
1973 self._wrappedctx = wrappedctx
2015 self._parents = [wrappedctx]
1974 self._parents = [wrappedctx]
2016 # Drop old manifest cache as it is now out of date.
1975 # Drop old manifest cache as it is now out of date.
2017 # This is necessary when, e.g., rebasing several nodes with one
1976 # This is necessary when, e.g., rebasing several nodes with one
2018 # ``overlayworkingctx`` (e.g. with --collapse).
1977 # ``overlayworkingctx`` (e.g. with --collapse).
2019 util.clearcachedproperty(self, '_manifest')
1978 util.clearcachedproperty(self, '_manifest')
2020
1979
2021 def data(self, path):
1980 def data(self, path):
2022 if self.isdirty(path):
1981 if self.isdirty(path):
2023 if self._cache[path]['exists']:
1982 if self._cache[path]['exists']:
2024 if self._cache[path]['data']:
1983 if self._cache[path]['data']:
2025 return self._cache[path]['data']
1984 return self._cache[path]['data']
2026 else:
1985 else:
2027 # Must fallback here, too, because we only set flags.
1986 # Must fallback here, too, because we only set flags.
2028 return self._wrappedctx[path].data()
1987 return self._wrappedctx[path].data()
2029 else:
1988 else:
2030 raise error.ProgrammingError("No such file or directory: %s" %
1989 raise error.ProgrammingError("No such file or directory: %s" %
2031 path)
1990 path)
2032 else:
1991 else:
2033 return self._wrappedctx[path].data()
1992 return self._wrappedctx[path].data()
2034
1993
2035 @propertycache
1994 @propertycache
2036 def _manifest(self):
1995 def _manifest(self):
2037 parents = self.parents()
1996 parents = self.parents()
2038 man = parents[0].manifest().copy()
1997 man = parents[0].manifest().copy()
2039
1998
2040 flag = self._flagfunc
1999 flag = self._flagfunc
2041 for path in self.added():
2000 for path in self.added():
2042 man[path] = addednodeid
2001 man[path] = addednodeid
2043 man.setflag(path, flag(path))
2002 man.setflag(path, flag(path))
2044 for path in self.modified():
2003 for path in self.modified():
2045 man[path] = modifiednodeid
2004 man[path] = modifiednodeid
2046 man.setflag(path, flag(path))
2005 man.setflag(path, flag(path))
2047 for path in self.removed():
2006 for path in self.removed():
2048 del man[path]
2007 del man[path]
2049 return man
2008 return man
2050
2009
2051 @propertycache
2010 @propertycache
2052 def _flagfunc(self):
2011 def _flagfunc(self):
2053 def f(path):
2012 def f(path):
2054 return self._cache[path]['flags']
2013 return self._cache[path]['flags']
2055 return f
2014 return f
2056
2015
2057 def files(self):
2016 def files(self):
2058 return sorted(self.added() + self.modified() + self.removed())
2017 return sorted(self.added() + self.modified() + self.removed())
2059
2018
2060 def modified(self):
2019 def modified(self):
2061 return [f for f in self._cache.keys() if self._cache[f]['exists'] and
2020 return [f for f in self._cache.keys() if self._cache[f]['exists'] and
2062 self._existsinparent(f)]
2021 self._existsinparent(f)]
2063
2022
2064 def added(self):
2023 def added(self):
2065 return [f for f in self._cache.keys() if self._cache[f]['exists'] and
2024 return [f for f in self._cache.keys() if self._cache[f]['exists'] and
2066 not self._existsinparent(f)]
2025 not self._existsinparent(f)]
2067
2026
2068 def removed(self):
2027 def removed(self):
2069 return [f for f in self._cache.keys() if
2028 return [f for f in self._cache.keys() if
2070 not self._cache[f]['exists'] and self._existsinparent(f)]
2029 not self._cache[f]['exists'] and self._existsinparent(f)]
2071
2030
2072 def isinmemory(self):
2031 def isinmemory(self):
2073 return True
2032 return True
2074
2033
2075 def filedate(self, path):
2034 def filedate(self, path):
2076 if self.isdirty(path):
2035 if self.isdirty(path):
2077 return self._cache[path]['date']
2036 return self._cache[path]['date']
2078 else:
2037 else:
2079 return self._wrappedctx[path].date()
2038 return self._wrappedctx[path].date()
2080
2039
2081 def markcopied(self, path, origin):
2040 def markcopied(self, path, origin):
2082 if self.isdirty(path):
2041 if self.isdirty(path):
2083 self._cache[path]['copied'] = origin
2042 self._cache[path]['copied'] = origin
2084 else:
2043 else:
2085 raise error.ProgrammingError('markcopied() called on clean context')
2044 raise error.ProgrammingError('markcopied() called on clean context')
2086
2045
2087 def copydata(self, path):
2046 def copydata(self, path):
2088 if self.isdirty(path):
2047 if self.isdirty(path):
2089 return self._cache[path]['copied']
2048 return self._cache[path]['copied']
2090 else:
2049 else:
2091 raise error.ProgrammingError('copydata() called on clean context')
2050 raise error.ProgrammingError('copydata() called on clean context')
2092
2051
2093 def flags(self, path):
2052 def flags(self, path):
2094 if self.isdirty(path):
2053 if self.isdirty(path):
2095 if self._cache[path]['exists']:
2054 if self._cache[path]['exists']:
2096 return self._cache[path]['flags']
2055 return self._cache[path]['flags']
2097 else:
2056 else:
2098 raise error.ProgrammingError("No such file or directory: %s" %
2057 raise error.ProgrammingError("No such file or directory: %s" %
2099 self._path)
2058 self._path)
2100 else:
2059 else:
2101 return self._wrappedctx[path].flags()
2060 return self._wrappedctx[path].flags()
2102
2061
2103 def _existsinparent(self, path):
2062 def _existsinparent(self, path):
2104 try:
2063 try:
2105 # ``commitctx` raises a ``ManifestLookupError`` if a path does not
2064 # ``commitctx` raises a ``ManifestLookupError`` if a path does not
2106 # exist, unlike ``workingctx``, which returns a ``workingfilectx``
2065 # exist, unlike ``workingctx``, which returns a ``workingfilectx``
2107 # with an ``exists()`` function.
2066 # with an ``exists()`` function.
2108 self._wrappedctx[path]
2067 self._wrappedctx[path]
2109 return True
2068 return True
2110 except error.ManifestLookupError:
2069 except error.ManifestLookupError:
2111 return False
2070 return False
2112
2071
2113 def _auditconflicts(self, path):
2072 def _auditconflicts(self, path):
2114 """Replicates conflict checks done by wvfs.write().
2073 """Replicates conflict checks done by wvfs.write().
2115
2074
2116 Since we never write to the filesystem and never call `applyupdates` in
2075 Since we never write to the filesystem and never call `applyupdates` in
2117 IMM, we'll never check that a path is actually writable -- e.g., because
2076 IMM, we'll never check that a path is actually writable -- e.g., because
2118 it adds `a/foo`, but `a` is actually a file in the other commit.
2077 it adds `a/foo`, but `a` is actually a file in the other commit.
2119 """
2078 """
2120 def fail(path, component):
2079 def fail(path, component):
2121 # p1() is the base and we're receiving "writes" for p2()'s
2080 # p1() is the base and we're receiving "writes" for p2()'s
2122 # files.
2081 # files.
2123 if 'l' in self.p1()[component].flags():
2082 if 'l' in self.p1()[component].flags():
2124 raise error.Abort("error: %s conflicts with symlink %s "
2083 raise error.Abort("error: %s conflicts with symlink %s "
2125 "in %s." % (path, component,
2084 "in %s." % (path, component,
2126 self.p1().rev()))
2085 self.p1().rev()))
2127 else:
2086 else:
2128 raise error.Abort("error: '%s' conflicts with file '%s' in "
2087 raise error.Abort("error: '%s' conflicts with file '%s' in "
2129 "%s." % (path, component,
2088 "%s." % (path, component,
2130 self.p1().rev()))
2089 self.p1().rev()))
2131
2090
2132 # Test that each new directory to be created to write this path from p2
2091 # Test that each new directory to be created to write this path from p2
2133 # is not a file in p1.
2092 # is not a file in p1.
2134 components = path.split('/')
2093 components = path.split('/')
2135 for i in xrange(len(components)):
2094 for i in xrange(len(components)):
2136 component = "/".join(components[0:i])
2095 component = "/".join(components[0:i])
2137 if component in self.p1():
2096 if component in self.p1():
2138 fail(path, component)
2097 fail(path, component)
2139
2098
2140 # Test the other direction -- that this path from p2 isn't a directory
2099 # Test the other direction -- that this path from p2 isn't a directory
2141 # in p1 (test that p1 doesn't any paths matching `path/*`).
2100 # in p1 (test that p1 doesn't any paths matching `path/*`).
2142 match = matchmod.match('/', '', [path + '/'], default=b'relpath')
2101 match = matchmod.match('/', '', [path + '/'], default=b'relpath')
2143 matches = self.p1().manifest().matches(match)
2102 matches = self.p1().manifest().matches(match)
2144 if len(matches) > 0:
2103 if len(matches) > 0:
2145 if len(matches) == 1 and matches.keys()[0] == path:
2104 if len(matches) == 1 and matches.keys()[0] == path:
2146 return
2105 return
2147 raise error.Abort("error: file '%s' cannot be written because "
2106 raise error.Abort("error: file '%s' cannot be written because "
2148 " '%s/' is a folder in %s (containing %d "
2107 " '%s/' is a folder in %s (containing %d "
2149 "entries: %s)"
2108 "entries: %s)"
2150 % (path, path, self.p1(), len(matches),
2109 % (path, path, self.p1(), len(matches),
2151 ', '.join(matches.keys())))
2110 ', '.join(matches.keys())))
2152
2111
2153 def write(self, path, data, flags='', **kwargs):
2112 def write(self, path, data, flags='', **kwargs):
2154 if data is None:
2113 if data is None:
2155 raise error.ProgrammingError("data must be non-None")
2114 raise error.ProgrammingError("data must be non-None")
2156 self._auditconflicts(path)
2115 self._auditconflicts(path)
2157 self._markdirty(path, exists=True, data=data, date=util.makedate(),
2116 self._markdirty(path, exists=True, data=data, date=util.makedate(),
2158 flags=flags)
2117 flags=flags)
2159
2118
2160 def setflags(self, path, l, x):
2119 def setflags(self, path, l, x):
2161 self._markdirty(path, exists=True, date=util.makedate(),
2120 self._markdirty(path, exists=True, date=util.makedate(),
2162 flags=(l and 'l' or '') + (x and 'x' or ''))
2121 flags=(l and 'l' or '') + (x and 'x' or ''))
2163
2122
2164 def remove(self, path):
2123 def remove(self, path):
2165 self._markdirty(path, exists=False)
2124 self._markdirty(path, exists=False)
2166
2125
2167 def exists(self, path):
2126 def exists(self, path):
2168 """exists behaves like `lexists`, but needs to follow symlinks and
2127 """exists behaves like `lexists`, but needs to follow symlinks and
2169 return False if they are broken.
2128 return False if they are broken.
2170 """
2129 """
2171 if self.isdirty(path):
2130 if self.isdirty(path):
2172 # If this path exists and is a symlink, "follow" it by calling
2131 # If this path exists and is a symlink, "follow" it by calling
2173 # exists on the destination path.
2132 # exists on the destination path.
2174 if (self._cache[path]['exists'] and
2133 if (self._cache[path]['exists'] and
2175 'l' in self._cache[path]['flags']):
2134 'l' in self._cache[path]['flags']):
2176 return self.exists(self._cache[path]['data'].strip())
2135 return self.exists(self._cache[path]['data'].strip())
2177 else:
2136 else:
2178 return self._cache[path]['exists']
2137 return self._cache[path]['exists']
2179
2138
2180 return self._existsinparent(path)
2139 return self._existsinparent(path)
2181
2140
2182 def lexists(self, path):
2141 def lexists(self, path):
2183 """lexists returns True if the path exists"""
2142 """lexists returns True if the path exists"""
2184 if self.isdirty(path):
2143 if self.isdirty(path):
2185 return self._cache[path]['exists']
2144 return self._cache[path]['exists']
2186
2145
2187 return self._existsinparent(path)
2146 return self._existsinparent(path)
2188
2147
2189 def size(self, path):
2148 def size(self, path):
2190 if self.isdirty(path):
2149 if self.isdirty(path):
2191 if self._cache[path]['exists']:
2150 if self._cache[path]['exists']:
2192 return len(self._cache[path]['data'])
2151 return len(self._cache[path]['data'])
2193 else:
2152 else:
2194 raise error.ProgrammingError("No such file or directory: %s" %
2153 raise error.ProgrammingError("No such file or directory: %s" %
2195 self._path)
2154 self._path)
2196 return self._wrappedctx[path].size()
2155 return self._wrappedctx[path].size()
2197
2156
2198 def tomemctx(self, text, branch=None, extra=None, date=None, parents=None,
2157 def tomemctx(self, text, branch=None, extra=None, date=None, parents=None,
2199 user=None, editor=None):
2158 user=None, editor=None):
2200 """Converts this ``overlayworkingctx`` into a ``memctx`` ready to be
2159 """Converts this ``overlayworkingctx`` into a ``memctx`` ready to be
2201 committed.
2160 committed.
2202
2161
2203 ``text`` is the commit message.
2162 ``text`` is the commit message.
2204 ``parents`` (optional) are rev numbers.
2163 ``parents`` (optional) are rev numbers.
2205 """
2164 """
2206 # Default parents to the wrapped contexts' if not passed.
2165 # Default parents to the wrapped contexts' if not passed.
2207 if parents is None:
2166 if parents is None:
2208 parents = self._wrappedctx.parents()
2167 parents = self._wrappedctx.parents()
2209 if len(parents) == 1:
2168 if len(parents) == 1:
2210 parents = (parents[0], None)
2169 parents = (parents[0], None)
2211
2170
2212 # ``parents`` is passed as rev numbers; convert to ``commitctxs``.
2171 # ``parents`` is passed as rev numbers; convert to ``commitctxs``.
2213 if parents[1] is None:
2172 if parents[1] is None:
2214 parents = (self._repo[parents[0]], None)
2173 parents = (self._repo[parents[0]], None)
2215 else:
2174 else:
2216 parents = (self._repo[parents[0]], self._repo[parents[1]])
2175 parents = (self._repo[parents[0]], self._repo[parents[1]])
2217
2176
2218 files = self._cache.keys()
2177 files = self._cache.keys()
2219 def getfile(repo, memctx, path):
2178 def getfile(repo, memctx, path):
2220 if self._cache[path]['exists']:
2179 if self._cache[path]['exists']:
2221 return memfilectx(repo, memctx, path,
2180 return memfilectx(repo, memctx, path,
2222 self._cache[path]['data'],
2181 self._cache[path]['data'],
2223 'l' in self._cache[path]['flags'],
2182 'l' in self._cache[path]['flags'],
2224 'x' in self._cache[path]['flags'],
2183 'x' in self._cache[path]['flags'],
2225 self._cache[path]['copied'])
2184 self._cache[path]['copied'])
2226 else:
2185 else:
2227 # Returning None, but including the path in `files`, is
2186 # Returning None, but including the path in `files`, is
2228 # necessary for memctx to register a deletion.
2187 # necessary for memctx to register a deletion.
2229 return None
2188 return None
2230 return memctx(self._repo, parents, text, files, getfile, date=date,
2189 return memctx(self._repo, parents, text, files, getfile, date=date,
2231 extra=extra, user=user, branch=branch, editor=editor)
2190 extra=extra, user=user, branch=branch, editor=editor)
2232
2191
2233 def isdirty(self, path):
2192 def isdirty(self, path):
2234 return path in self._cache
2193 return path in self._cache
2235
2194
2236 def isempty(self):
2195 def isempty(self):
2237 # We need to discard any keys that are actually clean before the empty
2196 # We need to discard any keys that are actually clean before the empty
2238 # commit check.
2197 # commit check.
2239 self._compact()
2198 self._compact()
2240 return len(self._cache) == 0
2199 return len(self._cache) == 0
2241
2200
2242 def clean(self):
2201 def clean(self):
2243 self._cache = {}
2202 self._cache = {}
2244
2203
2245 def _compact(self):
2204 def _compact(self):
2246 """Removes keys from the cache that are actually clean, by comparing
2205 """Removes keys from the cache that are actually clean, by comparing
2247 them with the underlying context.
2206 them with the underlying context.
2248
2207
2249 This can occur during the merge process, e.g. by passing --tool :local
2208 This can occur during the merge process, e.g. by passing --tool :local
2250 to resolve a conflict.
2209 to resolve a conflict.
2251 """
2210 """
2252 keys = []
2211 keys = []
2253 for path in self._cache.keys():
2212 for path in self._cache.keys():
2254 cache = self._cache[path]
2213 cache = self._cache[path]
2255 try:
2214 try:
2256 underlying = self._wrappedctx[path]
2215 underlying = self._wrappedctx[path]
2257 if (underlying.data() == cache['data'] and
2216 if (underlying.data() == cache['data'] and
2258 underlying.flags() == cache['flags']):
2217 underlying.flags() == cache['flags']):
2259 keys.append(path)
2218 keys.append(path)
2260 except error.ManifestLookupError:
2219 except error.ManifestLookupError:
2261 # Path not in the underlying manifest (created).
2220 # Path not in the underlying manifest (created).
2262 continue
2221 continue
2263
2222
2264 for path in keys:
2223 for path in keys:
2265 del self._cache[path]
2224 del self._cache[path]
2266 return keys
2225 return keys
2267
2226
2268 def _markdirty(self, path, exists, data=None, date=None, flags=''):
2227 def _markdirty(self, path, exists, data=None, date=None, flags=''):
2269 self._cache[path] = {
2228 self._cache[path] = {
2270 'exists': exists,
2229 'exists': exists,
2271 'data': data,
2230 'data': data,
2272 'date': date,
2231 'date': date,
2273 'flags': flags,
2232 'flags': flags,
2274 'copied': None,
2233 'copied': None,
2275 }
2234 }
2276
2235
2277 def filectx(self, path, filelog=None):
2236 def filectx(self, path, filelog=None):
2278 return overlayworkingfilectx(self._repo, path, parent=self,
2237 return overlayworkingfilectx(self._repo, path, parent=self,
2279 filelog=filelog)
2238 filelog=filelog)
2280
2239
2281 class overlayworkingfilectx(committablefilectx):
2240 class overlayworkingfilectx(committablefilectx):
2282 """Wrap a ``workingfilectx`` but intercepts all writes into an in-memory
2241 """Wrap a ``workingfilectx`` but intercepts all writes into an in-memory
2283 cache, which can be flushed through later by calling ``flush()``."""
2242 cache, which can be flushed through later by calling ``flush()``."""
2284
2243
2285 def __init__(self, repo, path, filelog=None, parent=None):
2244 def __init__(self, repo, path, filelog=None, parent=None):
2286 super(overlayworkingfilectx, self).__init__(repo, path, filelog,
2245 super(overlayworkingfilectx, self).__init__(repo, path, filelog,
2287 parent)
2246 parent)
2288 self._repo = repo
2247 self._repo = repo
2289 self._parent = parent
2248 self._parent = parent
2290 self._path = path
2249 self._path = path
2291
2250
2292 def cmp(self, fctx):
2251 def cmp(self, fctx):
2293 return self.data() != fctx.data()
2252 return self.data() != fctx.data()
2294
2253
2295 def changectx(self):
2254 def changectx(self):
2296 return self._parent
2255 return self._parent
2297
2256
2298 def data(self):
2257 def data(self):
2299 return self._parent.data(self._path)
2258 return self._parent.data(self._path)
2300
2259
2301 def date(self):
2260 def date(self):
2302 return self._parent.filedate(self._path)
2261 return self._parent.filedate(self._path)
2303
2262
2304 def exists(self):
2263 def exists(self):
2305 return self.lexists()
2264 return self.lexists()
2306
2265
2307 def lexists(self):
2266 def lexists(self):
2308 return self._parent.exists(self._path)
2267 return self._parent.exists(self._path)
2309
2268
2310 def renamed(self):
2269 def renamed(self):
2311 path = self._parent.copydata(self._path)
2270 path = self._parent.copydata(self._path)
2312 if not path:
2271 if not path:
2313 return None
2272 return None
2314 return path, self._changectx._parents[0]._manifest.get(path, nullid)
2273 return path, self._changectx._parents[0]._manifest.get(path, nullid)
2315
2274
2316 def size(self):
2275 def size(self):
2317 return self._parent.size(self._path)
2276 return self._parent.size(self._path)
2318
2277
2319 def markcopied(self, origin):
2278 def markcopied(self, origin):
2320 self._parent.markcopied(self._path, origin)
2279 self._parent.markcopied(self._path, origin)
2321
2280
2322 def audit(self):
2281 def audit(self):
2323 pass
2282 pass
2324
2283
2325 def flags(self):
2284 def flags(self):
2326 return self._parent.flags(self._path)
2285 return self._parent.flags(self._path)
2327
2286
2328 def setflags(self, islink, isexec):
2287 def setflags(self, islink, isexec):
2329 return self._parent.setflags(self._path, islink, isexec)
2288 return self._parent.setflags(self._path, islink, isexec)
2330
2289
2331 def write(self, data, flags, backgroundclose=False, **kwargs):
2290 def write(self, data, flags, backgroundclose=False, **kwargs):
2332 return self._parent.write(self._path, data, flags, **kwargs)
2291 return self._parent.write(self._path, data, flags, **kwargs)
2333
2292
2334 def remove(self, ignoremissing=False):
2293 def remove(self, ignoremissing=False):
2335 return self._parent.remove(self._path)
2294 return self._parent.remove(self._path)
2336
2295
2337 def clearunknown(self):
2296 def clearunknown(self):
2338 pass
2297 pass
2339
2298
2340 class workingcommitctx(workingctx):
2299 class workingcommitctx(workingctx):
2341 """A workingcommitctx object makes access to data related to
2300 """A workingcommitctx object makes access to data related to
2342 the revision being committed convenient.
2301 the revision being committed convenient.
2343
2302
2344 This hides changes in the working directory, if they aren't
2303 This hides changes in the working directory, if they aren't
2345 committed in this context.
2304 committed in this context.
2346 """
2305 """
2347 def __init__(self, repo, changes,
2306 def __init__(self, repo, changes,
2348 text="", user=None, date=None, extra=None):
2307 text="", user=None, date=None, extra=None):
2349 super(workingctx, self).__init__(repo, text, user, date, extra,
2308 super(workingctx, self).__init__(repo, text, user, date, extra,
2350 changes)
2309 changes)
2351
2310
2352 def _dirstatestatus(self, match, ignored=False, clean=False, unknown=False):
2311 def _dirstatestatus(self, match, ignored=False, clean=False, unknown=False):
2353 """Return matched files only in ``self._status``
2312 """Return matched files only in ``self._status``
2354
2313
2355 Uncommitted files appear "clean" via this context, even if
2314 Uncommitted files appear "clean" via this context, even if
2356 they aren't actually so in the working directory.
2315 they aren't actually so in the working directory.
2357 """
2316 """
2358 if clean:
2317 if clean:
2359 clean = [f for f in self._manifest if f not in self._changedset]
2318 clean = [f for f in self._manifest if f not in self._changedset]
2360 else:
2319 else:
2361 clean = []
2320 clean = []
2362 return scmutil.status([f for f in self._status.modified if match(f)],
2321 return scmutil.status([f for f in self._status.modified if match(f)],
2363 [f for f in self._status.added if match(f)],
2322 [f for f in self._status.added if match(f)],
2364 [f for f in self._status.removed if match(f)],
2323 [f for f in self._status.removed if match(f)],
2365 [], [], [], clean)
2324 [], [], [], clean)
2366
2325
2367 @propertycache
2326 @propertycache
2368 def _changedset(self):
2327 def _changedset(self):
2369 """Return the set of files changed in this context
2328 """Return the set of files changed in this context
2370 """
2329 """
2371 changed = set(self._status.modified)
2330 changed = set(self._status.modified)
2372 changed.update(self._status.added)
2331 changed.update(self._status.added)
2373 changed.update(self._status.removed)
2332 changed.update(self._status.removed)
2374 return changed
2333 return changed
2375
2334
2376 def makecachingfilectxfn(func):
2335 def makecachingfilectxfn(func):
2377 """Create a filectxfn that caches based on the path.
2336 """Create a filectxfn that caches based on the path.
2378
2337
2379 We can't use util.cachefunc because it uses all arguments as the cache
2338 We can't use util.cachefunc because it uses all arguments as the cache
2380 key and this creates a cycle since the arguments include the repo and
2339 key and this creates a cycle since the arguments include the repo and
2381 memctx.
2340 memctx.
2382 """
2341 """
2383 cache = {}
2342 cache = {}
2384
2343
2385 def getfilectx(repo, memctx, path):
2344 def getfilectx(repo, memctx, path):
2386 if path not in cache:
2345 if path not in cache:
2387 cache[path] = func(repo, memctx, path)
2346 cache[path] = func(repo, memctx, path)
2388 return cache[path]
2347 return cache[path]
2389
2348
2390 return getfilectx
2349 return getfilectx
2391
2350
2392 def memfilefromctx(ctx):
2351 def memfilefromctx(ctx):
2393 """Given a context return a memfilectx for ctx[path]
2352 """Given a context return a memfilectx for ctx[path]
2394
2353
2395 This is a convenience method for building a memctx based on another
2354 This is a convenience method for building a memctx based on another
2396 context.
2355 context.
2397 """
2356 """
2398 def getfilectx(repo, memctx, path):
2357 def getfilectx(repo, memctx, path):
2399 fctx = ctx[path]
2358 fctx = ctx[path]
2400 # this is weird but apparently we only keep track of one parent
2359 # this is weird but apparently we only keep track of one parent
2401 # (why not only store that instead of a tuple?)
2360 # (why not only store that instead of a tuple?)
2402 copied = fctx.renamed()
2361 copied = fctx.renamed()
2403 if copied:
2362 if copied:
2404 copied = copied[0]
2363 copied = copied[0]
2405 return memfilectx(repo, memctx, path, fctx.data(),
2364 return memfilectx(repo, memctx, path, fctx.data(),
2406 islink=fctx.islink(), isexec=fctx.isexec(),
2365 islink=fctx.islink(), isexec=fctx.isexec(),
2407 copied=copied)
2366 copied=copied)
2408
2367
2409 return getfilectx
2368 return getfilectx
2410
2369
2411 def memfilefrompatch(patchstore):
2370 def memfilefrompatch(patchstore):
2412 """Given a patch (e.g. patchstore object) return a memfilectx
2371 """Given a patch (e.g. patchstore object) return a memfilectx
2413
2372
2414 This is a convenience method for building a memctx based on a patchstore.
2373 This is a convenience method for building a memctx based on a patchstore.
2415 """
2374 """
2416 def getfilectx(repo, memctx, path):
2375 def getfilectx(repo, memctx, path):
2417 data, mode, copied = patchstore.getfile(path)
2376 data, mode, copied = patchstore.getfile(path)
2418 if data is None:
2377 if data is None:
2419 return None
2378 return None
2420 islink, isexec = mode
2379 islink, isexec = mode
2421 return memfilectx(repo, memctx, path, data, islink=islink,
2380 return memfilectx(repo, memctx, path, data, islink=islink,
2422 isexec=isexec, copied=copied)
2381 isexec=isexec, copied=copied)
2423
2382
2424 return getfilectx
2383 return getfilectx
2425
2384
2426 class memctx(committablectx):
2385 class memctx(committablectx):
2427 """Use memctx to perform in-memory commits via localrepo.commitctx().
2386 """Use memctx to perform in-memory commits via localrepo.commitctx().
2428
2387
2429 Revision information is supplied at initialization time while
2388 Revision information is supplied at initialization time while
2430 related files data and is made available through a callback
2389 related files data and is made available through a callback
2431 mechanism. 'repo' is the current localrepo, 'parents' is a
2390 mechanism. 'repo' is the current localrepo, 'parents' is a
2432 sequence of two parent revisions identifiers (pass None for every
2391 sequence of two parent revisions identifiers (pass None for every
2433 missing parent), 'text' is the commit message and 'files' lists
2392 missing parent), 'text' is the commit message and 'files' lists
2434 names of files touched by the revision (normalized and relative to
2393 names of files touched by the revision (normalized and relative to
2435 repository root).
2394 repository root).
2436
2395
2437 filectxfn(repo, memctx, path) is a callable receiving the
2396 filectxfn(repo, memctx, path) is a callable receiving the
2438 repository, the current memctx object and the normalized path of
2397 repository, the current memctx object and the normalized path of
2439 requested file, relative to repository root. It is fired by the
2398 requested file, relative to repository root. It is fired by the
2440 commit function for every file in 'files', but calls order is
2399 commit function for every file in 'files', but calls order is
2441 undefined. If the file is available in the revision being
2400 undefined. If the file is available in the revision being
2442 committed (updated or added), filectxfn returns a memfilectx
2401 committed (updated or added), filectxfn returns a memfilectx
2443 object. If the file was removed, filectxfn return None for recent
2402 object. If the file was removed, filectxfn return None for recent
2444 Mercurial. Moved files are represented by marking the source file
2403 Mercurial. Moved files are represented by marking the source file
2445 removed and the new file added with copy information (see
2404 removed and the new file added with copy information (see
2446 memfilectx).
2405 memfilectx).
2447
2406
2448 user receives the committer name and defaults to current
2407 user receives the committer name and defaults to current
2449 repository username, date is the commit date in any format
2408 repository username, date is the commit date in any format
2450 supported by util.parsedate() and defaults to current date, extra
2409 supported by util.parsedate() and defaults to current date, extra
2451 is a dictionary of metadata or is left empty.
2410 is a dictionary of metadata or is left empty.
2452 """
2411 """
2453
2412
2454 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
2413 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
2455 # Extensions that need to retain compatibility across Mercurial 3.1 can use
2414 # Extensions that need to retain compatibility across Mercurial 3.1 can use
2456 # this field to determine what to do in filectxfn.
2415 # this field to determine what to do in filectxfn.
2457 _returnnoneformissingfiles = True
2416 _returnnoneformissingfiles = True
2458
2417
2459 def __init__(self, repo, parents, text, files, filectxfn, user=None,
2418 def __init__(self, repo, parents, text, files, filectxfn, user=None,
2460 date=None, extra=None, branch=None, editor=False):
2419 date=None, extra=None, branch=None, editor=False):
2461 super(memctx, self).__init__(repo, text, user, date, extra)
2420 super(memctx, self).__init__(repo, text, user, date, extra)
2462 self._rev = None
2421 self._rev = None
2463 self._node = None
2422 self._node = None
2464 parents = [(p or nullid) for p in parents]
2423 parents = [(p or nullid) for p in parents]
2465 p1, p2 = parents
2424 p1, p2 = parents
2466 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
2425 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
2467 files = sorted(set(files))
2426 files = sorted(set(files))
2468 self._files = files
2427 self._files = files
2469 if branch is not None:
2428 if branch is not None:
2470 self._extra['branch'] = encoding.fromlocal(branch)
2429 self._extra['branch'] = encoding.fromlocal(branch)
2471 self.substate = {}
2430 self.substate = {}
2472
2431
2473 if isinstance(filectxfn, patch.filestore):
2432 if isinstance(filectxfn, patch.filestore):
2474 filectxfn = memfilefrompatch(filectxfn)
2433 filectxfn = memfilefrompatch(filectxfn)
2475 elif not callable(filectxfn):
2434 elif not callable(filectxfn):
2476 # if store is not callable, wrap it in a function
2435 # if store is not callable, wrap it in a function
2477 filectxfn = memfilefromctx(filectxfn)
2436 filectxfn = memfilefromctx(filectxfn)
2478
2437
2479 # memoizing increases performance for e.g. vcs convert scenarios.
2438 # memoizing increases performance for e.g. vcs convert scenarios.
2480 self._filectxfn = makecachingfilectxfn(filectxfn)
2439 self._filectxfn = makecachingfilectxfn(filectxfn)
2481
2440
2482 if editor:
2441 if editor:
2483 self._text = editor(self._repo, self, [])
2442 self._text = editor(self._repo, self, [])
2484 self._repo.savecommitmessage(self._text)
2443 self._repo.savecommitmessage(self._text)
2485
2444
2486 def filectx(self, path, filelog=None):
2445 def filectx(self, path, filelog=None):
2487 """get a file context from the working directory
2446 """get a file context from the working directory
2488
2447
2489 Returns None if file doesn't exist and should be removed."""
2448 Returns None if file doesn't exist and should be removed."""
2490 return self._filectxfn(self._repo, self, path)
2449 return self._filectxfn(self._repo, self, path)
2491
2450
2492 def commit(self):
2451 def commit(self):
2493 """commit context to the repo"""
2452 """commit context to the repo"""
2494 return self._repo.commitctx(self)
2453 return self._repo.commitctx(self)
2495
2454
2496 @propertycache
2455 @propertycache
2497 def _manifest(self):
2456 def _manifest(self):
2498 """generate a manifest based on the return values of filectxfn"""
2457 """generate a manifest based on the return values of filectxfn"""
2499
2458
2500 # keep this simple for now; just worry about p1
2459 # keep this simple for now; just worry about p1
2501 pctx = self._parents[0]
2460 pctx = self._parents[0]
2502 man = pctx.manifest().copy()
2461 man = pctx.manifest().copy()
2503
2462
2504 for f in self._status.modified:
2463 for f in self._status.modified:
2505 p1node = nullid
2464 p1node = nullid
2506 p2node = nullid
2465 p2node = nullid
2507 p = pctx[f].parents() # if file isn't in pctx, check p2?
2466 p = pctx[f].parents() # if file isn't in pctx, check p2?
2508 if len(p) > 0:
2467 if len(p) > 0:
2509 p1node = p[0].filenode()
2468 p1node = p[0].filenode()
2510 if len(p) > 1:
2469 if len(p) > 1:
2511 p2node = p[1].filenode()
2470 p2node = p[1].filenode()
2512 man[f] = revlog.hash(self[f].data(), p1node, p2node)
2471 man[f] = revlog.hash(self[f].data(), p1node, p2node)
2513
2472
2514 for f in self._status.added:
2473 for f in self._status.added:
2515 man[f] = revlog.hash(self[f].data(), nullid, nullid)
2474 man[f] = revlog.hash(self[f].data(), nullid, nullid)
2516
2475
2517 for f in self._status.removed:
2476 for f in self._status.removed:
2518 if f in man:
2477 if f in man:
2519 del man[f]
2478 del man[f]
2520
2479
2521 return man
2480 return man
2522
2481
2523 @propertycache
2482 @propertycache
2524 def _status(self):
2483 def _status(self):
2525 """Calculate exact status from ``files`` specified at construction
2484 """Calculate exact status from ``files`` specified at construction
2526 """
2485 """
2527 man1 = self.p1().manifest()
2486 man1 = self.p1().manifest()
2528 p2 = self._parents[1]
2487 p2 = self._parents[1]
2529 # "1 < len(self._parents)" can't be used for checking
2488 # "1 < len(self._parents)" can't be used for checking
2530 # existence of the 2nd parent, because "memctx._parents" is
2489 # existence of the 2nd parent, because "memctx._parents" is
2531 # explicitly initialized by the list, of which length is 2.
2490 # explicitly initialized by the list, of which length is 2.
2532 if p2.node() != nullid:
2491 if p2.node() != nullid:
2533 man2 = p2.manifest()
2492 man2 = p2.manifest()
2534 managing = lambda f: f in man1 or f in man2
2493 managing = lambda f: f in man1 or f in man2
2535 else:
2494 else:
2536 managing = lambda f: f in man1
2495 managing = lambda f: f in man1
2537
2496
2538 modified, added, removed = [], [], []
2497 modified, added, removed = [], [], []
2539 for f in self._files:
2498 for f in self._files:
2540 if not managing(f):
2499 if not managing(f):
2541 added.append(f)
2500 added.append(f)
2542 elif self[f]:
2501 elif self[f]:
2543 modified.append(f)
2502 modified.append(f)
2544 else:
2503 else:
2545 removed.append(f)
2504 removed.append(f)
2546
2505
2547 return scmutil.status(modified, added, removed, [], [], [], [])
2506 return scmutil.status(modified, added, removed, [], [], [], [])
2548
2507
2549 class memfilectx(committablefilectx):
2508 class memfilectx(committablefilectx):
2550 """memfilectx represents an in-memory file to commit.
2509 """memfilectx represents an in-memory file to commit.
2551
2510
2552 See memctx and committablefilectx for more details.
2511 See memctx and committablefilectx for more details.
2553 """
2512 """
2554 def __init__(self, repo, changectx, path, data, islink=False,
2513 def __init__(self, repo, changectx, path, data, islink=False,
2555 isexec=False, copied=None):
2514 isexec=False, copied=None):
2556 """
2515 """
2557 path is the normalized file path relative to repository root.
2516 path is the normalized file path relative to repository root.
2558 data is the file content as a string.
2517 data is the file content as a string.
2559 islink is True if the file is a symbolic link.
2518 islink is True if the file is a symbolic link.
2560 isexec is True if the file is executable.
2519 isexec is True if the file is executable.
2561 copied is the source file path if current file was copied in the
2520 copied is the source file path if current file was copied in the
2562 revision being committed, or None."""
2521 revision being committed, or None."""
2563 super(memfilectx, self).__init__(repo, path, None, changectx)
2522 super(memfilectx, self).__init__(repo, path, None, changectx)
2564 self._data = data
2523 self._data = data
2565 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
2524 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
2566 self._copied = None
2525 self._copied = None
2567 if copied:
2526 if copied:
2568 self._copied = (copied, nullid)
2527 self._copied = (copied, nullid)
2569
2528
2570 def data(self):
2529 def data(self):
2571 return self._data
2530 return self._data
2572
2531
2573 def remove(self, ignoremissing=False):
2532 def remove(self, ignoremissing=False):
2574 """wraps unlink for a repo's working directory"""
2533 """wraps unlink for a repo's working directory"""
2575 # need to figure out what to do here
2534 # need to figure out what to do here
2576 del self._changectx[self._path]
2535 del self._changectx[self._path]
2577
2536
2578 def write(self, data, flags, **kwargs):
2537 def write(self, data, flags, **kwargs):
2579 """wraps repo.wwrite"""
2538 """wraps repo.wwrite"""
2580 self._data = data
2539 self._data = data
2581
2540
2582 class overlayfilectx(committablefilectx):
2541 class overlayfilectx(committablefilectx):
2583 """Like memfilectx but take an original filectx and optional parameters to
2542 """Like memfilectx but take an original filectx and optional parameters to
2584 override parts of it. This is useful when fctx.data() is expensive (i.e.
2543 override parts of it. This is useful when fctx.data() is expensive (i.e.
2585 flag processor is expensive) and raw data, flags, and filenode could be
2544 flag processor is expensive) and raw data, flags, and filenode could be
2586 reused (ex. rebase or mode-only amend a REVIDX_EXTSTORED file).
2545 reused (ex. rebase or mode-only amend a REVIDX_EXTSTORED file).
2587 """
2546 """
2588
2547
2589 def __init__(self, originalfctx, datafunc=None, path=None, flags=None,
2548 def __init__(self, originalfctx, datafunc=None, path=None, flags=None,
2590 copied=None, ctx=None):
2549 copied=None, ctx=None):
2591 """originalfctx: filecontext to duplicate
2550 """originalfctx: filecontext to duplicate
2592
2551
2593 datafunc: None or a function to override data (file content). It is a
2552 datafunc: None or a function to override data (file content). It is a
2594 function to be lazy. path, flags, copied, ctx: None or overridden value
2553 function to be lazy. path, flags, copied, ctx: None or overridden value
2595
2554
2596 copied could be (path, rev), or False. copied could also be just path,
2555 copied could be (path, rev), or False. copied could also be just path,
2597 and will be converted to (path, nullid). This simplifies some callers.
2556 and will be converted to (path, nullid). This simplifies some callers.
2598 """
2557 """
2599
2558
2600 if path is None:
2559 if path is None:
2601 path = originalfctx.path()
2560 path = originalfctx.path()
2602 if ctx is None:
2561 if ctx is None:
2603 ctx = originalfctx.changectx()
2562 ctx = originalfctx.changectx()
2604 ctxmatch = lambda: True
2563 ctxmatch = lambda: True
2605 else:
2564 else:
2606 ctxmatch = lambda: ctx == originalfctx.changectx()
2565 ctxmatch = lambda: ctx == originalfctx.changectx()
2607
2566
2608 repo = originalfctx.repo()
2567 repo = originalfctx.repo()
2609 flog = originalfctx.filelog()
2568 flog = originalfctx.filelog()
2610 super(overlayfilectx, self).__init__(repo, path, flog, ctx)
2569 super(overlayfilectx, self).__init__(repo, path, flog, ctx)
2611
2570
2612 if copied is None:
2571 if copied is None:
2613 copied = originalfctx.renamed()
2572 copied = originalfctx.renamed()
2614 copiedmatch = lambda: True
2573 copiedmatch = lambda: True
2615 else:
2574 else:
2616 if copied and not isinstance(copied, tuple):
2575 if copied and not isinstance(copied, tuple):
2617 # repo._filecommit will recalculate copyrev so nullid is okay
2576 # repo._filecommit will recalculate copyrev so nullid is okay
2618 copied = (copied, nullid)
2577 copied = (copied, nullid)
2619 copiedmatch = lambda: copied == originalfctx.renamed()
2578 copiedmatch = lambda: copied == originalfctx.renamed()
2620
2579
2621 # When data, copied (could affect data), ctx (could affect filelog
2580 # When data, copied (could affect data), ctx (could affect filelog
2622 # parents) are not overridden, rawdata, rawflags, and filenode may be
2581 # parents) are not overridden, rawdata, rawflags, and filenode may be
2623 # reused (repo._filecommit should double check filelog parents).
2582 # reused (repo._filecommit should double check filelog parents).
2624 #
2583 #
2625 # path, flags are not hashed in filelog (but in manifestlog) so they do
2584 # path, flags are not hashed in filelog (but in manifestlog) so they do
2626 # not affect reusable here.
2585 # not affect reusable here.
2627 #
2586 #
2628 # If ctx or copied is overridden to a same value with originalfctx,
2587 # If ctx or copied is overridden to a same value with originalfctx,
2629 # still consider it's reusable. originalfctx.renamed() may be a bit
2588 # still consider it's reusable. originalfctx.renamed() may be a bit
2630 # expensive so it's not called unless necessary. Assuming datafunc is
2589 # expensive so it's not called unless necessary. Assuming datafunc is
2631 # always expensive, do not call it for this "reusable" test.
2590 # always expensive, do not call it for this "reusable" test.
2632 reusable = datafunc is None and ctxmatch() and copiedmatch()
2591 reusable = datafunc is None and ctxmatch() and copiedmatch()
2633
2592
2634 if datafunc is None:
2593 if datafunc is None:
2635 datafunc = originalfctx.data
2594 datafunc = originalfctx.data
2636 if flags is None:
2595 if flags is None:
2637 flags = originalfctx.flags()
2596 flags = originalfctx.flags()
2638
2597
2639 self._datafunc = datafunc
2598 self._datafunc = datafunc
2640 self._flags = flags
2599 self._flags = flags
2641 self._copied = copied
2600 self._copied = copied
2642
2601
2643 if reusable:
2602 if reusable:
2644 # copy extra fields from originalfctx
2603 # copy extra fields from originalfctx
2645 attrs = ['rawdata', 'rawflags', '_filenode', '_filerev']
2604 attrs = ['rawdata', 'rawflags', '_filenode', '_filerev']
2646 for attr_ in attrs:
2605 for attr_ in attrs:
2647 if util.safehasattr(originalfctx, attr_):
2606 if util.safehasattr(originalfctx, attr_):
2648 setattr(self, attr_, getattr(originalfctx, attr_))
2607 setattr(self, attr_, getattr(originalfctx, attr_))
2649
2608
2650 def data(self):
2609 def data(self):
2651 return self._datafunc()
2610 return self._datafunc()
2652
2611
2653 class metadataonlyctx(committablectx):
2612 class metadataonlyctx(committablectx):
2654 """Like memctx but it's reusing the manifest of different commit.
2613 """Like memctx but it's reusing the manifest of different commit.
2655 Intended to be used by lightweight operations that are creating
2614 Intended to be used by lightweight operations that are creating
2656 metadata-only changes.
2615 metadata-only changes.
2657
2616
2658 Revision information is supplied at initialization time. 'repo' is the
2617 Revision information is supplied at initialization time. 'repo' is the
2659 current localrepo, 'ctx' is original revision which manifest we're reuisng
2618 current localrepo, 'ctx' is original revision which manifest we're reuisng
2660 'parents' is a sequence of two parent revisions identifiers (pass None for
2619 'parents' is a sequence of two parent revisions identifiers (pass None for
2661 every missing parent), 'text' is the commit.
2620 every missing parent), 'text' is the commit.
2662
2621
2663 user receives the committer name and defaults to current repository
2622 user receives the committer name and defaults to current repository
2664 username, date is the commit date in any format supported by
2623 username, date is the commit date in any format supported by
2665 util.parsedate() and defaults to current date, extra is a dictionary of
2624 util.parsedate() and defaults to current date, extra is a dictionary of
2666 metadata or is left empty.
2625 metadata or is left empty.
2667 """
2626 """
2668 def __new__(cls, repo, originalctx, *args, **kwargs):
2627 def __new__(cls, repo, originalctx, *args, **kwargs):
2669 return super(metadataonlyctx, cls).__new__(cls, repo)
2628 return super(metadataonlyctx, cls).__new__(cls, repo)
2670
2629
2671 def __init__(self, repo, originalctx, parents=None, text=None, user=None,
2630 def __init__(self, repo, originalctx, parents=None, text=None, user=None,
2672 date=None, extra=None, editor=False):
2631 date=None, extra=None, editor=False):
2673 if text is None:
2632 if text is None:
2674 text = originalctx.description()
2633 text = originalctx.description()
2675 super(metadataonlyctx, self).__init__(repo, text, user, date, extra)
2634 super(metadataonlyctx, self).__init__(repo, text, user, date, extra)
2676 self._rev = None
2635 self._rev = None
2677 self._node = None
2636 self._node = None
2678 self._originalctx = originalctx
2637 self._originalctx = originalctx
2679 self._manifestnode = originalctx.manifestnode()
2638 self._manifestnode = originalctx.manifestnode()
2680 if parents is None:
2639 if parents is None:
2681 parents = originalctx.parents()
2640 parents = originalctx.parents()
2682 else:
2641 else:
2683 parents = [repo[p] for p in parents if p is not None]
2642 parents = [repo[p] for p in parents if p is not None]
2684 parents = parents[:]
2643 parents = parents[:]
2685 while len(parents) < 2:
2644 while len(parents) < 2:
2686 parents.append(repo[nullid])
2645 parents.append(repo[nullid])
2687 p1, p2 = self._parents = parents
2646 p1, p2 = self._parents = parents
2688
2647
2689 # sanity check to ensure that the reused manifest parents are
2648 # sanity check to ensure that the reused manifest parents are
2690 # manifests of our commit parents
2649 # manifests of our commit parents
2691 mp1, mp2 = self.manifestctx().parents
2650 mp1, mp2 = self.manifestctx().parents
2692 if p1 != nullid and p1.manifestnode() != mp1:
2651 if p1 != nullid and p1.manifestnode() != mp1:
2693 raise RuntimeError('can\'t reuse the manifest: '
2652 raise RuntimeError('can\'t reuse the manifest: '
2694 'its p1 doesn\'t match the new ctx p1')
2653 'its p1 doesn\'t match the new ctx p1')
2695 if p2 != nullid and p2.manifestnode() != mp2:
2654 if p2 != nullid and p2.manifestnode() != mp2:
2696 raise RuntimeError('can\'t reuse the manifest: '
2655 raise RuntimeError('can\'t reuse the manifest: '
2697 'its p2 doesn\'t match the new ctx p2')
2656 'its p2 doesn\'t match the new ctx p2')
2698
2657
2699 self._files = originalctx.files()
2658 self._files = originalctx.files()
2700 self.substate = {}
2659 self.substate = {}
2701
2660
2702 if editor:
2661 if editor:
2703 self._text = editor(self._repo, self, [])
2662 self._text = editor(self._repo, self, [])
2704 self._repo.savecommitmessage(self._text)
2663 self._repo.savecommitmessage(self._text)
2705
2664
2706 def manifestnode(self):
2665 def manifestnode(self):
2707 return self._manifestnode
2666 return self._manifestnode
2708
2667
2709 @property
2668 @property
2710 def _manifestctx(self):
2669 def _manifestctx(self):
2711 return self._repo.manifestlog[self._manifestnode]
2670 return self._repo.manifestlog[self._manifestnode]
2712
2671
2713 def filectx(self, path, filelog=None):
2672 def filectx(self, path, filelog=None):
2714 return self._originalctx.filectx(path, filelog=filelog)
2673 return self._originalctx.filectx(path, filelog=filelog)
2715
2674
2716 def commit(self):
2675 def commit(self):
2717 """commit context to the repo"""
2676 """commit context to the repo"""
2718 return self._repo.commitctx(self)
2677 return self._repo.commitctx(self)
2719
2678
2720 @property
2679 @property
2721 def _manifest(self):
2680 def _manifest(self):
2722 return self._originalctx.manifest()
2681 return self._originalctx.manifest()
2723
2682
2724 @propertycache
2683 @propertycache
2725 def _status(self):
2684 def _status(self):
2726 """Calculate exact status from ``files`` specified in the ``origctx``
2685 """Calculate exact status from ``files`` specified in the ``origctx``
2727 and parents manifests.
2686 and parents manifests.
2728 """
2687 """
2729 man1 = self.p1().manifest()
2688 man1 = self.p1().manifest()
2730 p2 = self._parents[1]
2689 p2 = self._parents[1]
2731 # "1 < len(self._parents)" can't be used for checking
2690 # "1 < len(self._parents)" can't be used for checking
2732 # existence of the 2nd parent, because "metadataonlyctx._parents" is
2691 # existence of the 2nd parent, because "metadataonlyctx._parents" is
2733 # explicitly initialized by the list, of which length is 2.
2692 # explicitly initialized by the list, of which length is 2.
2734 if p2.node() != nullid:
2693 if p2.node() != nullid:
2735 man2 = p2.manifest()
2694 man2 = p2.manifest()
2736 managing = lambda f: f in man1 or f in man2
2695 managing = lambda f: f in man1 or f in man2
2737 else:
2696 else:
2738 managing = lambda f: f in man1
2697 managing = lambda f: f in man1
2739
2698
2740 modified, added, removed = [], [], []
2699 modified, added, removed = [], [], []
2741 for f in self._files:
2700 for f in self._files:
2742 if not managing(f):
2701 if not managing(f):
2743 added.append(f)
2702 added.append(f)
2744 elif f in self:
2703 elif f in self:
2745 modified.append(f)
2704 modified.append(f)
2746 else:
2705 else:
2747 removed.append(f)
2706 removed.append(f)
2748
2707
2749 return scmutil.status(modified, added, removed, [], [], [], [])
2708 return scmutil.status(modified, added, removed, [], [], [], [])
2750
2709
2751 class arbitraryfilectx(object):
2710 class arbitraryfilectx(object):
2752 """Allows you to use filectx-like functions on a file in an arbitrary
2711 """Allows you to use filectx-like functions on a file in an arbitrary
2753 location on disk, possibly not in the working directory.
2712 location on disk, possibly not in the working directory.
2754 """
2713 """
2755 def __init__(self, path, repo=None):
2714 def __init__(self, path, repo=None):
2756 # Repo is optional because contrib/simplemerge uses this class.
2715 # Repo is optional because contrib/simplemerge uses this class.
2757 self._repo = repo
2716 self._repo = repo
2758 self._path = path
2717 self._path = path
2759
2718
2760 def cmp(self, fctx):
2719 def cmp(self, fctx):
2761 # filecmp follows symlinks whereas `cmp` should not, so skip the fast
2720 # filecmp follows symlinks whereas `cmp` should not, so skip the fast
2762 # path if either side is a symlink.
2721 # path if either side is a symlink.
2763 symlinks = ('l' in self.flags() or 'l' in fctx.flags())
2722 symlinks = ('l' in self.flags() or 'l' in fctx.flags())
2764 if not symlinks and isinstance(fctx, workingfilectx) and self._repo:
2723 if not symlinks and isinstance(fctx, workingfilectx) and self._repo:
2765 # Add a fast-path for merge if both sides are disk-backed.
2724 # Add a fast-path for merge if both sides are disk-backed.
2766 # Note that filecmp uses the opposite return values (True if same)
2725 # Note that filecmp uses the opposite return values (True if same)
2767 # from our cmp functions (True if different).
2726 # from our cmp functions (True if different).
2768 return not filecmp.cmp(self.path(), self._repo.wjoin(fctx.path()))
2727 return not filecmp.cmp(self.path(), self._repo.wjoin(fctx.path()))
2769 return self.data() != fctx.data()
2728 return self.data() != fctx.data()
2770
2729
2771 def path(self):
2730 def path(self):
2772 return self._path
2731 return self._path
2773
2732
2774 def flags(self):
2733 def flags(self):
2775 return ''
2734 return ''
2776
2735
2777 def data(self):
2736 def data(self):
2778 return util.readfile(self._path)
2737 return util.readfile(self._path)
2779
2738
2780 def decodeddata(self):
2739 def decodeddata(self):
2781 with open(self._path, "rb") as f:
2740 with open(self._path, "rb") as f:
2782 return f.read()
2741 return f.read()
2783
2742
2784 def remove(self):
2743 def remove(self):
2785 util.unlink(self._path)
2744 util.unlink(self._path)
2786
2745
2787 def write(self, data, flags, **kwargs):
2746 def write(self, data, flags, **kwargs):
2788 assert not flags
2747 assert not flags
2789 with open(self._path, "w") as f:
2748 with open(self._path, "w") as f:
2790 f.write(data)
2749 f.write(data)
General Comments 0
You need to be logged in to leave comments. Login now