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