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