##// END OF EJS Templates
subrepo: use hg.repository instead of creating localrepo directly...
Abderrahim Kitouni -
r9092:9aebeea7 default
parent child Browse files
Show More
@@ -1,193 +1,192 b''
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 localrepo = hg = None
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()
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 for s, l in s1.items():
56 56 a = sa.get(s, nullstate)
57 57 if s in s2:
58 58 r = s2[s]
59 59 if l == r or r == a: # no change or local is newer
60 60 sm[s] = l
61 61 continue
62 62 elif l == a: # other side changed
63 63 wctx.sub(s).get(r)
64 64 sm[s] = r
65 65 elif l[0] != r[0]: # sources differ
66 66 if repo.ui.promptchoice(
67 67 _(' subrepository sources for %s differ\n'
68 68 'use (l)ocal source (%s) or (r)emote source (%s)?')
69 69 % (s, l[0], r[0]),
70 70 (_('&Local'), _('&Remote')), 0):
71 71 wctx.sub(s).get(r)
72 72 sm[s] = r
73 73 elif l[1] == a[1]: # local side is unchanged
74 74 wctx.sub(s).get(r)
75 75 sm[s] = r
76 76 else:
77 77 wctx.sub(s).merge(r)
78 78 sm[s] = l
79 79 elif l == a: # remote removed, local unchanged
80 80 wctx.sub(s).remove()
81 81 else:
82 82 if repo.ui.promptchoice(
83 83 _(' local changed subrepository %s which remote removed\n'
84 84 'use (c)hanged version or (d)elete?') % s,
85 85 (_('&Changed'), _('&Delete')), 0):
86 86 wctx.sub(s).remove()
87 87
88 88 for s, r in s2.items():
89 89 if s in s1:
90 90 continue
91 91 elif s not in sa:
92 92 wctx.sub(s).get(r)
93 93 sm[s] = r
94 94 elif r != sa[s]:
95 95 if repo.ui.promptchoice(
96 96 _(' remote changed subrepository %s which local removed\n'
97 97 'use (c)hanged version or (d)elete?') % s,
98 98 (_('&Changed'), _('&Delete')), 0) == 0:
99 99 wctx.sub(s).get(r)
100 100 sm[s] = r
101 101
102 102 # record merged .hgsubstate
103 103 writestate(repo, sm)
104 104
105 105 def _abssource(repo, push=False):
106 106 if hasattr(repo, '_subparent'):
107 107 source = repo._subsource
108 108 if source.startswith('/') or '://' in source:
109 109 return source
110 110 return os.path.join(_abssource(repo._subparent), repo._subsource)
111 111 if push and repo.ui.config('paths', 'default-push'):
112 112 return repo.ui.config('paths', 'default-push', repo.root)
113 113 return repo.ui.config('paths', 'default', repo.root)
114 114
115 115 def subrepo(ctx, path):
116 116 # subrepo inherently violates our import layering rules
117 117 # because it wants to make repo objects from deep inside the stack
118 118 # so we manually delay the circular imports to not break
119 119 # scripts that don't use our demand-loading
120 global localrepo, hg
121 import localrepo as l, hg as h
122 localrepo = l
120 global hg
121 import hg as h
123 122 hg = h
124 123
125 124 util.path_auditor(ctx._repo.root)(path)
126 125 state = ctx.substate.get(path, nullstate)
127 126 if state[0].startswith('['): # future expansion
128 127 raise error.Abort('unknown subrepo source %s' % state[0])
129 128 return hgsubrepo(ctx, path, state)
130 129
131 130 class hgsubrepo(object):
132 131 def __init__(self, ctx, path, state):
133 132 self._path = path
134 133 self._state = state
135 134 r = ctx._repo
136 135 root = r.wjoin(path)
137 136 if os.path.exists(os.path.join(root, '.hg')):
138 self._repo = localrepo.localrepository(r.ui, root)
137 self._repo = hg.repository(r.ui, root)
139 138 else:
140 139 util.makedirs(root)
141 self._repo = localrepo.localrepository(r.ui, root, create=True)
140 self._repo = hg.repository(r.ui, root, create=True)
142 141 self._repo._subparent = r
143 142 self._repo._subsource = state[0]
144 143
145 144 def dirty(self):
146 145 r = self._state[1]
147 146 if r == '':
148 147 return True
149 148 w = self._repo[None]
150 149 if w.p1() != self._repo[r]: # version checked out changed
151 150 return True
152 151 return w.dirty() # working directory changed
153 152
154 153 def commit(self, text, user, date):
155 154 n = self._repo.commit(text, user, date)
156 155 if not n:
157 156 return self._repo['.'].hex() # different version checked out
158 157 return node.hex(n)
159 158
160 159 def remove(self):
161 160 # we can't fully delete the repository as it may contain
162 161 # local-only history
163 162 self._repo.ui.note(_('removing subrepo %s\n') % self._path)
164 163 hg.clean(self._repo, node.nullid, False)
165 164
166 165 def get(self, state):
167 166 source, revision = state
168 167 try:
169 168 self._repo.lookup(revision)
170 169 except error.RepoError:
171 170 self._repo._subsource = source
172 171 self._repo.ui.status(_('pulling subrepo %s\n') % self._path)
173 172 srcurl = _abssource(self._repo)
174 173 other = hg.repository(self._repo.ui, srcurl)
175 174 self._repo.pull(other)
176 175
177 176 hg.clean(self._repo, revision, False)
178 177
179 178 def merge(self, state):
180 179 hg.merge(self._repo, state[1], remind=False)
181 180
182 181 def push(self, force):
183 182 # push subrepos depth-first for coherent ordering
184 183 c = self._repo['']
185 184 subs = c.substate # only repos that are committed
186 185 for s in sorted(subs):
187 186 c.sub(s).push(force)
188 187
189 188 self._repo.ui.status(_('pushing subrepo %s\n') % self._path)
190 189 dsturl = _abssource(self._repo, True)
191 190 other = hg.repository(self._repo.ui, dsturl)
192 191 self._repo.push(other, force)
193 192
General Comments 0
You need to be logged in to leave comments. Login now