Show More
@@ -1,391 +1,394 b'' | |||
|
1 | 1 | # hg.py - repository classes for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | from i18n import _ |
|
10 | 10 | from lock import release |
|
11 | 11 | import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo |
|
12 | 12 | import lock, util, extensions, error, encoding, node |
|
13 | 13 | import merge as _merge |
|
14 | 14 | import verify as _verify |
|
15 | 15 | import errno, os, shutil |
|
16 | 16 | |
|
17 | 17 | def _local(path): |
|
18 | 18 | return (os.path.isfile(util.drop_scheme('file', path)) and |
|
19 | 19 | bundlerepo or localrepo) |
|
20 | 20 | |
|
21 | 21 | def addbranchrevs(lrepo, repo, branches, revs): |
|
22 | 22 | if not branches: |
|
23 | 23 | return revs or None, revs and revs[0] or None |
|
24 | revs = revs and list(revs) or [] | |
|
25 | if not repo.capable('branchmap'): | |
|
26 | revs.extend(branches) | |
|
27 | return revs, revs[0] | |
|
24 | 28 | branchmap = repo.branchmap() |
|
25 | revs = revs and list(revs) or [] | |
|
26 | 29 | for branch in branches: |
|
27 | 30 | if branch == '.': |
|
28 | 31 | if not lrepo or not lrepo.local(): |
|
29 | 32 | raise util.Abort(_("dirstate branch not accessible")) |
|
30 | 33 | revs.append(lrepo.dirstate.branch()) |
|
31 | 34 | else: |
|
32 | 35 | butf8 = encoding.fromlocal(branch) |
|
33 | 36 | if butf8 in branchmap: |
|
34 | 37 | revs.extend(node.hex(r) for r in reversed(branchmap[butf8])) |
|
35 | 38 | else: |
|
36 | 39 | revs.append(branch) |
|
37 | 40 | return revs, revs[0] |
|
38 | 41 | |
|
39 | 42 | def parseurl(url, branches=None): |
|
40 | 43 | '''parse url#branch, returning url, branches+[branch]''' |
|
41 | 44 | |
|
42 | 45 | if '#' not in url: |
|
43 | 46 | return url, branches or [] |
|
44 | 47 | url, branch = url.split('#', 1) |
|
45 | 48 | return url, (branches or []) + [branch] |
|
46 | 49 | |
|
47 | 50 | schemes = { |
|
48 | 51 | 'bundle': bundlerepo, |
|
49 | 52 | 'file': _local, |
|
50 | 53 | 'http': httprepo, |
|
51 | 54 | 'https': httprepo, |
|
52 | 55 | 'ssh': sshrepo, |
|
53 | 56 | 'static-http': statichttprepo, |
|
54 | 57 | } |
|
55 | 58 | |
|
56 | 59 | def _lookup(path): |
|
57 | 60 | scheme = 'file' |
|
58 | 61 | if path: |
|
59 | 62 | c = path.find(':') |
|
60 | 63 | if c > 0: |
|
61 | 64 | scheme = path[:c] |
|
62 | 65 | thing = schemes.get(scheme) or schemes['file'] |
|
63 | 66 | try: |
|
64 | 67 | return thing(path) |
|
65 | 68 | except TypeError: |
|
66 | 69 | return thing |
|
67 | 70 | |
|
68 | 71 | def islocal(repo): |
|
69 | 72 | '''return true if repo or path is local''' |
|
70 | 73 | if isinstance(repo, str): |
|
71 | 74 | try: |
|
72 | 75 | return _lookup(repo).islocal(repo) |
|
73 | 76 | except AttributeError: |
|
74 | 77 | return False |
|
75 | 78 | return repo.local() |
|
76 | 79 | |
|
77 | 80 | def repository(ui, path='', create=False): |
|
78 | 81 | """return a repository object for the specified path""" |
|
79 | 82 | repo = _lookup(path).instance(ui, path, create) |
|
80 | 83 | ui = getattr(repo, "ui", ui) |
|
81 | 84 | for name, module in extensions.extensions(): |
|
82 | 85 | hook = getattr(module, 'reposetup', None) |
|
83 | 86 | if hook: |
|
84 | 87 | hook(ui, repo) |
|
85 | 88 | return repo |
|
86 | 89 | |
|
87 | 90 | def defaultdest(source): |
|
88 | 91 | '''return default destination of clone if none is given''' |
|
89 | 92 | return os.path.basename(os.path.normpath(source)) |
|
90 | 93 | |
|
91 | 94 | def localpath(path): |
|
92 | 95 | if path.startswith('file://localhost/'): |
|
93 | 96 | return path[16:] |
|
94 | 97 | if path.startswith('file://'): |
|
95 | 98 | return path[7:] |
|
96 | 99 | if path.startswith('file:'): |
|
97 | 100 | return path[5:] |
|
98 | 101 | return path |
|
99 | 102 | |
|
100 | 103 | def share(ui, source, dest=None, update=True): |
|
101 | 104 | '''create a shared repository''' |
|
102 | 105 | |
|
103 | 106 | if not islocal(source): |
|
104 | 107 | raise util.Abort(_('can only share local repositories')) |
|
105 | 108 | |
|
106 | 109 | if not dest: |
|
107 | 110 | dest = defaultdest(source) |
|
108 | 111 | else: |
|
109 | 112 | dest = ui.expandpath(dest) |
|
110 | 113 | |
|
111 | 114 | if isinstance(source, str): |
|
112 | 115 | origsource = ui.expandpath(source) |
|
113 | 116 | source, branches = parseurl(origsource) |
|
114 | 117 | srcrepo = repository(ui, source) |
|
115 | 118 | rev, checkout = addbranchrevs(srcrepo, srcrepo, branches, None) |
|
116 | 119 | else: |
|
117 | 120 | srcrepo = source |
|
118 | 121 | origsource = source = srcrepo.url() |
|
119 | 122 | checkout = None |
|
120 | 123 | |
|
121 | 124 | sharedpath = srcrepo.sharedpath # if our source is already sharing |
|
122 | 125 | |
|
123 | 126 | root = os.path.realpath(dest) |
|
124 | 127 | roothg = os.path.join(root, '.hg') |
|
125 | 128 | |
|
126 | 129 | if os.path.exists(roothg): |
|
127 | 130 | raise util.Abort(_('destination already exists')) |
|
128 | 131 | |
|
129 | 132 | if not os.path.isdir(root): |
|
130 | 133 | os.mkdir(root) |
|
131 | 134 | os.mkdir(roothg) |
|
132 | 135 | |
|
133 | 136 | requirements = '' |
|
134 | 137 | try: |
|
135 | 138 | requirements = srcrepo.opener('requires').read() |
|
136 | 139 | except IOError, inst: |
|
137 | 140 | if inst.errno != errno.ENOENT: |
|
138 | 141 | raise |
|
139 | 142 | |
|
140 | 143 | requirements += 'shared\n' |
|
141 | 144 | file(os.path.join(roothg, 'requires'), 'w').write(requirements) |
|
142 | 145 | file(os.path.join(roothg, 'sharedpath'), 'w').write(sharedpath) |
|
143 | 146 | |
|
144 | 147 | default = srcrepo.ui.config('paths', 'default') |
|
145 | 148 | if default: |
|
146 | 149 | f = file(os.path.join(roothg, 'hgrc'), 'w') |
|
147 | 150 | f.write('[paths]\ndefault = %s\n' % default) |
|
148 | 151 | f.close() |
|
149 | 152 | |
|
150 | 153 | r = repository(ui, root) |
|
151 | 154 | |
|
152 | 155 | if update: |
|
153 | 156 | r.ui.status(_("updating working directory\n")) |
|
154 | 157 | if update is not True: |
|
155 | 158 | checkout = update |
|
156 | 159 | for test in (checkout, 'default', 'tip'): |
|
157 | 160 | if test is None: |
|
158 | 161 | continue |
|
159 | 162 | try: |
|
160 | 163 | uprev = r.lookup(test) |
|
161 | 164 | break |
|
162 | 165 | except error.RepoLookupError: |
|
163 | 166 | continue |
|
164 | 167 | _update(r, uprev) |
|
165 | 168 | |
|
166 | 169 | def clone(ui, source, dest=None, pull=False, rev=None, update=True, |
|
167 | 170 | stream=False, branch=None): |
|
168 | 171 | """Make a copy of an existing repository. |
|
169 | 172 | |
|
170 | 173 | Create a copy of an existing repository in a new directory. The |
|
171 | 174 | source and destination are URLs, as passed to the repository |
|
172 | 175 | function. Returns a pair of repository objects, the source and |
|
173 | 176 | newly created destination. |
|
174 | 177 | |
|
175 | 178 | The location of the source is added to the new repository's |
|
176 | 179 | .hg/hgrc file, as the default to be used for future pulls and |
|
177 | 180 | pushes. |
|
178 | 181 | |
|
179 | 182 | If an exception is raised, the partly cloned/updated destination |
|
180 | 183 | repository will be deleted. |
|
181 | 184 | |
|
182 | 185 | Arguments: |
|
183 | 186 | |
|
184 | 187 | source: repository object or URL |
|
185 | 188 | |
|
186 | 189 | dest: URL of destination repository to create (defaults to base |
|
187 | 190 | name of source repository) |
|
188 | 191 | |
|
189 | 192 | pull: always pull from source repository, even in local case |
|
190 | 193 | |
|
191 | 194 | stream: stream raw data uncompressed from repository (fast over |
|
192 | 195 | LAN, slow over WAN) |
|
193 | 196 | |
|
194 | 197 | rev: revision to clone up to (implies pull=True) |
|
195 | 198 | |
|
196 | 199 | update: update working directory after clone completes, if |
|
197 | 200 | destination is local repository (True means update to default rev, |
|
198 | 201 | anything else is treated as a revision) |
|
199 | 202 | |
|
200 | 203 | branch: branches to clone |
|
201 | 204 | """ |
|
202 | 205 | |
|
203 | 206 | if isinstance(source, str): |
|
204 | 207 | origsource = ui.expandpath(source) |
|
205 | 208 | source, branch = parseurl(origsource, branch) |
|
206 | 209 | src_repo = repository(ui, source) |
|
207 | 210 | else: |
|
208 | 211 | src_repo = source |
|
209 | 212 | origsource = source = src_repo.url() |
|
210 | 213 | rev, checkout = addbranchrevs(src_repo, src_repo, branch, rev) |
|
211 | 214 | |
|
212 | 215 | if dest is None: |
|
213 | 216 | dest = defaultdest(source) |
|
214 | 217 | ui.status(_("destination directory: %s\n") % dest) |
|
215 | 218 | else: |
|
216 | 219 | dest = ui.expandpath(dest) |
|
217 | 220 | |
|
218 | 221 | dest = localpath(dest) |
|
219 | 222 | source = localpath(source) |
|
220 | 223 | |
|
221 | 224 | if os.path.exists(dest): |
|
222 | 225 | if not os.path.isdir(dest): |
|
223 | 226 | raise util.Abort(_("destination '%s' already exists") % dest) |
|
224 | 227 | elif os.listdir(dest): |
|
225 | 228 | raise util.Abort(_("destination '%s' is not empty") % dest) |
|
226 | 229 | |
|
227 | 230 | class DirCleanup(object): |
|
228 | 231 | def __init__(self, dir_): |
|
229 | 232 | self.rmtree = shutil.rmtree |
|
230 | 233 | self.dir_ = dir_ |
|
231 | 234 | def close(self): |
|
232 | 235 | self.dir_ = None |
|
233 | 236 | def cleanup(self): |
|
234 | 237 | if self.dir_: |
|
235 | 238 | self.rmtree(self.dir_, True) |
|
236 | 239 | |
|
237 | 240 | src_lock = dest_lock = dir_cleanup = None |
|
238 | 241 | try: |
|
239 | 242 | if islocal(dest): |
|
240 | 243 | dir_cleanup = DirCleanup(dest) |
|
241 | 244 | |
|
242 | 245 | abspath = origsource |
|
243 | 246 | copy = False |
|
244 | 247 | if src_repo.cancopy() and islocal(dest): |
|
245 | 248 | abspath = os.path.abspath(util.drop_scheme('file', origsource)) |
|
246 | 249 | copy = not pull and not rev |
|
247 | 250 | |
|
248 | 251 | if copy: |
|
249 | 252 | try: |
|
250 | 253 | # we use a lock here because if we race with commit, we |
|
251 | 254 | # can end up with extra data in the cloned revlogs that's |
|
252 | 255 | # not pointed to by changesets, thus causing verify to |
|
253 | 256 | # fail |
|
254 | 257 | src_lock = src_repo.lock(wait=False) |
|
255 | 258 | except error.LockError: |
|
256 | 259 | copy = False |
|
257 | 260 | |
|
258 | 261 | if copy: |
|
259 | 262 | src_repo.hook('preoutgoing', throw=True, source='clone') |
|
260 | 263 | hgdir = os.path.realpath(os.path.join(dest, ".hg")) |
|
261 | 264 | if not os.path.exists(dest): |
|
262 | 265 | os.mkdir(dest) |
|
263 | 266 | else: |
|
264 | 267 | # only clean up directories we create ourselves |
|
265 | 268 | dir_cleanup.dir_ = hgdir |
|
266 | 269 | try: |
|
267 | 270 | dest_path = hgdir |
|
268 | 271 | os.mkdir(dest_path) |
|
269 | 272 | except OSError, inst: |
|
270 | 273 | if inst.errno == errno.EEXIST: |
|
271 | 274 | dir_cleanup.close() |
|
272 | 275 | raise util.Abort(_("destination '%s' already exists") |
|
273 | 276 | % dest) |
|
274 | 277 | raise |
|
275 | 278 | |
|
276 | 279 | for f in src_repo.store.copylist(): |
|
277 | 280 | src = os.path.join(src_repo.sharedpath, f) |
|
278 | 281 | dst = os.path.join(dest_path, f) |
|
279 | 282 | dstbase = os.path.dirname(dst) |
|
280 | 283 | if dstbase and not os.path.exists(dstbase): |
|
281 | 284 | os.mkdir(dstbase) |
|
282 | 285 | if os.path.exists(src): |
|
283 | 286 | if dst.endswith('data'): |
|
284 | 287 | # lock to avoid premature writing to the target |
|
285 | 288 | dest_lock = lock.lock(os.path.join(dstbase, "lock")) |
|
286 | 289 | util.copyfiles(src, dst) |
|
287 | 290 | |
|
288 | 291 | # we need to re-init the repo after manually copying the data |
|
289 | 292 | # into it |
|
290 | 293 | dest_repo = repository(ui, dest) |
|
291 | 294 | src_repo.hook('outgoing', source='clone', node='0'*40) |
|
292 | 295 | else: |
|
293 | 296 | try: |
|
294 | 297 | dest_repo = repository(ui, dest, create=True) |
|
295 | 298 | except OSError, inst: |
|
296 | 299 | if inst.errno == errno.EEXIST: |
|
297 | 300 | dir_cleanup.close() |
|
298 | 301 | raise util.Abort(_("destination '%s' already exists") |
|
299 | 302 | % dest) |
|
300 | 303 | raise |
|
301 | 304 | |
|
302 | 305 | revs = None |
|
303 | 306 | if rev: |
|
304 | 307 | if 'lookup' not in src_repo.capabilities: |
|
305 | 308 | raise util.Abort(_("src repository does not support " |
|
306 | 309 | "revision lookup and so doesn't " |
|
307 | 310 | "support clone by revision")) |
|
308 | 311 | revs = [src_repo.lookup(r) for r in rev] |
|
309 | 312 | checkout = revs[0] |
|
310 | 313 | if dest_repo.local(): |
|
311 | 314 | dest_repo.clone(src_repo, heads=revs, stream=stream) |
|
312 | 315 | elif src_repo.local(): |
|
313 | 316 | src_repo.push(dest_repo, revs=revs) |
|
314 | 317 | else: |
|
315 | 318 | raise util.Abort(_("clone from remote to remote not supported")) |
|
316 | 319 | |
|
317 | 320 | if dir_cleanup: |
|
318 | 321 | dir_cleanup.close() |
|
319 | 322 | |
|
320 | 323 | if dest_repo.local(): |
|
321 | 324 | fp = dest_repo.opener("hgrc", "w", text=True) |
|
322 | 325 | fp.write("[paths]\n") |
|
323 | 326 | fp.write("default = %s\n" % abspath) |
|
324 | 327 | fp.close() |
|
325 | 328 | |
|
326 | 329 | dest_repo.ui.setconfig('paths', 'default', abspath) |
|
327 | 330 | |
|
328 | 331 | if update: |
|
329 | 332 | if update is not True: |
|
330 | 333 | checkout = update |
|
331 | 334 | if src_repo.local(): |
|
332 | 335 | checkout = src_repo.lookup(update) |
|
333 | 336 | for test in (checkout, 'default', 'tip'): |
|
334 | 337 | if test is None: |
|
335 | 338 | continue |
|
336 | 339 | try: |
|
337 | 340 | uprev = dest_repo.lookup(test) |
|
338 | 341 | break |
|
339 | 342 | except error.RepoLookupError: |
|
340 | 343 | continue |
|
341 | 344 | bn = dest_repo[uprev].branch() |
|
342 | 345 | dest_repo.ui.status(_("updating to branch %s\n") |
|
343 | 346 | % encoding.tolocal(bn)) |
|
344 | 347 | _update(dest_repo, uprev) |
|
345 | 348 | |
|
346 | 349 | return src_repo, dest_repo |
|
347 | 350 | finally: |
|
348 | 351 | release(src_lock, dest_lock) |
|
349 | 352 | if dir_cleanup is not None: |
|
350 | 353 | dir_cleanup.cleanup() |
|
351 | 354 | |
|
352 | 355 | def _showstats(repo, stats): |
|
353 | 356 | repo.ui.status(_("%d files updated, %d files merged, " |
|
354 | 357 | "%d files removed, %d files unresolved\n") % stats) |
|
355 | 358 | |
|
356 | 359 | def update(repo, node): |
|
357 | 360 | """update the working directory to node, merging linear changes""" |
|
358 | 361 | stats = _merge.update(repo, node, False, False, None) |
|
359 | 362 | _showstats(repo, stats) |
|
360 | 363 | if stats[3]: |
|
361 | 364 | repo.ui.status(_("use 'hg resolve' to retry unresolved file merges\n")) |
|
362 | 365 | return stats[3] > 0 |
|
363 | 366 | |
|
364 | 367 | # naming conflict in clone() |
|
365 | 368 | _update = update |
|
366 | 369 | |
|
367 | 370 | def clean(repo, node, show_stats=True): |
|
368 | 371 | """forcibly switch the working directory to node, clobbering changes""" |
|
369 | 372 | stats = _merge.update(repo, node, False, True, None) |
|
370 | 373 | if show_stats: |
|
371 | 374 | _showstats(repo, stats) |
|
372 | 375 | return stats[3] > 0 |
|
373 | 376 | |
|
374 | 377 | def merge(repo, node, force=None, remind=True): |
|
375 | 378 | """branch merge with node, resolving changes""" |
|
376 | 379 | stats = _merge.update(repo, node, True, force, False) |
|
377 | 380 | _showstats(repo, stats) |
|
378 | 381 | if stats[3]: |
|
379 | 382 | repo.ui.status(_("use 'hg resolve' to retry unresolved file merges " |
|
380 | 383 | "or 'hg update -C' to abandon\n")) |
|
381 | 384 | elif remind: |
|
382 | 385 | repo.ui.status(_("(branch merge, don't forget to commit)\n")) |
|
383 | 386 | return stats[3] > 0 |
|
384 | 387 | |
|
385 | 388 | def revert(repo, node, choose): |
|
386 | 389 | """revert changes to revision in node without updating dirstate""" |
|
387 | 390 | return _merge.update(repo, node, False, True, choose)[3] > 0 |
|
388 | 391 | |
|
389 | 392 | def verify(repo): |
|
390 | 393 | """verify the consistency of a repository""" |
|
391 | 394 | return _verify.verify(repo) |
General Comments 0
You need to be logged in to leave comments.
Login now