Show More
@@ -1,131 +1,132 b'' | |||
|
1 | 1 | # repair.py - functions for repository repair for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005, 2006 Chris Mason <mason@suse.com> |
|
4 | 4 | # Copyright 2007 Matt Mackall |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms |
|
7 | 7 | # of the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | |
|
9 | 9 | import changegroup, os |
|
10 | 10 | from node import * |
|
11 | 11 | |
|
12 | 12 | def strip(ui, repo, node, backup="all"): |
|
13 | 13 | def limitheads(cl, stop): |
|
14 | 14 | """return the list of all nodes that have no children""" |
|
15 | 15 | p = {} |
|
16 | 16 | h = [] |
|
17 | 17 | stoprev = 0 |
|
18 | 18 | if stop in cl.nodemap: |
|
19 | 19 | stoprev = cl.rev(stop) |
|
20 | 20 | |
|
21 | 21 | for r in xrange(cl.count() - 1, -1, -1): |
|
22 | 22 | n = cl.node(r) |
|
23 | 23 | if n not in p: |
|
24 | 24 | h.append(n) |
|
25 | 25 | if n == stop: |
|
26 | 26 | break |
|
27 | 27 | if r < stoprev: |
|
28 | 28 | break |
|
29 | 29 | for pn in cl.parents(n): |
|
30 | 30 | p[pn] = 1 |
|
31 | 31 | return h |
|
32 | 32 | |
|
33 | 33 | def bundle(repo, bases, heads, node, suffix): |
|
34 | 34 | cg = repo.changegroupsubset(bases, heads, 'strip') |
|
35 | 35 | backupdir = repo.join("strip-backup") |
|
36 | 36 | if not os.path.isdir(backupdir): |
|
37 | 37 | os.mkdir(backupdir) |
|
38 | 38 | name = os.path.join(backupdir, "%s-%s" % (short(node), suffix)) |
|
39 | 39 | ui.warn("saving bundle to %s\n" % name) |
|
40 | 40 | return changegroup.writebundle(cg, name, "HG10BZ") |
|
41 | 41 | |
|
42 |
def |
|
|
43 | mm = repo.changectx(node).manifest() | |
|
44 | seen = {} | |
|
42 | def collectfilenodes(repo, striprev): | |
|
43 | """find out the first node that should be stripped from each filelog""" | |
|
44 | mm = repo.changectx(striprev).manifest() | |
|
45 | filenodes = {} | |
|
45 | 46 | |
|
46 | 47 | for x in xrange(striprev, repo.changelog.count()): |
|
47 |
for |
|
|
48 |
if |
|
|
48 | for name in repo.changectx(x).files(): | |
|
49 | if name in filenodes: | |
|
49 | 50 | continue |
|
50 |
|
|
|
51 | if f in mm: | |
|
52 | filerev = mm[f] | |
|
53 | else: | |
|
54 | filerev = 0 | |
|
55 | seen[f] = filerev | |
|
51 | filenodes[name] = mm.get(name) | |
|
52 | ||
|
53 | return filenodes | |
|
54 | ||
|
55 | def stripall(repo, striprev, filenodes): | |
|
56 | """strip the requested nodes from the filelogs""" | |
|
56 | 57 | # we go in two steps here so the strip loop happens in a |
|
57 | 58 | # sensible order. When stripping many files, this helps keep |
|
58 | 59 | # our disk access patterns under control. |
|
59 | seen_list = seen.keys() | |
|
60 | seen_list.sort() | |
|
61 | for f in seen_list: | |
|
62 | ff = repo.file(f) | |
|
63 | filerev = seen[f] | |
|
64 |
|
|
|
65 | if filerev in ff.nodemap: | |
|
66 | filerev = ff.rev(filerev) | |
|
67 |
|
|
|
68 | filerev = 0 | |
|
69 | ff.strip(filerev, striprev) | |
|
60 | ||
|
61 | files = filenodes.keys() | |
|
62 | files.sort() | |
|
63 | for name in files: | |
|
64 | f = repo.file(name) | |
|
65 | fnode = filenodes[name] | |
|
66 | frev = 0 | |
|
67 | if fnode is not None and fnode in f.nodemap: | |
|
68 | frev = f.rev(fnode) | |
|
69 | f.strip(frev, striprev) | |
|
70 | 70 | |
|
71 | 71 | cl = repo.changelog |
|
72 | 72 | # TODO delete the undo files, and handle undo of merge sets |
|
73 | 73 | pp = cl.parents(node) |
|
74 | 74 | striprev = cl.rev(node) |
|
75 | 75 | |
|
76 | 76 | # save is a list of all the branches we are truncating away |
|
77 | 77 | # that we actually want to keep. changegroup will be used |
|
78 | 78 | # to preserve them and add them back after the truncate |
|
79 | 79 | saveheads = [] |
|
80 | 80 | savebases = {} |
|
81 | 81 | |
|
82 | 82 | heads = limitheads(cl, node) |
|
83 | 83 | seen = {} |
|
84 | 84 | |
|
85 | 85 | # search through all the heads, finding those where the revision |
|
86 | 86 | # we want to strip away is an ancestor. Also look for merges |
|
87 | 87 | # that might be turned into new heads by the strip. |
|
88 | 88 | while heads: |
|
89 | 89 | h = heads.pop() |
|
90 | 90 | n = h |
|
91 | 91 | while True: |
|
92 | 92 | seen[n] = 1 |
|
93 | 93 | pp = cl.parents(n) |
|
94 | 94 | if pp[1] != nullid: |
|
95 | 95 | for p in pp: |
|
96 | 96 | if cl.rev(p) > striprev and p not in seen: |
|
97 | 97 | heads.append(p) |
|
98 | 98 | if pp[0] == nullid: |
|
99 | 99 | break |
|
100 | 100 | if cl.rev(pp[0]) < striprev: |
|
101 | 101 | break |
|
102 | 102 | n = pp[0] |
|
103 | 103 | if n == node: |
|
104 | 104 | break |
|
105 | 105 | r = cl.reachable(h, node) |
|
106 | 106 | if node not in r: |
|
107 | 107 | saveheads.append(h) |
|
108 | 108 | for x in r: |
|
109 | 109 | if cl.rev(x) > striprev: |
|
110 | 110 | savebases[x] = 1 |
|
111 | 111 | |
|
112 | 112 | # create a changegroup for all the branches we need to keep |
|
113 | 113 | if backup == "all": |
|
114 | 114 | bundle(repo, [node], cl.heads(), node, 'backup') |
|
115 | 115 | if saveheads: |
|
116 | 116 | chgrpfile = bundle(repo, savebases.keys(), saveheads, node, 'temp') |
|
117 | 117 | |
|
118 | stripall(striprev) | |
|
118 | filenodes = collectfilenodes(repo, striprev) | |
|
119 | stripall(repo, striprev, filenodes) | |
|
119 | 120 | |
|
120 | 121 | change = cl.read(node) |
|
121 | 122 | cl.strip(striprev, striprev) |
|
122 | 123 | repo.manifest.strip(repo.manifest.rev(change[0]), striprev) |
|
123 | 124 | if saveheads: |
|
124 | 125 | ui.status("adding branch\n") |
|
125 | 126 | f = open(chgrpfile, "rb") |
|
126 | 127 | gen = changegroup.readbundle(f, chgrpfile) |
|
127 | 128 | repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile) |
|
128 | 129 | f.close() |
|
129 | 130 | if backup != "strip": |
|
130 | 131 | os.unlink(chgrpfile) |
|
131 | 132 |
General Comments 0
You need to be logged in to leave comments.
Login now