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