##// END OF EJS Templates
subrepo: Subversion support
Augie Fackler -
r10178:cd477be6 default
parent child Browse files
Show More
@@ -0,0 +1,72 b''
1 #!/bin/sh
2
3 "$TESTDIR/hghave" svn || exit 80
4
5 escapedwd=$(pwd | \
6 python -c \
7 "import sys,urllib; print urllib.pathname2url(sys.stdin.read().strip())"
8 )
9 filterpath="sed s+$escapedwd+/root+"
10
11 echo % create subversion repo
12
13 SVNREPO="file://$escapedwd/svn-repo"
14 WCROOT="$(pwd)/svn-wc"
15 svnadmin create svn-repo
16 svn co $SVNREPO svn-wc
17 cd svn-wc
18 echo alpha > alpha
19 svn add alpha
20 svn ci -m 'Add alpha'
21 cd ..
22
23 echo % create hg repo
24
25 rm -rf sub
26 mkdir sub
27 cd sub
28 hg init t
29 cd t
30
31 echo % first revision, no sub
32 echo a > a
33 hg ci -Am0
34
35 echo % add first svn sub
36 echo "s = [svn]$SVNREPO" >> .hgsub
37 svn co --quiet $SVNREPO s
38 hg add .hgsub
39 hg ci -m1
40 echo % debugsub
41 hg debugsub | $filterpath
42
43 echo
44 echo % change file in svn and hg, commit
45 echo a >> a
46 echo alpha >> s/alpha
47 hg commit -m 'Message!'
48 hg debugsub | $filterpath
49
50 echo
51 echo a > s/a
52 echo % should be empty despite change to s/a
53 hg st
54
55 echo
56 echo % add a commit from svn
57 pushd "$WCROOT" > /dev/null
58 svn up
59 echo xyz >> alpha
60 svn ci -m 'amend a from svn'
61 popd > /dev/null
62 echo % this commit from hg will fail
63 echo zzz >> s/alpha
64 hg ci -m 'amend alpha from hg'
65
66 echo
67 echo % clone
68 cd ..
69 hg clone t tc
70 cd tc
71 echo % debugsub in clone
72 hg debugsub | $filterpath
@@ -0,0 +1,48 b''
1 % create subversion repo
2 Checked out revision 0.
3 A alpha
4 Adding alpha
5 Transmitting file data .
6 Committed revision 1.
7 % create hg repo
8 % first revision, no sub
9 adding a
10 % add first svn sub
11 committing subrepository s
12 % debugsub
13 path s
14 source file:///root/svn-repo
15 revision 1
16
17 % change file in svn and hg, commit
18 committing subrepository s
19 Sending s/alpha
20 Transmitting file data .
21 Committed revision 2.
22 At revision 2.
23 path s
24 source file:///root/svn-repo
25 revision 2
26
27 % should be empty despite change to s/a
28
29 % add a commit from svn
30 U alpha
31 Updated to revision 2.
32 Sending alpha
33 Transmitting file data .
34 Committed revision 3.
35 % this commit from hg will fail
36 committing subrepository s
37 abort: svn: Commit failed (details follow):
38 svn: File '/alpha' is out of date
39
40 % clone
41 updating to branch default
42 A s/alpha
43 Checked out revision 2.
44 3 files updated, 0 files merged, 0 files removed, 0 files unresolved
45 % debugsub in clone
46 path s
47 source file:///root/svn-repo
48 revision 2
@@ -1,255 +1,337 b''
1 # subrepo.py - sub-repository handling for Mercurial
1 # subrepo.py - sub-repository handling for Mercurial
2 #
2 #
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7
7
8 import errno, os
8 import errno, os, re
9 from i18n import _
9 from i18n import _
10 import config, util, node, error
10 import config, util, node, error
11 hg = None
11 hg = None
12
12
13 nullstate = ('', '', 'empty')
13 nullstate = ('', '', 'empty')
14
14
15 def state(ctx):
15 def state(ctx):
16 p = config.config()
16 p = config.config()
17 def read(f, sections=None, remap=None):
17 def read(f, sections=None, remap=None):
18 if f in ctx:
18 if f in ctx:
19 p.parse(f, ctx[f].data(), sections, remap, read)
19 p.parse(f, ctx[f].data(), sections, remap, read)
20 else:
20 else:
21 raise util.Abort(_("subrepo spec file %s not found") % f)
21 raise util.Abort(_("subrepo spec file %s not found") % f)
22
22
23 if '.hgsub' in ctx:
23 if '.hgsub' in ctx:
24 read('.hgsub')
24 read('.hgsub')
25
25
26 rev = {}
26 rev = {}
27 if '.hgsubstate' in ctx:
27 if '.hgsubstate' in ctx:
28 try:
28 try:
29 for l in ctx['.hgsubstate'].data().splitlines():
29 for l in ctx['.hgsubstate'].data().splitlines():
30 revision, path = l.split(" ", 1)
30 revision, path = l.split(" ", 1)
31 rev[path] = revision
31 rev[path] = revision
32 except IOError, err:
32 except IOError, err:
33 if err.errno != errno.ENOENT:
33 if err.errno != errno.ENOENT:
34 raise
34 raise
35
35
36 state = {}
36 state = {}
37 for path, src in p[''].items():
37 for path, src in p[''].items():
38 kind = 'hg'
38 kind = 'hg'
39 if src.startswith('['):
39 if src.startswith('['):
40 if ']' not in src:
40 if ']' not in src:
41 raise util.Abort(_('missing ] in subrepo source'))
41 raise util.Abort(_('missing ] in subrepo source'))
42 kind, src = src.split(']', 1)
42 kind, src = src.split(']', 1)
43 kind = kind[1:]
43 kind = kind[1:]
44 state[path] = (src, rev.get(path, ''), kind)
44 state[path] = (src, rev.get(path, ''), kind)
45
45
46 return state
46 return state
47
47
48 def writestate(repo, state):
48 def writestate(repo, state):
49 repo.wwrite('.hgsubstate',
49 repo.wwrite('.hgsubstate',
50 ''.join(['%s %s\n' % (state[s][1], s)
50 ''.join(['%s %s\n' % (state[s][1], s)
51 for s in sorted(state)]), '')
51 for s in sorted(state)]), '')
52
52
53 def submerge(repo, wctx, mctx, actx):
53 def submerge(repo, wctx, mctx, actx):
54 # working context, merging context, ancestor context
54 # working context, merging context, ancestor context
55 if mctx == actx: # backwards?
55 if mctx == actx: # backwards?
56 actx = wctx.p1()
56 actx = wctx.p1()
57 s1 = wctx.substate
57 s1 = wctx.substate
58 s2 = mctx.substate
58 s2 = mctx.substate
59 sa = actx.substate
59 sa = actx.substate
60 sm = {}
60 sm = {}
61
61
62 repo.ui.debug("subrepo merge %s %s %s\n" % (wctx, mctx, actx))
62 repo.ui.debug("subrepo merge %s %s %s\n" % (wctx, mctx, actx))
63
63
64 def debug(s, msg, r=""):
64 def debug(s, msg, r=""):
65 if r:
65 if r:
66 r = "%s:%s:%s" % r
66 r = "%s:%s:%s" % r
67 repo.ui.debug(" subrepo %s: %s %s\n" % (s, msg, r))
67 repo.ui.debug(" subrepo %s: %s %s\n" % (s, msg, r))
68
68
69 for s, l in s1.items():
69 for s, l in s1.items():
70 if wctx != actx and wctx.sub(s).dirty():
70 if wctx != actx and wctx.sub(s).dirty():
71 l = (l[0], l[1] + "+")
71 l = (l[0], l[1] + "+")
72 a = sa.get(s, nullstate)
72 a = sa.get(s, nullstate)
73 if s in s2:
73 if s in s2:
74 r = s2[s]
74 r = s2[s]
75 if l == r or r == a: # no change or local is newer
75 if l == r or r == a: # no change or local is newer
76 sm[s] = l
76 sm[s] = l
77 continue
77 continue
78 elif l == a: # other side changed
78 elif l == a: # other side changed
79 debug(s, "other changed, get", r)
79 debug(s, "other changed, get", r)
80 wctx.sub(s).get(r)
80 wctx.sub(s).get(r)
81 sm[s] = r
81 sm[s] = r
82 elif l[0] != r[0]: # sources differ
82 elif l[0] != r[0]: # sources differ
83 if repo.ui.promptchoice(
83 if repo.ui.promptchoice(
84 _(' subrepository sources for %s differ\n'
84 _(' subrepository sources for %s differ\n'
85 'use (l)ocal source (%s) or (r)emote source (%s)?')
85 'use (l)ocal source (%s) or (r)emote source (%s)?')
86 % (s, l[0], r[0]),
86 % (s, l[0], r[0]),
87 (_('&Local'), _('&Remote')), 0):
87 (_('&Local'), _('&Remote')), 0):
88 debug(s, "prompt changed, get", r)
88 debug(s, "prompt changed, get", r)
89 wctx.sub(s).get(r)
89 wctx.sub(s).get(r)
90 sm[s] = r
90 sm[s] = r
91 elif l[1] == a[1]: # local side is unchanged
91 elif l[1] == a[1]: # local side is unchanged
92 debug(s, "other side changed, get", r)
92 debug(s, "other side changed, get", r)
93 wctx.sub(s).get(r)
93 wctx.sub(s).get(r)
94 sm[s] = r
94 sm[s] = r
95 else:
95 else:
96 debug(s, "both sides changed, merge with", r)
96 debug(s, "both sides changed, merge with", r)
97 wctx.sub(s).merge(r)
97 wctx.sub(s).merge(r)
98 sm[s] = l
98 sm[s] = l
99 elif l == a: # remote removed, local unchanged
99 elif l == a: # remote removed, local unchanged
100 debug(s, "remote removed, remove")
100 debug(s, "remote removed, remove")
101 wctx.sub(s).remove()
101 wctx.sub(s).remove()
102 else:
102 else:
103 if repo.ui.promptchoice(
103 if repo.ui.promptchoice(
104 _(' local changed subrepository %s which remote removed\n'
104 _(' local changed subrepository %s which remote removed\n'
105 'use (c)hanged version or (d)elete?') % s,
105 'use (c)hanged version or (d)elete?') % s,
106 (_('&Changed'), _('&Delete')), 0):
106 (_('&Changed'), _('&Delete')), 0):
107 debug(s, "prompt remove")
107 debug(s, "prompt remove")
108 wctx.sub(s).remove()
108 wctx.sub(s).remove()
109
109
110 for s, r in s2.items():
110 for s, r in s2.items():
111 if s in s1:
111 if s in s1:
112 continue
112 continue
113 elif s not in sa:
113 elif s not in sa:
114 debug(s, "remote added, get", r)
114 debug(s, "remote added, get", r)
115 mctx.sub(s).get(r)
115 mctx.sub(s).get(r)
116 sm[s] = r
116 sm[s] = r
117 elif r != sa[s]:
117 elif r != sa[s]:
118 if repo.ui.promptchoice(
118 if repo.ui.promptchoice(
119 _(' remote changed subrepository %s which local removed\n'
119 _(' remote changed subrepository %s which local removed\n'
120 'use (c)hanged version or (d)elete?') % s,
120 'use (c)hanged version or (d)elete?') % s,
121 (_('&Changed'), _('&Delete')), 0) == 0:
121 (_('&Changed'), _('&Delete')), 0) == 0:
122 debug(s, "prompt recreate", r)
122 debug(s, "prompt recreate", r)
123 wctx.sub(s).get(r)
123 wctx.sub(s).get(r)
124 sm[s] = r
124 sm[s] = r
125
125
126 # record merged .hgsubstate
126 # record merged .hgsubstate
127 writestate(repo, sm)
127 writestate(repo, sm)
128
128
129 def _abssource(repo, push=False):
129 def _abssource(repo, push=False):
130 if hasattr(repo, '_subparent'):
130 if hasattr(repo, '_subparent'):
131 source = repo._subsource
131 source = repo._subsource
132 if source.startswith('/') or '://' in source:
132 if source.startswith('/') or '://' in source:
133 return source
133 return source
134 parent = _abssource(repo._subparent)
134 parent = _abssource(repo._subparent)
135 if '://' in parent:
135 if '://' in parent:
136 if parent[-1] == '/':
136 if parent[-1] == '/':
137 parent = parent[:-1]
137 parent = parent[:-1]
138 return parent + '/' + source
138 return parent + '/' + source
139 return os.path.join(parent, repo._subsource)
139 return os.path.join(parent, repo._subsource)
140 if push and repo.ui.config('paths', 'default-push'):
140 if push and repo.ui.config('paths', 'default-push'):
141 return repo.ui.config('paths', 'default-push', repo.root)
141 return repo.ui.config('paths', 'default-push', repo.root)
142 return repo.ui.config('paths', 'default', repo.root)
142 return repo.ui.config('paths', 'default', repo.root)
143
143
144 def subrepo(ctx, path):
144 def subrepo(ctx, path):
145 # subrepo inherently violates our import layering rules
145 # subrepo inherently violates our import layering rules
146 # because it wants to make repo objects from deep inside the stack
146 # because it wants to make repo objects from deep inside the stack
147 # so we manually delay the circular imports to not break
147 # so we manually delay the circular imports to not break
148 # scripts that don't use our demand-loading
148 # scripts that don't use our demand-loading
149 global hg
149 global hg
150 import hg as h
150 import hg as h
151 hg = h
151 hg = h
152
152
153 util.path_auditor(ctx._repo.root)(path)
153 util.path_auditor(ctx._repo.root)(path)
154 state = ctx.substate.get(path, nullstate)
154 state = ctx.substate.get(path, nullstate)
155 if state[2] not in types:
155 if state[2] not in types:
156 raise util.Abort(_('unknown subrepo type %s') % t)
156 raise util.Abort(_('unknown subrepo type %s') % t)
157 return types[state[2]](ctx, path, state[:2])
157 return types[state[2]](ctx, path, state[:2])
158
158
159 # subrepo classes need to implement the following methods:
159 # subrepo classes need to implement the following methods:
160 # __init__(self, ctx, path, state)
160 # __init__(self, ctx, path, state)
161 # dirty(self): returns true if the dirstate of the subrepo
161 # dirty(self): returns true if the dirstate of the subrepo
162 # does not match current stored state
162 # does not match current stored state
163 # commit(self, text, user, date): commit the current changes
163 # commit(self, text, user, date): commit the current changes
164 # to the subrepo with the given log message. Use given
164 # to the subrepo with the given log message. Use given
165 # user and date if possible. Return the new state of the subrepo.
165 # user and date if possible. Return the new state of the subrepo.
166 # remove(self): remove the subrepo (should verify the dirstate
166 # remove(self): remove the subrepo (should verify the dirstate
167 # is not dirty first)
167 # is not dirty first)
168 # get(self, state): run whatever commands are needed to put the
168 # get(self, state): run whatever commands are needed to put the
169 # subrepo into this state
169 # subrepo into this state
170 # merge(self, state): merge currently-saved state with the new state.
170 # merge(self, state): merge currently-saved state with the new state.
171 # push(self, force): perform whatever action is analagous to 'hg push'
171 # push(self, force): perform whatever action is analagous to 'hg push'
172 # This may be a no-op on some systems.
172 # This may be a no-op on some systems.
173
173
174 class hgsubrepo(object):
174 class hgsubrepo(object):
175 def __init__(self, ctx, path, state):
175 def __init__(self, ctx, path, state):
176 self._path = path
176 self._path = path
177 self._state = state
177 self._state = state
178 r = ctx._repo
178 r = ctx._repo
179 root = r.wjoin(path)
179 root = r.wjoin(path)
180 if os.path.exists(os.path.join(root, '.hg')):
180 if os.path.exists(os.path.join(root, '.hg')):
181 self._repo = hg.repository(r.ui, root)
181 self._repo = hg.repository(r.ui, root)
182 else:
182 else:
183 util.makedirs(root)
183 util.makedirs(root)
184 self._repo = hg.repository(r.ui, root, create=True)
184 self._repo = hg.repository(r.ui, root, create=True)
185 f = file(os.path.join(root, '.hg', 'hgrc'), 'w')
185 f = file(os.path.join(root, '.hg', 'hgrc'), 'w')
186 f.write('[paths]\ndefault = %s\n' % state[0])
186 f.write('[paths]\ndefault = %s\n' % state[0])
187 f.close()
187 f.close()
188 self._repo._subparent = r
188 self._repo._subparent = r
189 self._repo._subsource = state[0]
189 self._repo._subsource = state[0]
190
190
191 def dirty(self):
191 def dirty(self):
192 r = self._state[1]
192 r = self._state[1]
193 if r == '':
193 if r == '':
194 return True
194 return True
195 w = self._repo[None]
195 w = self._repo[None]
196 if w.p1() != self._repo[r]: # version checked out changed
196 if w.p1() != self._repo[r]: # version checked out changed
197 return True
197 return True
198 return w.dirty() # working directory changed
198 return w.dirty() # working directory changed
199
199
200 def commit(self, text, user, date):
200 def commit(self, text, user, date):
201 self._repo.ui.debug("committing subrepo %s\n" % self._path)
201 self._repo.ui.debug("committing subrepo %s\n" % self._path)
202 n = self._repo.commit(text, user, date)
202 n = self._repo.commit(text, user, date)
203 if not n:
203 if not n:
204 return self._repo['.'].hex() # different version checked out
204 return self._repo['.'].hex() # different version checked out
205 return node.hex(n)
205 return node.hex(n)
206
206
207 def remove(self):
207 def remove(self):
208 # we can't fully delete the repository as it may contain
208 # we can't fully delete the repository as it may contain
209 # local-only history
209 # local-only history
210 self._repo.ui.note(_('removing subrepo %s\n') % self._path)
210 self._repo.ui.note(_('removing subrepo %s\n') % self._path)
211 hg.clean(self._repo, node.nullid, False)
211 hg.clean(self._repo, node.nullid, False)
212
212
213 def _get(self, state):
213 def _get(self, state):
214 source, revision, kind = state
214 source, revision, kind = state
215 try:
215 try:
216 self._repo.lookup(revision)
216 self._repo.lookup(revision)
217 except error.RepoError:
217 except error.RepoError:
218 self._repo._subsource = source
218 self._repo._subsource = source
219 self._repo.ui.status(_('pulling subrepo %s\n') % self._path)
219 self._repo.ui.status(_('pulling subrepo %s\n') % self._path)
220 srcurl = _abssource(self._repo)
220 srcurl = _abssource(self._repo)
221 other = hg.repository(self._repo.ui, srcurl)
221 other = hg.repository(self._repo.ui, srcurl)
222 self._repo.pull(other)
222 self._repo.pull(other)
223
223
224 def get(self, state):
224 def get(self, state):
225 self._get(state)
225 self._get(state)
226 source, revision, kind = state
226 source, revision, kind = state
227 self._repo.ui.debug("getting subrepo %s\n" % self._path)
227 self._repo.ui.debug("getting subrepo %s\n" % self._path)
228 hg.clean(self._repo, revision, False)
228 hg.clean(self._repo, revision, False)
229
229
230 def merge(self, state):
230 def merge(self, state):
231 self._get(state)
231 self._get(state)
232 cur = self._repo['.']
232 cur = self._repo['.']
233 dst = self._repo[state[1]]
233 dst = self._repo[state[1]]
234 if dst.ancestor(cur) == cur:
234 if dst.ancestor(cur) == cur:
235 self._repo.ui.debug("updating subrepo %s\n" % self._path)
235 self._repo.ui.debug("updating subrepo %s\n" % self._path)
236 hg.update(self._repo, state[1])
236 hg.update(self._repo, state[1])
237 else:
237 else:
238 self._repo.ui.debug("merging subrepo %s\n" % self._path)
238 self._repo.ui.debug("merging subrepo %s\n" % self._path)
239 hg.merge(self._repo, state[1], remind=False)
239 hg.merge(self._repo, state[1], remind=False)
240
240
241 def push(self, force):
241 def push(self, force):
242 # push subrepos depth-first for coherent ordering
242 # push subrepos depth-first for coherent ordering
243 c = self._repo['']
243 c = self._repo['']
244 subs = c.substate # only repos that are committed
244 subs = c.substate # only repos that are committed
245 for s in sorted(subs):
245 for s in sorted(subs):
246 c.sub(s).push(force)
246 c.sub(s).push(force)
247
247
248 self._repo.ui.status(_('pushing subrepo %s\n') % self._path)
248 self._repo.ui.status(_('pushing subrepo %s\n') % self._path)
249 dsturl = _abssource(self._repo, True)
249 dsturl = _abssource(self._repo, True)
250 other = hg.repository(self._repo.ui, dsturl)
250 other = hg.repository(self._repo.ui, dsturl)
251 self._repo.push(other, force)
251 self._repo.push(other, force)
252
252
253 class svnsubrepo(object):
254 def __init__(self, ctx, path, state):
255 self._path = path
256 self._state = state
257 self._ctx = ctx
258 self._ui = ctx._repo.ui
259
260 def _svncommand(self, commands):
261 cmd = ['svn'] + commands + [self._path]
262 cmd = [util.shellquote(arg) for arg in cmd]
263 cmd = util.quotecommand(' '.join(cmd))
264 write, read, err = util.popen3(cmd)
265 retdata = read.read()
266 err = err.read().strip()
267 if err:
268 raise util.Abort(err)
269 return retdata
270
271 def _wcrev(self):
272 info = self._svncommand(['info'])
273 mat = re.search('Revision: ([\d]+)\n', info)
274 if not mat:
275 return 0
276 return mat.groups()[0]
277
278 def _url(self):
279 info = self._svncommand(['info'])
280 mat = re.search('URL: ([^\n]+)\n', info)
281 if not mat:
282 return 0
283 return mat.groups()[0]
284
285 def _wcclean(self):
286 status = self._svncommand(['status'])
287 status = '\n'.join([s for s in status.splitlines() if s[0] != '?'])
288 if status.strip():
289 return False
290 return True
291
292 def dirty(self):
293 if self._wcrev() == self._state[1] and self._wcclean():
294 return False
295 return True
296
297 def commit(self, text, user, date):
298 # user and date are out of our hands since svn is centralized
299 if self._wcclean():
300 return self._wcrev()
301 commitinfo = self._svncommand(['commit', '-m', text])
302 self._ui.status(commitinfo)
303 newrev = re.search('Committed revision ([\d]+).', commitinfo)
304 if not newrev:
305 raise util.Abort(commitinfo.splitlines()[-1])
306 newrev = newrev.groups()[0]
307 self._ui.status(self._svncommand(['update', '-r', newrev]))
308 return newrev
309
310 def remove(self):
311 if self.dirty():
312 self._repo.ui.warn('Not removing repo %s because'
313 'it has changes.\n' % self._path)
314 return
315 self._repo.ui.note('removing subrepo %s\n' % self._path)
316 shutil.rmtree(self._ctx.repo.join(self._path))
317
318 def get(self, state):
319 status = self._svncommand(['checkout', state[0], '--revision', state[1]])
320 if not re.search('Checked out revision [\d]+.', status):
321 raise util.Abort(status.splitlines()[-1])
322 self._ui.status(status)
323
324 def merge(self, state):
325 old = int(self._state[1])
326 new = int(state[1])
327 if new > old:
328 self.get(state)
329
330 def push(self, force):
331 # nothing for svn
332 pass
333
253 types = {
334 types = {
254 'hg': hgsubrepo,
335 'hg': hgsubrepo,
336 'svn': svnsubrepo,
255 }
337 }
General Comments 0
You need to be logged in to leave comments. Login now