##// END OF EJS Templates
filectx: allow passing filelog in init to avoid opening new filelogs
Matt Mackall -
r3124:4d021b91 default
parent child Browse files
Show More
@@ -1,131 +1,140
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
9
10 class changectx(object):
10 class changectx(object):
11 """A changecontext object makes access to data related to a particular
11 """A changecontext object makes access to data related to a particular
12 changeset convenient."""
12 changeset convenient."""
13 def __init__(self, repo, changeid):
13 def __init__(self, repo, changeid):
14 """changeid is a revision number, node, or tag"""
14 """changeid is a revision number, node, or tag"""
15 self._repo = repo
15 self._repo = repo
16
16
17 self._node = self._repo.lookup(changeid)
17 self._node = self._repo.lookup(changeid)
18 self._rev = self._repo.changelog.rev(self._node)
18 self._rev = self._repo.changelog.rev(self._node)
19
19
20 def changeset(self):
20 def changeset(self):
21 try:
21 try:
22 return self._changeset
22 return self._changeset
23 except AttributeError:
23 except AttributeError:
24 self._changeset = self._repo.changelog.read(self.node())
24 self._changeset = self._repo.changelog.read(self.node())
25 return self._changeset
25 return self._changeset
26
26
27 def manifest(self):
27 def manifest(self):
28 try:
28 try:
29 return self._manifest
29 return self._manifest
30 except AttributeError:
30 except AttributeError:
31 self._manifest = self._repo.manifest.read(self.changeset()[0])
31 self._manifest = self._repo.manifest.read(self.changeset()[0])
32 return self._manifest
32 return self._manifest
33
33
34 def rev(self): return self._rev
34 def rev(self): return self._rev
35 def node(self): return self._node
35 def node(self): return self._node
36 def user(self): return self.changeset()[1]
36 def user(self): return self.changeset()[1]
37 def date(self): return self.changeset()[2]
37 def date(self): return self.changeset()[2]
38 def files(self): return self.changeset()[3]
38 def files(self): return self.changeset()[3]
39 def description(self): return self.changeset()[4]
39 def description(self): return self.changeset()[4]
40
40
41 def parents(self):
41 def parents(self):
42 """return contexts for each parent changeset"""
42 """return contexts for each parent changeset"""
43 p = self._repo.changelog.parents(self._node)
43 p = self._repo.changelog.parents(self._node)
44 return [ changectx(self._repo, x) for x in p ]
44 return [ changectx(self._repo, x) for x in p ]
45
45
46 def children(self):
46 def children(self):
47 """return contexts for each child changeset"""
47 """return contexts for each child changeset"""
48 c = self._repo.changelog.children(self._node)
48 c = self._repo.changelog.children(self._node)
49 return [ changectx(self._repo, x) for x in c ]
49 return [ changectx(self._repo, x) for x in c ]
50
50
51 def filenode(self, path):
51 def filenode(self, path):
52 node, flag = self._repo.manifest.find(self.changeset()[0], path)
52 node, flag = self._repo.manifest.find(self.changeset()[0], path)
53 return node
53 return node
54
54
55 def filectx(self, path, fileid=None):
55 def filectx(self, path, fileid=None):
56 """get a file context from this changeset"""
56 """get a file context from this changeset"""
57 if fileid is None:
57 if fileid is None:
58 fileid = self.filenode(path)
58 fileid = self.filenode(path)
59 return filectx(self._repo, path, fileid=fileid)
59 return filectx(self._repo, path, fileid=fileid)
60
60
61 def filectxs(self):
61 def filectxs(self):
62 """generate a file context for each file in this changeset's
62 """generate a file context for each file in this changeset's
63 manifest"""
63 manifest"""
64 mf = self.manifest()
64 mf = self.manifest()
65 m = mf.keys()
65 m = mf.keys()
66 m.sort()
66 m.sort()
67 for f in m:
67 for f in m:
68 yield self.filectx(f, fileid=mf[f])
68 yield self.filectx(f, fileid=mf[f])
69
69
70 class filectx(object):
70 class filectx(object):
71 """A filecontext object makes access to data related to a particular
71 """A filecontext object makes access to data related to a particular
72 filerevision convenient."""
72 filerevision convenient."""
73 def __init__(self, repo, path, changeid=None, fileid=None):
73 def __init__(self, repo, path, changeid=None, fileid=None, filelog=None):
74 """changeid can be a changeset revision, node, or tag.
74 """changeid can be a changeset revision, node, or tag.
75 fileid can be a file revision or node."""
75 fileid can be a file revision or node."""
76 self._repo = repo
76 self._repo = repo
77 self._path = path
77 self._path = path
78
78
79 assert changeid or fileid
79 assert changeid or fileid
80
80
81 if filelog:
82 self._filelog = filelog
83 else:
84 self._filelog = self._repo.file(self._path)
85
81 if not fileid:
86 if not fileid:
82 # if given a changeset id, go ahead and look up the file
87 # if given a changeset id, go ahead and look up the file
83 self._changeid = changeid
88 self._changeid = changeid
84 self._changectx = self.changectx()
89 self._changectx = self.changectx()
85 self._filelog = self._repo.file(self._path)
86 self._filenode = self._changectx.filenode(self._path)
90 self._filenode = self._changectx.filenode(self._path)
87 else:
91 else:
88 # else be lazy
92 # else delay changectx creation
89 self._filelog = self._repo.file(self._path)
90 self._filenode = self._filelog.lookup(fileid)
93 self._filenode = self._filelog.lookup(fileid)
91 self._changeid = self._filelog.linkrev(self._filenode)
94 self._changeid = self._filelog.linkrev(self._filenode)
92 self._filerev = self._filelog.rev(self._filenode)
95 self._filerev = self._filelog.rev(self._filenode)
93
96
94 def changectx(self):
97 def changectx(self):
95 try:
98 try:
96 return self._changectx
99 return self._changectx
97 except AttributeError:
100 except AttributeError:
98 self._changectx = changectx(self._repo, self._changeid)
101 self._changectx = changectx(self._repo, self._changeid)
99 return self._changectx
102 return self._changectx
100
103
101 def filerev(self): return self._filerev
104 def filerev(self): return self._filerev
102 def filenode(self): return self._filenode
105 def filenode(self): return self._filenode
103 def filelog(self): return self._filelog
106 def filelog(self): return self._filelog
104
107
105 def rev(self): return self.changectx().rev()
108 def rev(self): return self.changectx().rev()
106 def node(self): return self.changectx().node()
109 def node(self): return self.changectx().node()
107 def user(self): return self.changectx().user()
110 def user(self): return self.changectx().user()
108 def date(self): return self.changectx().date()
111 def date(self): return self.changectx().date()
109 def files(self): return self.changectx().files()
112 def files(self): return self.changectx().files()
110 def description(self): return self.changectx().description()
113 def description(self): return self.changectx().description()
111 def manifest(self): return self.changectx().manifest()
114 def manifest(self): return self.changectx().manifest()
112
115
113 def data(self): return self._filelog.read(self._filenode)
116 def data(self): return self._filelog.read(self._filenode)
114 def renamed(self): return self._filelog.renamed(self._filenode)
117 def renamed(self): return self._filelog.renamed(self._filenode)
115 def path(self): return self._path
118 def path(self): return self._path
116
119
117 def parents(self):
120 def parents(self):
118 p = [ (self._path, n) for n in self._filelog.parents(self._filenode) ]
121 p = self._path
119 r = self.renamed()
122 fl = self._filelog
123 pl = [ (p, n, fl) for n in self._filelog.parents(self._filenode) ]
120
124
125 r = self.renamed()
121 if r:
126 if r:
122 p[0] = r
127 pl[0] = (r[0], r[1], None)
123 return [ filectx(self._repo, p, fileid=n) for p,n in p if n != nullid ]
128
129 return [ filectx(self._repo, p, fileid=n, filelog=l)
130 for p,n,l in pl if n != nullid ]
124
131
125 def children(self):
132 def children(self):
126 # hard for renames
133 # hard for renames
127 c = self._filelog.children(self._filenode)
134 c = self._filelog.children(self._filenode)
128 return [ filectx(self._repo, self._path, fileid=x) for x in c ]
135 return [ filectx(self._repo, self._path, fileid=x,
136 filelog=self._filelog) for x in c ]
129
137
130 def annotate(self):
138 def annotate(self):
131 return self._filelog.annotate(self._filenode)
139 return self._filelog.annotate(self._filenode)
140
General Comments 0
You need to be logged in to leave comments. Login now