Show More
@@ -1,244 +1,245 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 | 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 | p.parse(f, ctx[f].data(), sections, remap, read) |
|
20 | 20 | else: |
|
21 | 21 | raise util.Abort(_("subrepo spec file %s not found") % f) |
|
22 | 22 | |
|
23 | 23 | if '.hgsub' in ctx: |
|
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 | # working context, merging context, ancestor context | |
|
48 | 49 | if mctx == actx: # backwards? |
|
49 | 50 | actx = wctx.p1() |
|
50 | 51 | s1 = wctx.substate |
|
51 | 52 | s2 = mctx.substate |
|
52 | 53 | sa = actx.substate |
|
53 | 54 | sm = {} |
|
54 | 55 | |
|
55 | 56 | repo.ui.debug("subrepo merge %s %s %s\n" % (wctx, mctx, actx)) |
|
56 | 57 | |
|
57 | 58 | def debug(s, msg, r=""): |
|
58 | 59 | if r: |
|
59 | 60 | r = "%s:%s" % r |
|
60 | 61 | repo.ui.debug(" subrepo %s: %s %s\n" % (s, msg, r)) |
|
61 | 62 | |
|
62 | 63 | for s, l in s1.items(): |
|
63 | 64 | if wctx != actx and wctx.sub(s).dirty(): |
|
64 | 65 | l = (l[0], l[1] + "+") |
|
65 | 66 | a = sa.get(s, nullstate) |
|
66 | 67 | if s in s2: |
|
67 | 68 | r = s2[s] |
|
68 | 69 | if l == r or r == a: # no change or local is newer |
|
69 | 70 | sm[s] = l |
|
70 | 71 | continue |
|
71 | 72 | elif l == a: # other side changed |
|
72 | 73 | debug(s, "other changed, get", r) |
|
73 | 74 | wctx.sub(s).get(r) |
|
74 | 75 | sm[s] = r |
|
75 | 76 | elif l[0] != r[0]: # sources differ |
|
76 | 77 | if repo.ui.promptchoice( |
|
77 | 78 | _(' subrepository sources for %s differ\n' |
|
78 | 79 | 'use (l)ocal source (%s) or (r)emote source (%s)?') |
|
79 | 80 | % (s, l[0], r[0]), |
|
80 | 81 | (_('&Local'), _('&Remote')), 0): |
|
81 | 82 | debug(s, "prompt changed, get", r) |
|
82 | 83 | wctx.sub(s).get(r) |
|
83 | 84 | sm[s] = r |
|
84 | 85 | elif l[1] == a[1]: # local side is unchanged |
|
85 | 86 | debug(s, "other side changed, get", r) |
|
86 | 87 | wctx.sub(s).get(r) |
|
87 | 88 | sm[s] = r |
|
88 | 89 | else: |
|
89 | 90 | debug(s, "both sides changed, merge with", r) |
|
90 | 91 | wctx.sub(s).merge(r) |
|
91 | 92 | sm[s] = l |
|
92 | 93 | elif l == a: # remote removed, local unchanged |
|
93 | 94 | debug(s, "remote removed, remove") |
|
94 | 95 | wctx.sub(s).remove() |
|
95 | 96 | else: |
|
96 | 97 | if repo.ui.promptchoice( |
|
97 | 98 | _(' local changed subrepository %s which remote removed\n' |
|
98 | 99 | 'use (c)hanged version or (d)elete?') % s, |
|
99 | 100 | (_('&Changed'), _('&Delete')), 0): |
|
100 | 101 | debug(s, "prompt remove") |
|
101 | 102 | wctx.sub(s).remove() |
|
102 | 103 | |
|
103 | 104 | for s, r in s2.items(): |
|
104 | 105 | if s in s1: |
|
105 | 106 | continue |
|
106 | 107 | elif s not in sa: |
|
107 | 108 | debug(s, "remote added, get", r) |
|
108 |
|
|
|
109 | mctx.sub(s).get(r) | |
|
109 | 110 | sm[s] = r |
|
110 | 111 | elif r != sa[s]: |
|
111 | 112 | if repo.ui.promptchoice( |
|
112 | 113 | _(' remote changed subrepository %s which local removed\n' |
|
113 | 114 | 'use (c)hanged version or (d)elete?') % s, |
|
114 | 115 | (_('&Changed'), _('&Delete')), 0) == 0: |
|
115 | 116 | debug(s, "prompt recreate", r) |
|
116 | 117 | wctx.sub(s).get(r) |
|
117 | 118 | sm[s] = r |
|
118 | 119 | |
|
119 | 120 | # record merged .hgsubstate |
|
120 | 121 | writestate(repo, sm) |
|
121 | 122 | |
|
122 | 123 | def _abssource(repo, push=False): |
|
123 | 124 | if hasattr(repo, '_subparent'): |
|
124 | 125 | source = repo._subsource |
|
125 | 126 | if source.startswith('/') or '://' in source: |
|
126 | 127 | return source |
|
127 | 128 | parent = _abssource(repo._subparent) |
|
128 | 129 | if '://' in parent: |
|
129 | 130 | if parent[-1] == '/': |
|
130 | 131 | parent = parent[:-1] |
|
131 | 132 | return parent + '/' + source |
|
132 | 133 | return os.path.join(parent, repo._subsource) |
|
133 | 134 | if push and repo.ui.config('paths', 'default-push'): |
|
134 | 135 | return repo.ui.config('paths', 'default-push', repo.root) |
|
135 | 136 | return repo.ui.config('paths', 'default', repo.root) |
|
136 | 137 | |
|
137 | 138 | def subrepo(ctx, path): |
|
138 | 139 | # subrepo inherently violates our import layering rules |
|
139 | 140 | # because it wants to make repo objects from deep inside the stack |
|
140 | 141 | # so we manually delay the circular imports to not break |
|
141 | 142 | # scripts that don't use our demand-loading |
|
142 | 143 | global hg |
|
143 | 144 | import hg as h |
|
144 | 145 | hg = h |
|
145 | 146 | |
|
146 | 147 | util.path_auditor(ctx._repo.root)(path) |
|
147 | 148 | state = ctx.substate.get(path, nullstate) |
|
148 | 149 | if state[0].startswith('['): # future expansion |
|
149 | 150 | raise error.Abort('unknown subrepo source %s' % state[0]) |
|
150 | 151 | return hgsubrepo(ctx, path, state) |
|
151 | 152 | |
|
152 | 153 | # subrepo classes need to implement the following methods: |
|
153 | 154 | # __init__(self, ctx, path, state) |
|
154 | 155 | # dirty(self): returns true if the dirstate of the subrepo |
|
155 | 156 | # does not match current stored state |
|
156 | 157 | # commit(self, text, user, date): commit the current changes |
|
157 | 158 | # to the subrepo with the given log message. Use given |
|
158 | 159 | # user and date if possible. Return the new state of the subrepo. |
|
159 | 160 | # remove(self): remove the subrepo (should verify the dirstate |
|
160 | 161 | # is not dirty first) |
|
161 | 162 | # get(self, state): run whatever commands are needed to put the |
|
162 | 163 | # subrepo into this state |
|
163 | 164 | # merge(self, state): merge currently-saved state with the new state. |
|
164 | 165 | # push(self, force): perform whatever action is analagous to 'hg push' |
|
165 | 166 | # This may be a no-op on some systems. |
|
166 | 167 | |
|
167 | 168 | class hgsubrepo(object): |
|
168 | 169 | def __init__(self, ctx, path, state): |
|
169 | 170 | self._path = path |
|
170 | 171 | self._state = state |
|
171 | 172 | r = ctx._repo |
|
172 | 173 | root = r.wjoin(path) |
|
173 | 174 | if os.path.exists(os.path.join(root, '.hg')): |
|
174 | 175 | self._repo = hg.repository(r.ui, root) |
|
175 | 176 | else: |
|
176 | 177 | util.makedirs(root) |
|
177 | 178 | self._repo = hg.repository(r.ui, root, create=True) |
|
178 | 179 | f = file(os.path.join(root, '.hg', 'hgrc'), 'w') |
|
179 | 180 | f.write('[paths]\ndefault = %s\n' % state[0]) |
|
180 | 181 | f.close() |
|
181 | 182 | self._repo._subparent = r |
|
182 | 183 | self._repo._subsource = state[0] |
|
183 | 184 | |
|
184 | 185 | def dirty(self): |
|
185 | 186 | r = self._state[1] |
|
186 | 187 | if r == '': |
|
187 | 188 | return True |
|
188 | 189 | w = self._repo[None] |
|
189 | 190 | if w.p1() != self._repo[r]: # version checked out changed |
|
190 | 191 | return True |
|
191 | 192 | return w.dirty() # working directory changed |
|
192 | 193 | |
|
193 | 194 | def commit(self, text, user, date): |
|
194 | 195 | self._repo.ui.debug("committing subrepo %s\n" % self._path) |
|
195 | 196 | n = self._repo.commit(text, user, date) |
|
196 | 197 | if not n: |
|
197 | 198 | return self._repo['.'].hex() # different version checked out |
|
198 | 199 | return node.hex(n) |
|
199 | 200 | |
|
200 | 201 | def remove(self): |
|
201 | 202 | # we can't fully delete the repository as it may contain |
|
202 | 203 | # local-only history |
|
203 | 204 | self._repo.ui.note(_('removing subrepo %s\n') % self._path) |
|
204 | 205 | hg.clean(self._repo, node.nullid, False) |
|
205 | 206 | |
|
206 | 207 | def _get(self, state): |
|
207 | 208 | source, revision = state |
|
208 | 209 | try: |
|
209 | 210 | self._repo.lookup(revision) |
|
210 | 211 | except error.RepoError: |
|
211 | 212 | self._repo._subsource = source |
|
212 | 213 | self._repo.ui.status(_('pulling subrepo %s\n') % self._path) |
|
213 | 214 | srcurl = _abssource(self._repo) |
|
214 | 215 | other = hg.repository(self._repo.ui, srcurl) |
|
215 | 216 | self._repo.pull(other) |
|
216 | 217 | |
|
217 | 218 | def get(self, state): |
|
218 | 219 | self._get(state) |
|
219 | 220 | source, revision = state |
|
220 | 221 | self._repo.ui.debug("getting subrepo %s\n" % self._path) |
|
221 | 222 | hg.clean(self._repo, revision, False) |
|
222 | 223 | |
|
223 | 224 | def merge(self, state): |
|
224 | 225 | self._get(state) |
|
225 | 226 | cur = self._repo['.'] |
|
226 | 227 | dst = self._repo[state[1]] |
|
227 | 228 | if dst.ancestor(cur) == cur: |
|
228 | 229 | self._repo.ui.debug("updating subrepo %s\n" % self._path) |
|
229 | 230 | hg.update(self._repo, state[1]) |
|
230 | 231 | else: |
|
231 | 232 | self._repo.ui.debug("merging subrepo %s\n" % self._path) |
|
232 | 233 | hg.merge(self._repo, state[1], remind=False) |
|
233 | 234 | |
|
234 | 235 | def push(self, force): |
|
235 | 236 | # push subrepos depth-first for coherent ordering |
|
236 | 237 | c = self._repo[''] |
|
237 | 238 | subs = c.substate # only repos that are committed |
|
238 | 239 | for s in sorted(subs): |
|
239 | 240 | c.sub(s).push(force) |
|
240 | 241 | |
|
241 | 242 | self._repo.ui.status(_('pushing subrepo %s\n') % self._path) |
|
242 | 243 | dsturl = _abssource(self._repo, True) |
|
243 | 244 | other = hg.repository(self._repo.ui, dsturl) |
|
244 | 245 | self._repo.push(other, force) |
General Comments 0
You need to be logged in to leave comments.
Login now