##// END OF EJS Templates
subrepo: do a linear update when appropriate
Matt Mackall -
r9781:eccc8aac default
parent child Browse files
Show More
@@ -1,215 +1,220 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
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 = ('', '')
13 nullstate = ('', '')
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 try:
19 try:
20 p.parse(f, ctx[f].data(), sections, remap)
20 p.parse(f, ctx[f].data(), sections, remap)
21 except IOError, err:
21 except IOError, err:
22 if err.errno != errno.ENOENT:
22 if err.errno != errno.ENOENT:
23 raise
23 raise
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 state[path] = (src, rev.get(path, ''))
38 state[path] = (src, rev.get(path, ''))
39
39
40 return state
40 return state
41
41
42 def writestate(repo, state):
42 def writestate(repo, state):
43 repo.wwrite('.hgsubstate',
43 repo.wwrite('.hgsubstate',
44 ''.join(['%s %s\n' % (state[s][1], s)
44 ''.join(['%s %s\n' % (state[s][1], s)
45 for s in sorted(state)]), '')
45 for s in sorted(state)]), '')
46
46
47 def submerge(repo, wctx, mctx, actx):
47 def submerge(repo, wctx, mctx, actx):
48 if mctx == actx: # backwards?
48 if mctx == actx: # backwards?
49 actx = wctx.p1()
49 actx = wctx.p1()
50 s1 = wctx.substate
50 s1 = wctx.substate
51 s2 = mctx.substate
51 s2 = mctx.substate
52 sa = actx.substate
52 sa = actx.substate
53 sm = {}
53 sm = {}
54
54
55 def debug(s, msg, r=""):
55 def debug(s, msg, r=""):
56 if r:
56 if r:
57 r = "%s:%s" % r
57 r = "%s:%s" % r
58 repo.ui.debug(_(" subrepo %s: %s %s\n") % (s, msg, r))
58 repo.ui.debug(_(" subrepo %s: %s %s\n") % (s, msg, r))
59
59
60 for s, l in s1.items():
60 for s, l in s1.items():
61 if wctx.sub(s).dirty():
61 if wctx.sub(s).dirty():
62 l = (l[0], l[1] + "+")
62 l = (l[0], l[1] + "+")
63 a = sa.get(s, nullstate)
63 a = sa.get(s, nullstate)
64 if s in s2:
64 if s in s2:
65 r = s2[s]
65 r = s2[s]
66 if l == r or r == a: # no change or local is newer
66 if l == r or r == a: # no change or local is newer
67 sm[s] = l
67 sm[s] = l
68 continue
68 continue
69 elif l == a: # other side changed
69 elif l == a: # other side changed
70 debug(s, _("other changed, get"), r)
70 debug(s, _("other changed, get"), r)
71 wctx.sub(s).get(r)
71 wctx.sub(s).get(r)
72 sm[s] = r
72 sm[s] = r
73 elif l[0] != r[0]: # sources differ
73 elif l[0] != r[0]: # sources differ
74 if repo.ui.promptchoice(
74 if repo.ui.promptchoice(
75 _(' subrepository sources for %s differ\n'
75 _(' subrepository sources for %s differ\n'
76 'use (l)ocal source (%s) or (r)emote source (%s)?')
76 'use (l)ocal source (%s) or (r)emote source (%s)?')
77 % (s, l[0], r[0]),
77 % (s, l[0], r[0]),
78 (_('&Local'), _('&Remote')), 0):
78 (_('&Local'), _('&Remote')), 0):
79 debug(s, _("prompt changed, get"), r)
79 debug(s, _("prompt 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[1] == a[1]: # local side is unchanged
82 elif l[1] == a[1]: # local side is unchanged
83 debug(s, _("other side changed, get"), r)
83 debug(s, _("other side changed, get"), r)
84 wctx.sub(s).get(r)
84 wctx.sub(s).get(r)
85 sm[s] = r
85 sm[s] = r
86 else:
86 else:
87 debug(s, _("both sides changed, merge with"), r)
87 debug(s, _("both sides changed, merge with"), r)
88 wctx.sub(s).merge(r)
88 wctx.sub(s).merge(r)
89 sm[s] = l
89 sm[s] = l
90 elif l == a: # remote removed, local unchanged
90 elif l == a: # remote removed, local unchanged
91 debug(s, _("remote removed, remove"))
91 debug(s, _("remote removed, remove"))
92 wctx.sub(s).remove()
92 wctx.sub(s).remove()
93 else:
93 else:
94 if repo.ui.promptchoice(
94 if repo.ui.promptchoice(
95 _(' local changed subrepository %s which remote removed\n'
95 _(' local changed subrepository %s which remote removed\n'
96 'use (c)hanged version or (d)elete?') % s,
96 'use (c)hanged version or (d)elete?') % s,
97 (_('&Changed'), _('&Delete')), 0):
97 (_('&Changed'), _('&Delete')), 0):
98 debug(s, _("prompt remove"))
98 debug(s, _("prompt remove"))
99 wctx.sub(s).remove()
99 wctx.sub(s).remove()
100
100
101 for s, r in s2.items():
101 for s, r in s2.items():
102 if s in s1:
102 if s in s1:
103 continue
103 continue
104 elif s not in sa:
104 elif s not in sa:
105 debug(s, _("remote added, get"), r)
105 debug(s, _("remote added, get"), r)
106 wctx.sub(s).get(r)
106 wctx.sub(s).get(r)
107 sm[s] = r
107 sm[s] = r
108 elif r != sa[s]:
108 elif r != sa[s]:
109 if repo.ui.promptchoice(
109 if repo.ui.promptchoice(
110 _(' remote changed subrepository %s which local removed\n'
110 _(' remote changed subrepository %s which local removed\n'
111 'use (c)hanged version or (d)elete?') % s,
111 'use (c)hanged version or (d)elete?') % s,
112 (_('&Changed'), _('&Delete')), 0) == 0:
112 (_('&Changed'), _('&Delete')), 0) == 0:
113 debug(s, _("prompt recreate"), r)
113 debug(s, _("prompt recreate"), r)
114 wctx.sub(s).get(r)
114 wctx.sub(s).get(r)
115 sm[s] = r
115 sm[s] = r
116
116
117 # record merged .hgsubstate
117 # record merged .hgsubstate
118 writestate(repo, sm)
118 writestate(repo, sm)
119
119
120 def _abssource(repo, push=False):
120 def _abssource(repo, push=False):
121 if hasattr(repo, '_subparent'):
121 if hasattr(repo, '_subparent'):
122 source = repo._subsource
122 source = repo._subsource
123 if source.startswith('/') or '://' in source:
123 if source.startswith('/') or '://' in source:
124 return source
124 return source
125 parent = _abssource(repo._subparent)
125 parent = _abssource(repo._subparent)
126 if '://' in parent:
126 if '://' in parent:
127 if parent[-1] == '/':
127 if parent[-1] == '/':
128 parent = parent[:-1]
128 parent = parent[:-1]
129 return parent + '/' + source
129 return parent + '/' + source
130 return os.path.join(parent, repo._subsource)
130 return os.path.join(parent, repo._subsource)
131 if push and repo.ui.config('paths', 'default-push'):
131 if push and repo.ui.config('paths', 'default-push'):
132 return repo.ui.config('paths', 'default-push', repo.root)
132 return repo.ui.config('paths', 'default-push', repo.root)
133 return repo.ui.config('paths', 'default', repo.root)
133 return repo.ui.config('paths', 'default', repo.root)
134
134
135 def subrepo(ctx, path):
135 def subrepo(ctx, path):
136 # subrepo inherently violates our import layering rules
136 # subrepo inherently violates our import layering rules
137 # because it wants to make repo objects from deep inside the stack
137 # because it wants to make repo objects from deep inside the stack
138 # so we manually delay the circular imports to not break
138 # so we manually delay the circular imports to not break
139 # scripts that don't use our demand-loading
139 # scripts that don't use our demand-loading
140 global hg
140 global hg
141 import hg as h
141 import hg as h
142 hg = h
142 hg = h
143
143
144 util.path_auditor(ctx._repo.root)(path)
144 util.path_auditor(ctx._repo.root)(path)
145 state = ctx.substate.get(path, nullstate)
145 state = ctx.substate.get(path, nullstate)
146 if state[0].startswith('['): # future expansion
146 if state[0].startswith('['): # future expansion
147 raise error.Abort('unknown subrepo source %s' % state[0])
147 raise error.Abort('unknown subrepo source %s' % state[0])
148 return hgsubrepo(ctx, path, state)
148 return hgsubrepo(ctx, path, state)
149
149
150 class hgsubrepo(object):
150 class hgsubrepo(object):
151 def __init__(self, ctx, path, state):
151 def __init__(self, ctx, path, state):
152 self._path = path
152 self._path = path
153 self._state = state
153 self._state = state
154 r = ctx._repo
154 r = ctx._repo
155 root = r.wjoin(path)
155 root = r.wjoin(path)
156 if os.path.exists(os.path.join(root, '.hg')):
156 if os.path.exists(os.path.join(root, '.hg')):
157 self._repo = hg.repository(r.ui, root)
157 self._repo = hg.repository(r.ui, root)
158 else:
158 else:
159 util.makedirs(root)
159 util.makedirs(root)
160 self._repo = hg.repository(r.ui, root, create=True)
160 self._repo = hg.repository(r.ui, root, create=True)
161 self._repo._subparent = r
161 self._repo._subparent = r
162 self._repo._subsource = state[0]
162 self._repo._subsource = state[0]
163
163
164 def dirty(self):
164 def dirty(self):
165 r = self._state[1]
165 r = self._state[1]
166 if r == '':
166 if r == '':
167 return True
167 return True
168 w = self._repo[None]
168 w = self._repo[None]
169 if w.p1() != self._repo[r]: # version checked out changed
169 if w.p1() != self._repo[r]: # version checked out changed
170 return True
170 return True
171 return w.dirty() # working directory changed
171 return w.dirty() # working directory changed
172
172
173 def commit(self, text, user, date):
173 def commit(self, text, user, date):
174 n = self._repo.commit(text, user, date)
174 n = self._repo.commit(text, user, date)
175 if not n:
175 if not n:
176 return self._repo['.'].hex() # different version checked out
176 return self._repo['.'].hex() # different version checked out
177 return node.hex(n)
177 return node.hex(n)
178
178
179 def remove(self):
179 def remove(self):
180 # we can't fully delete the repository as it may contain
180 # we can't fully delete the repository as it may contain
181 # local-only history
181 # local-only history
182 self._repo.ui.note(_('removing subrepo %s\n') % self._path)
182 self._repo.ui.note(_('removing subrepo %s\n') % self._path)
183 hg.clean(self._repo, node.nullid, False)
183 hg.clean(self._repo, node.nullid, False)
184
184
185 def _get(self, state):
185 def _get(self, state):
186 source, revision = state
186 source, revision = state
187 try:
187 try:
188 self._repo.lookup(revision)
188 self._repo.lookup(revision)
189 except error.RepoError:
189 except error.RepoError:
190 self._repo._subsource = source
190 self._repo._subsource = source
191 self._repo.ui.status(_('pulling subrepo %s\n') % self._path)
191 self._repo.ui.status(_('pulling subrepo %s\n') % self._path)
192 srcurl = _abssource(self._repo)
192 srcurl = _abssource(self._repo)
193 other = hg.repository(self._repo.ui, srcurl)
193 other = hg.repository(self._repo.ui, srcurl)
194 self._repo.pull(other)
194 self._repo.pull(other)
195
195
196 def get(self, state):
196 def get(self, state):
197 self._get(state)
197 self._get(state)
198 source, revision = state
198 source, revision = state
199 hg.clean(self._repo, revision, False)
199 hg.clean(self._repo, revision, False)
200
200
201 def merge(self, state):
201 def merge(self, state):
202 self._get(state)
202 self._get(state)
203 hg.merge(self._repo, state[1], remind=False)
203 cur = self._repo['.']
204 dst = self._repo[state[1]]
205 if dst.ancestor(cur) == cur:
206 hg.update(self._repo, state[1])
207 else:
208 hg.merge(self._repo, state[1], remind=False)
204
209
205 def push(self, force):
210 def push(self, force):
206 # push subrepos depth-first for coherent ordering
211 # push subrepos depth-first for coherent ordering
207 c = self._repo['']
212 c = self._repo['']
208 subs = c.substate # only repos that are committed
213 subs = c.substate # only repos that are committed
209 for s in sorted(subs):
214 for s in sorted(subs):
210 c.sub(s).push(force)
215 c.sub(s).push(force)
211
216
212 self._repo.ui.status(_('pushing subrepo %s\n') % self._path)
217 self._repo.ui.status(_('pushing subrepo %s\n') % self._path)
213 dsturl = _abssource(self._repo, True)
218 dsturl = _abssource(self._repo, True)
214 other = hg.repository(self._repo.ui, dsturl)
219 other = hg.repository(self._repo.ui, dsturl)
215 self._repo.push(other, force)
220 self._repo.push(other, force)
General Comments 0
You need to be logged in to leave comments. Login now