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