author | Matt Mackall <mpm@selenic.com> |
Mon, 14 Aug 2006 15:07:00 -0500 | |
changeset 2887 | 05257fd28591 |
parent 2859 | 345bac2bc4ec |
child 2890 | 5df3e5cf16bc |
permissions | -rw-r--r-- |
1089 | 1 |
# filelog.py - file history class for mercurial |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
2 |
# |
2859 | 3 |
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
4 |
# |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
7 |
|
262 | 8 |
from revlog import * |
9 |
from demandload import * |
|
2470
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2222
diff
changeset
|
10 |
demandload(globals(), "bdiff os") |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
11 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
12 |
class filelog(revlog): |
2222
c9e264b115e6
Use revlogng and inlined data files by default
mason@suse.com
parents:
2072
diff
changeset
|
13 |
def __init__(self, opener, path, defversion=REVLOG_DEFAULT_VERSION): |
144
ea9188538222
Fix transaction handling bug by reverting fileopener change
mpm@selenic.com
parents:
140
diff
changeset
|
14 |
revlog.__init__(self, opener, |
786
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
15 |
os.path.join("data", self.encodedir(path + ".i")), |
2072 | 16 |
os.path.join("data", self.encodedir(path + ".d")), |
17 |
defversion) |
|
786
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
18 |
|
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
19 |
# This avoids a collision between a file named foo and a dir named |
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
20 |
# foo.i or foo.d |
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
21 |
def encodedir(self, path): |
856
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
22 |
return (path |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
23 |
.replace(".hg/", ".hg.hg/") |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
24 |
.replace(".i/", ".i.hg/") |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
25 |
.replace(".d/", ".d.hg/")) |
786
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
26 |
|
902b12d55751
Fix the directory and revlog collision problem
mpm@selenic.com
parents:
785
diff
changeset
|
27 |
def decodedir(self, path): |
856
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
28 |
return (path |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
29 |
.replace(".d.hg/", ".d/") |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
30 |
.replace(".i.hg/", ".i/") |
fbe964ae7325
Fixed encoding of directories ending in .d or .i:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
839
diff
changeset
|
31 |
.replace(".hg.hg/", ".hg/")) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
32 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
33 |
def read(self, node): |
360 | 34 |
t = self.revision(node) |
686
d7d68d27ebe5
Reapply startswith() changes that got lost with stale edit
Matt Mackall <mpm@selenic.com>
parents:
681
diff
changeset
|
35 |
if not t.startswith('\1\n'): |
360 | 36 |
return t |
2579
0875cda033fd
use __contains__, index or split instead of str.find
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
37 |
s = t.index('\1\n', 2) |
360 | 38 |
return t[s+2:] |
39 |
||
40 |
def readmeta(self, node): |
|
41 |
t = self.revision(node) |
|
686
d7d68d27ebe5
Reapply startswith() changes that got lost with stale edit
Matt Mackall <mpm@selenic.com>
parents:
681
diff
changeset
|
42 |
if not t.startswith('\1\n'): |
1116 | 43 |
return {} |
2579
0875cda033fd
use __contains__, index or split instead of str.find
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
44 |
s = t.index('\1\n', 2) |
360 | 45 |
mt = t[2:s] |
1116 | 46 |
m = {} |
360 | 47 |
for l in mt.splitlines(): |
48 |
k, v = l.split(": ", 1) |
|
49 |
m[k] = v |
|
50 |
return m |
|
51 |
||
52 |
def add(self, text, meta, transaction, link, p1=None, p2=None): |
|
686
d7d68d27ebe5
Reapply startswith() changes that got lost with stale edit
Matt Mackall <mpm@selenic.com>
parents:
681
diff
changeset
|
53 |
if meta or text.startswith('\1\n'): |
360 | 54 |
mt = "" |
55 |
if meta: |
|
56 |
mt = [ "%s: %s\n" % (k, v) for k,v in meta.items() ] |
|
1540
8ca9f5b17257
minor optimization: save some string trash
twaldmann@thinkmo.de
parents:
1117
diff
changeset
|
57 |
text = "\1\n%s\1\n%s" % ("".join(mt), text) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
58 |
return self.addrevision(text, transaction, link, p1, p2) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
59 |
|
1116 | 60 |
def renamed(self, node): |
1595
dca956c9767d
Re-enable the renamed check fastpath
Matt Mackall <mpm@selenic.com>
parents:
1541
diff
changeset
|
61 |
if self.parents(node)[0] != nullid: |
1116 | 62 |
return False |
63 |
m = self.readmeta(node) |
|
64 |
if m and m.has_key("copy"): |
|
65 |
return (m["copy"], bin(m["copyrev"])) |
|
66 |
return False |
|
67 |
||
2887
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
68 |
def cmp(self, node, text): |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
69 |
"""compare text with a given file revision""" |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
70 |
|
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
71 |
# for renames, we have to go the slow way |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
72 |
if self.renamed(node): |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
73 |
t2 = self.read(node) |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
74 |
return t2 == text |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
75 |
|
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
76 |
p1, p2 = self.parents(node) |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
77 |
h = hash(text, p1, p2) |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
78 |
|
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
79 |
return h != node |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
80 |
|
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
81 |
def makenode(self, node, text): |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
82 |
"""calculate a file nodeid for text, descended or possibly |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
83 |
unchanged from node""" |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
84 |
|
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
85 |
if self.cmp(node, text): |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
86 |
return hash(text, node, nullid) |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
87 |
return node |
05257fd28591
filelog: add hash-based comparisons
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
88 |
|
79 | 89 |
def annotate(self, node): |
199 | 90 |
|
91 |
def decorate(text, rev): |
|
436 | 92 |
return ([rev] * len(text.splitlines()), text) |
199 | 93 |
|
94 |
def pair(parent, child): |
|
436 | 95 |
for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]): |
471 | 96 |
child[0][b1:b2] = parent[0][a1:a2] |
97 |
return child |
|
199 | 98 |
|
200 | 99 |
# find all ancestors |
216
201115f2859b
hg annotate: actually annotate the given version
mpm@selenic.com
parents:
210
diff
changeset
|
100 |
needed = {node:1} |
199 | 101 |
visit = [node] |
102 |
while visit: |
|
103 |
n = visit.pop(0) |
|
104 |
for p in self.parents(n): |
|
105 |
if p not in needed: |
|
106 |
needed[p] = 1 |
|
107 |
visit.append(p) |
|
200 | 108 |
else: |
109 |
# count how many times we'll use this |
|
110 |
needed[p] += 1 |
|
199 | 111 |
|
200 | 112 |
# sort by revision which is a topological order |
471 | 113 |
visit = [ (self.rev(n), n) for n in needed.keys() ] |
199 | 114 |
visit.sort() |
115 |
hist = {} |
|
116 |
||
471 | 117 |
for r,n in visit: |
199 | 118 |
curr = decorate(self.read(n), self.linkrev(n)) |
119 |
for p in self.parents(n): |
|
120 |
if p != nullid: |
|
121 |
curr = pair(hist[p], curr) |
|
200 | 122 |
# trim the history of unneeded revs |
123 |
needed[p] -= 1 |
|
124 |
if not needed[p]: |
|
125 |
del hist[p] |
|
199 | 126 |
hist[n] = curr |
127 |
||
436 | 128 |
return zip(hist[n][0], hist[n][1].splitlines(1)) |