Show More
@@ -1,201 +1,201 | |||
|
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 | revision, path = l.split() | |
|
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 | 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 | parent = _abssource(repo._subparent) |
|
111 | 111 | if '://' in parent: |
|
112 | 112 | if parent[-1] == '/': |
|
113 | 113 | parent = parent[:-1] |
|
114 | 114 | return parent + '/' + source |
|
115 | 115 | return os.path.join(parent, repo._subsource) |
|
116 | 116 | if push and repo.ui.config('paths', 'default-push'): |
|
117 | 117 | return repo.ui.config('paths', 'default-push', repo.root) |
|
118 | 118 | return repo.ui.config('paths', 'default', repo.root) |
|
119 | 119 | |
|
120 | 120 | def subrepo(ctx, path): |
|
121 | 121 | # subrepo inherently violates our import layering rules |
|
122 | 122 | # because it wants to make repo objects from deep inside the stack |
|
123 | 123 | # so we manually delay the circular imports to not break |
|
124 | 124 | # scripts that don't use our demand-loading |
|
125 | 125 | global hg |
|
126 | 126 | import hg as h |
|
127 | 127 | hg = h |
|
128 | 128 | |
|
129 | 129 | util.path_auditor(ctx._repo.root)(path) |
|
130 | 130 | state = ctx.substate.get(path, nullstate) |
|
131 | 131 | if state[0].startswith('['): # future expansion |
|
132 | 132 | raise error.Abort('unknown subrepo source %s' % state[0]) |
|
133 | 133 | return hgsubrepo(ctx, path, state) |
|
134 | 134 | |
|
135 | 135 | class hgsubrepo(object): |
|
136 | 136 | def __init__(self, ctx, path, state): |
|
137 | 137 | self._path = path |
|
138 | 138 | self._state = state |
|
139 | 139 | r = ctx._repo |
|
140 | 140 | root = r.wjoin(path) |
|
141 | 141 | if os.path.exists(os.path.join(root, '.hg')): |
|
142 | 142 | self._repo = hg.repository(r.ui, root) |
|
143 | 143 | else: |
|
144 | 144 | util.makedirs(root) |
|
145 | 145 | self._repo = hg.repository(r.ui, root, create=True) |
|
146 | 146 | self._repo._subparent = r |
|
147 | 147 | self._repo._subsource = state[0] |
|
148 | 148 | |
|
149 | 149 | def dirty(self): |
|
150 | 150 | r = self._state[1] |
|
151 | 151 | if r == '': |
|
152 | 152 | return True |
|
153 | 153 | w = self._repo[None] |
|
154 | 154 | if w.p1() != self._repo[r]: # version checked out changed |
|
155 | 155 | return True |
|
156 | 156 | return w.dirty() # working directory changed |
|
157 | 157 | |
|
158 | 158 | def commit(self, text, user, date): |
|
159 | 159 | n = self._repo.commit(text, user, date) |
|
160 | 160 | if not n: |
|
161 | 161 | return self._repo['.'].hex() # different version checked out |
|
162 | 162 | return node.hex(n) |
|
163 | 163 | |
|
164 | 164 | def remove(self): |
|
165 | 165 | # we can't fully delete the repository as it may contain |
|
166 | 166 | # local-only history |
|
167 | 167 | self._repo.ui.note(_('removing subrepo %s\n') % self._path) |
|
168 | 168 | hg.clean(self._repo, node.nullid, False) |
|
169 | 169 | |
|
170 | 170 | def _get(self, state): |
|
171 | 171 | source, revision = state |
|
172 | 172 | try: |
|
173 | 173 | self._repo.lookup(revision) |
|
174 | 174 | except error.RepoError: |
|
175 | 175 | self._repo._subsource = source |
|
176 | 176 | self._repo.ui.status(_('pulling subrepo %s\n') % self._path) |
|
177 | 177 | srcurl = _abssource(self._repo) |
|
178 | 178 | other = hg.repository(self._repo.ui, srcurl) |
|
179 | 179 | self._repo.pull(other) |
|
180 | 180 | |
|
181 | 181 | def get(self, state): |
|
182 | 182 | self._get(state) |
|
183 | 183 | source, revision = state |
|
184 | 184 | hg.clean(self._repo, revision, False) |
|
185 | 185 | |
|
186 | 186 | def merge(self, state): |
|
187 | 187 | self._get(state) |
|
188 | 188 | hg.merge(self._repo, state[1], remind=False) |
|
189 | 189 | |
|
190 | 190 | def push(self, force): |
|
191 | 191 | # push subrepos depth-first for coherent ordering |
|
192 | 192 | c = self._repo[''] |
|
193 | 193 | subs = c.substate # only repos that are committed |
|
194 | 194 | for s in sorted(subs): |
|
195 | 195 | c.sub(s).push(force) |
|
196 | 196 | |
|
197 | 197 | self._repo.ui.status(_('pushing subrepo %s\n') % self._path) |
|
198 | 198 | dsturl = _abssource(self._repo, True) |
|
199 | 199 | other = hg.repository(self._repo.ui, dsturl) |
|
200 | 200 | self._repo.push(other, force) |
|
201 | 201 |
General Comments 0
You need to be logged in to leave comments.
Login now