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