##// END OF EJS Templates
fix warning from pychecker
Benoit Boissinot -
r6408:3f5f23ba default
parent child Browse files
Show More
@@ -1,136 +1,135 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 nullrev, short
11 11
12 12 def _bundle(repo, bases, heads, node, suffix, extranodes=None):
13 13 """create a bundle with the specified revisions as a backup"""
14 14 cg = repo.changegroupsubset(bases, heads, 'strip', extranodes)
15 15 backupdir = repo.join("strip-backup")
16 16 if not os.path.isdir(backupdir):
17 17 os.mkdir(backupdir)
18 18 name = os.path.join(backupdir, "%s-%s" % (short(node), suffix))
19 19 repo.ui.warn("saving bundle to %s\n" % name)
20 20 return changegroup.writebundle(cg, name, "HG10BZ")
21 21
22 22 def _collectfiles(repo, striprev):
23 23 """find out the filelogs affected by the strip"""
24 24 files = {}
25 25
26 26 for x in xrange(striprev, repo.changelog.count()):
27 27 for name in repo.changectx(x).files():
28 28 if name in files:
29 29 continue
30 30 files[name] = 1
31 31
32 32 files = files.keys()
33 33 files.sort()
34 34 return files
35 35
36 36 def _collectextranodes(repo, files, link):
37 37 """return the nodes that have to be saved before the strip"""
38 38 def collectone(revlog):
39 39 extra = []
40 40 startrev = count = revlog.count()
41 41 # find the truncation point of the revlog
42 42 for i in xrange(0, count):
43 43 node = revlog.node(i)
44 44 lrev = revlog.linkrev(node)
45 45 if lrev >= link:
46 46 startrev = i + 1
47 47 break
48 48
49 49 # see if any revision after that point has a linkrev less than link
50 50 # (we have to manually save these guys)
51 51 for i in xrange(startrev, count):
52 52 node = revlog.node(i)
53 53 lrev = revlog.linkrev(node)
54 54 if lrev < link:
55 55 extra.append((node, cl.node(lrev)))
56 56
57 57 return extra
58 58
59 59 extranodes = {}
60 60 cl = repo.changelog
61 61 extra = collectone(repo.manifest)
62 62 if extra:
63 63 extranodes[1] = extra
64 64 for fname in files:
65 65 f = repo.file(fname)
66 66 extra = collectone(f)
67 67 if extra:
68 68 extranodes[fname] = extra
69 69
70 70 return extranodes
71 71
72 72 def strip(ui, repo, node, backup="all"):
73 73 cl = repo.changelog
74 74 # TODO delete the undo files, and handle undo of merge sets
75 pp = cl.parents(node)
76 75 striprev = cl.rev(node)
77 76
78 77 # Some revisions with rev > striprev may not be descendants of striprev.
79 78 # We have to find these revisions and put them in a bundle, so that
80 79 # we can restore them after the truncations.
81 80 # To create the bundle we use repo.changegroupsubset which requires
82 81 # the list of heads and bases of the set of interesting revisions.
83 82 # (head = revision in the set that has no descendant in the set;
84 83 # base = revision in the set that has no ancestor in the set)
85 84 tostrip = {striprev: 1}
86 85 saveheads = {}
87 86 savebases = []
88 87 for r in xrange(striprev + 1, cl.count()):
89 88 parents = cl.parentrevs(r)
90 89 if parents[0] in tostrip or parents[1] in tostrip:
91 90 # r is a descendant of striprev
92 91 tostrip[r] = 1
93 92 # if this is a merge and one of the parents does not descend
94 93 # from striprev, mark that parent as a savehead.
95 94 if parents[1] != nullrev:
96 95 for p in parents:
97 96 if p not in tostrip and p > striprev:
98 97 saveheads[p] = 1
99 98 else:
100 99 # if no parents of this revision will be stripped, mark it as
101 100 # a savebase
102 101 if parents[0] < striprev and parents[1] < striprev:
103 102 savebases.append(cl.node(r))
104 103
105 104 for p in parents:
106 105 if p in saveheads:
107 106 del saveheads[p]
108 107 saveheads[r] = 1
109 108
110 109 saveheads = [cl.node(r) for r in saveheads]
111 110 files = _collectfiles(repo, striprev)
112 111
113 112 extranodes = _collectextranodes(repo, files, striprev)
114 113
115 114 # create a changegroup for all the branches we need to keep
116 115 if backup == "all":
117 116 _bundle(repo, [node], cl.heads(), node, 'backup')
118 117 if saveheads or extranodes:
119 118 chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
120 119 extranodes)
121 120
122 121 cl.strip(striprev)
123 122 repo.manifest.strip(striprev)
124 123 for name in files:
125 124 f = repo.file(name)
126 125 f.strip(striprev)
127 126
128 127 if saveheads or extranodes:
129 128 ui.status("adding branch\n")
130 129 f = open(chgrpfile, "rb")
131 130 gen = changegroup.readbundle(f, chgrpfile)
132 131 repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
133 132 f.close()
134 133 if backup != "strip":
135 134 os.unlink(chgrpfile)
136 135
General Comments 0
You need to be logged in to leave comments. Login now