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