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