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