##// END OF EJS Templates
changectx: use repo.ui diffopts, allow argument override
Steve Borho -
r11105:6f454384 default
parent child Browse files
Show More
@@ -1,967 +1,969 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
11 import os, errno
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):
207 def diff(self, ctx2=None, match=None, opts=None):
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 return patch.diff(self._repo, ctx2.node(), self.node(), match=match)
213 diffopts = patch.diffopts(self._repo.ui, opts)
214 return patch.diff(self._repo, ctx2.node(), self.node(),
215 match=match, opts=diffopts)
214
216
215 class filectx(object):
217 class filectx(object):
216 """A filecontext object makes access to data related to a particular
218 """A filecontext object makes access to data related to a particular
217 filerevision convenient."""
219 filerevision convenient."""
218 def __init__(self, repo, path, changeid=None, fileid=None,
220 def __init__(self, repo, path, changeid=None, fileid=None,
219 filelog=None, changectx=None):
221 filelog=None, changectx=None):
220 """changeid can be a changeset revision, node, or tag.
222 """changeid can be a changeset revision, node, or tag.
221 fileid can be a file revision or node."""
223 fileid can be a file revision or node."""
222 self._repo = repo
224 self._repo = repo
223 self._path = path
225 self._path = path
224
226
225 assert (changeid is not None
227 assert (changeid is not None
226 or fileid is not None
228 or fileid is not None
227 or changectx is not None), \
229 or changectx is not None), \
228 ("bad args: changeid=%r, fileid=%r, changectx=%r"
230 ("bad args: changeid=%r, fileid=%r, changectx=%r"
229 % (changeid, fileid, changectx))
231 % (changeid, fileid, changectx))
230
232
231 if filelog:
233 if filelog:
232 self._filelog = filelog
234 self._filelog = filelog
233
235
234 if changeid is not None:
236 if changeid is not None:
235 self._changeid = changeid
237 self._changeid = changeid
236 if changectx is not None:
238 if changectx is not None:
237 self._changectx = changectx
239 self._changectx = changectx
238 if fileid is not None:
240 if fileid is not None:
239 self._fileid = fileid
241 self._fileid = fileid
240
242
241 @propertycache
243 @propertycache
242 def _changectx(self):
244 def _changectx(self):
243 return changectx(self._repo, self._changeid)
245 return changectx(self._repo, self._changeid)
244
246
245 @propertycache
247 @propertycache
246 def _filelog(self):
248 def _filelog(self):
247 return self._repo.file(self._path)
249 return self._repo.file(self._path)
248
250
249 @propertycache
251 @propertycache
250 def _changeid(self):
252 def _changeid(self):
251 if '_changectx' in self.__dict__:
253 if '_changectx' in self.__dict__:
252 return self._changectx.rev()
254 return self._changectx.rev()
253 else:
255 else:
254 return self._filelog.linkrev(self._filerev)
256 return self._filelog.linkrev(self._filerev)
255
257
256 @propertycache
258 @propertycache
257 def _filenode(self):
259 def _filenode(self):
258 if '_fileid' in self.__dict__:
260 if '_fileid' in self.__dict__:
259 return self._filelog.lookup(self._fileid)
261 return self._filelog.lookup(self._fileid)
260 else:
262 else:
261 return self._changectx.filenode(self._path)
263 return self._changectx.filenode(self._path)
262
264
263 @propertycache
265 @propertycache
264 def _filerev(self):
266 def _filerev(self):
265 return self._filelog.rev(self._filenode)
267 return self._filelog.rev(self._filenode)
266
268
267 @propertycache
269 @propertycache
268 def _repopath(self):
270 def _repopath(self):
269 return self._path
271 return self._path
270
272
271 def __nonzero__(self):
273 def __nonzero__(self):
272 try:
274 try:
273 self._filenode
275 self._filenode
274 return True
276 return True
275 except error.LookupError:
277 except error.LookupError:
276 # file is missing
278 # file is missing
277 return False
279 return False
278
280
279 def __str__(self):
281 def __str__(self):
280 return "%s@%s" % (self.path(), short(self.node()))
282 return "%s@%s" % (self.path(), short(self.node()))
281
283
282 def __repr__(self):
284 def __repr__(self):
283 return "<filectx %s>" % str(self)
285 return "<filectx %s>" % str(self)
284
286
285 def __hash__(self):
287 def __hash__(self):
286 try:
288 try:
287 return hash((self._path, self._filenode))
289 return hash((self._path, self._filenode))
288 except AttributeError:
290 except AttributeError:
289 return id(self)
291 return id(self)
290
292
291 def __eq__(self, other):
293 def __eq__(self, other):
292 try:
294 try:
293 return (self._path == other._path
295 return (self._path == other._path
294 and self._filenode == other._filenode)
296 and self._filenode == other._filenode)
295 except AttributeError:
297 except AttributeError:
296 return False
298 return False
297
299
298 def __ne__(self, other):
300 def __ne__(self, other):
299 return not (self == other)
301 return not (self == other)
300
302
301 def filectx(self, fileid):
303 def filectx(self, fileid):
302 '''opens an arbitrary revision of the file without
304 '''opens an arbitrary revision of the file without
303 opening a new filelog'''
305 opening a new filelog'''
304 return filectx(self._repo, self._path, fileid=fileid,
306 return filectx(self._repo, self._path, fileid=fileid,
305 filelog=self._filelog)
307 filelog=self._filelog)
306
308
307 def filerev(self):
309 def filerev(self):
308 return self._filerev
310 return self._filerev
309 def filenode(self):
311 def filenode(self):
310 return self._filenode
312 return self._filenode
311 def flags(self):
313 def flags(self):
312 return self._changectx.flags(self._path)
314 return self._changectx.flags(self._path)
313 def filelog(self):
315 def filelog(self):
314 return self._filelog
316 return self._filelog
315
317
316 def rev(self):
318 def rev(self):
317 if '_changectx' in self.__dict__:
319 if '_changectx' in self.__dict__:
318 return self._changectx.rev()
320 return self._changectx.rev()
319 if '_changeid' in self.__dict__:
321 if '_changeid' in self.__dict__:
320 return self._changectx.rev()
322 return self._changectx.rev()
321 return self._filelog.linkrev(self._filerev)
323 return self._filelog.linkrev(self._filerev)
322
324
323 def linkrev(self):
325 def linkrev(self):
324 return self._filelog.linkrev(self._filerev)
326 return self._filelog.linkrev(self._filerev)
325 def node(self):
327 def node(self):
326 return self._changectx.node()
328 return self._changectx.node()
327 def hex(self):
329 def hex(self):
328 return hex(self.node())
330 return hex(self.node())
329 def user(self):
331 def user(self):
330 return self._changectx.user()
332 return self._changectx.user()
331 def date(self):
333 def date(self):
332 return self._changectx.date()
334 return self._changectx.date()
333 def files(self):
335 def files(self):
334 return self._changectx.files()
336 return self._changectx.files()
335 def description(self):
337 def description(self):
336 return self._changectx.description()
338 return self._changectx.description()
337 def branch(self):
339 def branch(self):
338 return self._changectx.branch()
340 return self._changectx.branch()
339 def extra(self):
341 def extra(self):
340 return self._changectx.extra()
342 return self._changectx.extra()
341 def manifest(self):
343 def manifest(self):
342 return self._changectx.manifest()
344 return self._changectx.manifest()
343 def changectx(self):
345 def changectx(self):
344 return self._changectx
346 return self._changectx
345
347
346 def data(self):
348 def data(self):
347 return self._filelog.read(self._filenode)
349 return self._filelog.read(self._filenode)
348 def path(self):
350 def path(self):
349 return self._path
351 return self._path
350 def size(self):
352 def size(self):
351 return self._filelog.size(self._filerev)
353 return self._filelog.size(self._filerev)
352
354
353 def cmp(self, text):
355 def cmp(self, text):
354 return self._filelog.cmp(self._filenode, text)
356 return self._filelog.cmp(self._filenode, text)
355
357
356 def renamed(self):
358 def renamed(self):
357 """check if file was actually renamed in this changeset revision
359 """check if file was actually renamed in this changeset revision
358
360
359 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
360 if file revisions linkrev points back to the changeset in question
362 if file revisions linkrev points back to the changeset in question
361 or both changeset parents contain different file revisions.
363 or both changeset parents contain different file revisions.
362 """
364 """
363
365
364 renamed = self._filelog.renamed(self._filenode)
366 renamed = self._filelog.renamed(self._filenode)
365 if not renamed:
367 if not renamed:
366 return renamed
368 return renamed
367
369
368 if self.rev() == self.linkrev():
370 if self.rev() == self.linkrev():
369 return renamed
371 return renamed
370
372
371 name = self.path()
373 name = self.path()
372 fnode = self._filenode
374 fnode = self._filenode
373 for p in self._changectx.parents():
375 for p in self._changectx.parents():
374 try:
376 try:
375 if fnode == p.filenode(name):
377 if fnode == p.filenode(name):
376 return None
378 return None
377 except error.LookupError:
379 except error.LookupError:
378 pass
380 pass
379 return renamed
381 return renamed
380
382
381 def parents(self):
383 def parents(self):
382 p = self._path
384 p = self._path
383 fl = self._filelog
385 fl = self._filelog
384 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)]
385
387
386 r = self._filelog.renamed(self._filenode)
388 r = self._filelog.renamed(self._filenode)
387 if r:
389 if r:
388 pl[0] = (r[0], r[1], None)
390 pl[0] = (r[0], r[1], None)
389
391
390 return [filectx(self._repo, p, fileid=n, filelog=l)
392 return [filectx(self._repo, p, fileid=n, filelog=l)
391 for p, n, l in pl if n != nullid]
393 for p, n, l in pl if n != nullid]
392
394
393 def children(self):
395 def children(self):
394 # hard for renames
396 # hard for renames
395 c = self._filelog.children(self._filenode)
397 c = self._filelog.children(self._filenode)
396 return [filectx(self._repo, self._path, fileid=x,
398 return [filectx(self._repo, self._path, fileid=x,
397 filelog=self._filelog) for x in c]
399 filelog=self._filelog) for x in c]
398
400
399 def annotate(self, follow=False, linenumber=None):
401 def annotate(self, follow=False, linenumber=None):
400 '''returns a list of tuples of (ctx, line) for each line
402 '''returns a list of tuples of (ctx, line) for each line
401 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
402 that line was last changed.
404 that line was last changed.
403 This returns tuples of ((ctx, linenumber), line) for each line,
405 This returns tuples of ((ctx, linenumber), line) for each line,
404 if "linenumber" parameter is NOT "None".
406 if "linenumber" parameter is NOT "None".
405 In such tuples, linenumber means one at the first appearance
407 In such tuples, linenumber means one at the first appearance
406 in the managed file.
408 in the managed file.
407 To reduce annotation cost,
409 To reduce annotation cost,
408 this returns fixed value(False is used) as linenumber,
410 this returns fixed value(False is used) as linenumber,
409 if "linenumber" parameter is "False".'''
411 if "linenumber" parameter is "False".'''
410
412
411 def decorate_compat(text, rev):
413 def decorate_compat(text, rev):
412 return ([rev] * len(text.splitlines()), text)
414 return ([rev] * len(text.splitlines()), text)
413
415
414 def without_linenumber(text, rev):
416 def without_linenumber(text, rev):
415 return ([(rev, False)] * len(text.splitlines()), text)
417 return ([(rev, False)] * len(text.splitlines()), text)
416
418
417 def with_linenumber(text, rev):
419 def with_linenumber(text, rev):
418 size = len(text.splitlines())
420 size = len(text.splitlines())
419 return ([(rev, i) for i in xrange(1, size + 1)], text)
421 return ([(rev, i) for i in xrange(1, size + 1)], text)
420
422
421 decorate = (((linenumber is None) and decorate_compat) or
423 decorate = (((linenumber is None) and decorate_compat) or
422 (linenumber and with_linenumber) or
424 (linenumber and with_linenumber) or
423 without_linenumber)
425 without_linenumber)
424
426
425 def pair(parent, child):
427 def pair(parent, child):
426 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]):
427 child[0][b1:b2] = parent[0][a1:a2]
429 child[0][b1:b2] = parent[0][a1:a2]
428 return child
430 return child
429
431
430 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
432 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
431 def getctx(path, fileid):
433 def getctx(path, fileid):
432 log = path == self._path and self._filelog or getlog(path)
434 log = path == self._path and self._filelog or getlog(path)
433 return filectx(self._repo, path, fileid=fileid, filelog=log)
435 return filectx(self._repo, path, fileid=fileid, filelog=log)
434 getctx = util.lrucachefunc(getctx)
436 getctx = util.lrucachefunc(getctx)
435
437
436 def parents(f):
438 def parents(f):
437 # we want to reuse filectx objects as much as possible
439 # we want to reuse filectx objects as much as possible
438 p = f._path
440 p = f._path
439 if f._filerev is None: # working dir
441 if f._filerev is None: # working dir
440 pl = [(n.path(), n.filerev()) for n in f.parents()]
442 pl = [(n.path(), n.filerev()) for n in f.parents()]
441 else:
443 else:
442 pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
444 pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
443
445
444 if follow:
446 if follow:
445 r = f.renamed()
447 r = f.renamed()
446 if r:
448 if r:
447 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
449 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
448
450
449 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]
450
452
451 # use linkrev to find the first changeset where self appeared
453 # use linkrev to find the first changeset where self appeared
452 if self.rev() != self.linkrev():
454 if self.rev() != self.linkrev():
453 base = self.filectx(self.filerev())
455 base = self.filectx(self.filerev())
454 else:
456 else:
455 base = self
457 base = self
456
458
457 # find all ancestors
459 # find all ancestors
458 needed = {base: 1}
460 needed = {base: 1}
459 visit = [base]
461 visit = [base]
460 files = [base._path]
462 files = [base._path]
461 while visit:
463 while visit:
462 f = visit.pop(0)
464 f = visit.pop(0)
463 for p in parents(f):
465 for p in parents(f):
464 if p not in needed:
466 if p not in needed:
465 needed[p] = 1
467 needed[p] = 1
466 visit.append(p)
468 visit.append(p)
467 if p._path not in files:
469 if p._path not in files:
468 files.append(p._path)
470 files.append(p._path)
469 else:
471 else:
470 # count how many times we'll use this
472 # count how many times we'll use this
471 needed[p] += 1
473 needed[p] += 1
472
474
473 # sort by revision (per file) which is a topological order
475 # sort by revision (per file) which is a topological order
474 visit = []
476 visit = []
475 for f in files:
477 for f in files:
476 visit.extend(n for n in needed if n._path == f)
478 visit.extend(n for n in needed if n._path == f)
477
479
478 hist = {}
480 hist = {}
479 for f in sorted(visit, key=lambda x: x.rev()):
481 for f in sorted(visit, key=lambda x: x.rev()):
480 curr = decorate(f.data(), f)
482 curr = decorate(f.data(), f)
481 for p in parents(f):
483 for p in parents(f):
482 curr = pair(hist[p], curr)
484 curr = pair(hist[p], curr)
483 # trim the history of unneeded revs
485 # trim the history of unneeded revs
484 needed[p] -= 1
486 needed[p] -= 1
485 if not needed[p]:
487 if not needed[p]:
486 del hist[p]
488 del hist[p]
487 hist[f] = curr
489 hist[f] = curr
488
490
489 return zip(hist[f][0], hist[f][1].splitlines(True))
491 return zip(hist[f][0], hist[f][1].splitlines(True))
490
492
491 def ancestor(self, fc2):
493 def ancestor(self, fc2):
492 """
494 """
493 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
494 """
496 """
495
497
496 actx = self.changectx().ancestor(fc2.changectx())
498 actx = self.changectx().ancestor(fc2.changectx())
497
499
498 # the trivial case: changesets are unrelated, files must be too
500 # the trivial case: changesets are unrelated, files must be too
499 if not actx:
501 if not actx:
500 return None
502 return None
501
503
502 # the easy case: no (relevant) renames
504 # the easy case: no (relevant) renames
503 if fc2.path() == self.path() and self.path() in actx:
505 if fc2.path() == self.path() and self.path() in actx:
504 return actx[self.path()]
506 return actx[self.path()]
505 acache = {}
507 acache = {}
506
508
507 # prime the ancestor cache for the working directory
509 # prime the ancestor cache for the working directory
508 for c in (self, fc2):
510 for c in (self, fc2):
509 if c._filerev is None:
511 if c._filerev is None:
510 pl = [(n.path(), n.filenode()) for n in c.parents()]
512 pl = [(n.path(), n.filenode()) for n in c.parents()]
511 acache[(c._path, None)] = pl
513 acache[(c._path, None)] = pl
512
514
513 flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
515 flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
514 def parents(vertex):
516 def parents(vertex):
515 if vertex in acache:
517 if vertex in acache:
516 return acache[vertex]
518 return acache[vertex]
517 f, n = vertex
519 f, n = vertex
518 if f not in flcache:
520 if f not in flcache:
519 flcache[f] = self._repo.file(f)
521 flcache[f] = self._repo.file(f)
520 fl = flcache[f]
522 fl = flcache[f]
521 pl = [(f, p) for p in fl.parents(n) if p != nullid]
523 pl = [(f, p) for p in fl.parents(n) if p != nullid]
522 re = fl.renamed(n)
524 re = fl.renamed(n)
523 if re:
525 if re:
524 pl.append(re)
526 pl.append(re)
525 acache[vertex] = pl
527 acache[vertex] = pl
526 return pl
528 return pl
527
529
528 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
530 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
529 v = ancestor.ancestor(a, b, parents)
531 v = ancestor.ancestor(a, b, parents)
530 if v:
532 if v:
531 f, n = v
533 f, n = v
532 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
534 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
533
535
534 return None
536 return None
535
537
536 def ancestors(self):
538 def ancestors(self):
537 seen = set(str(self))
539 seen = set(str(self))
538 visit = [self]
540 visit = [self]
539 while visit:
541 while visit:
540 for parent in visit.pop(0).parents():
542 for parent in visit.pop(0).parents():
541 s = str(parent)
543 s = str(parent)
542 if s not in seen:
544 if s not in seen:
543 visit.append(parent)
545 visit.append(parent)
544 seen.add(s)
546 seen.add(s)
545 yield parent
547 yield parent
546
548
547 class workingctx(changectx):
549 class workingctx(changectx):
548 """A workingctx object makes access to data related to
550 """A workingctx object makes access to data related to
549 the current working directory convenient.
551 the current working directory convenient.
550 date - any valid date string or (unixtime, offset), or None.
552 date - any valid date string or (unixtime, offset), or None.
551 user - username string, or None.
553 user - username string, or None.
552 extra - a dictionary of extra values, or None.
554 extra - a dictionary of extra values, or None.
553 changes - a list of file lists as returned by localrepo.status()
555 changes - a list of file lists as returned by localrepo.status()
554 or None to use the repository status.
556 or None to use the repository status.
555 """
557 """
556 def __init__(self, repo, text="", user=None, date=None, extra=None,
558 def __init__(self, repo, text="", user=None, date=None, extra=None,
557 changes=None):
559 changes=None):
558 self._repo = repo
560 self._repo = repo
559 self._rev = None
561 self._rev = None
560 self._node = None
562 self._node = None
561 self._text = text
563 self._text = text
562 if date:
564 if date:
563 self._date = util.parsedate(date)
565 self._date = util.parsedate(date)
564 if user:
566 if user:
565 self._user = user
567 self._user = user
566 if changes:
568 if changes:
567 self._status = list(changes[:4])
569 self._status = list(changes[:4])
568 self._unknown = changes[4]
570 self._unknown = changes[4]
569 self._ignored = changes[5]
571 self._ignored = changes[5]
570 self._clean = changes[6]
572 self._clean = changes[6]
571 else:
573 else:
572 self._unknown = None
574 self._unknown = None
573 self._ignored = None
575 self._ignored = None
574 self._clean = None
576 self._clean = None
575
577
576 self._extra = {}
578 self._extra = {}
577 if extra:
579 if extra:
578 self._extra = extra.copy()
580 self._extra = extra.copy()
579 if 'branch' not in self._extra:
581 if 'branch' not in self._extra:
580 branch = self._repo.dirstate.branch()
582 branch = self._repo.dirstate.branch()
581 try:
583 try:
582 branch = branch.decode('UTF-8').encode('UTF-8')
584 branch = branch.decode('UTF-8').encode('UTF-8')
583 except UnicodeDecodeError:
585 except UnicodeDecodeError:
584 raise util.Abort(_('branch name not in UTF-8!'))
586 raise util.Abort(_('branch name not in UTF-8!'))
585 self._extra['branch'] = branch
587 self._extra['branch'] = branch
586 if self._extra['branch'] == '':
588 if self._extra['branch'] == '':
587 self._extra['branch'] = 'default'
589 self._extra['branch'] = 'default'
588
590
589 def __str__(self):
591 def __str__(self):
590 return str(self._parents[0]) + "+"
592 return str(self._parents[0]) + "+"
591
593
592 def __nonzero__(self):
594 def __nonzero__(self):
593 return True
595 return True
594
596
595 def __contains__(self, key):
597 def __contains__(self, key):
596 return self._repo.dirstate[key] not in "?r"
598 return self._repo.dirstate[key] not in "?r"
597
599
598 @propertycache
600 @propertycache
599 def _manifest(self):
601 def _manifest(self):
600 """generate a manifest corresponding to the working directory"""
602 """generate a manifest corresponding to the working directory"""
601
603
602 if self._unknown is None:
604 if self._unknown is None:
603 self.status(unknown=True)
605 self.status(unknown=True)
604
606
605 man = self._parents[0].manifest().copy()
607 man = self._parents[0].manifest().copy()
606 copied = self._repo.dirstate.copies()
608 copied = self._repo.dirstate.copies()
607 if len(self._parents) > 1:
609 if len(self._parents) > 1:
608 man2 = self.p2().manifest()
610 man2 = self.p2().manifest()
609 def getman(f):
611 def getman(f):
610 if f in man:
612 if f in man:
611 return man
613 return man
612 return man2
614 return man2
613 else:
615 else:
614 getman = lambda f: man
616 getman = lambda f: man
615 def cf(f):
617 def cf(f):
616 f = copied.get(f, f)
618 f = copied.get(f, f)
617 return getman(f).flags(f)
619 return getman(f).flags(f)
618 ff = self._repo.dirstate.flagfunc(cf)
620 ff = self._repo.dirstate.flagfunc(cf)
619 modified, added, removed, deleted = self._status
621 modified, added, removed, deleted = self._status
620 unknown = self._unknown
622 unknown = self._unknown
621 for i, l in (("a", added), ("m", modified), ("u", unknown)):
623 for i, l in (("a", added), ("m", modified), ("u", unknown)):
622 for f in l:
624 for f in l:
623 orig = copied.get(f, f)
625 orig = copied.get(f, f)
624 man[f] = getman(orig).get(orig, nullid) + i
626 man[f] = getman(orig).get(orig, nullid) + i
625 try:
627 try:
626 man.set(f, ff(f))
628 man.set(f, ff(f))
627 except OSError:
629 except OSError:
628 pass
630 pass
629
631
630 for f in deleted + removed:
632 for f in deleted + removed:
631 if f in man:
633 if f in man:
632 del man[f]
634 del man[f]
633
635
634 return man
636 return man
635
637
636 @propertycache
638 @propertycache
637 def _status(self):
639 def _status(self):
638 return self._repo.status()[:4]
640 return self._repo.status()[:4]
639
641
640 @propertycache
642 @propertycache
641 def _user(self):
643 def _user(self):
642 return self._repo.ui.username()
644 return self._repo.ui.username()
643
645
644 @propertycache
646 @propertycache
645 def _date(self):
647 def _date(self):
646 return util.makedate()
648 return util.makedate()
647
649
648 @propertycache
650 @propertycache
649 def _parents(self):
651 def _parents(self):
650 p = self._repo.dirstate.parents()
652 p = self._repo.dirstate.parents()
651 if p[1] == nullid:
653 if p[1] == nullid:
652 p = p[:-1]
654 p = p[:-1]
653 self._parents = [changectx(self._repo, x) for x in p]
655 self._parents = [changectx(self._repo, x) for x in p]
654 return self._parents
656 return self._parents
655
657
656 def status(self, ignored=False, clean=False, unknown=False):
658 def status(self, ignored=False, clean=False, unknown=False):
657 """Explicit status query
659 """Explicit status query
658 Unless this method is used to query the working copy status, the
660 Unless this method is used to query the working copy status, the
659 _status property will implicitly read the status using its default
661 _status property will implicitly read the status using its default
660 arguments."""
662 arguments."""
661 stat = self._repo.status(ignored=ignored, clean=clean, unknown=unknown)
663 stat = self._repo.status(ignored=ignored, clean=clean, unknown=unknown)
662 self._unknown = self._ignored = self._clean = None
664 self._unknown = self._ignored = self._clean = None
663 if unknown:
665 if unknown:
664 self._unknown = stat[4]
666 self._unknown = stat[4]
665 if ignored:
667 if ignored:
666 self._ignored = stat[5]
668 self._ignored = stat[5]
667 if clean:
669 if clean:
668 self._clean = stat[6]
670 self._clean = stat[6]
669 self._status = stat[:4]
671 self._status = stat[:4]
670 return stat
672 return stat
671
673
672 def manifest(self):
674 def manifest(self):
673 return self._manifest
675 return self._manifest
674 def user(self):
676 def user(self):
675 return self._user or self._repo.ui.username()
677 return self._user or self._repo.ui.username()
676 def date(self):
678 def date(self):
677 return self._date
679 return self._date
678 def description(self):
680 def description(self):
679 return self._text
681 return self._text
680 def files(self):
682 def files(self):
681 return sorted(self._status[0] + self._status[1] + self._status[2])
683 return sorted(self._status[0] + self._status[1] + self._status[2])
682
684
683 def modified(self):
685 def modified(self):
684 return self._status[0]
686 return self._status[0]
685 def added(self):
687 def added(self):
686 return self._status[1]
688 return self._status[1]
687 def removed(self):
689 def removed(self):
688 return self._status[2]
690 return self._status[2]
689 def deleted(self):
691 def deleted(self):
690 return self._status[3]
692 return self._status[3]
691 def unknown(self):
693 def unknown(self):
692 assert self._unknown is not None # must call status first
694 assert self._unknown is not None # must call status first
693 return self._unknown
695 return self._unknown
694 def ignored(self):
696 def ignored(self):
695 assert self._ignored is not None # must call status first
697 assert self._ignored is not None # must call status first
696 return self._ignored
698 return self._ignored
697 def clean(self):
699 def clean(self):
698 assert self._clean is not None # must call status first
700 assert self._clean is not None # must call status first
699 return self._clean
701 return self._clean
700 def branch(self):
702 def branch(self):
701 return self._extra['branch']
703 return self._extra['branch']
702 def extra(self):
704 def extra(self):
703 return self._extra
705 return self._extra
704
706
705 def tags(self):
707 def tags(self):
706 t = []
708 t = []
707 [t.extend(p.tags()) for p in self.parents()]
709 [t.extend(p.tags()) for p in self.parents()]
708 return t
710 return t
709
711
710 def children(self):
712 def children(self):
711 return []
713 return []
712
714
713 def flags(self, path):
715 def flags(self, path):
714 if '_manifest' in self.__dict__:
716 if '_manifest' in self.__dict__:
715 try:
717 try:
716 return self._manifest.flags(path)
718 return self._manifest.flags(path)
717 except KeyError:
719 except KeyError:
718 return ''
720 return ''
719
721
720 orig = self._repo.dirstate.copies().get(path, path)
722 orig = self._repo.dirstate.copies().get(path, path)
721
723
722 def findflag(ctx):
724 def findflag(ctx):
723 mnode = ctx.changeset()[0]
725 mnode = ctx.changeset()[0]
724 node, flag = self._repo.manifest.find(mnode, orig)
726 node, flag = self._repo.manifest.find(mnode, orig)
725 ff = self._repo.dirstate.flagfunc(lambda x: flag or None)
727 ff = self._repo.dirstate.flagfunc(lambda x: flag or None)
726 try:
728 try:
727 return ff(path)
729 return ff(path)
728 except OSError:
730 except OSError:
729 pass
731 pass
730
732
731 flag = findflag(self._parents[0])
733 flag = findflag(self._parents[0])
732 if flag is None and len(self.parents()) > 1:
734 if flag is None and len(self.parents()) > 1:
733 flag = findflag(self._parents[1])
735 flag = findflag(self._parents[1])
734 if flag is None or self._repo.dirstate[path] == 'r':
736 if flag is None or self._repo.dirstate[path] == 'r':
735 return ''
737 return ''
736 return flag
738 return flag
737
739
738 def filectx(self, path, filelog=None):
740 def filectx(self, path, filelog=None):
739 """get a file context from the working directory"""
741 """get a file context from the working directory"""
740 return workingfilectx(self._repo, path, workingctx=self,
742 return workingfilectx(self._repo, path, workingctx=self,
741 filelog=filelog)
743 filelog=filelog)
742
744
743 def ancestor(self, c2):
745 def ancestor(self, c2):
744 """return the ancestor context of self and c2"""
746 """return the ancestor context of self and c2"""
745 return self._parents[0].ancestor(c2) # punt on two parents for now
747 return self._parents[0].ancestor(c2) # punt on two parents for now
746
748
747 def walk(self, match):
749 def walk(self, match):
748 return sorted(self._repo.dirstate.walk(match, self.substate.keys(),
750 return sorted(self._repo.dirstate.walk(match, self.substate.keys(),
749 True, False))
751 True, False))
750
752
751 def dirty(self, missing=False):
753 def dirty(self, missing=False):
752 "check whether a working directory is modified"
754 "check whether a working directory is modified"
753
755
754 return (self.p2() or self.branch() != self.p1().branch() or
756 return (self.p2() or self.branch() != self.p1().branch() or
755 self.modified() or self.added() or self.removed() or
757 self.modified() or self.added() or self.removed() or
756 (missing and self.deleted()))
758 (missing and self.deleted()))
757
759
758 class workingfilectx(filectx):
760 class workingfilectx(filectx):
759 """A workingfilectx object makes access to data related to a particular
761 """A workingfilectx object makes access to data related to a particular
760 file in the working directory convenient."""
762 file in the working directory convenient."""
761 def __init__(self, repo, path, filelog=None, workingctx=None):
763 def __init__(self, repo, path, filelog=None, workingctx=None):
762 """changeid can be a changeset revision, node, or tag.
764 """changeid can be a changeset revision, node, or tag.
763 fileid can be a file revision or node."""
765 fileid can be a file revision or node."""
764 self._repo = repo
766 self._repo = repo
765 self._path = path
767 self._path = path
766 self._changeid = None
768 self._changeid = None
767 self._filerev = self._filenode = None
769 self._filerev = self._filenode = None
768
770
769 if filelog:
771 if filelog:
770 self._filelog = filelog
772 self._filelog = filelog
771 if workingctx:
773 if workingctx:
772 self._changectx = workingctx
774 self._changectx = workingctx
773
775
774 @propertycache
776 @propertycache
775 def _changectx(self):
777 def _changectx(self):
776 return workingctx(self._repo)
778 return workingctx(self._repo)
777
779
778 def __nonzero__(self):
780 def __nonzero__(self):
779 return True
781 return True
780
782
781 def __str__(self):
783 def __str__(self):
782 return "%s@%s" % (self.path(), self._changectx)
784 return "%s@%s" % (self.path(), self._changectx)
783
785
784 def data(self):
786 def data(self):
785 return self._repo.wread(self._path)
787 return self._repo.wread(self._path)
786 def renamed(self):
788 def renamed(self):
787 rp = self._repo.dirstate.copied(self._path)
789 rp = self._repo.dirstate.copied(self._path)
788 if not rp:
790 if not rp:
789 return None
791 return None
790 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
792 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
791
793
792 def parents(self):
794 def parents(self):
793 '''return parent filectxs, following copies if necessary'''
795 '''return parent filectxs, following copies if necessary'''
794 def filenode(ctx, path):
796 def filenode(ctx, path):
795 return ctx._manifest.get(path, nullid)
797 return ctx._manifest.get(path, nullid)
796
798
797 path = self._path
799 path = self._path
798 fl = self._filelog
800 fl = self._filelog
799 pcl = self._changectx._parents
801 pcl = self._changectx._parents
800 renamed = self.renamed()
802 renamed = self.renamed()
801
803
802 if renamed:
804 if renamed:
803 pl = [renamed + (None,)]
805 pl = [renamed + (None,)]
804 else:
806 else:
805 pl = [(path, filenode(pcl[0], path), fl)]
807 pl = [(path, filenode(pcl[0], path), fl)]
806
808
807 for pc in pcl[1:]:
809 for pc in pcl[1:]:
808 pl.append((path, filenode(pc, path), fl))
810 pl.append((path, filenode(pc, path), fl))
809
811
810 return [filectx(self._repo, p, fileid=n, filelog=l)
812 return [filectx(self._repo, p, fileid=n, filelog=l)
811 for p, n, l in pl if n != nullid]
813 for p, n, l in pl if n != nullid]
812
814
813 def children(self):
815 def children(self):
814 return []
816 return []
815
817
816 def size(self):
818 def size(self):
817 return os.stat(self._repo.wjoin(self._path)).st_size
819 return os.stat(self._repo.wjoin(self._path)).st_size
818 def date(self):
820 def date(self):
819 t, tz = self._changectx.date()
821 t, tz = self._changectx.date()
820 try:
822 try:
821 return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
823 return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
822 except OSError, err:
824 except OSError, err:
823 if err.errno != errno.ENOENT:
825 if err.errno != errno.ENOENT:
824 raise
826 raise
825 return (t, tz)
827 return (t, tz)
826
828
827 def cmp(self, text):
829 def cmp(self, text):
828 return self._repo.wread(self._path) == text
830 return self._repo.wread(self._path) == text
829
831
830 class memctx(object):
832 class memctx(object):
831 """Use memctx to perform in-memory commits via localrepo.commitctx().
833 """Use memctx to perform in-memory commits via localrepo.commitctx().
832
834
833 Revision information is supplied at initialization time while
835 Revision information is supplied at initialization time while
834 related files data and is made available through a callback
836 related files data and is made available through a callback
835 mechanism. 'repo' is the current localrepo, 'parents' is a
837 mechanism. 'repo' is the current localrepo, 'parents' is a
836 sequence of two parent revisions identifiers (pass None for every
838 sequence of two parent revisions identifiers (pass None for every
837 missing parent), 'text' is the commit message and 'files' lists
839 missing parent), 'text' is the commit message and 'files' lists
838 names of files touched by the revision (normalized and relative to
840 names of files touched by the revision (normalized and relative to
839 repository root).
841 repository root).
840
842
841 filectxfn(repo, memctx, path) is a callable receiving the
843 filectxfn(repo, memctx, path) is a callable receiving the
842 repository, the current memctx object and the normalized path of
844 repository, the current memctx object and the normalized path of
843 requested file, relative to repository root. It is fired by the
845 requested file, relative to repository root. It is fired by the
844 commit function for every file in 'files', but calls order is
846 commit function for every file in 'files', but calls order is
845 undefined. If the file is available in the revision being
847 undefined. If the file is available in the revision being
846 committed (updated or added), filectxfn returns a memfilectx
848 committed (updated or added), filectxfn returns a memfilectx
847 object. If the file was removed, filectxfn raises an
849 object. If the file was removed, filectxfn raises an
848 IOError. Moved files are represented by marking the source file
850 IOError. Moved files are represented by marking the source file
849 removed and the new file added with copy information (see
851 removed and the new file added with copy information (see
850 memfilectx).
852 memfilectx).
851
853
852 user receives the committer name and defaults to current
854 user receives the committer name and defaults to current
853 repository username, date is the commit date in any format
855 repository username, date is the commit date in any format
854 supported by util.parsedate() and defaults to current date, extra
856 supported by util.parsedate() and defaults to current date, extra
855 is a dictionary of metadata or is left empty.
857 is a dictionary of metadata or is left empty.
856 """
858 """
857 def __init__(self, repo, parents, text, files, filectxfn, user=None,
859 def __init__(self, repo, parents, text, files, filectxfn, user=None,
858 date=None, extra=None):
860 date=None, extra=None):
859 self._repo = repo
861 self._repo = repo
860 self._rev = None
862 self._rev = None
861 self._node = None
863 self._node = None
862 self._text = text
864 self._text = text
863 self._date = date and util.parsedate(date) or util.makedate()
865 self._date = date and util.parsedate(date) or util.makedate()
864 self._user = user
866 self._user = user
865 parents = [(p or nullid) for p in parents]
867 parents = [(p or nullid) for p in parents]
866 p1, p2 = parents
868 p1, p2 = parents
867 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
869 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
868 files = sorted(set(files))
870 files = sorted(set(files))
869 self._status = [files, [], [], [], []]
871 self._status = [files, [], [], [], []]
870 self._filectxfn = filectxfn
872 self._filectxfn = filectxfn
871
873
872 self._extra = extra and extra.copy() or {}
874 self._extra = extra and extra.copy() or {}
873 if 'branch' not in self._extra:
875 if 'branch' not in self._extra:
874 self._extra['branch'] = 'default'
876 self._extra['branch'] = 'default'
875 elif self._extra.get('branch') == '':
877 elif self._extra.get('branch') == '':
876 self._extra['branch'] = 'default'
878 self._extra['branch'] = 'default'
877
879
878 def __str__(self):
880 def __str__(self):
879 return str(self._parents[0]) + "+"
881 return str(self._parents[0]) + "+"
880
882
881 def __int__(self):
883 def __int__(self):
882 return self._rev
884 return self._rev
883
885
884 def __nonzero__(self):
886 def __nonzero__(self):
885 return True
887 return True
886
888
887 def __getitem__(self, key):
889 def __getitem__(self, key):
888 return self.filectx(key)
890 return self.filectx(key)
889
891
890 def p1(self):
892 def p1(self):
891 return self._parents[0]
893 return self._parents[0]
892 def p2(self):
894 def p2(self):
893 return self._parents[1]
895 return self._parents[1]
894
896
895 def user(self):
897 def user(self):
896 return self._user or self._repo.ui.username()
898 return self._user or self._repo.ui.username()
897 def date(self):
899 def date(self):
898 return self._date
900 return self._date
899 def description(self):
901 def description(self):
900 return self._text
902 return self._text
901 def files(self):
903 def files(self):
902 return self.modified()
904 return self.modified()
903 def modified(self):
905 def modified(self):
904 return self._status[0]
906 return self._status[0]
905 def added(self):
907 def added(self):
906 return self._status[1]
908 return self._status[1]
907 def removed(self):
909 def removed(self):
908 return self._status[2]
910 return self._status[2]
909 def deleted(self):
911 def deleted(self):
910 return self._status[3]
912 return self._status[3]
911 def unknown(self):
913 def unknown(self):
912 return self._status[4]
914 return self._status[4]
913 def ignored(self):
915 def ignored(self):
914 return self._status[5]
916 return self._status[5]
915 def clean(self):
917 def clean(self):
916 return self._status[6]
918 return self._status[6]
917 def branch(self):
919 def branch(self):
918 return self._extra['branch']
920 return self._extra['branch']
919 def extra(self):
921 def extra(self):
920 return self._extra
922 return self._extra
921 def flags(self, f):
923 def flags(self, f):
922 return self[f].flags()
924 return self[f].flags()
923
925
924 def parents(self):
926 def parents(self):
925 """return contexts for each parent changeset"""
927 """return contexts for each parent changeset"""
926 return self._parents
928 return self._parents
927
929
928 def filectx(self, path, filelog=None):
930 def filectx(self, path, filelog=None):
929 """get a file context from the working directory"""
931 """get a file context from the working directory"""
930 return self._filectxfn(self._repo, self, path)
932 return self._filectxfn(self._repo, self, path)
931
933
932 class memfilectx(object):
934 class memfilectx(object):
933 """memfilectx represents an in-memory file to commit.
935 """memfilectx represents an in-memory file to commit.
934
936
935 See memctx for more details.
937 See memctx for more details.
936 """
938 """
937 def __init__(self, path, data, islink, isexec, copied):
939 def __init__(self, path, data, islink, isexec, copied):
938 """
940 """
939 path is the normalized file path relative to repository root.
941 path is the normalized file path relative to repository root.
940 data is the file content as a string.
942 data is the file content as a string.
941 islink is True if the file is a symbolic link.
943 islink is True if the file is a symbolic link.
942 isexec is True if the file is executable.
944 isexec is True if the file is executable.
943 copied is the source file path if current file was copied in the
945 copied is the source file path if current file was copied in the
944 revision being committed, or None."""
946 revision being committed, or None."""
945 self._path = path
947 self._path = path
946 self._data = data
948 self._data = data
947 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
949 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
948 self._copied = None
950 self._copied = None
949 if copied:
951 if copied:
950 self._copied = (copied, nullid)
952 self._copied = (copied, nullid)
951
953
952 def __nonzero__(self):
954 def __nonzero__(self):
953 return True
955 return True
954 def __str__(self):
956 def __str__(self):
955 return "%s@%s" % (self.path(), self._changectx)
957 return "%s@%s" % (self.path(), self._changectx)
956 def path(self):
958 def path(self):
957 return self._path
959 return self._path
958 def data(self):
960 def data(self):
959 return self._data
961 return self._data
960 def flags(self):
962 def flags(self):
961 return self._flags
963 return self._flags
962 def isexec(self):
964 def isexec(self):
963 return 'x' in self._flags
965 return 'x' in self._flags
964 def islink(self):
966 def islink(self):
965 return 'l' in self._flags
967 return 'l' in self._flags
966 def renamed(self):
968 def renamed(self):
967 return self._copied
969 return self._copied
General Comments 0
You need to be logged in to leave comments. Login now