##// END OF EJS Templates
Fix annotate where linkrev != rev without exporting linkrev
Brendan Cully -
r3404:1a437b0f default
parent child Browse files
Show More
@@ -1,481 +1,487 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 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from node import *
8 from node import *
9 from i18n import gettext as _
9 from i18n import gettext as _
10 from demandload import demandload
10 from demandload import demandload
11 demandload(globals(), "ancestor bdiff repo revlog util os")
11 demandload(globals(), "ancestor bdiff repo revlog util os")
12
12
13 class changectx(object):
13 class changectx(object):
14 """A changecontext object makes access to data related to a particular
14 """A changecontext object makes access to data related to a particular
15 changeset convenient."""
15 changeset convenient."""
16 def __init__(self, repo, changeid=None):
16 def __init__(self, repo, changeid=None):
17 """changeid is a revision number, node, or tag"""
17 """changeid is a revision number, node, or tag"""
18 self._repo = repo
18 self._repo = repo
19
19
20 if not changeid and changeid != 0:
20 if not changeid and changeid != 0:
21 p1, p2 = self._repo.dirstate.parents()
21 p1, p2 = self._repo.dirstate.parents()
22 self._rev = self._repo.changelog.rev(p1)
22 self._rev = self._repo.changelog.rev(p1)
23 if self._rev == -1:
23 if self._rev == -1:
24 changeid = 'tip'
24 changeid = 'tip'
25 else:
25 else:
26 self._node = p1
26 self._node = p1
27 return
27 return
28
28
29 self._node = self._repo.lookup(changeid)
29 self._node = self._repo.lookup(changeid)
30 self._rev = self._repo.changelog.rev(self._node)
30 self._rev = self._repo.changelog.rev(self._node)
31
31
32 def __str__(self):
32 def __str__(self):
33 return short(self.node())
33 return short(self.node())
34
34
35 def __repr__(self):
35 def __repr__(self):
36 return "<changectx %s>" % str(self)
36 return "<changectx %s>" % str(self)
37
37
38 def __eq__(self, other):
38 def __eq__(self, other):
39 return self._rev == other._rev
39 return self._rev == other._rev
40
40
41 def __nonzero__(self):
41 def __nonzero__(self):
42 return self._rev != -1
42 return self._rev != -1
43
43
44 def __getattr__(self, name):
44 def __getattr__(self, name):
45 if name == '_changeset':
45 if name == '_changeset':
46 self._changeset = self._repo.changelog.read(self.node())
46 self._changeset = self._repo.changelog.read(self.node())
47 return self._changeset
47 return self._changeset
48 elif name == '_manifest':
48 elif name == '_manifest':
49 self._manifest = self._repo.manifest.read(self._changeset[0])
49 self._manifest = self._repo.manifest.read(self._changeset[0])
50 return self._manifest
50 return self._manifest
51 elif name == '_manifestdelta':
51 elif name == '_manifestdelta':
52 md = self._repo.manifest.readdelta(self._changeset[0])
52 md = self._repo.manifest.readdelta(self._changeset[0])
53 self._manifestdelta = md
53 self._manifestdelta = md
54 return self._manifestdelta
54 return self._manifestdelta
55 else:
55 else:
56 raise AttributeError, name
56 raise AttributeError, name
57
57
58 def changeset(self): return self._changeset
58 def changeset(self): return self._changeset
59 def manifest(self): return self._manifest
59 def manifest(self): return self._manifest
60
60
61 def rev(self): return self._rev
61 def rev(self): return self._rev
62 def node(self): return self._node
62 def node(self): return self._node
63 def user(self): return self._changeset[1]
63 def user(self): return self._changeset[1]
64 def date(self): return self._changeset[2]
64 def date(self): return self._changeset[2]
65 def files(self): return self._changeset[3]
65 def files(self): return self._changeset[3]
66 def description(self): return self._changeset[4]
66 def description(self): return self._changeset[4]
67
67
68 def parents(self):
68 def parents(self):
69 """return contexts for each parent changeset"""
69 """return contexts for each parent changeset"""
70 p = self._repo.changelog.parents(self._node)
70 p = self._repo.changelog.parents(self._node)
71 return [ changectx(self._repo, x) for x in p ]
71 return [ changectx(self._repo, x) for x in p ]
72
72
73 def children(self):
73 def children(self):
74 """return contexts for each child changeset"""
74 """return contexts for each child changeset"""
75 c = self._repo.changelog.children(self._node)
75 c = self._repo.changelog.children(self._node)
76 return [ changectx(self._repo, x) for x in c ]
76 return [ changectx(self._repo, x) for x in c ]
77
77
78 def filenode(self, path):
78 def filenode(self, path):
79 if '_manifest' in self.__dict__:
79 if '_manifest' in self.__dict__:
80 try:
80 try:
81 return self._manifest[path]
81 return self._manifest[path]
82 except KeyError:
82 except KeyError:
83 raise repo.LookupError(_("'%s' not found in manifest") % path)
83 raise repo.LookupError(_("'%s' not found in manifest") % path)
84 if '_manifestdelta' in self.__dict__ or path in self.files():
84 if '_manifestdelta' in self.__dict__ or path in self.files():
85 if path in self._manifestdelta:
85 if path in self._manifestdelta:
86 return self._manifestdelta[path]
86 return self._manifestdelta[path]
87 node, flag = self._repo.manifest.find(self._changeset[0], path)
87 node, flag = self._repo.manifest.find(self._changeset[0], path)
88 if not node:
88 if not node:
89 raise repo.LookupError(_("'%s' not found in manifest") % path)
89 raise repo.LookupError(_("'%s' not found in manifest") % path)
90
90
91 return node
91 return node
92
92
93 def filectx(self, path, fileid=None):
93 def filectx(self, path, fileid=None):
94 """get a file context from this changeset"""
94 """get a file context from this changeset"""
95 if fileid is None:
95 if fileid is None:
96 fileid = self.filenode(path)
96 fileid = self.filenode(path)
97 return filectx(self._repo, path, fileid=fileid, changectx=self)
97 return filectx(self._repo, path, fileid=fileid, changectx=self)
98
98
99 def filectxs(self):
99 def filectxs(self):
100 """generate a file context for each file in this changeset's
100 """generate a file context for each file in this changeset's
101 manifest"""
101 manifest"""
102 mf = self.manifest()
102 mf = self.manifest()
103 m = mf.keys()
103 m = mf.keys()
104 m.sort()
104 m.sort()
105 for f in m:
105 for f in m:
106 yield self.filectx(f, fileid=mf[f])
106 yield self.filectx(f, fileid=mf[f])
107
107
108 def ancestor(self, c2):
108 def ancestor(self, c2):
109 """
109 """
110 return the ancestor context of self and c2
110 return the ancestor context of self and c2
111 """
111 """
112 n = self._repo.changelog.ancestor(self._node, c2._node)
112 n = self._repo.changelog.ancestor(self._node, c2._node)
113 return changectx(self._repo, n)
113 return changectx(self._repo, n)
114
114
115 class filectx(object):
115 class filectx(object):
116 """A filecontext object makes access to data related to a particular
116 """A filecontext object makes access to data related to a particular
117 filerevision convenient."""
117 filerevision convenient."""
118 def __init__(self, repo, path, changeid=None, fileid=None,
118 def __init__(self, repo, path, changeid=None, fileid=None,
119 filelog=None, changectx=None):
119 filelog=None, changectx=None):
120 """changeid can be a changeset revision, node, or tag.
120 """changeid can be a changeset revision, node, or tag.
121 fileid can be a file revision or node."""
121 fileid can be a file revision or node."""
122 self._repo = repo
122 self._repo = repo
123 self._path = path
123 self._path = path
124
124
125 assert changeid is not None or fileid is not None
125 assert changeid is not None or fileid is not None
126
126
127 if filelog:
127 if filelog:
128 self._filelog = filelog
128 self._filelog = filelog
129 if changectx:
129 if changectx:
130 self._changectx = changectx
130 self._changectx = changectx
131 self._changeid = changectx.node()
131 self._changeid = changectx.node()
132
132
133 if fileid is None:
133 if fileid is None:
134 self._changeid = changeid
134 self._changeid = changeid
135 else:
135 else:
136 self._fileid = fileid
136 self._fileid = fileid
137
137
138 def __getattr__(self, name):
138 def __getattr__(self, name):
139 if name == '_changectx':
139 if name == '_changectx':
140 self._changectx = changectx(self._repo, self._changeid)
140 self._changectx = changectx(self._repo, self._changeid)
141 return self._changectx
141 return self._changectx
142 elif name == '_filelog':
142 elif name == '_filelog':
143 self._filelog = self._repo.file(self._path)
143 self._filelog = self._repo.file(self._path)
144 return self._filelog
144 return self._filelog
145 elif name == '_changeid':
145 elif name == '_changeid':
146 self._changeid = self._filelog.linkrev(self._filenode)
146 self._changeid = self._filelog.linkrev(self._filenode)
147 return self._changeid
147 return self._changeid
148 elif name == '_filenode':
148 elif name == '_filenode':
149 try:
149 try:
150 if '_fileid' in self.__dict__:
150 if '_fileid' in self.__dict__:
151 self._filenode = self._filelog.lookup(self._fileid)
151 self._filenode = self._filelog.lookup(self._fileid)
152 else:
152 else:
153 self._filenode = self._changectx.filenode(self._path)
153 self._filenode = self._changectx.filenode(self._path)
154 except revlog.RevlogError, inst:
154 except revlog.RevlogError, inst:
155 raise repo.LookupError(str(inst))
155 raise repo.LookupError(str(inst))
156 return self._filenode
156 return self._filenode
157 elif name == '_filerev':
157 elif name == '_filerev':
158 self._filerev = self._filelog.rev(self._filenode)
158 self._filerev = self._filelog.rev(self._filenode)
159 return self._filerev
159 return self._filerev
160 else:
160 else:
161 raise AttributeError, name
161 raise AttributeError, name
162
162
163 def __nonzero__(self):
163 def __nonzero__(self):
164 return self._filerev != nullid
164 return self._filerev != nullid
165
165
166 def __str__(self):
166 def __str__(self):
167 return "%s@%s" % (self.path(), short(self.node()))
167 return "%s@%s" % (self.path(), short(self.node()))
168
168
169 def __repr__(self):
169 def __repr__(self):
170 return "<filectx %s>" % str(self)
170 return "<filectx %s>" % str(self)
171
171
172 def __eq__(self, other):
172 def __eq__(self, other):
173 return self._path == other._path and self._changeid == other._changeid
173 return self._path == other._path and self._changeid == other._changeid
174
174
175 def filectx(self, fileid):
175 def filectx(self, fileid):
176 '''opens an arbitrary revision of the file without
176 '''opens an arbitrary revision of the file without
177 opening a new filelog'''
177 opening a new filelog'''
178 return filectx(self._repo, self._path, fileid=fileid,
178 return filectx(self._repo, self._path, fileid=fileid,
179 filelog=self._filelog)
179 filelog=self._filelog)
180
180
181 def filerev(self): return self._filerev
181 def filerev(self): return self._filerev
182 def filenode(self): return self._filenode
182 def filenode(self): return self._filenode
183 def filelog(self): return self._filelog
183 def filelog(self): return self._filelog
184
184
185 def rev(self):
185 def rev(self):
186 if '_changectx' in self.__dict__:
186 if '_changectx' in self.__dict__:
187 return self._changectx.rev()
187 return self._changectx.rev()
188 return self._filelog.linkrev(self._filenode)
188 return self._filelog.linkrev(self._filenode)
189
189
190 def node(self): return self._changectx.node()
190 def node(self): return self._changectx.node()
191 def user(self): return self._changectx.user()
191 def user(self): return self._changectx.user()
192 def date(self): return self._changectx.date()
192 def date(self): return self._changectx.date()
193 def files(self): return self._changectx.files()
193 def files(self): return self._changectx.files()
194 def description(self): return self._changectx.description()
194 def description(self): return self._changectx.description()
195 def manifest(self): return self._changectx.manifest()
195 def manifest(self): return self._changectx.manifest()
196 def changectx(self): return self._changectx
196 def changectx(self): return self._changectx
197
197
198 def data(self): return self._filelog.read(self._filenode)
198 def data(self): return self._filelog.read(self._filenode)
199 def renamed(self): return self._filelog.renamed(self._filenode)
199 def renamed(self): return self._filelog.renamed(self._filenode)
200 def path(self): return self._path
200 def path(self): return self._path
201 def size(self): return self._filelog.size(self._filerev)
201 def size(self): return self._filelog.size(self._filerev)
202
202
203 def cmp(self, text): return self._filelog.cmp(self._filenode, text)
203 def cmp(self, text): return self._filelog.cmp(self._filenode, text)
204
204
205 def parents(self):
205 def parents(self):
206 p = self._path
206 p = self._path
207 fl = self._filelog
207 fl = self._filelog
208 pl = [ (p, n, fl) for n in self._filelog.parents(self._filenode) ]
208 pl = [ (p, n, fl) for n in self._filelog.parents(self._filenode) ]
209
209
210 r = self.renamed()
210 r = self.renamed()
211 if r:
211 if r:
212 pl[0] = (r[0], r[1], None)
212 pl[0] = (r[0], r[1], None)
213
213
214 return [ filectx(self._repo, p, fileid=n, filelog=l)
214 return [ filectx(self._repo, p, fileid=n, filelog=l)
215 for p,n,l in pl if n != nullid ]
215 for p,n,l in pl if n != nullid ]
216
216
217 def children(self):
217 def children(self):
218 # hard for renames
218 # hard for renames
219 c = self._filelog.children(self._filenode)
219 c = self._filelog.children(self._filenode)
220 return [ filectx(self._repo, self._path, fileid=x,
220 return [ filectx(self._repo, self._path, fileid=x,
221 filelog=self._filelog) for x in c ]
221 filelog=self._filelog) for x in c ]
222
222
223 def annotate(self, follow=False):
223 def annotate(self, follow=False):
224 '''returns a list of tuples of (ctx, line) for each line
224 '''returns a list of tuples of (ctx, line) for each line
225 in the file, where ctx is the filectx of the node where
225 in the file, where ctx is the filectx of the node where
226 that line was last changed'''
226 that line was last changed'''
227
227
228 def decorate(text, rev):
228 def decorate(text, rev):
229 return ([rev] * len(text.splitlines()), text)
229 return ([rev] * len(text.splitlines()), text)
230
230
231 def pair(parent, child):
231 def pair(parent, child):
232 for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
232 for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
233 child[0][b1:b2] = parent[0][a1:a2]
233 child[0][b1:b2] = parent[0][a1:a2]
234 return child
234 return child
235
235
236 getlog = util.cachefunc(lambda x: self._repo.file(x))
236 getlog = util.cachefunc(lambda x: self._repo.file(x))
237 def getctx(path, fileid):
237 def getctx(path, fileid):
238 log = path == self._path and self._filelog or getlog(path)
238 log = path == self._path and self._filelog or getlog(path)
239 return filectx(self._repo, path, fileid=fileid, filelog=log)
239 return filectx(self._repo, path, fileid=fileid, filelog=log)
240 getctx = util.cachefunc(getctx)
240 getctx = util.cachefunc(getctx)
241
241
242 def parents(f):
242 def parents(f):
243 # we want to reuse filectx objects as much as possible
243 # we want to reuse filectx objects as much as possible
244 p = f._path
244 p = f._path
245 if f._filerev is None: # working dir
245 if f._filerev is None: # working dir
246 pl = [ (n.path(), n.filerev()) for n in f.parents() ]
246 pl = [ (n.path(), n.filerev()) for n in f.parents() ]
247 else:
247 else:
248 pl = [ (p, n) for n in f._filelog.parentrevs(f._filerev) ]
248 pl = [ (p, n) for n in f._filelog.parentrevs(f._filerev) ]
249
249
250 if follow:
250 if follow:
251 r = f.renamed()
251 r = f.renamed()
252 if r:
252 if r:
253 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
253 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
254
254
255 return [ getctx(p, n) for p, n in pl if n != -1 ]
255 return [ getctx(p, n) for p, n in pl if n != -1 ]
256
256
257 # use linkrev to find the first changeset where self appeared
258 if self.rev() != self._filelog.linkrev(self._filenode):
259 base = self.filectx(self.filerev())
260 else:
261 base = self
262
257 # find all ancestors
263 # find all ancestors
258 needed = {self: 1}
264 needed = {base: 1}
259 visit = [self]
265 visit = [base]
260 files = [self._path]
266 files = [base._path]
261 while visit:
267 while visit:
262 f = visit.pop(0)
268 f = visit.pop(0)
263 for p in parents(f):
269 for p in parents(f):
264 if p not in needed:
270 if p not in needed:
265 needed[p] = 1
271 needed[p] = 1
266 visit.append(p)
272 visit.append(p)
267 if p._path not in files:
273 if p._path not in files:
268 files.append(p._path)
274 files.append(p._path)
269 else:
275 else:
270 # count how many times we'll use this
276 # count how many times we'll use this
271 needed[p] += 1
277 needed[p] += 1
272
278
273 # sort by revision (per file) which is a topological order
279 # sort by revision (per file) which is a topological order
274 visit = []
280 visit = []
275 files.reverse()
281 files.reverse()
276 for f in files:
282 for f in files:
277 fn = [(n._filerev, n) for n in needed.keys() if n._path == f]
283 fn = [(n._filerev, n) for n in needed.keys() if n._path == f]
278 fn.sort()
284 fn.sort()
279 visit.extend(fn)
285 visit.extend(fn)
280 hist = {}
286 hist = {}
281
287
282 for r, f in visit:
288 for r, f in visit:
283 curr = decorate(f.data(), f)
289 curr = decorate(f.data(), f)
284 for p in parents(f):
290 for p in parents(f):
285 if p != nullid:
291 if p != nullid:
286 curr = pair(hist[p], curr)
292 curr = pair(hist[p], curr)
287 # trim the history of unneeded revs
293 # trim the history of unneeded revs
288 needed[p] -= 1
294 needed[p] -= 1
289 if not needed[p]:
295 if not needed[p]:
290 del hist[p]
296 del hist[p]
291 hist[f] = curr
297 hist[f] = curr
292
298
293 return zip(hist[f][0], hist[f][1].splitlines(1))
299 return zip(hist[f][0], hist[f][1].splitlines(1))
294
300
295 def ancestor(self, fc2):
301 def ancestor(self, fc2):
296 """
302 """
297 find the common ancestor file context, if any, of self, and fc2
303 find the common ancestor file context, if any, of self, and fc2
298 """
304 """
299
305
300 acache = {}
306 acache = {}
301
307
302 # prime the ancestor cache for the working directory
308 # prime the ancestor cache for the working directory
303 for c in (self, fc2):
309 for c in (self, fc2):
304 if c._filerev == None:
310 if c._filerev == None:
305 pl = [ (n.path(), n.filenode()) for n in c.parents() ]
311 pl = [ (n.path(), n.filenode()) for n in c.parents() ]
306 acache[(c._path, None)] = pl
312 acache[(c._path, None)] = pl
307
313
308 flcache = {self._path:self._filelog, fc2._path:fc2._filelog}
314 flcache = {self._path:self._filelog, fc2._path:fc2._filelog}
309 def parents(vertex):
315 def parents(vertex):
310 if vertex in acache:
316 if vertex in acache:
311 return acache[vertex]
317 return acache[vertex]
312 f, n = vertex
318 f, n = vertex
313 if f not in flcache:
319 if f not in flcache:
314 flcache[f] = self._repo.file(f)
320 flcache[f] = self._repo.file(f)
315 fl = flcache[f]
321 fl = flcache[f]
316 pl = [ (f,p) for p in fl.parents(n) if p != nullid ]
322 pl = [ (f,p) for p in fl.parents(n) if p != nullid ]
317 re = fl.renamed(n)
323 re = fl.renamed(n)
318 if re:
324 if re:
319 pl.append(re)
325 pl.append(re)
320 acache[vertex]=pl
326 acache[vertex]=pl
321 return pl
327 return pl
322
328
323 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
329 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
324 v = ancestor.ancestor(a, b, parents)
330 v = ancestor.ancestor(a, b, parents)
325 if v:
331 if v:
326 f,n = v
332 f,n = v
327 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
333 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
328
334
329 return None
335 return None
330
336
331 class workingctx(changectx):
337 class workingctx(changectx):
332 """A workingctx object makes access to data related to
338 """A workingctx object makes access to data related to
333 the current working directory convenient."""
339 the current working directory convenient."""
334 def __init__(self, repo):
340 def __init__(self, repo):
335 self._repo = repo
341 self._repo = repo
336 self._rev = None
342 self._rev = None
337 self._node = None
343 self._node = None
338
344
339 def __str__(self):
345 def __str__(self):
340 return str(self._parents[0]) + "+"
346 return str(self._parents[0]) + "+"
341
347
342 def __nonzero__(self):
348 def __nonzero__(self):
343 return True
349 return True
344
350
345 def __getattr__(self, name):
351 def __getattr__(self, name):
346 if name == '_parents':
352 if name == '_parents':
347 self._parents = self._repo.parents()
353 self._parents = self._repo.parents()
348 return self._parents
354 return self._parents
349 if name == '_status':
355 if name == '_status':
350 self._status = self._repo.status()
356 self._status = self._repo.status()
351 return self._status
357 return self._status
352 if name == '_manifest':
358 if name == '_manifest':
353 self._buildmanifest()
359 self._buildmanifest()
354 return self._manifest
360 return self._manifest
355 else:
361 else:
356 raise AttributeError, name
362 raise AttributeError, name
357
363
358 def _buildmanifest(self):
364 def _buildmanifest(self):
359 """generate a manifest corresponding to the working directory"""
365 """generate a manifest corresponding to the working directory"""
360
366
361 man = self._parents[0].manifest().copy()
367 man = self._parents[0].manifest().copy()
362 copied = self._repo.dirstate.copies()
368 copied = self._repo.dirstate.copies()
363 modified, added, removed, deleted, unknown = self._status[:5]
369 modified, added, removed, deleted, unknown = self._status[:5]
364 for i,l in (("a", added), ("m", modified), ("u", unknown)):
370 for i,l in (("a", added), ("m", modified), ("u", unknown)):
365 for f in l:
371 for f in l:
366 man[f] = man.get(copied.get(f, f), nullid) + i
372 man[f] = man.get(copied.get(f, f), nullid) + i
367 man.set(f, util.is_exec(self._repo.wjoin(f), man.execf(f)))
373 man.set(f, util.is_exec(self._repo.wjoin(f), man.execf(f)))
368
374
369 for f in deleted + removed:
375 for f in deleted + removed:
370 if f in man:
376 if f in man:
371 del man[f]
377 del man[f]
372
378
373 self._manifest = man
379 self._manifest = man
374
380
375 def manifest(self): return self._manifest
381 def manifest(self): return self._manifest
376
382
377 def user(self): return self._repo.ui.username()
383 def user(self): return self._repo.ui.username()
378 def date(self): return util.makedate()
384 def date(self): return util.makedate()
379 def description(self): return ""
385 def description(self): return ""
380 def files(self):
386 def files(self):
381 f = self.modified() + self.added() + self.removed()
387 f = self.modified() + self.added() + self.removed()
382 f.sort()
388 f.sort()
383 return f
389 return f
384
390
385 def modified(self): return self._status[0]
391 def modified(self): return self._status[0]
386 def added(self): return self._status[1]
392 def added(self): return self._status[1]
387 def removed(self): return self._status[2]
393 def removed(self): return self._status[2]
388 def deleted(self): return self._status[3]
394 def deleted(self): return self._status[3]
389 def unknown(self): return self._status[4]
395 def unknown(self): return self._status[4]
390 def clean(self): return self._status[5]
396 def clean(self): return self._status[5]
391
397
392 def parents(self):
398 def parents(self):
393 """return contexts for each parent changeset"""
399 """return contexts for each parent changeset"""
394 return self._parents
400 return self._parents
395
401
396 def children(self):
402 def children(self):
397 return []
403 return []
398
404
399 def filectx(self, path):
405 def filectx(self, path):
400 """get a file context from the working directory"""
406 """get a file context from the working directory"""
401 return workingfilectx(self._repo, path, workingctx=self)
407 return workingfilectx(self._repo, path, workingctx=self)
402
408
403 def ancestor(self, c2):
409 def ancestor(self, c2):
404 """return the ancestor context of self and c2"""
410 """return the ancestor context of self and c2"""
405 return self._parents[0].ancestor(c2) # punt on two parents for now
411 return self._parents[0].ancestor(c2) # punt on two parents for now
406
412
407 class workingfilectx(filectx):
413 class workingfilectx(filectx):
408 """A workingfilectx object makes access to data related to a particular
414 """A workingfilectx object makes access to data related to a particular
409 file in the working directory convenient."""
415 file in the working directory convenient."""
410 def __init__(self, repo, path, filelog=None, workingctx=None):
416 def __init__(self, repo, path, filelog=None, workingctx=None):
411 """changeid can be a changeset revision, node, or tag.
417 """changeid can be a changeset revision, node, or tag.
412 fileid can be a file revision or node."""
418 fileid can be a file revision or node."""
413 self._repo = repo
419 self._repo = repo
414 self._path = path
420 self._path = path
415 self._changeid = None
421 self._changeid = None
416 self._filerev = self._filenode = None
422 self._filerev = self._filenode = None
417
423
418 if filelog:
424 if filelog:
419 self._filelog = filelog
425 self._filelog = filelog
420 if workingctx:
426 if workingctx:
421 self._changectx = workingctx
427 self._changectx = workingctx
422
428
423 def __getattr__(self, name):
429 def __getattr__(self, name):
424 if name == '_changectx':
430 if name == '_changectx':
425 self._changectx = workingctx(repo)
431 self._changectx = workingctx(repo)
426 return self._changectx
432 return self._changectx
427 elif name == '_repopath':
433 elif name == '_repopath':
428 self._repopath = (self._repo.dirstate.copied(self._path)
434 self._repopath = (self._repo.dirstate.copied(self._path)
429 or self._path)
435 or self._path)
430 return self._repopath
436 return self._repopath
431 elif name == '_filelog':
437 elif name == '_filelog':
432 self._filelog = self._repo.file(self._repopath)
438 self._filelog = self._repo.file(self._repopath)
433 return self._filelog
439 return self._filelog
434 else:
440 else:
435 raise AttributeError, name
441 raise AttributeError, name
436
442
437 def __nonzero__(self):
443 def __nonzero__(self):
438 return True
444 return True
439
445
440 def __str__(self):
446 def __str__(self):
441 return "%s@%s" % (self.path(), self._changectx)
447 return "%s@%s" % (self.path(), self._changectx)
442
448
443 def filectx(self, fileid):
449 def filectx(self, fileid):
444 '''opens an arbitrary revision of the file without
450 '''opens an arbitrary revision of the file without
445 opening a new filelog'''
451 opening a new filelog'''
446 return filectx(self._repo, self._repopath, fileid=fileid,
452 return filectx(self._repo, self._repopath, fileid=fileid,
447 filelog=self._filelog)
453 filelog=self._filelog)
448
454
449 def rev(self):
455 def rev(self):
450 if '_changectx' in self.__dict__:
456 if '_changectx' in self.__dict__:
451 return self._changectx.rev()
457 return self._changectx.rev()
452 return self._filelog.linkrev(self._filenode)
458 return self._filelog.linkrev(self._filenode)
453
459
454 def data(self): return self._repo.wread(self._path)
460 def data(self): return self._repo.wread(self._path)
455 def renamed(self):
461 def renamed(self):
456 rp = self._repopath
462 rp = self._repopath
457 if rp == self._path:
463 if rp == self._path:
458 return None
464 return None
459 return rp, self._workingctx._parents._manifest.get(rp, nullid)
465 return rp, self._workingctx._parents._manifest.get(rp, nullid)
460
466
461 def parents(self):
467 def parents(self):
462 '''return parent filectxs, following copies if necessary'''
468 '''return parent filectxs, following copies if necessary'''
463 p = self._path
469 p = self._path
464 rp = self._repopath
470 rp = self._repopath
465 pcl = self._changectx._parents
471 pcl = self._changectx._parents
466 fl = self._filelog
472 fl = self._filelog
467 pl = [ (rp, pcl[0]._manifest.get(rp, nullid), fl) ]
473 pl = [ (rp, pcl[0]._manifest.get(rp, nullid), fl) ]
468 if len(pcl) > 1:
474 if len(pcl) > 1:
469 if rp != p:
475 if rp != p:
470 fl = None
476 fl = None
471 pl.append((p, pcl[1]._manifest.get(p, nullid), fl))
477 pl.append((p, pcl[1]._manifest.get(p, nullid), fl))
472
478
473 return [ filectx(self._repo, p, fileid=n, filelog=l)
479 return [ filectx(self._repo, p, fileid=n, filelog=l)
474 for p,n,l in pl if n != nullid ]
480 for p,n,l in pl if n != nullid ]
475
481
476 def children(self):
482 def children(self):
477 return []
483 return []
478
484
479 def size(self): return os.stat(self._repo.wjoin(self._path)).st_size
485 def size(self): return os.stat(self._repo.wjoin(self._path)).st_size
480
486
481 def cmp(self, text): return self._repo.wread(self._path) == text
487 def cmp(self, text): return self._repo.wread(self._path) == text
General Comments 0
You need to be logged in to leave comments. Login now