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