Show More
@@ -1,160 +1,167 | |||||
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 of the |
|
6 | # This software may be used and distributed according to the terms of the | |
7 | # GNU General Public License version 2 or any later version. |
|
7 | # GNU General Public License version 2 or any later version. | |
8 |
|
8 | |||
9 | import changegroup |
|
9 | import changegroup | |
10 | from node import nullrev, short |
|
10 | from node import nullrev, short | |
11 | from i18n import _ |
|
11 | from i18n import _ | |
12 | import os |
|
12 | import os | |
13 |
|
13 | |||
14 | def _bundle(repo, bases, heads, node, suffix, extranodes=None): |
|
14 | def _bundle(repo, bases, heads, node, suffix, extranodes=None, compress=True): | |
15 | """create a bundle with the specified revisions as a backup""" |
|
15 | """create a bundle with the specified revisions as a backup""" | |
16 | cg = repo.changegroupsubset(bases, heads, 'strip', extranodes) |
|
16 | cg = repo.changegroupsubset(bases, heads, 'strip', extranodes) | |
17 | backupdir = repo.join("strip-backup") |
|
17 | backupdir = repo.join("strip-backup") | |
18 | if not os.path.isdir(backupdir): |
|
18 | if not os.path.isdir(backupdir): | |
19 | os.mkdir(backupdir) |
|
19 | os.mkdir(backupdir) | |
20 | name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix)) |
|
20 | name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix)) | |
21 | return changegroup.writebundle(cg, name, "HG10BZ") |
|
21 | if compress: | |
|
22 | bundletype = "HG10BZ" | |||
|
23 | else: | |||
|
24 | bundletype = "HG10UN" | |||
|
25 | return changegroup.writebundle(cg, name, bundletype) | |||
22 |
|
26 | |||
23 | def _collectfiles(repo, striprev): |
|
27 | def _collectfiles(repo, striprev): | |
24 | """find out the filelogs affected by the strip""" |
|
28 | """find out the filelogs affected by the strip""" | |
25 | files = set() |
|
29 | files = set() | |
26 |
|
30 | |||
27 | for x in xrange(striprev, len(repo)): |
|
31 | for x in xrange(striprev, len(repo)): | |
28 | files.update(repo[x].files()) |
|
32 | files.update(repo[x].files()) | |
29 |
|
33 | |||
30 | return sorted(files) |
|
34 | return sorted(files) | |
31 |
|
35 | |||
32 | def _collectextranodes(repo, files, link): |
|
36 | def _collectextranodes(repo, files, link): | |
33 | """return the nodes that have to be saved before the strip""" |
|
37 | """return the nodes that have to be saved before the strip""" | |
34 | def collectone(revlog): |
|
38 | def collectone(revlog): | |
35 | extra = [] |
|
39 | extra = [] | |
36 | startrev = count = len(revlog) |
|
40 | startrev = count = len(revlog) | |
37 | # find the truncation point of the revlog |
|
41 | # find the truncation point of the revlog | |
38 | for i in xrange(count): |
|
42 | for i in xrange(count): | |
39 | lrev = revlog.linkrev(i) |
|
43 | lrev = revlog.linkrev(i) | |
40 | if lrev >= link: |
|
44 | if lrev >= link: | |
41 | startrev = i + 1 |
|
45 | startrev = i + 1 | |
42 | break |
|
46 | break | |
43 |
|
47 | |||
44 | # see if any revision after that point has a linkrev less than link |
|
48 | # see if any revision after that point has a linkrev less than link | |
45 | # (we have to manually save these guys) |
|
49 | # (we have to manually save these guys) | |
46 | for i in xrange(startrev, count): |
|
50 | for i in xrange(startrev, count): | |
47 | node = revlog.node(i) |
|
51 | node = revlog.node(i) | |
48 | lrev = revlog.linkrev(i) |
|
52 | lrev = revlog.linkrev(i) | |
49 | if lrev < link: |
|
53 | if lrev < link: | |
50 | extra.append((node, cl.node(lrev))) |
|
54 | extra.append((node, cl.node(lrev))) | |
51 |
|
55 | |||
52 | return extra |
|
56 | return extra | |
53 |
|
57 | |||
54 | extranodes = {} |
|
58 | extranodes = {} | |
55 | cl = repo.changelog |
|
59 | cl = repo.changelog | |
56 | extra = collectone(repo.manifest) |
|
60 | extra = collectone(repo.manifest) | |
57 | if extra: |
|
61 | if extra: | |
58 | extranodes[1] = extra |
|
62 | extranodes[1] = extra | |
59 | for fname in files: |
|
63 | for fname in files: | |
60 | f = repo.file(fname) |
|
64 | f = repo.file(fname) | |
61 | extra = collectone(f) |
|
65 | extra = collectone(f) | |
62 | if extra: |
|
66 | if extra: | |
63 | extranodes[fname] = extra |
|
67 | extranodes[fname] = extra | |
64 |
|
68 | |||
65 | return extranodes |
|
69 | return extranodes | |
66 |
|
70 | |||
67 | def strip(ui, repo, node, backup="all"): |
|
71 | def strip(ui, repo, node, backup="all"): | |
68 | cl = repo.changelog |
|
72 | cl = repo.changelog | |
69 | # TODO delete the undo files, and handle undo of merge sets |
|
73 | # TODO delete the undo files, and handle undo of merge sets | |
70 | striprev = cl.rev(node) |
|
74 | striprev = cl.rev(node) | |
71 |
|
75 | |||
|
76 | keeppartialbundle = backup == 'strip' | |||
|
77 | ||||
72 | # Some revisions with rev > striprev may not be descendants of striprev. |
|
78 | # Some revisions with rev > striprev may not be descendants of striprev. | |
73 | # We have to find these revisions and put them in a bundle, so that |
|
79 | # We have to find these revisions and put them in a bundle, so that | |
74 | # we can restore them after the truncations. |
|
80 | # we can restore them after the truncations. | |
75 | # To create the bundle we use repo.changegroupsubset which requires |
|
81 | # To create the bundle we use repo.changegroupsubset which requires | |
76 | # the list of heads and bases of the set of interesting revisions. |
|
82 | # the list of heads and bases of the set of interesting revisions. | |
77 | # (head = revision in the set that has no descendant in the set; |
|
83 | # (head = revision in the set that has no descendant in the set; | |
78 | # base = revision in the set that has no ancestor in the set) |
|
84 | # base = revision in the set that has no ancestor in the set) | |
79 | tostrip = set((striprev,)) |
|
85 | tostrip = set((striprev,)) | |
80 | saveheads = set() |
|
86 | saveheads = set() | |
81 | savebases = [] |
|
87 | savebases = [] | |
82 | for r in xrange(striprev + 1, len(cl)): |
|
88 | for r in xrange(striprev + 1, len(cl)): | |
83 | parents = cl.parentrevs(r) |
|
89 | parents = cl.parentrevs(r) | |
84 | if parents[0] in tostrip or parents[1] in tostrip: |
|
90 | if parents[0] in tostrip or parents[1] in tostrip: | |
85 | # r is a descendant of striprev |
|
91 | # r is a descendant of striprev | |
86 | tostrip.add(r) |
|
92 | tostrip.add(r) | |
87 | # if this is a merge and one of the parents does not descend |
|
93 | # if this is a merge and one of the parents does not descend | |
88 | # from striprev, mark that parent as a savehead. |
|
94 | # from striprev, mark that parent as a savehead. | |
89 | if parents[1] != nullrev: |
|
95 | if parents[1] != nullrev: | |
90 | for p in parents: |
|
96 | for p in parents: | |
91 | if p not in tostrip and p > striprev: |
|
97 | if p not in tostrip and p > striprev: | |
92 | saveheads.add(p) |
|
98 | saveheads.add(p) | |
93 | else: |
|
99 | else: | |
94 | # if no parents of this revision will be stripped, mark it as |
|
100 | # if no parents of this revision will be stripped, mark it as | |
95 | # a savebase |
|
101 | # a savebase | |
96 | if parents[0] < striprev and parents[1] < striprev: |
|
102 | if parents[0] < striprev and parents[1] < striprev: | |
97 | savebases.append(cl.node(r)) |
|
103 | savebases.append(cl.node(r)) | |
98 |
|
104 | |||
99 | saveheads.difference_update(parents) |
|
105 | saveheads.difference_update(parents) | |
100 | saveheads.add(r) |
|
106 | saveheads.add(r) | |
101 |
|
107 | |||
102 | saveheads = [cl.node(r) for r in saveheads] |
|
108 | saveheads = [cl.node(r) for r in saveheads] | |
103 | files = _collectfiles(repo, striprev) |
|
109 | files = _collectfiles(repo, striprev) | |
104 |
|
110 | |||
105 | extranodes = _collectextranodes(repo, files, striprev) |
|
111 | extranodes = _collectextranodes(repo, files, striprev) | |
106 |
|
112 | |||
107 | # create a changegroup for all the branches we need to keep |
|
113 | # create a changegroup for all the branches we need to keep | |
108 | backupfile = None |
|
114 | backupfile = None | |
109 | if backup == "all": |
|
115 | if backup == "all": | |
110 | backupfile = _bundle(repo, [node], cl.heads(), node, 'backup') |
|
116 | backupfile = _bundle(repo, [node], cl.heads(), node, 'backup') | |
111 | repo.ui.status(_("saved backup bundle to %s\n") % backupfile) |
|
117 | repo.ui.status(_("saved backup bundle to %s\n") % backupfile) | |
112 | if saveheads or extranodes: |
|
118 | if saveheads or extranodes: | |
|
119 | # do not compress partial bundle if we remove it from disk later | |||
113 | chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp', |
|
120 | chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp', | |
114 | extranodes) |
|
121 | extranodes=extranodes, compress=keeppartialbundle) | |
115 |
|
122 | |||
116 | mfst = repo.manifest |
|
123 | mfst = repo.manifest | |
117 |
|
124 | |||
118 | tr = repo.transaction("strip") |
|
125 | tr = repo.transaction("strip") | |
119 | offset = len(tr.entries) |
|
126 | offset = len(tr.entries) | |
120 |
|
127 | |||
121 | try: |
|
128 | try: | |
122 | tr.startgroup() |
|
129 | tr.startgroup() | |
123 | cl.strip(striprev, tr) |
|
130 | cl.strip(striprev, tr) | |
124 | mfst.strip(striprev, tr) |
|
131 | mfst.strip(striprev, tr) | |
125 | for fn in files: |
|
132 | for fn in files: | |
126 | repo.file(fn).strip(striprev, tr) |
|
133 | repo.file(fn).strip(striprev, tr) | |
127 | tr.endgroup() |
|
134 | tr.endgroup() | |
128 |
|
135 | |||
129 | try: |
|
136 | try: | |
130 | for i in xrange(offset, len(tr.entries)): |
|
137 | for i in xrange(offset, len(tr.entries)): | |
131 | file, troffset, ignore = tr.entries[i] |
|
138 | file, troffset, ignore = tr.entries[i] | |
132 | repo.sopener(file, 'a').truncate(troffset) |
|
139 | repo.sopener(file, 'a').truncate(troffset) | |
133 | tr.close() |
|
140 | tr.close() | |
134 | except: |
|
141 | except: | |
135 | tr.abort() |
|
142 | tr.abort() | |
136 | raise |
|
143 | raise | |
137 |
|
144 | |||
138 | if saveheads or extranodes: |
|
145 | if saveheads or extranodes: | |
139 | ui.note(_("adding branch\n")) |
|
146 | ui.note(_("adding branch\n")) | |
140 | f = open(chgrpfile, "rb") |
|
147 | f = open(chgrpfile, "rb") | |
141 | gen = changegroup.readbundle(f, chgrpfile) |
|
148 | gen = changegroup.readbundle(f, chgrpfile) | |
142 | if not repo.ui.verbose: |
|
149 | if not repo.ui.verbose: | |
143 | # silence internal shuffling chatter |
|
150 | # silence internal shuffling chatter | |
144 | repo.ui.pushbuffer() |
|
151 | repo.ui.pushbuffer() | |
145 | repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True) |
|
152 | repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True) | |
146 | if not repo.ui.verbose: |
|
153 | if not repo.ui.verbose: | |
147 | repo.ui.popbuffer() |
|
154 | repo.ui.popbuffer() | |
148 | f.close() |
|
155 | f.close() | |
149 | if backup != "strip": |
|
156 | if not keeppartialbundle: | |
150 | os.unlink(chgrpfile) |
|
157 | os.unlink(chgrpfile) | |
151 | except: |
|
158 | except: | |
152 | if backupfile: |
|
159 | if backupfile: | |
153 | ui.warn(_("strip failed, full bundle stored in '%s'\n") |
|
160 | ui.warn(_("strip failed, full bundle stored in '%s'\n") | |
154 | % backupfile) |
|
161 | % backupfile) | |
155 | elif saveheads: |
|
162 | elif saveheads: | |
156 | ui.warn(_("strip failed, partial bundle stored in '%s'\n") |
|
163 | ui.warn(_("strip failed, partial bundle stored in '%s'\n") | |
157 | % chgrpfile) |
|
164 | % chgrpfile) | |
158 | raise |
|
165 | raise | |
159 |
|
166 | |||
160 | repo.destroyed() |
|
167 | repo.destroyed() |
General Comments 0
You need to be logged in to leave comments.
Login now