Show More
@@ -1,201 +1,209 b'' | |||
|
1 | 1 | # hg backend for convert extension |
|
2 | 2 | |
|
3 | 3 | # Note for hg->hg conversion: Old versions of Mercurial didn't trim |
|
4 | 4 | # the whitespace from the ends of commit messages, but new versions |
|
5 | 5 | # do. Changesets created by those older versions, then converted, may |
|
6 | 6 | # thus have different hashes for changesets that are otherwise |
|
7 | 7 | # identical. |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | import os, time |
|
11 | 11 | from mercurial.i18n import _ |
|
12 | 12 | from mercurial.node import * |
|
13 | 13 | from mercurial import hg, lock, revlog, util |
|
14 | 14 | |
|
15 | 15 | from common import NoRepo, commit, converter_source, converter_sink |
|
16 | 16 | |
|
17 | 17 | class mercurial_sink(converter_sink): |
|
18 | 18 | def __init__(self, ui, path): |
|
19 | 19 | self.path = path |
|
20 | 20 | self.ui = ui |
|
21 | 21 | self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True) |
|
22 | 22 | self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False) |
|
23 | self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default') | |
|
23 | 24 | self.lastbranch = None |
|
24 | 25 | try: |
|
25 | 26 | self.repo = hg.repository(self.ui, path) |
|
26 | 27 | except: |
|
27 | 28 | raise NoRepo("could not open hg repo %s as sink" % path) |
|
28 | 29 | self.lock = None |
|
29 | 30 | self.wlock = None |
|
30 | 31 | |
|
31 | 32 | def before(self): |
|
32 | 33 | self.wlock = self.repo.wlock() |
|
33 | 34 | self.lock = self.repo.lock() |
|
34 | 35 | |
|
35 | 36 | def after(self): |
|
36 | 37 | self.lock = None |
|
37 | 38 | self.wlock = None |
|
38 | 39 | |
|
39 | 40 | def revmapfile(self): |
|
40 | 41 | return os.path.join(self.path, ".hg", "shamap") |
|
41 | 42 | |
|
42 | 43 | def authorfile(self): |
|
43 | 44 | return os.path.join(self.path, ".hg", "authormap") |
|
44 | 45 | |
|
45 | 46 | def getheads(self): |
|
46 | 47 | h = self.repo.changelog.heads() |
|
47 | 48 | return [ hex(x) for x in h ] |
|
48 | 49 | |
|
49 | 50 | def putfile(self, f, e, data): |
|
50 | 51 | self.repo.wwrite(f, data, e) |
|
51 | 52 | if f not in self.repo.dirstate: |
|
52 | 53 | self.repo.dirstate.add(f) |
|
53 | 54 | |
|
54 | 55 | def copyfile(self, source, dest): |
|
55 | 56 | self.repo.copy(source, dest) |
|
56 | 57 | |
|
57 | 58 | def delfile(self, f): |
|
58 | 59 | try: |
|
59 | 60 | os.unlink(self.repo.wjoin(f)) |
|
60 | 61 | #self.repo.remove([f]) |
|
61 | 62 | except: |
|
62 | 63 | pass |
|
63 | 64 | |
|
64 | 65 | def setbranch(self, branch, pbranch, parents): |
|
65 | 66 | if (not self.clonebranches) or (branch == self.lastbranch): |
|
66 | 67 | return |
|
67 | 68 | |
|
68 | 69 | self.lastbranch = branch |
|
69 | 70 | self.after() |
|
70 | 71 | if not branch: |
|
71 | 72 | branch = 'default' |
|
72 | 73 | if not pbranch: |
|
73 | 74 | pbranch = 'default' |
|
74 | 75 | |
|
75 | 76 | branchpath = os.path.join(self.path, branch) |
|
76 | 77 | try: |
|
77 | 78 | self.repo = hg.repository(self.ui, branchpath) |
|
78 | 79 | except: |
|
79 | 80 | if not parents: |
|
80 | 81 | self.repo = hg.repository(self.ui, branchpath, create=True) |
|
81 | 82 | else: |
|
82 | 83 | self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch)) |
|
83 | 84 | hg.clone(self.ui, os.path.join(self.path, pbranch), |
|
84 | 85 | branchpath, rev=parents, update=False, |
|
85 | 86 | stream=True) |
|
86 | 87 | self.repo = hg.repository(self.ui, branchpath) |
|
87 | 88 | |
|
88 | 89 | def putcommit(self, files, parents, commit): |
|
89 | 90 | seen = {} |
|
90 | 91 | pl = [] |
|
91 | 92 | for p in parents: |
|
92 | 93 | if p not in seen: |
|
93 | 94 | pl.append(p) |
|
94 | 95 | seen[p] = 1 |
|
95 | 96 | parents = pl |
|
96 | 97 | |
|
97 | 98 | if len(parents) < 2: parents.append("0" * 40) |
|
98 | 99 | if len(parents) < 2: parents.append("0" * 40) |
|
99 | 100 | p2 = parents.pop(0) |
|
100 | 101 | |
|
101 | 102 | text = commit.desc |
|
102 | 103 | extra = {} |
|
103 | 104 | if self.branchnames and commit.branch: |
|
104 | 105 | extra['branch'] = commit.branch |
|
105 | 106 | if commit.rev: |
|
106 | 107 | extra['convert_revision'] = commit.rev |
|
107 | 108 | |
|
108 | 109 | while parents: |
|
109 | 110 | p1 = p2 |
|
110 | 111 | p2 = parents.pop(0) |
|
111 | 112 | a = self.repo.rawcommit(files, text, commit.author, commit.date, |
|
112 | 113 | bin(p1), bin(p2), extra=extra) |
|
113 | 114 | self.repo.dirstate.invalidate() |
|
114 | 115 | text = "(octopus merge fixup)\n" |
|
115 | 116 | p2 = hg.hex(self.repo.changelog.tip()) |
|
116 | 117 | |
|
117 | 118 | return p2 |
|
118 | 119 | |
|
119 | 120 | def puttags(self, tags): |
|
120 | 121 | try: |
|
121 | 122 | old = self.repo.wfile(".hgtags").read() |
|
122 | 123 | oldlines = old.splitlines(1) |
|
123 | 124 | oldlines.sort() |
|
124 | 125 | except: |
|
125 | 126 | oldlines = [] |
|
126 | 127 | |
|
127 | 128 | k = tags.keys() |
|
128 | 129 | k.sort() |
|
129 | 130 | newlines = [] |
|
130 | 131 | for tag in k: |
|
131 | 132 | newlines.append("%s %s\n" % (tags[tag], tag)) |
|
132 | 133 | |
|
133 | 134 | newlines.sort() |
|
134 | 135 | |
|
135 | 136 | if newlines != oldlines: |
|
136 | 137 | self.ui.status("updating tags\n") |
|
137 | 138 | f = self.repo.wfile(".hgtags", "w") |
|
138 | 139 | f.write("".join(newlines)) |
|
139 | 140 | f.close() |
|
140 | 141 | if not oldlines: self.repo.add([".hgtags"]) |
|
141 | 142 | date = "%s 0" % int(time.mktime(time.gmtime())) |
|
143 | extra = {} | |
|
144 | if self.tagsbranch != 'default': | |
|
145 | extra['branch'] = self.tagsbranch | |
|
146 | try: | |
|
147 | tagparent = self.repo.changectx(self.tagsbranch).node() | |
|
148 | except hg.RepoError, inst: | |
|
149 | tagparent = nullid | |
|
142 | 150 | self.repo.rawcommit([".hgtags"], "update tags", "convert-repo", |
|
143 |
date, |
|
|
151 | date, tagparent, nullid) | |
|
144 | 152 | return hex(self.repo.changelog.tip()) |
|
145 | 153 | |
|
146 | 154 | class mercurial_source(converter_source): |
|
147 | 155 | def __init__(self, ui, path, rev=None): |
|
148 | 156 | converter_source.__init__(self, ui, path, rev) |
|
149 | 157 | self.repo = hg.repository(self.ui, path) |
|
150 | 158 | self.lastrev = None |
|
151 | 159 | self.lastctx = None |
|
152 | 160 | |
|
153 | 161 | def changectx(self, rev): |
|
154 | 162 | if self.lastrev != rev: |
|
155 | 163 | self.lastctx = self.repo.changectx(rev) |
|
156 | 164 | self.lastrev = rev |
|
157 | 165 | return self.lastctx |
|
158 | 166 | |
|
159 | 167 | def getheads(self): |
|
160 | 168 | if self.rev: |
|
161 | 169 | return [hex(self.repo.changectx(self.rev).node())] |
|
162 | 170 | else: |
|
163 | 171 | return [hex(node) for node in self.repo.heads()] |
|
164 | 172 | |
|
165 | 173 | def getfile(self, name, rev): |
|
166 | 174 | try: |
|
167 | 175 | return self.changectx(rev).filectx(name).data() |
|
168 | 176 | except revlog.LookupError, err: |
|
169 | 177 | raise IOError(err) |
|
170 | 178 | |
|
171 | 179 | def getmode(self, name, rev): |
|
172 | 180 | m = self.changectx(rev).manifest() |
|
173 | 181 | return (m.execf(name) and 'x' or '') + (m.linkf(name) and 'l' or '') |
|
174 | 182 | |
|
175 | 183 | def getchanges(self, rev): |
|
176 | 184 | ctx = self.changectx(rev) |
|
177 | 185 | m, a, r = self.repo.status(ctx.parents()[0].node(), ctx.node())[:3] |
|
178 | 186 | changes = [(name, rev) for name in m + a + r] |
|
179 | 187 | changes.sort() |
|
180 | 188 | return (changes, self.getcopies(ctx)) |
|
181 | 189 | |
|
182 | 190 | def getcopies(self, ctx): |
|
183 | 191 | added = self.repo.status(ctx.parents()[0].node(), ctx.node())[1] |
|
184 | 192 | copies = {} |
|
185 | 193 | for name in added: |
|
186 | 194 | try: |
|
187 | 195 | copies[name] = ctx.filectx(name).renamed()[0] |
|
188 | 196 | except TypeError: |
|
189 | 197 | pass |
|
190 | 198 | return copies |
|
191 | 199 | |
|
192 | 200 | def getcommit(self, rev): |
|
193 | 201 | ctx = self.changectx(rev) |
|
194 | 202 | parents = [hex(p.node()) for p in ctx.parents() if p.node() != nullid] |
|
195 | 203 | return commit(author=ctx.user(), date=util.datestr(ctx.date()), |
|
196 | 204 | desc=ctx.description(), parents=parents, |
|
197 | 205 | branch=ctx.branch()) |
|
198 | 206 | |
|
199 | 207 | def gettags(self): |
|
200 | 208 | tags = [t for t in self.repo.tagslist() if t[0] != 'tip'] |
|
201 | 209 | return dict([(name, hex(node)) for name, node in tags]) |
General Comments 0
You need to be logged in to leave comments.
Login now