##// END OF EJS Templates
workingfilectx.cmp: invert boolean return value...
Nicolas Dumazet -
r11538:16fe9880 stable
parent child Browse files
Show More
@@ -1,1078 +1,1078 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
8 from node import nullid, nullrev, short, hex
9 from i18n import _
9 from i18n import _
10 import ancestor, bdiff, error, util, subrepo, patch
10 import ancestor, bdiff, error, util, subrepo, patch
11 import os, errno, stat
11 import os, errno, stat
12
12
13 propertycache = util.propertycache
13 propertycache = util.propertycache
14
14
15 class changectx(object):
15 class changectx(object):
16 """A changecontext object makes access to data related to a particular
16 """A changecontext object makes access to data related to a particular
17 changeset convenient."""
17 changeset convenient."""
18 def __init__(self, repo, changeid=''):
18 def __init__(self, repo, changeid=''):
19 """changeid is a revision number, node, or tag"""
19 """changeid is a revision number, node, or tag"""
20 if changeid == '':
20 if changeid == '':
21 changeid = '.'
21 changeid = '.'
22 self._repo = repo
22 self._repo = repo
23 if isinstance(changeid, (long, int)):
23 if isinstance(changeid, (long, int)):
24 self._rev = changeid
24 self._rev = changeid
25 self._node = self._repo.changelog.node(changeid)
25 self._node = self._repo.changelog.node(changeid)
26 else:
26 else:
27 self._node = self._repo.lookup(changeid)
27 self._node = self._repo.lookup(changeid)
28 self._rev = self._repo.changelog.rev(self._node)
28 self._rev = self._repo.changelog.rev(self._node)
29
29
30 def __str__(self):
30 def __str__(self):
31 return short(self.node())
31 return short(self.node())
32
32
33 def __int__(self):
33 def __int__(self):
34 return self.rev()
34 return self.rev()
35
35
36 def __repr__(self):
36 def __repr__(self):
37 return "<changectx %s>" % str(self)
37 return "<changectx %s>" % str(self)
38
38
39 def __hash__(self):
39 def __hash__(self):
40 try:
40 try:
41 return hash(self._rev)
41 return hash(self._rev)
42 except AttributeError:
42 except AttributeError:
43 return id(self)
43 return id(self)
44
44
45 def __eq__(self, other):
45 def __eq__(self, other):
46 try:
46 try:
47 return self._rev == other._rev
47 return self._rev == other._rev
48 except AttributeError:
48 except AttributeError:
49 return False
49 return False
50
50
51 def __ne__(self, other):
51 def __ne__(self, other):
52 return not (self == other)
52 return not (self == other)
53
53
54 def __nonzero__(self):
54 def __nonzero__(self):
55 return self._rev != nullrev
55 return self._rev != nullrev
56
56
57 @propertycache
57 @propertycache
58 def _changeset(self):
58 def _changeset(self):
59 return self._repo.changelog.read(self.node())
59 return self._repo.changelog.read(self.node())
60
60
61 @propertycache
61 @propertycache
62 def _manifest(self):
62 def _manifest(self):
63 return self._repo.manifest.read(self._changeset[0])
63 return self._repo.manifest.read(self._changeset[0])
64
64
65 @propertycache
65 @propertycache
66 def _manifestdelta(self):
66 def _manifestdelta(self):
67 return self._repo.manifest.readdelta(self._changeset[0])
67 return self._repo.manifest.readdelta(self._changeset[0])
68
68
69 @propertycache
69 @propertycache
70 def _parents(self):
70 def _parents(self):
71 p = self._repo.changelog.parentrevs(self._rev)
71 p = self._repo.changelog.parentrevs(self._rev)
72 if p[1] == nullrev:
72 if p[1] == nullrev:
73 p = p[:-1]
73 p = p[:-1]
74 return [changectx(self._repo, x) for x in p]
74 return [changectx(self._repo, x) for x in p]
75
75
76 @propertycache
76 @propertycache
77 def substate(self):
77 def substate(self):
78 return subrepo.state(self)
78 return subrepo.state(self)
79
79
80 def __contains__(self, key):
80 def __contains__(self, key):
81 return key in self._manifest
81 return key in self._manifest
82
82
83 def __getitem__(self, key):
83 def __getitem__(self, key):
84 return self.filectx(key)
84 return self.filectx(key)
85
85
86 def __iter__(self):
86 def __iter__(self):
87 for f in sorted(self._manifest):
87 for f in sorted(self._manifest):
88 yield f
88 yield f
89
89
90 def changeset(self):
90 def changeset(self):
91 return self._changeset
91 return self._changeset
92 def manifest(self):
92 def manifest(self):
93 return self._manifest
93 return self._manifest
94 def manifestnode(self):
94 def manifestnode(self):
95 return self._changeset[0]
95 return self._changeset[0]
96
96
97 def rev(self):
97 def rev(self):
98 return self._rev
98 return self._rev
99 def node(self):
99 def node(self):
100 return self._node
100 return self._node
101 def hex(self):
101 def hex(self):
102 return hex(self._node)
102 return hex(self._node)
103 def user(self):
103 def user(self):
104 return self._changeset[1]
104 return self._changeset[1]
105 def date(self):
105 def date(self):
106 return self._changeset[2]
106 return self._changeset[2]
107 def files(self):
107 def files(self):
108 return self._changeset[3]
108 return self._changeset[3]
109 def description(self):
109 def description(self):
110 return self._changeset[4]
110 return self._changeset[4]
111 def branch(self):
111 def branch(self):
112 return self._changeset[5].get("branch")
112 return self._changeset[5].get("branch")
113 def extra(self):
113 def extra(self):
114 return self._changeset[5]
114 return self._changeset[5]
115 def tags(self):
115 def tags(self):
116 return self._repo.nodetags(self._node)
116 return self._repo.nodetags(self._node)
117
117
118 def parents(self):
118 def parents(self):
119 """return contexts for each parent changeset"""
119 """return contexts for each parent changeset"""
120 return self._parents
120 return self._parents
121
121
122 def p1(self):
122 def p1(self):
123 return self._parents[0]
123 return self._parents[0]
124
124
125 def p2(self):
125 def p2(self):
126 if len(self._parents) == 2:
126 if len(self._parents) == 2:
127 return self._parents[1]
127 return self._parents[1]
128 return changectx(self._repo, -1)
128 return changectx(self._repo, -1)
129
129
130 def children(self):
130 def children(self):
131 """return contexts for each child changeset"""
131 """return contexts for each child changeset"""
132 c = self._repo.changelog.children(self._node)
132 c = self._repo.changelog.children(self._node)
133 return [changectx(self._repo, x) for x in c]
133 return [changectx(self._repo, x) for x in c]
134
134
135 def ancestors(self):
135 def ancestors(self):
136 for a in self._repo.changelog.ancestors(self._rev):
136 for a in self._repo.changelog.ancestors(self._rev):
137 yield changectx(self._repo, a)
137 yield changectx(self._repo, a)
138
138
139 def descendants(self):
139 def descendants(self):
140 for d in self._repo.changelog.descendants(self._rev):
140 for d in self._repo.changelog.descendants(self._rev):
141 yield changectx(self._repo, d)
141 yield changectx(self._repo, d)
142
142
143 def _fileinfo(self, path):
143 def _fileinfo(self, path):
144 if '_manifest' in self.__dict__:
144 if '_manifest' in self.__dict__:
145 try:
145 try:
146 return self._manifest[path], self._manifest.flags(path)
146 return self._manifest[path], self._manifest.flags(path)
147 except KeyError:
147 except KeyError:
148 raise error.LookupError(self._node, path,
148 raise error.LookupError(self._node, path,
149 _('not found in manifest'))
149 _('not found in manifest'))
150 if '_manifestdelta' in self.__dict__ or path in self.files():
150 if '_manifestdelta' in self.__dict__ or path in self.files():
151 if path in self._manifestdelta:
151 if path in self._manifestdelta:
152 return self._manifestdelta[path], self._manifestdelta.flags(path)
152 return self._manifestdelta[path], self._manifestdelta.flags(path)
153 node, flag = self._repo.manifest.find(self._changeset[0], path)
153 node, flag = self._repo.manifest.find(self._changeset[0], path)
154 if not node:
154 if not node:
155 raise error.LookupError(self._node, path,
155 raise error.LookupError(self._node, path,
156 _('not found in manifest'))
156 _('not found in manifest'))
157
157
158 return node, flag
158 return node, flag
159
159
160 def filenode(self, path):
160 def filenode(self, path):
161 return self._fileinfo(path)[0]
161 return self._fileinfo(path)[0]
162
162
163 def flags(self, path):
163 def flags(self, path):
164 try:
164 try:
165 return self._fileinfo(path)[1]
165 return self._fileinfo(path)[1]
166 except error.LookupError:
166 except error.LookupError:
167 return ''
167 return ''
168
168
169 def filectx(self, path, fileid=None, filelog=None):
169 def filectx(self, path, fileid=None, filelog=None):
170 """get a file context from this changeset"""
170 """get a file context from this changeset"""
171 if fileid is None:
171 if fileid is None:
172 fileid = self.filenode(path)
172 fileid = self.filenode(path)
173 return filectx(self._repo, path, fileid=fileid,
173 return filectx(self._repo, path, fileid=fileid,
174 changectx=self, filelog=filelog)
174 changectx=self, filelog=filelog)
175
175
176 def ancestor(self, c2):
176 def ancestor(self, c2):
177 """
177 """
178 return the ancestor context of self and c2
178 return the ancestor context of self and c2
179 """
179 """
180 # deal with workingctxs
180 # deal with workingctxs
181 n2 = c2._node
181 n2 = c2._node
182 if n2 == None:
182 if n2 == None:
183 n2 = c2._parents[0]._node
183 n2 = c2._parents[0]._node
184 n = self._repo.changelog.ancestor(self._node, n2)
184 n = self._repo.changelog.ancestor(self._node, n2)
185 return changectx(self._repo, n)
185 return changectx(self._repo, n)
186
186
187 def walk(self, match):
187 def walk(self, match):
188 fset = set(match.files())
188 fset = set(match.files())
189 # for dirstate.walk, files=['.'] means "walk the whole tree".
189 # for dirstate.walk, files=['.'] means "walk the whole tree".
190 # follow that here, too
190 # follow that here, too
191 fset.discard('.')
191 fset.discard('.')
192 for fn in self:
192 for fn in self:
193 for ffn in fset:
193 for ffn in fset:
194 # match if the file is the exact name or a directory
194 # match if the file is the exact name or a directory
195 if ffn == fn or fn.startswith("%s/" % ffn):
195 if ffn == fn or fn.startswith("%s/" % ffn):
196 fset.remove(ffn)
196 fset.remove(ffn)
197 break
197 break
198 if match(fn):
198 if match(fn):
199 yield fn
199 yield fn
200 for fn in sorted(fset):
200 for fn in sorted(fset):
201 if match.bad(fn, 'No such file in rev ' + str(self)) and match(fn):
201 if match.bad(fn, 'No such file in rev ' + str(self)) and match(fn):
202 yield fn
202 yield fn
203
203
204 def sub(self, path):
204 def sub(self, path):
205 return subrepo.subrepo(self, path)
205 return subrepo.subrepo(self, path)
206
206
207 def diff(self, ctx2=None, match=None, **opts):
207 def diff(self, ctx2=None, match=None, **opts):
208 """Returns a diff generator for the given contexts and matcher"""
208 """Returns a diff generator for the given contexts and matcher"""
209 if ctx2 is None:
209 if ctx2 is None:
210 ctx2 = self.p1()
210 ctx2 = self.p1()
211 if ctx2 is not None and not isinstance(ctx2, changectx):
211 if ctx2 is not None and not isinstance(ctx2, changectx):
212 ctx2 = self._repo[ctx2]
212 ctx2 = self._repo[ctx2]
213 diffopts = patch.diffopts(self._repo.ui, opts)
213 diffopts = patch.diffopts(self._repo.ui, opts)
214 return patch.diff(self._repo, ctx2.node(), self.node(),
214 return patch.diff(self._repo, ctx2.node(), self.node(),
215 match=match, opts=diffopts)
215 match=match, opts=diffopts)
216
216
217 class filectx(object):
217 class filectx(object):
218 """A filecontext object makes access to data related to a particular
218 """A filecontext object makes access to data related to a particular
219 filerevision convenient."""
219 filerevision convenient."""
220 def __init__(self, repo, path, changeid=None, fileid=None,
220 def __init__(self, repo, path, changeid=None, fileid=None,
221 filelog=None, changectx=None):
221 filelog=None, changectx=None):
222 """changeid can be a changeset revision, node, or tag.
222 """changeid can be a changeset revision, node, or tag.
223 fileid can be a file revision or node."""
223 fileid can be a file revision or node."""
224 self._repo = repo
224 self._repo = repo
225 self._path = path
225 self._path = path
226
226
227 assert (changeid is not None
227 assert (changeid is not None
228 or fileid is not None
228 or fileid is not None
229 or changectx is not None), \
229 or changectx is not None), \
230 ("bad args: changeid=%r, fileid=%r, changectx=%r"
230 ("bad args: changeid=%r, fileid=%r, changectx=%r"
231 % (changeid, fileid, changectx))
231 % (changeid, fileid, changectx))
232
232
233 if filelog:
233 if filelog:
234 self._filelog = filelog
234 self._filelog = filelog
235
235
236 if changeid is not None:
236 if changeid is not None:
237 self._changeid = changeid
237 self._changeid = changeid
238 if changectx is not None:
238 if changectx is not None:
239 self._changectx = changectx
239 self._changectx = changectx
240 if fileid is not None:
240 if fileid is not None:
241 self._fileid = fileid
241 self._fileid = fileid
242
242
243 @propertycache
243 @propertycache
244 def _changectx(self):
244 def _changectx(self):
245 return changectx(self._repo, self._changeid)
245 return changectx(self._repo, self._changeid)
246
246
247 @propertycache
247 @propertycache
248 def _filelog(self):
248 def _filelog(self):
249 return self._repo.file(self._path)
249 return self._repo.file(self._path)
250
250
251 @propertycache
251 @propertycache
252 def _changeid(self):
252 def _changeid(self):
253 if '_changectx' in self.__dict__:
253 if '_changectx' in self.__dict__:
254 return self._changectx.rev()
254 return self._changectx.rev()
255 else:
255 else:
256 return self._filelog.linkrev(self._filerev)
256 return self._filelog.linkrev(self._filerev)
257
257
258 @propertycache
258 @propertycache
259 def _filenode(self):
259 def _filenode(self):
260 if '_fileid' in self.__dict__:
260 if '_fileid' in self.__dict__:
261 return self._filelog.lookup(self._fileid)
261 return self._filelog.lookup(self._fileid)
262 else:
262 else:
263 return self._changectx.filenode(self._path)
263 return self._changectx.filenode(self._path)
264
264
265 @propertycache
265 @propertycache
266 def _filerev(self):
266 def _filerev(self):
267 return self._filelog.rev(self._filenode)
267 return self._filelog.rev(self._filenode)
268
268
269 @propertycache
269 @propertycache
270 def _repopath(self):
270 def _repopath(self):
271 return self._path
271 return self._path
272
272
273 def __nonzero__(self):
273 def __nonzero__(self):
274 try:
274 try:
275 self._filenode
275 self._filenode
276 return True
276 return True
277 except error.LookupError:
277 except error.LookupError:
278 # file is missing
278 # file is missing
279 return False
279 return False
280
280
281 def __str__(self):
281 def __str__(self):
282 return "%s@%s" % (self.path(), short(self.node()))
282 return "%s@%s" % (self.path(), short(self.node()))
283
283
284 def __repr__(self):
284 def __repr__(self):
285 return "<filectx %s>" % str(self)
285 return "<filectx %s>" % str(self)
286
286
287 def __hash__(self):
287 def __hash__(self):
288 try:
288 try:
289 return hash((self._path, self._filenode))
289 return hash((self._path, self._filenode))
290 except AttributeError:
290 except AttributeError:
291 return id(self)
291 return id(self)
292
292
293 def __eq__(self, other):
293 def __eq__(self, other):
294 try:
294 try:
295 return (self._path == other._path
295 return (self._path == other._path
296 and self._filenode == other._filenode)
296 and self._filenode == other._filenode)
297 except AttributeError:
297 except AttributeError:
298 return False
298 return False
299
299
300 def __ne__(self, other):
300 def __ne__(self, other):
301 return not (self == other)
301 return not (self == other)
302
302
303 def filectx(self, fileid):
303 def filectx(self, fileid):
304 '''opens an arbitrary revision of the file without
304 '''opens an arbitrary revision of the file without
305 opening a new filelog'''
305 opening a new filelog'''
306 return filectx(self._repo, self._path, fileid=fileid,
306 return filectx(self._repo, self._path, fileid=fileid,
307 filelog=self._filelog)
307 filelog=self._filelog)
308
308
309 def filerev(self):
309 def filerev(self):
310 return self._filerev
310 return self._filerev
311 def filenode(self):
311 def filenode(self):
312 return self._filenode
312 return self._filenode
313 def flags(self):
313 def flags(self):
314 return self._changectx.flags(self._path)
314 return self._changectx.flags(self._path)
315 def filelog(self):
315 def filelog(self):
316 return self._filelog
316 return self._filelog
317
317
318 def rev(self):
318 def rev(self):
319 if '_changectx' in self.__dict__:
319 if '_changectx' in self.__dict__:
320 return self._changectx.rev()
320 return self._changectx.rev()
321 if '_changeid' in self.__dict__:
321 if '_changeid' in self.__dict__:
322 return self._changectx.rev()
322 return self._changectx.rev()
323 return self._filelog.linkrev(self._filerev)
323 return self._filelog.linkrev(self._filerev)
324
324
325 def linkrev(self):
325 def linkrev(self):
326 return self._filelog.linkrev(self._filerev)
326 return self._filelog.linkrev(self._filerev)
327 def node(self):
327 def node(self):
328 return self._changectx.node()
328 return self._changectx.node()
329 def hex(self):
329 def hex(self):
330 return hex(self.node())
330 return hex(self.node())
331 def user(self):
331 def user(self):
332 return self._changectx.user()
332 return self._changectx.user()
333 def date(self):
333 def date(self):
334 return self._changectx.date()
334 return self._changectx.date()
335 def files(self):
335 def files(self):
336 return self._changectx.files()
336 return self._changectx.files()
337 def description(self):
337 def description(self):
338 return self._changectx.description()
338 return self._changectx.description()
339 def branch(self):
339 def branch(self):
340 return self._changectx.branch()
340 return self._changectx.branch()
341 def extra(self):
341 def extra(self):
342 return self._changectx.extra()
342 return self._changectx.extra()
343 def manifest(self):
343 def manifest(self):
344 return self._changectx.manifest()
344 return self._changectx.manifest()
345 def changectx(self):
345 def changectx(self):
346 return self._changectx
346 return self._changectx
347
347
348 def data(self):
348 def data(self):
349 return self._filelog.read(self._filenode)
349 return self._filelog.read(self._filenode)
350 def path(self):
350 def path(self):
351 return self._path
351 return self._path
352 def size(self):
352 def size(self):
353 return self._filelog.size(self._filerev)
353 return self._filelog.size(self._filerev)
354
354
355 def cmp(self, text):
355 def cmp(self, text):
356 return self._filelog.cmp(self._filenode, text)
356 return self._filelog.cmp(self._filenode, text)
357
357
358 def renamed(self):
358 def renamed(self):
359 """check if file was actually renamed in this changeset revision
359 """check if file was actually renamed in this changeset revision
360
360
361 If rename logged in file revision, we report copy for changeset only
361 If rename logged in file revision, we report copy for changeset only
362 if file revisions linkrev points back to the changeset in question
362 if file revisions linkrev points back to the changeset in question
363 or both changeset parents contain different file revisions.
363 or both changeset parents contain different file revisions.
364 """
364 """
365
365
366 renamed = self._filelog.renamed(self._filenode)
366 renamed = self._filelog.renamed(self._filenode)
367 if not renamed:
367 if not renamed:
368 return renamed
368 return renamed
369
369
370 if self.rev() == self.linkrev():
370 if self.rev() == self.linkrev():
371 return renamed
371 return renamed
372
372
373 name = self.path()
373 name = self.path()
374 fnode = self._filenode
374 fnode = self._filenode
375 for p in self._changectx.parents():
375 for p in self._changectx.parents():
376 try:
376 try:
377 if fnode == p.filenode(name):
377 if fnode == p.filenode(name):
378 return None
378 return None
379 except error.LookupError:
379 except error.LookupError:
380 pass
380 pass
381 return renamed
381 return renamed
382
382
383 def parents(self):
383 def parents(self):
384 p = self._path
384 p = self._path
385 fl = self._filelog
385 fl = self._filelog
386 pl = [(p, n, fl) for n in self._filelog.parents(self._filenode)]
386 pl = [(p, n, fl) for n in self._filelog.parents(self._filenode)]
387
387
388 r = self._filelog.renamed(self._filenode)
388 r = self._filelog.renamed(self._filenode)
389 if r:
389 if r:
390 pl[0] = (r[0], r[1], None)
390 pl[0] = (r[0], r[1], None)
391
391
392 return [filectx(self._repo, p, fileid=n, filelog=l)
392 return [filectx(self._repo, p, fileid=n, filelog=l)
393 for p, n, l in pl if n != nullid]
393 for p, n, l in pl if n != nullid]
394
394
395 def children(self):
395 def children(self):
396 # hard for renames
396 # hard for renames
397 c = self._filelog.children(self._filenode)
397 c = self._filelog.children(self._filenode)
398 return [filectx(self._repo, self._path, fileid=x,
398 return [filectx(self._repo, self._path, fileid=x,
399 filelog=self._filelog) for x in c]
399 filelog=self._filelog) for x in c]
400
400
401 def annotate(self, follow=False, linenumber=None):
401 def annotate(self, follow=False, linenumber=None):
402 '''returns a list of tuples of (ctx, line) for each line
402 '''returns a list of tuples of (ctx, line) for each line
403 in the file, where ctx is the filectx of the node where
403 in the file, where ctx is the filectx of the node where
404 that line was last changed.
404 that line was last changed.
405 This returns tuples of ((ctx, linenumber), line) for each line,
405 This returns tuples of ((ctx, linenumber), line) for each line,
406 if "linenumber" parameter is NOT "None".
406 if "linenumber" parameter is NOT "None".
407 In such tuples, linenumber means one at the first appearance
407 In such tuples, linenumber means one at the first appearance
408 in the managed file.
408 in the managed file.
409 To reduce annotation cost,
409 To reduce annotation cost,
410 this returns fixed value(False is used) as linenumber,
410 this returns fixed value(False is used) as linenumber,
411 if "linenumber" parameter is "False".'''
411 if "linenumber" parameter is "False".'''
412
412
413 def decorate_compat(text, rev):
413 def decorate_compat(text, rev):
414 return ([rev] * len(text.splitlines()), text)
414 return ([rev] * len(text.splitlines()), text)
415
415
416 def without_linenumber(text, rev):
416 def without_linenumber(text, rev):
417 return ([(rev, False)] * len(text.splitlines()), text)
417 return ([(rev, False)] * len(text.splitlines()), text)
418
418
419 def with_linenumber(text, rev):
419 def with_linenumber(text, rev):
420 size = len(text.splitlines())
420 size = len(text.splitlines())
421 return ([(rev, i) for i in xrange(1, size + 1)], text)
421 return ([(rev, i) for i in xrange(1, size + 1)], text)
422
422
423 decorate = (((linenumber is None) and decorate_compat) or
423 decorate = (((linenumber is None) and decorate_compat) or
424 (linenumber and with_linenumber) or
424 (linenumber and with_linenumber) or
425 without_linenumber)
425 without_linenumber)
426
426
427 def pair(parent, child):
427 def pair(parent, child):
428 for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
428 for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
429 child[0][b1:b2] = parent[0][a1:a2]
429 child[0][b1:b2] = parent[0][a1:a2]
430 return child
430 return child
431
431
432 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
432 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
433 def getctx(path, fileid):
433 def getctx(path, fileid):
434 log = path == self._path and self._filelog or getlog(path)
434 log = path == self._path and self._filelog or getlog(path)
435 return filectx(self._repo, path, fileid=fileid, filelog=log)
435 return filectx(self._repo, path, fileid=fileid, filelog=log)
436 getctx = util.lrucachefunc(getctx)
436 getctx = util.lrucachefunc(getctx)
437
437
438 def parents(f):
438 def parents(f):
439 # we want to reuse filectx objects as much as possible
439 # we want to reuse filectx objects as much as possible
440 p = f._path
440 p = f._path
441 if f._filerev is None: # working dir
441 if f._filerev is None: # working dir
442 pl = [(n.path(), n.filerev()) for n in f.parents()]
442 pl = [(n.path(), n.filerev()) for n in f.parents()]
443 else:
443 else:
444 pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
444 pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
445
445
446 if follow:
446 if follow:
447 r = f.renamed()
447 r = f.renamed()
448 if r:
448 if r:
449 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
449 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
450
450
451 return [getctx(p, n) for p, n in pl if n != nullrev]
451 return [getctx(p, n) for p, n in pl if n != nullrev]
452
452
453 # use linkrev to find the first changeset where self appeared
453 # use linkrev to find the first changeset where self appeared
454 if self.rev() != self.linkrev():
454 if self.rev() != self.linkrev():
455 base = self.filectx(self.filerev())
455 base = self.filectx(self.filerev())
456 else:
456 else:
457 base = self
457 base = self
458
458
459 # find all ancestors
459 # find all ancestors
460 needed = {base: 1}
460 needed = {base: 1}
461 visit = [base]
461 visit = [base]
462 files = [base._path]
462 files = [base._path]
463 while visit:
463 while visit:
464 f = visit.pop(0)
464 f = visit.pop(0)
465 for p in parents(f):
465 for p in parents(f):
466 if p not in needed:
466 if p not in needed:
467 needed[p] = 1
467 needed[p] = 1
468 visit.append(p)
468 visit.append(p)
469 if p._path not in files:
469 if p._path not in files:
470 files.append(p._path)
470 files.append(p._path)
471 else:
471 else:
472 # count how many times we'll use this
472 # count how many times we'll use this
473 needed[p] += 1
473 needed[p] += 1
474
474
475 # sort by revision (per file) which is a topological order
475 # sort by revision (per file) which is a topological order
476 visit = []
476 visit = []
477 for f in files:
477 for f in files:
478 visit.extend(n for n in needed if n._path == f)
478 visit.extend(n for n in needed if n._path == f)
479
479
480 hist = {}
480 hist = {}
481 for f in sorted(visit, key=lambda x: x.rev()):
481 for f in sorted(visit, key=lambda x: x.rev()):
482 curr = decorate(f.data(), f)
482 curr = decorate(f.data(), f)
483 for p in parents(f):
483 for p in parents(f):
484 curr = pair(hist[p], curr)
484 curr = pair(hist[p], curr)
485 # trim the history of unneeded revs
485 # trim the history of unneeded revs
486 needed[p] -= 1
486 needed[p] -= 1
487 if not needed[p]:
487 if not needed[p]:
488 del hist[p]
488 del hist[p]
489 hist[f] = curr
489 hist[f] = curr
490
490
491 return zip(hist[f][0], hist[f][1].splitlines(True))
491 return zip(hist[f][0], hist[f][1].splitlines(True))
492
492
493 def ancestor(self, fc2, actx=None):
493 def ancestor(self, fc2, actx=None):
494 """
494 """
495 find the common ancestor file context, if any, of self, and fc2
495 find the common ancestor file context, if any, of self, and fc2
496
496
497 If actx is given, it must be the changectx of the common ancestor
497 If actx is given, it must be the changectx of the common ancestor
498 of self's and fc2's respective changesets.
498 of self's and fc2's respective changesets.
499 """
499 """
500
500
501 if actx is None:
501 if actx is None:
502 actx = self.changectx().ancestor(fc2.changectx())
502 actx = self.changectx().ancestor(fc2.changectx())
503
503
504 # the trivial case: changesets are unrelated, files must be too
504 # the trivial case: changesets are unrelated, files must be too
505 if not actx:
505 if not actx:
506 return None
506 return None
507
507
508 # the easy case: no (relevant) renames
508 # the easy case: no (relevant) renames
509 if fc2.path() == self.path() and self.path() in actx:
509 if fc2.path() == self.path() and self.path() in actx:
510 return actx[self.path()]
510 return actx[self.path()]
511 acache = {}
511 acache = {}
512
512
513 # prime the ancestor cache for the working directory
513 # prime the ancestor cache for the working directory
514 for c in (self, fc2):
514 for c in (self, fc2):
515 if c._filerev is None:
515 if c._filerev is None:
516 pl = [(n.path(), n.filenode()) for n in c.parents()]
516 pl = [(n.path(), n.filenode()) for n in c.parents()]
517 acache[(c._path, None)] = pl
517 acache[(c._path, None)] = pl
518
518
519 flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
519 flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
520 def parents(vertex):
520 def parents(vertex):
521 if vertex in acache:
521 if vertex in acache:
522 return acache[vertex]
522 return acache[vertex]
523 f, n = vertex
523 f, n = vertex
524 if f not in flcache:
524 if f not in flcache:
525 flcache[f] = self._repo.file(f)
525 flcache[f] = self._repo.file(f)
526 fl = flcache[f]
526 fl = flcache[f]
527 pl = [(f, p) for p in fl.parents(n) if p != nullid]
527 pl = [(f, p) for p in fl.parents(n) if p != nullid]
528 re = fl.renamed(n)
528 re = fl.renamed(n)
529 if re:
529 if re:
530 pl.append(re)
530 pl.append(re)
531 acache[vertex] = pl
531 acache[vertex] = pl
532 return pl
532 return pl
533
533
534 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
534 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
535 v = ancestor.ancestor(a, b, parents)
535 v = ancestor.ancestor(a, b, parents)
536 if v:
536 if v:
537 f, n = v
537 f, n = v
538 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
538 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
539
539
540 return None
540 return None
541
541
542 def ancestors(self):
542 def ancestors(self):
543 seen = set(str(self))
543 seen = set(str(self))
544 visit = [self]
544 visit = [self]
545 while visit:
545 while visit:
546 for parent in visit.pop(0).parents():
546 for parent in visit.pop(0).parents():
547 s = str(parent)
547 s = str(parent)
548 if s not in seen:
548 if s not in seen:
549 visit.append(parent)
549 visit.append(parent)
550 seen.add(s)
550 seen.add(s)
551 yield parent
551 yield parent
552
552
553 class workingctx(changectx):
553 class workingctx(changectx):
554 """A workingctx object makes access to data related to
554 """A workingctx object makes access to data related to
555 the current working directory convenient.
555 the current working directory convenient.
556 date - any valid date string or (unixtime, offset), or None.
556 date - any valid date string or (unixtime, offset), or None.
557 user - username string, or None.
557 user - username string, or None.
558 extra - a dictionary of extra values, or None.
558 extra - a dictionary of extra values, or None.
559 changes - a list of file lists as returned by localrepo.status()
559 changes - a list of file lists as returned by localrepo.status()
560 or None to use the repository status.
560 or None to use the repository status.
561 """
561 """
562 def __init__(self, repo, text="", user=None, date=None, extra=None,
562 def __init__(self, repo, text="", user=None, date=None, extra=None,
563 changes=None):
563 changes=None):
564 self._repo = repo
564 self._repo = repo
565 self._rev = None
565 self._rev = None
566 self._node = None
566 self._node = None
567 self._text = text
567 self._text = text
568 if date:
568 if date:
569 self._date = util.parsedate(date)
569 self._date = util.parsedate(date)
570 if user:
570 if user:
571 self._user = user
571 self._user = user
572 if changes:
572 if changes:
573 self._status = list(changes[:4])
573 self._status = list(changes[:4])
574 self._unknown = changes[4]
574 self._unknown = changes[4]
575 self._ignored = changes[5]
575 self._ignored = changes[5]
576 self._clean = changes[6]
576 self._clean = changes[6]
577 else:
577 else:
578 self._unknown = None
578 self._unknown = None
579 self._ignored = None
579 self._ignored = None
580 self._clean = None
580 self._clean = None
581
581
582 self._extra = {}
582 self._extra = {}
583 if extra:
583 if extra:
584 self._extra = extra.copy()
584 self._extra = extra.copy()
585 if 'branch' not in self._extra:
585 if 'branch' not in self._extra:
586 branch = self._repo.dirstate.branch()
586 branch = self._repo.dirstate.branch()
587 try:
587 try:
588 branch = branch.decode('UTF-8').encode('UTF-8')
588 branch = branch.decode('UTF-8').encode('UTF-8')
589 except UnicodeDecodeError:
589 except UnicodeDecodeError:
590 raise util.Abort(_('branch name not in UTF-8!'))
590 raise util.Abort(_('branch name not in UTF-8!'))
591 self._extra['branch'] = branch
591 self._extra['branch'] = branch
592 if self._extra['branch'] == '':
592 if self._extra['branch'] == '':
593 self._extra['branch'] = 'default'
593 self._extra['branch'] = 'default'
594
594
595 def __str__(self):
595 def __str__(self):
596 return str(self._parents[0]) + "+"
596 return str(self._parents[0]) + "+"
597
597
598 def __nonzero__(self):
598 def __nonzero__(self):
599 return True
599 return True
600
600
601 def __contains__(self, key):
601 def __contains__(self, key):
602 return self._repo.dirstate[key] not in "?r"
602 return self._repo.dirstate[key] not in "?r"
603
603
604 @propertycache
604 @propertycache
605 def _manifest(self):
605 def _manifest(self):
606 """generate a manifest corresponding to the working directory"""
606 """generate a manifest corresponding to the working directory"""
607
607
608 if self._unknown is None:
608 if self._unknown is None:
609 self.status(unknown=True)
609 self.status(unknown=True)
610
610
611 man = self._parents[0].manifest().copy()
611 man = self._parents[0].manifest().copy()
612 copied = self._repo.dirstate.copies()
612 copied = self._repo.dirstate.copies()
613 if len(self._parents) > 1:
613 if len(self._parents) > 1:
614 man2 = self.p2().manifest()
614 man2 = self.p2().manifest()
615 def getman(f):
615 def getman(f):
616 if f in man:
616 if f in man:
617 return man
617 return man
618 return man2
618 return man2
619 else:
619 else:
620 getman = lambda f: man
620 getman = lambda f: man
621 def cf(f):
621 def cf(f):
622 f = copied.get(f, f)
622 f = copied.get(f, f)
623 return getman(f).flags(f)
623 return getman(f).flags(f)
624 ff = self._repo.dirstate.flagfunc(cf)
624 ff = self._repo.dirstate.flagfunc(cf)
625 modified, added, removed, deleted = self._status
625 modified, added, removed, deleted = self._status
626 unknown = self._unknown
626 unknown = self._unknown
627 for i, l in (("a", added), ("m", modified), ("u", unknown)):
627 for i, l in (("a", added), ("m", modified), ("u", unknown)):
628 for f in l:
628 for f in l:
629 orig = copied.get(f, f)
629 orig = copied.get(f, f)
630 man[f] = getman(orig).get(orig, nullid) + i
630 man[f] = getman(orig).get(orig, nullid) + i
631 try:
631 try:
632 man.set(f, ff(f))
632 man.set(f, ff(f))
633 except OSError:
633 except OSError:
634 pass
634 pass
635
635
636 for f in deleted + removed:
636 for f in deleted + removed:
637 if f in man:
637 if f in man:
638 del man[f]
638 del man[f]
639
639
640 return man
640 return man
641
641
642 @propertycache
642 @propertycache
643 def _status(self):
643 def _status(self):
644 return self._repo.status()[:4]
644 return self._repo.status()[:4]
645
645
646 @propertycache
646 @propertycache
647 def _user(self):
647 def _user(self):
648 return self._repo.ui.username()
648 return self._repo.ui.username()
649
649
650 @propertycache
650 @propertycache
651 def _date(self):
651 def _date(self):
652 return util.makedate()
652 return util.makedate()
653
653
654 @propertycache
654 @propertycache
655 def _parents(self):
655 def _parents(self):
656 p = self._repo.dirstate.parents()
656 p = self._repo.dirstate.parents()
657 if p[1] == nullid:
657 if p[1] == nullid:
658 p = p[:-1]
658 p = p[:-1]
659 self._parents = [changectx(self._repo, x) for x in p]
659 self._parents = [changectx(self._repo, x) for x in p]
660 return self._parents
660 return self._parents
661
661
662 def status(self, ignored=False, clean=False, unknown=False):
662 def status(self, ignored=False, clean=False, unknown=False):
663 """Explicit status query
663 """Explicit status query
664 Unless this method is used to query the working copy status, the
664 Unless this method is used to query the working copy status, the
665 _status property will implicitly read the status using its default
665 _status property will implicitly read the status using its default
666 arguments."""
666 arguments."""
667 stat = self._repo.status(ignored=ignored, clean=clean, unknown=unknown)
667 stat = self._repo.status(ignored=ignored, clean=clean, unknown=unknown)
668 self._unknown = self._ignored = self._clean = None
668 self._unknown = self._ignored = self._clean = None
669 if unknown:
669 if unknown:
670 self._unknown = stat[4]
670 self._unknown = stat[4]
671 if ignored:
671 if ignored:
672 self._ignored = stat[5]
672 self._ignored = stat[5]
673 if clean:
673 if clean:
674 self._clean = stat[6]
674 self._clean = stat[6]
675 self._status = stat[:4]
675 self._status = stat[:4]
676 return stat
676 return stat
677
677
678 def manifest(self):
678 def manifest(self):
679 return self._manifest
679 return self._manifest
680 def user(self):
680 def user(self):
681 return self._user or self._repo.ui.username()
681 return self._user or self._repo.ui.username()
682 def date(self):
682 def date(self):
683 return self._date
683 return self._date
684 def description(self):
684 def description(self):
685 return self._text
685 return self._text
686 def files(self):
686 def files(self):
687 return sorted(self._status[0] + self._status[1] + self._status[2])
687 return sorted(self._status[0] + self._status[1] + self._status[2])
688
688
689 def modified(self):
689 def modified(self):
690 return self._status[0]
690 return self._status[0]
691 def added(self):
691 def added(self):
692 return self._status[1]
692 return self._status[1]
693 def removed(self):
693 def removed(self):
694 return self._status[2]
694 return self._status[2]
695 def deleted(self):
695 def deleted(self):
696 return self._status[3]
696 return self._status[3]
697 def unknown(self):
697 def unknown(self):
698 assert self._unknown is not None # must call status first
698 assert self._unknown is not None # must call status first
699 return self._unknown
699 return self._unknown
700 def ignored(self):
700 def ignored(self):
701 assert self._ignored is not None # must call status first
701 assert self._ignored is not None # must call status first
702 return self._ignored
702 return self._ignored
703 def clean(self):
703 def clean(self):
704 assert self._clean is not None # must call status first
704 assert self._clean is not None # must call status first
705 return self._clean
705 return self._clean
706 def branch(self):
706 def branch(self):
707 return self._extra['branch']
707 return self._extra['branch']
708 def extra(self):
708 def extra(self):
709 return self._extra
709 return self._extra
710
710
711 def tags(self):
711 def tags(self):
712 t = []
712 t = []
713 [t.extend(p.tags()) for p in self.parents()]
713 [t.extend(p.tags()) for p in self.parents()]
714 return t
714 return t
715
715
716 def children(self):
716 def children(self):
717 return []
717 return []
718
718
719 def flags(self, path):
719 def flags(self, path):
720 if '_manifest' in self.__dict__:
720 if '_manifest' in self.__dict__:
721 try:
721 try:
722 return self._manifest.flags(path)
722 return self._manifest.flags(path)
723 except KeyError:
723 except KeyError:
724 return ''
724 return ''
725
725
726 orig = self._repo.dirstate.copies().get(path, path)
726 orig = self._repo.dirstate.copies().get(path, path)
727
727
728 def findflag(ctx):
728 def findflag(ctx):
729 mnode = ctx.changeset()[0]
729 mnode = ctx.changeset()[0]
730 node, flag = self._repo.manifest.find(mnode, orig)
730 node, flag = self._repo.manifest.find(mnode, orig)
731 ff = self._repo.dirstate.flagfunc(lambda x: flag or '')
731 ff = self._repo.dirstate.flagfunc(lambda x: flag or '')
732 try:
732 try:
733 return ff(path)
733 return ff(path)
734 except OSError:
734 except OSError:
735 pass
735 pass
736
736
737 flag = findflag(self._parents[0])
737 flag = findflag(self._parents[0])
738 if flag is None and len(self.parents()) > 1:
738 if flag is None and len(self.parents()) > 1:
739 flag = findflag(self._parents[1])
739 flag = findflag(self._parents[1])
740 if flag is None or self._repo.dirstate[path] == 'r':
740 if flag is None or self._repo.dirstate[path] == 'r':
741 return ''
741 return ''
742 return flag
742 return flag
743
743
744 def filectx(self, path, filelog=None):
744 def filectx(self, path, filelog=None):
745 """get a file context from the working directory"""
745 """get a file context from the working directory"""
746 return workingfilectx(self._repo, path, workingctx=self,
746 return workingfilectx(self._repo, path, workingctx=self,
747 filelog=filelog)
747 filelog=filelog)
748
748
749 def ancestor(self, c2):
749 def ancestor(self, c2):
750 """return the ancestor context of self and c2"""
750 """return the ancestor context of self and c2"""
751 return self._parents[0].ancestor(c2) # punt on two parents for now
751 return self._parents[0].ancestor(c2) # punt on two parents for now
752
752
753 def walk(self, match):
753 def walk(self, match):
754 return sorted(self._repo.dirstate.walk(match, self.substate.keys(),
754 return sorted(self._repo.dirstate.walk(match, self.substate.keys(),
755 True, False))
755 True, False))
756
756
757 def dirty(self, missing=False):
757 def dirty(self, missing=False):
758 "check whether a working directory is modified"
758 "check whether a working directory is modified"
759 # check subrepos first
759 # check subrepos first
760 for s in self.substate:
760 for s in self.substate:
761 if self.sub(s).dirty():
761 if self.sub(s).dirty():
762 return True
762 return True
763 # check current working dir
763 # check current working dir
764 return (self.p2() or self.branch() != self.p1().branch() or
764 return (self.p2() or self.branch() != self.p1().branch() or
765 self.modified() or self.added() or self.removed() or
765 self.modified() or self.added() or self.removed() or
766 (missing and self.deleted()))
766 (missing and self.deleted()))
767
767
768 def add(self, list):
768 def add(self, list):
769 wlock = self._repo.wlock()
769 wlock = self._repo.wlock()
770 ui, ds = self._repo.ui, self._repo.dirstate
770 ui, ds = self._repo.ui, self._repo.dirstate
771 try:
771 try:
772 rejected = []
772 rejected = []
773 for f in list:
773 for f in list:
774 p = self._repo.wjoin(f)
774 p = self._repo.wjoin(f)
775 try:
775 try:
776 st = os.lstat(p)
776 st = os.lstat(p)
777 except:
777 except:
778 ui.warn(_("%s does not exist!\n") % f)
778 ui.warn(_("%s does not exist!\n") % f)
779 rejected.append(f)
779 rejected.append(f)
780 continue
780 continue
781 if st.st_size > 10000000:
781 if st.st_size > 10000000:
782 ui.warn(_("%s: up to %d MB of RAM may be required "
782 ui.warn(_("%s: up to %d MB of RAM may be required "
783 "to manage this file\n"
783 "to manage this file\n"
784 "(use 'hg revert %s' to cancel the "
784 "(use 'hg revert %s' to cancel the "
785 "pending addition)\n")
785 "pending addition)\n")
786 % (f, 3 * st.st_size // 1000000, f))
786 % (f, 3 * st.st_size // 1000000, f))
787 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
787 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
788 ui.warn(_("%s not added: only files and symlinks "
788 ui.warn(_("%s not added: only files and symlinks "
789 "supported currently\n") % f)
789 "supported currently\n") % f)
790 rejected.append(p)
790 rejected.append(p)
791 elif ds[f] in 'amn':
791 elif ds[f] in 'amn':
792 ui.warn(_("%s already tracked!\n") % f)
792 ui.warn(_("%s already tracked!\n") % f)
793 elif ds[f] == 'r':
793 elif ds[f] == 'r':
794 ds.normallookup(f)
794 ds.normallookup(f)
795 else:
795 else:
796 ds.add(f)
796 ds.add(f)
797 return rejected
797 return rejected
798 finally:
798 finally:
799 wlock.release()
799 wlock.release()
800
800
801 def forget(self, list):
801 def forget(self, list):
802 wlock = self._repo.wlock()
802 wlock = self._repo.wlock()
803 try:
803 try:
804 for f in list:
804 for f in list:
805 if self._repo.dirstate[f] != 'a':
805 if self._repo.dirstate[f] != 'a':
806 self._repo.ui.warn(_("%s not added!\n") % f)
806 self._repo.ui.warn(_("%s not added!\n") % f)
807 else:
807 else:
808 self._repo.dirstate.forget(f)
808 self._repo.dirstate.forget(f)
809 finally:
809 finally:
810 wlock.release()
810 wlock.release()
811
811
812 def remove(self, list, unlink=False):
812 def remove(self, list, unlink=False):
813 if unlink:
813 if unlink:
814 for f in list:
814 for f in list:
815 try:
815 try:
816 util.unlink(self._repo.wjoin(f))
816 util.unlink(self._repo.wjoin(f))
817 except OSError, inst:
817 except OSError, inst:
818 if inst.errno != errno.ENOENT:
818 if inst.errno != errno.ENOENT:
819 raise
819 raise
820 wlock = self._repo.wlock()
820 wlock = self._repo.wlock()
821 try:
821 try:
822 for f in list:
822 for f in list:
823 if unlink and os.path.exists(self._repo.wjoin(f)):
823 if unlink and os.path.exists(self._repo.wjoin(f)):
824 self._repo.ui.warn(_("%s still exists!\n") % f)
824 self._repo.ui.warn(_("%s still exists!\n") % f)
825 elif self._repo.dirstate[f] == 'a':
825 elif self._repo.dirstate[f] == 'a':
826 self._repo.dirstate.forget(f)
826 self._repo.dirstate.forget(f)
827 elif f not in self._repo.dirstate:
827 elif f not in self._repo.dirstate:
828 self._repo.ui.warn(_("%s not tracked!\n") % f)
828 self._repo.ui.warn(_("%s not tracked!\n") % f)
829 else:
829 else:
830 self._repo.dirstate.remove(f)
830 self._repo.dirstate.remove(f)
831 finally:
831 finally:
832 wlock.release()
832 wlock.release()
833
833
834 def undelete(self, list):
834 def undelete(self, list):
835 pctxs = self.parents()
835 pctxs = self.parents()
836 wlock = self._repo.wlock()
836 wlock = self._repo.wlock()
837 try:
837 try:
838 for f in list:
838 for f in list:
839 if self._repo.dirstate[f] != 'r':
839 if self._repo.dirstate[f] != 'r':
840 self._repo.ui.warn(_("%s not removed!\n") % f)
840 self._repo.ui.warn(_("%s not removed!\n") % f)
841 else:
841 else:
842 fctx = f in pctxs[0] and pctxs[0] or pctxs[1]
842 fctx = f in pctxs[0] and pctxs[0] or pctxs[1]
843 t = fctx.data()
843 t = fctx.data()
844 self._repo.wwrite(f, t, fctx.flags())
844 self._repo.wwrite(f, t, fctx.flags())
845 self._repo.dirstate.normal(f)
845 self._repo.dirstate.normal(f)
846 finally:
846 finally:
847 wlock.release()
847 wlock.release()
848
848
849 def copy(self, source, dest):
849 def copy(self, source, dest):
850 p = self._repo.wjoin(dest)
850 p = self._repo.wjoin(dest)
851 if not (os.path.exists(p) or os.path.islink(p)):
851 if not (os.path.exists(p) or os.path.islink(p)):
852 self._repo.ui.warn(_("%s does not exist!\n") % dest)
852 self._repo.ui.warn(_("%s does not exist!\n") % dest)
853 elif not (os.path.isfile(p) or os.path.islink(p)):
853 elif not (os.path.isfile(p) or os.path.islink(p)):
854 self._repo.ui.warn(_("copy failed: %s is not a file or a "
854 self._repo.ui.warn(_("copy failed: %s is not a file or a "
855 "symbolic link\n") % dest)
855 "symbolic link\n") % dest)
856 else:
856 else:
857 wlock = self._repo.wlock()
857 wlock = self._repo.wlock()
858 try:
858 try:
859 if self._repo.dirstate[dest] in '?r':
859 if self._repo.dirstate[dest] in '?r':
860 self._repo.dirstate.add(dest)
860 self._repo.dirstate.add(dest)
861 self._repo.dirstate.copy(source, dest)
861 self._repo.dirstate.copy(source, dest)
862 finally:
862 finally:
863 wlock.release()
863 wlock.release()
864
864
865 class workingfilectx(filectx):
865 class workingfilectx(filectx):
866 """A workingfilectx object makes access to data related to a particular
866 """A workingfilectx object makes access to data related to a particular
867 file in the working directory convenient."""
867 file in the working directory convenient."""
868 def __init__(self, repo, path, filelog=None, workingctx=None):
868 def __init__(self, repo, path, filelog=None, workingctx=None):
869 """changeid can be a changeset revision, node, or tag.
869 """changeid can be a changeset revision, node, or tag.
870 fileid can be a file revision or node."""
870 fileid can be a file revision or node."""
871 self._repo = repo
871 self._repo = repo
872 self._path = path
872 self._path = path
873 self._changeid = None
873 self._changeid = None
874 self._filerev = self._filenode = None
874 self._filerev = self._filenode = None
875
875
876 if filelog:
876 if filelog:
877 self._filelog = filelog
877 self._filelog = filelog
878 if workingctx:
878 if workingctx:
879 self._changectx = workingctx
879 self._changectx = workingctx
880
880
881 @propertycache
881 @propertycache
882 def _changectx(self):
882 def _changectx(self):
883 return workingctx(self._repo)
883 return workingctx(self._repo)
884
884
885 def __nonzero__(self):
885 def __nonzero__(self):
886 return True
886 return True
887
887
888 def __str__(self):
888 def __str__(self):
889 return "%s@%s" % (self.path(), self._changectx)
889 return "%s@%s" % (self.path(), self._changectx)
890
890
891 def data(self):
891 def data(self):
892 return self._repo.wread(self._path)
892 return self._repo.wread(self._path)
893 def renamed(self):
893 def renamed(self):
894 rp = self._repo.dirstate.copied(self._path)
894 rp = self._repo.dirstate.copied(self._path)
895 if not rp:
895 if not rp:
896 return None
896 return None
897 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
897 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
898
898
899 def parents(self):
899 def parents(self):
900 '''return parent filectxs, following copies if necessary'''
900 '''return parent filectxs, following copies if necessary'''
901 def filenode(ctx, path):
901 def filenode(ctx, path):
902 return ctx._manifest.get(path, nullid)
902 return ctx._manifest.get(path, nullid)
903
903
904 path = self._path
904 path = self._path
905 fl = self._filelog
905 fl = self._filelog
906 pcl = self._changectx._parents
906 pcl = self._changectx._parents
907 renamed = self.renamed()
907 renamed = self.renamed()
908
908
909 if renamed:
909 if renamed:
910 pl = [renamed + (None,)]
910 pl = [renamed + (None,)]
911 else:
911 else:
912 pl = [(path, filenode(pcl[0], path), fl)]
912 pl = [(path, filenode(pcl[0], path), fl)]
913
913
914 for pc in pcl[1:]:
914 for pc in pcl[1:]:
915 pl.append((path, filenode(pc, path), fl))
915 pl.append((path, filenode(pc, path), fl))
916
916
917 return [filectx(self._repo, p, fileid=n, filelog=l)
917 return [filectx(self._repo, p, fileid=n, filelog=l)
918 for p, n, l in pl if n != nullid]
918 for p, n, l in pl if n != nullid]
919
919
920 def children(self):
920 def children(self):
921 return []
921 return []
922
922
923 def size(self):
923 def size(self):
924 return os.stat(self._repo.wjoin(self._path)).st_size
924 return os.stat(self._repo.wjoin(self._path)).st_size
925 def date(self):
925 def date(self):
926 t, tz = self._changectx.date()
926 t, tz = self._changectx.date()
927 try:
927 try:
928 return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
928 return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
929 except OSError, err:
929 except OSError, err:
930 if err.errno != errno.ENOENT:
930 if err.errno != errno.ENOENT:
931 raise
931 raise
932 return (t, tz)
932 return (t, tz)
933
933
934 def cmp(self, text):
934 def cmp(self, text):
935 return self._repo.wread(self._path) == text
935 return self._repo.wread(self._path) != text
936
936
937 class memctx(object):
937 class memctx(object):
938 """Use memctx to perform in-memory commits via localrepo.commitctx().
938 """Use memctx to perform in-memory commits via localrepo.commitctx().
939
939
940 Revision information is supplied at initialization time while
940 Revision information is supplied at initialization time while
941 related files data and is made available through a callback
941 related files data and is made available through a callback
942 mechanism. 'repo' is the current localrepo, 'parents' is a
942 mechanism. 'repo' is the current localrepo, 'parents' is a
943 sequence of two parent revisions identifiers (pass None for every
943 sequence of two parent revisions identifiers (pass None for every
944 missing parent), 'text' is the commit message and 'files' lists
944 missing parent), 'text' is the commit message and 'files' lists
945 names of files touched by the revision (normalized and relative to
945 names of files touched by the revision (normalized and relative to
946 repository root).
946 repository root).
947
947
948 filectxfn(repo, memctx, path) is a callable receiving the
948 filectxfn(repo, memctx, path) is a callable receiving the
949 repository, the current memctx object and the normalized path of
949 repository, the current memctx object and the normalized path of
950 requested file, relative to repository root. It is fired by the
950 requested file, relative to repository root. It is fired by the
951 commit function for every file in 'files', but calls order is
951 commit function for every file in 'files', but calls order is
952 undefined. If the file is available in the revision being
952 undefined. If the file is available in the revision being
953 committed (updated or added), filectxfn returns a memfilectx
953 committed (updated or added), filectxfn returns a memfilectx
954 object. If the file was removed, filectxfn raises an
954 object. If the file was removed, filectxfn raises an
955 IOError. Moved files are represented by marking the source file
955 IOError. Moved files are represented by marking the source file
956 removed and the new file added with copy information (see
956 removed and the new file added with copy information (see
957 memfilectx).
957 memfilectx).
958
958
959 user receives the committer name and defaults to current
959 user receives the committer name and defaults to current
960 repository username, date is the commit date in any format
960 repository username, date is the commit date in any format
961 supported by util.parsedate() and defaults to current date, extra
961 supported by util.parsedate() and defaults to current date, extra
962 is a dictionary of metadata or is left empty.
962 is a dictionary of metadata or is left empty.
963 """
963 """
964 def __init__(self, repo, parents, text, files, filectxfn, user=None,
964 def __init__(self, repo, parents, text, files, filectxfn, user=None,
965 date=None, extra=None):
965 date=None, extra=None):
966 self._repo = repo
966 self._repo = repo
967 self._rev = None
967 self._rev = None
968 self._node = None
968 self._node = None
969 self._text = text
969 self._text = text
970 self._date = date and util.parsedate(date) or util.makedate()
970 self._date = date and util.parsedate(date) or util.makedate()
971 self._user = user
971 self._user = user
972 parents = [(p or nullid) for p in parents]
972 parents = [(p or nullid) for p in parents]
973 p1, p2 = parents
973 p1, p2 = parents
974 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
974 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
975 files = sorted(set(files))
975 files = sorted(set(files))
976 self._status = [files, [], [], [], []]
976 self._status = [files, [], [], [], []]
977 self._filectxfn = filectxfn
977 self._filectxfn = filectxfn
978
978
979 self._extra = extra and extra.copy() or {}
979 self._extra = extra and extra.copy() or {}
980 if 'branch' not in self._extra:
980 if 'branch' not in self._extra:
981 self._extra['branch'] = 'default'
981 self._extra['branch'] = 'default'
982 elif self._extra.get('branch') == '':
982 elif self._extra.get('branch') == '':
983 self._extra['branch'] = 'default'
983 self._extra['branch'] = 'default'
984
984
985 def __str__(self):
985 def __str__(self):
986 return str(self._parents[0]) + "+"
986 return str(self._parents[0]) + "+"
987
987
988 def __int__(self):
988 def __int__(self):
989 return self._rev
989 return self._rev
990
990
991 def __nonzero__(self):
991 def __nonzero__(self):
992 return True
992 return True
993
993
994 def __getitem__(self, key):
994 def __getitem__(self, key):
995 return self.filectx(key)
995 return self.filectx(key)
996
996
997 def p1(self):
997 def p1(self):
998 return self._parents[0]
998 return self._parents[0]
999 def p2(self):
999 def p2(self):
1000 return self._parents[1]
1000 return self._parents[1]
1001
1001
1002 def user(self):
1002 def user(self):
1003 return self._user or self._repo.ui.username()
1003 return self._user or self._repo.ui.username()
1004 def date(self):
1004 def date(self):
1005 return self._date
1005 return self._date
1006 def description(self):
1006 def description(self):
1007 return self._text
1007 return self._text
1008 def files(self):
1008 def files(self):
1009 return self.modified()
1009 return self.modified()
1010 def modified(self):
1010 def modified(self):
1011 return self._status[0]
1011 return self._status[0]
1012 def added(self):
1012 def added(self):
1013 return self._status[1]
1013 return self._status[1]
1014 def removed(self):
1014 def removed(self):
1015 return self._status[2]
1015 return self._status[2]
1016 def deleted(self):
1016 def deleted(self):
1017 return self._status[3]
1017 return self._status[3]
1018 def unknown(self):
1018 def unknown(self):
1019 return self._status[4]
1019 return self._status[4]
1020 def ignored(self):
1020 def ignored(self):
1021 return self._status[5]
1021 return self._status[5]
1022 def clean(self):
1022 def clean(self):
1023 return self._status[6]
1023 return self._status[6]
1024 def branch(self):
1024 def branch(self):
1025 return self._extra['branch']
1025 return self._extra['branch']
1026 def extra(self):
1026 def extra(self):
1027 return self._extra
1027 return self._extra
1028 def flags(self, f):
1028 def flags(self, f):
1029 return self[f].flags()
1029 return self[f].flags()
1030
1030
1031 def parents(self):
1031 def parents(self):
1032 """return contexts for each parent changeset"""
1032 """return contexts for each parent changeset"""
1033 return self._parents
1033 return self._parents
1034
1034
1035 def filectx(self, path, filelog=None):
1035 def filectx(self, path, filelog=None):
1036 """get a file context from the working directory"""
1036 """get a file context from the working directory"""
1037 return self._filectxfn(self._repo, self, path)
1037 return self._filectxfn(self._repo, self, path)
1038
1038
1039 def commit(self):
1039 def commit(self):
1040 """commit context to the repo"""
1040 """commit context to the repo"""
1041 return self._repo.commitctx(self)
1041 return self._repo.commitctx(self)
1042
1042
1043 class memfilectx(object):
1043 class memfilectx(object):
1044 """memfilectx represents an in-memory file to commit.
1044 """memfilectx represents an in-memory file to commit.
1045
1045
1046 See memctx for more details.
1046 See memctx for more details.
1047 """
1047 """
1048 def __init__(self, path, data, islink=False, isexec=False, copied=None):
1048 def __init__(self, path, data, islink=False, isexec=False, copied=None):
1049 """
1049 """
1050 path is the normalized file path relative to repository root.
1050 path is the normalized file path relative to repository root.
1051 data is the file content as a string.
1051 data is the file content as a string.
1052 islink is True if the file is a symbolic link.
1052 islink is True if the file is a symbolic link.
1053 isexec is True if the file is executable.
1053 isexec is True if the file is executable.
1054 copied is the source file path if current file was copied in the
1054 copied is the source file path if current file was copied in the
1055 revision being committed, or None."""
1055 revision being committed, or None."""
1056 self._path = path
1056 self._path = path
1057 self._data = data
1057 self._data = data
1058 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1058 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1059 self._copied = None
1059 self._copied = None
1060 if copied:
1060 if copied:
1061 self._copied = (copied, nullid)
1061 self._copied = (copied, nullid)
1062
1062
1063 def __nonzero__(self):
1063 def __nonzero__(self):
1064 return True
1064 return True
1065 def __str__(self):
1065 def __str__(self):
1066 return "%s@%s" % (self.path(), self._changectx)
1066 return "%s@%s" % (self.path(), self._changectx)
1067 def path(self):
1067 def path(self):
1068 return self._path
1068 return self._path
1069 def data(self):
1069 def data(self):
1070 return self._data
1070 return self._data
1071 def flags(self):
1071 def flags(self):
1072 return self._flags
1072 return self._flags
1073 def isexec(self):
1073 def isexec(self):
1074 return 'x' in self._flags
1074 return 'x' in self._flags
1075 def islink(self):
1075 def islink(self):
1076 return 'l' in self._flags
1076 return 'l' in self._flags
1077 def renamed(self):
1077 def renamed(self):
1078 return self._copied
1078 return self._copied
General Comments 0
You need to be logged in to leave comments. Login now