Show More
@@ -1,131 +1,131 b'' | |||
|
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 | 73 | def __init__(self, repo, path, changeid=None, fileid=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 | 81 | if not fileid: |
|
82 | 82 | # if given a changeset id, go ahead and look up the file |
|
83 | 83 | self._changeid = changeid |
|
84 | 84 | self._changectx = self.changectx() |
|
85 | 85 | self._filelog = self._repo.file(self._path) |
|
86 | 86 | self._filenode = self._changectx.filenode(self._path) |
|
87 | 87 | else: |
|
88 | 88 | # else be lazy |
|
89 | 89 | self._filelog = self._repo.file(self._path) |
|
90 | 90 | self._filenode = self._filelog.lookup(fileid) |
|
91 | 91 | self._changeid = self._filelog.linkrev(self._filenode) |
|
92 | 92 | self._filerev = self._filelog.rev(self._filenode) |
|
93 | 93 | |
|
94 | 94 | def changectx(self): |
|
95 | 95 | try: |
|
96 | 96 | return self._changectx |
|
97 | 97 | except AttributeError: |
|
98 | 98 | self._changectx = changectx(self._repo, self._changeid) |
|
99 | 99 | return self._changectx |
|
100 | 100 | |
|
101 | 101 | def filerev(self): return self._filerev |
|
102 | 102 | def filenode(self): return self._filenode |
|
103 | 103 | def filelog(self): return self._filelog |
|
104 | 104 | |
|
105 | 105 | def rev(self): return self.changectx().rev() |
|
106 | 106 | def node(self): return self.changectx().node() |
|
107 | 107 | def user(self): return self.changectx().user() |
|
108 | 108 | def date(self): return self.changectx().date() |
|
109 | 109 | def files(self): return self.changectx().files() |
|
110 | 110 | def description(self): return self.changectx().description() |
|
111 | 111 | def manifest(self): return self.changectx().manifest() |
|
112 | 112 | |
|
113 | 113 | def data(self): return self._filelog.read(self._filenode) |
|
114 | def metadata(self): return self._filelog.readmeta(self._filenode) | |
|
115 | 114 | def renamed(self): return self._filelog.renamed(self._filenode) |
|
116 | 115 | def path(self): return self._path |
|
117 | 116 | |
|
118 | 117 | def parents(self): |
|
119 | 118 | p = [ (self._path, n) for n in self._filelog.parents(self._filenode) ] |
|
120 | 119 | r = self.renamed() |
|
120 | ||
|
121 | 121 | if r: |
|
122 | 122 | p[0] = r |
|
123 | 123 | return [ filectx(self._repo, p, fileid=n) for p,n in p if n != nullid ] |
|
124 | 124 | |
|
125 | 125 | def children(self): |
|
126 | 126 | # hard for renames |
|
127 | 127 | c = self._filelog.children(self._filenode) |
|
128 | 128 | return [ filectx(self._repo, self._path, fileid=x) for x in c ] |
|
129 | 129 | |
|
130 | 130 | def annotate(self): |
|
131 | 131 | return self._filelog.annotate(self._filenode) |
@@ -1,155 +1,155 b'' | |||
|
1 | 1 | # filelog.py - file history class for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005, 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 revlog import * |
|
9 | 9 | from demandload import * |
|
10 | 10 | demandload(globals(), "bdiff os") |
|
11 | 11 | |
|
12 | 12 | class filelog(revlog): |
|
13 | 13 | def __init__(self, opener, path, defversion=REVLOG_DEFAULT_VERSION): |
|
14 | 14 | revlog.__init__(self, opener, |
|
15 | 15 | os.path.join("data", self.encodedir(path + ".i")), |
|
16 | 16 | os.path.join("data", self.encodedir(path + ".d")), |
|
17 | 17 | defversion) |
|
18 | 18 | |
|
19 | 19 | # This avoids a collision between a file named foo and a dir named |
|
20 | 20 | # foo.i or foo.d |
|
21 | 21 | def encodedir(self, path): |
|
22 | 22 | return (path |
|
23 | 23 | .replace(".hg/", ".hg.hg/") |
|
24 | 24 | .replace(".i/", ".i.hg/") |
|
25 | 25 | .replace(".d/", ".d.hg/")) |
|
26 | 26 | |
|
27 | 27 | def decodedir(self, path): |
|
28 | 28 | return (path |
|
29 | 29 | .replace(".d.hg/", ".d/") |
|
30 | 30 | .replace(".i.hg/", ".i/") |
|
31 | 31 | .replace(".hg.hg/", ".hg/")) |
|
32 | 32 | |
|
33 | 33 | def read(self, node): |
|
34 | 34 | t = self.revision(node) |
|
35 | 35 | if not t.startswith('\1\n'): |
|
36 | 36 | return t |
|
37 | 37 | s = t.index('\1\n', 2) |
|
38 | 38 | return t[s+2:] |
|
39 | 39 | |
|
40 | def readmeta(self, node): | |
|
40 | def _readmeta(self, node): | |
|
41 | 41 | t = self.revision(node) |
|
42 | 42 | if not t.startswith('\1\n'): |
|
43 | 43 | return {} |
|
44 | 44 | s = t.index('\1\n', 2) |
|
45 | 45 | mt = t[2:s] |
|
46 | 46 | m = {} |
|
47 | 47 | for l in mt.splitlines(): |
|
48 | 48 | k, v = l.split(": ", 1) |
|
49 | 49 | m[k] = v |
|
50 | 50 | return m |
|
51 | 51 | |
|
52 | 52 | def add(self, text, meta, transaction, link, p1=None, p2=None): |
|
53 | 53 | if meta or text.startswith('\1\n'): |
|
54 | 54 | mt = "" |
|
55 | 55 | if meta: |
|
56 | 56 | mt = [ "%s: %s\n" % (k, v) for k,v in meta.items() ] |
|
57 | 57 | text = "\1\n%s\1\n%s" % ("".join(mt), text) |
|
58 | 58 | return self.addrevision(text, transaction, link, p1, p2) |
|
59 | 59 | |
|
60 | 60 | def renamed(self, node): |
|
61 | 61 | if self.parents(node)[0] != nullid: |
|
62 | 62 | return False |
|
63 | m = self.readmeta(node) | |
|
63 | m = self._readmeta(node) | |
|
64 | 64 | if m and m.has_key("copy"): |
|
65 | 65 | return (m["copy"], bin(m["copyrev"])) |
|
66 | 66 | return False |
|
67 | 67 | |
|
68 | 68 | def size(self, rev): |
|
69 | 69 | """return the size of a given revision""" |
|
70 | 70 | |
|
71 | 71 | # for revisions with renames, we have to go the slow way |
|
72 | 72 | node = self.node(rev) |
|
73 | 73 | if self.renamed(node): |
|
74 | 74 | return len(self.read(node)) |
|
75 | 75 | |
|
76 | 76 | return revlog.size(self, rev) |
|
77 | 77 | |
|
78 | 78 | def cmp(self, node, text): |
|
79 | 79 | """compare text with a given file revision""" |
|
80 | 80 | |
|
81 | 81 | # for renames, we have to go the slow way |
|
82 | 82 | if self.renamed(node): |
|
83 | 83 | t2 = self.read(node) |
|
84 | 84 | return t2 != text |
|
85 | 85 | |
|
86 | 86 | return revlog.cmp(self, node, text) |
|
87 | 87 | |
|
88 | 88 | def annotate(self, node): |
|
89 | 89 | |
|
90 | 90 | def decorate(text, rev): |
|
91 | 91 | return ([rev] * len(text.splitlines()), text) |
|
92 | 92 | |
|
93 | 93 | def pair(parent, child): |
|
94 | 94 | for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]): |
|
95 | 95 | child[0][b1:b2] = parent[0][a1:a2] |
|
96 | 96 | return child |
|
97 | 97 | |
|
98 | 98 | # find all ancestors |
|
99 | 99 | needed = {(self, node):1} |
|
100 | 100 | files = [self] |
|
101 | 101 | visit = [(self, node)] |
|
102 | 102 | while visit: |
|
103 | 103 | f, n = visit.pop(0) |
|
104 | 104 | rn = f.renamed(n) |
|
105 | 105 | if rn: |
|
106 | 106 | f, n = rn |
|
107 | 107 | f = filelog(self.opener, f, self.defversion) |
|
108 | 108 | files.insert(0, f) |
|
109 | 109 | if (f, n) not in needed: |
|
110 | 110 | needed[(f, n)] = 1 |
|
111 | 111 | else: |
|
112 | 112 | needed[(f, n)] += 1 |
|
113 | 113 | for p in f.parents(n): |
|
114 | 114 | if p == nullid: |
|
115 | 115 | continue |
|
116 | 116 | if (f, p) not in needed: |
|
117 | 117 | needed[(f, p)] = 1 |
|
118 | 118 | visit.append((f, p)) |
|
119 | 119 | else: |
|
120 | 120 | # count how many times we'll use this |
|
121 | 121 | needed[(f, p)] += 1 |
|
122 | 122 | |
|
123 | 123 | # sort by revision (per file) which is a topological order |
|
124 | 124 | visit = [] |
|
125 | 125 | for f in files: |
|
126 | 126 | fn = [(f.rev(n[1]), f, n[1]) for n in needed.keys() if n[0] == f] |
|
127 | 127 | fn.sort() |
|
128 | 128 | visit.extend(fn) |
|
129 | 129 | hist = {} |
|
130 | 130 | |
|
131 | 131 | for i in range(len(visit)): |
|
132 | 132 | r, f, n = visit[i] |
|
133 | 133 | curr = decorate(f.read(n), f.linkrev(n)) |
|
134 | 134 | if r == -1: |
|
135 | 135 | continue |
|
136 | 136 | parents = f.parents(n) |
|
137 | 137 | # follow parents across renames |
|
138 | 138 | if r < 1 and i > 0: |
|
139 | 139 | j = i |
|
140 | 140 | while j > 0 and visit[j][1] == f: |
|
141 | 141 | j -= 1 |
|
142 | 142 | parents = (visit[j][2],) |
|
143 | 143 | f = visit[j][1] |
|
144 | 144 | else: |
|
145 | 145 | parents = f.parents(n) |
|
146 | 146 | for p in parents: |
|
147 | 147 | if p != nullid: |
|
148 | 148 | curr = pair(hist[p], curr) |
|
149 | 149 | # trim the history of unneeded revs |
|
150 | 150 | needed[(f, p)] -= 1 |
|
151 | 151 | if not needed[(f, p)]: |
|
152 | 152 | del hist[p] |
|
153 | 153 | hist[n] = curr |
|
154 | 154 | |
|
155 | 155 | return zip(hist[n][0], hist[n][1].splitlines(1)) |
General Comments 0
You need to be logged in to leave comments.
Login now