##// END OF EJS Templates
filectx: lazy linkrev usage
Matt Mackall -
r3150:a5e4c817 default
parent child Browse files
Show More
@@ -1,193 +1,197
1 1 # context.py - changeset and file context objects for mercurial
2 2 #
3 3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from node import *
9 9 from demandload import demandload
10 10 demandload(globals(), "ancestor util")
11 11
12 12 class changectx(object):
13 13 """A changecontext object makes access to data related to a particular
14 14 changeset convenient."""
15 15 def __init__(self, repo, changeid=None):
16 16 """changeid is a revision number, node, or tag"""
17 17 self._repo = repo
18 18
19 19 if not changeid and changeid != 0:
20 20 p1, p2 = self._repo.dirstate.parents()
21 21 self._rev = self._repo.changelog.rev(p1)
22 22 if self._rev == -1:
23 23 changeid = 'tip'
24 24 else:
25 25 self._node = p1
26 26 return
27 27
28 28 self._node = self._repo.lookup(changeid)
29 29 self._rev = self._repo.changelog.rev(self._node)
30 30
31 31 def changeset(self):
32 32 try:
33 33 return self._changeset
34 34 except AttributeError:
35 35 self._changeset = self._repo.changelog.read(self.node())
36 36 return self._changeset
37 37
38 38 def manifest(self):
39 39 try:
40 40 return self._manifest
41 41 except AttributeError:
42 42 self._manifest = self._repo.manifest.read(self.changeset()[0])
43 43 return self._manifest
44 44
45 45 def rev(self): return self._rev
46 46 def node(self): return self._node
47 47 def user(self): return self.changeset()[1]
48 48 def date(self): return self.changeset()[2]
49 49 def files(self): return self.changeset()[3]
50 50 def description(self): return self.changeset()[4]
51 51
52 52 def parents(self):
53 53 """return contexts for each parent changeset"""
54 54 p = self._repo.changelog.parents(self._node)
55 55 return [ changectx(self._repo, x) for x in p ]
56 56
57 57 def children(self):
58 58 """return contexts for each child changeset"""
59 59 c = self._repo.changelog.children(self._node)
60 60 return [ changectx(self._repo, x) for x in c ]
61 61
62 62 def filenode(self, path):
63 63 node, flag = self._repo.manifest.find(self.changeset()[0], path)
64 64 return node
65 65
66 66 def filectx(self, path, fileid=None):
67 67 """get a file context from this changeset"""
68 68 if fileid is None:
69 69 fileid = self.filenode(path)
70 70 return filectx(self._repo, path, fileid=fileid)
71 71
72 72 def filectxs(self):
73 73 """generate a file context for each file in this changeset's
74 74 manifest"""
75 75 mf = self.manifest()
76 76 m = mf.keys()
77 77 m.sort()
78 78 for f in m:
79 79 yield self.filectx(f, fileid=mf[f])
80 80
81 81 def ancestor(self, c2):
82 82 """
83 83 return the ancestor context of self and c2
84 84 """
85 85 n = self._repo.changelog.ancestor(self._node, c2._node)
86 86 return changectx(self._repo, n)
87 87
88 88 class filectx(object):
89 89 """A filecontext object makes access to data related to a particular
90 90 filerevision convenient."""
91 91 def __init__(self, repo, path, changeid=None, fileid=None, filelog=None):
92 92 """changeid can be a changeset revision, node, or tag.
93 93 fileid can be a file revision or node."""
94 94 self._repo = repo
95 95 self._path = path
96 96
97 97 assert changeid is not None or fileid is not None
98 98
99 99 if filelog:
100 100 self._filelog = filelog
101 101 else:
102 102 self._filelog = self._repo.file(self._path)
103 103
104 104 if fileid is None:
105 105 self._changeid = changeid
106 106 else:
107 107 self._filenode = self._filelog.lookup(fileid)
108 108 self._changeid = self._filelog.linkrev(self._filenode)
109 109
110 110 def __getattr__(self, name):
111 111 if name == '_changectx':
112 112 self._changectx = changectx(self._repo, self._changeid)
113 113 return self._changectx
114 114 elif name == '_filenode':
115 115 self._filenode = self._changectx.filenode(self._path)
116 116 return self._filenode
117 117 elif name == '_filerev':
118 118 self._filerev = self._filelog.rev(self._filenode)
119 119 return self._filerev
120 120 else:
121 121 raise AttributeError, name
122 122
123 123 def filerev(self): return self._filerev
124 124 def filenode(self): return self._filenode
125 125 def filelog(self): return self._filelog
126 126
127 def rev(self): return self._changectx.rev()
127 def rev(self):
128 if hasattr(self, "_changectx"):
129 return self._changectx.rev()
130 return self._filelog.linkrev(self._filenode)
131
128 132 def node(self): return self._changectx.node()
129 133 def user(self): return self._changectx.user()
130 134 def date(self): return self._changectx.date()
131 135 def files(self): return self._changectx.files()
132 136 def description(self): return self._changectx.description()
133 137 def manifest(self): return self._changectx.manifest()
134 138 def changectx(self): return self._changectx
135 139
136 140 def data(self): return self._filelog.read(self._filenode)
137 141 def renamed(self): return self._filelog.renamed(self._filenode)
138 142 def path(self): return self._path
139 143
140 144 def parents(self):
141 145 p = self._path
142 146 fl = self._filelog
143 147 pl = [ (p, n, fl) for n in self._filelog.parents(self._filenode) ]
144 148
145 149 r = self.renamed()
146 150 if r:
147 151 pl[0] = (r[0], r[1], None)
148 152
149 153 return [ filectx(self._repo, p, fileid=n, filelog=l)
150 154 for p,n,l in pl if n != nullid ]
151 155
152 156 def children(self):
153 157 # hard for renames
154 158 c = self._filelog.children(self._filenode)
155 159 return [ filectx(self._repo, self._path, fileid=x,
156 160 filelog=self._filelog) for x in c ]
157 161
158 162 def annotate(self):
159 163 getctx = util.cachefunc(lambda x: filectx(self._repo, self._path,
160 164 changeid=x,
161 165 filelog=self._filelog))
162 166 hist = self._filelog.annotate(self._filenode)
163 167
164 168 return [(getctx(rev), line) for rev, line in hist]
165 169
166 170 def ancestor(self, fc2):
167 171 """
168 172 find the common ancestor file context, if any, of self, and fc2
169 173 """
170 174
171 175 acache = {}
172 176 flcache = {self._path:self._filelog, fc2._path:fc2._filelog}
173 177 def parents(vertex):
174 178 if vertex in acache:
175 179 return acache[vertex]
176 180 f, n = vertex
177 181 if f not in flcache:
178 182 flcache[f] = self._repo.file(f)
179 183 fl = flcache[f]
180 184 pl = [ (f,p) for p in fl.parents(n) if p != nullid ]
181 185 re = fl.renamed(n)
182 186 if re:
183 187 pl.append(re)
184 188 acache[vertex]=pl
185 189 return pl
186 190
187 191 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
188 192 v = ancestor.ancestor(a, b, parents)
189 193 if v:
190 194 f,n = v
191 195 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
192 196
193 197 return None
General Comments 0
You need to be logged in to leave comments. Login now