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