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