annotate mercurial/hg.py @ 11306:deaeb2d84d8a stable

fix encoding bug in 05ac42e56452
author Sune Foldager <cryo@cyanite.org>
date Tue, 08 Jun 2010 13:19:22 +0200
parents 05ac42e56452
children 3f1409082720
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
1 # hg.py - repository classes for mercurial
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
2 #
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4478
diff changeset
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2847
diff changeset
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
5 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8179
diff changeset
6 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10099
diff changeset
7 # GNU General Public License version 2 or any later version.
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
8
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
9 from i18n import _
8109
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7935
diff changeset
10 from lock import release
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3869
diff changeset
11 import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
12 import lock, util, extensions, error, encoding, node
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3869
diff changeset
13 import merge as _merge
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3869
diff changeset
14 import verify as _verify
8312
b87a50b7125c separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents: 8225
diff changeset
15 import errno, os, shutil
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
16
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
17 def _local(path):
11154
17031fea4e95 expand paths to local repository or bundle in appropriate classes
Alexander Solovyov <piranha@piranha.org.ua>
parents: 10728
diff changeset
18 path = util.expandpath(util.drop_scheme('file', path))
17031fea4e95 expand paths to local repository or bundle in appropriate classes
Alexander Solovyov <piranha@piranha.org.ua>
parents: 10728
diff changeset
19 return (os.path.isfile(path) and bundlerepo or localrepo)
2469
2e91ba371c4c hg.repository: make protocol table driven.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2431
diff changeset
20
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
21 def addbranchrevs(lrepo, repo, branches, revs):
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
22 if not branches:
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
23 return revs or None, revs and revs[0] or None
10380
ee72d89c0d9f addbranchrevs: fallback for older servers
Sune Foldager <cryo@cyanite.org>
parents: 10379
diff changeset
24 revs = revs and list(revs) or []
ee72d89c0d9f addbranchrevs: fallback for older servers
Sune Foldager <cryo@cyanite.org>
parents: 10379
diff changeset
25 if not repo.capable('branchmap'):
ee72d89c0d9f addbranchrevs: fallback for older servers
Sune Foldager <cryo@cyanite.org>
parents: 10379
diff changeset
26 revs.extend(branches)
ee72d89c0d9f addbranchrevs: fallback for older servers
Sune Foldager <cryo@cyanite.org>
parents: 10379
diff changeset
27 return revs, revs[0]
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
28 branchmap = repo.branchmap()
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
29 for branch in branches:
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
30 if branch == '.':
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
31 if not lrepo or not lrepo.local():
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
32 raise util.Abort(_("dirstate branch not accessible"))
11306
deaeb2d84d8a fix encoding bug in 05ac42e56452
Sune Foldager <cryo@cyanite.org>
parents: 11299
diff changeset
33 butf8 = lrepo.dirstate.branch()
deaeb2d84d8a fix encoding bug in 05ac42e56452
Sune Foldager <cryo@cyanite.org>
parents: 11299
diff changeset
34 branch = encoding.tolocal(butf8)
deaeb2d84d8a fix encoding bug in 05ac42e56452
Sune Foldager <cryo@cyanite.org>
parents: 11299
diff changeset
35 else:
deaeb2d84d8a fix encoding bug in 05ac42e56452
Sune Foldager <cryo@cyanite.org>
parents: 11299
diff changeset
36 butf8 = encoding.fromlocal(branch)
11299
05ac42e56452 push/pull: fix bug in "--branch ." handling
Sune Foldager <cryo@cyanite.org>
parents: 11255
diff changeset
37 if butf8 in branchmap:
05ac42e56452 push/pull: fix bug in "--branch ." handling
Sune Foldager <cryo@cyanite.org>
parents: 11255
diff changeset
38 revs.extend(node.hex(r) for r in reversed(branchmap[butf8]))
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
39 else:
11299
05ac42e56452 push/pull: fix bug in "--branch ." handling
Sune Foldager <cryo@cyanite.org>
parents: 11255
diff changeset
40 revs.append(branch)
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
41 return revs, revs[0]
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
42
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
43 def parseurl(url, branches=None):
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
44 '''parse url#branch, returning url, branches+[branch]'''
5177
92236732d5a1 move parseurl from cmdutil to hg
Matt Mackall <mpm@selenic.com>
parents: 4917
diff changeset
45
92236732d5a1 move parseurl from cmdutil to hg
Matt Mackall <mpm@selenic.com>
parents: 4917
diff changeset
46 if '#' not in url:
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
47 return url, branches or []
7045
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
48 url, branch = url.split('#', 1)
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
49 return url, (branches or []) + [branch]
5177
92236732d5a1 move parseurl from cmdutil to hg
Matt Mackall <mpm@selenic.com>
parents: 4917
diff changeset
50
2472
e6ec81a8feea make repo scheme table driven.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2431
diff changeset
51 schemes = {
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
52 'bundle': bundlerepo,
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
53 'file': _local,
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
54 'http': httprepo,
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
55 'https': httprepo,
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
56 'ssh': sshrepo,
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
57 'static-http': statichttprepo,
4853
bf10a03a6b24 Removed deprecated hg:// and old-http:// protocols (issue406)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4818
diff changeset
58 }
2469
2e91ba371c4c hg.repository: make protocol table driven.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2431
diff changeset
59
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
60 def _lookup(path):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
61 scheme = 'file'
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
62 if path:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
63 c = path.find(':')
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
64 if c > 0:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
65 scheme = path[:c]
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
66 thing = schemes.get(scheme) or schemes['file']
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
67 try:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
68 return thing(path)
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
69 except TypeError:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
70 return thing
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents: 2774
diff changeset
71
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
72 def islocal(repo):
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
73 '''return true if repo or path is local'''
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
74 if isinstance(repo, str):
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
75 try:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
76 return _lookup(repo).islocal(repo)
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
77 except AttributeError:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2738
diff changeset
78 return False
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
79 return repo.local()
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
80
3195
705e30c0a230 Make hg.repository work with no path argument
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
81 def repository(ui, path='', create=False):
2774
8cd3e19bf4a5 Add a doc string
Matt Mackall <mpm@selenic.com>
parents: 2768
diff changeset
82 """return a repository object for the specified path"""
2847
2ff57e3113a4 call reposetup functions of extension modules whenever repo created
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2830
diff changeset
83 repo = _lookup(path).instance(ui, path, create)
4074
0f9381cf9723 Try to pass repo.ui to reposetup hooks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3869
diff changeset
84 ui = getattr(repo, "ui", ui)
5192
60acf1432ee0 Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5187
diff changeset
85 for name, module in extensions.extensions():
60acf1432ee0 Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5187
diff changeset
86 hook = getattr(module, 'reposetup', None)
60acf1432ee0 Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5187
diff changeset
87 if hook:
60acf1432ee0 Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5187
diff changeset
88 hook(ui, repo)
2847
2ff57e3113a4 call reposetup functions of extension modules whenever repo created
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2830
diff changeset
89 return repo
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
90
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
91 def defaultdest(source):
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
92 '''return default destination of clone if none is given'''
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
93 return os.path.basename(os.path.normpath(source))
2774
8cd3e19bf4a5 Add a doc string
Matt Mackall <mpm@selenic.com>
parents: 2768
diff changeset
94
6524
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
95 def localpath(path):
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
96 if path.startswith('file://localhost/'):
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
97 return path[16:]
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
98 if path.startswith('file://'):
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
99 return path[7:]
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
100 if path.startswith('file:'):
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
101 return path[5:]
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
102 return path
23ef198efce9 Hoist localpath to the top level
Bryan O'Sullivan <bos@serpentine.com>
parents: 6518
diff changeset
103
8807
8bf6eb68ddaf share: allow dest to default to the basename of source
Matt Mackall <mpm@selenic.com>
parents: 8800
diff changeset
104 def share(ui, source, dest=None, update=True):
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
105 '''create a shared repository'''
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
106
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
107 if not islocal(source):
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
108 raise util.Abort(_('can only share local repositories'))
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
109
8807
8bf6eb68ddaf share: allow dest to default to the basename of source
Matt Mackall <mpm@selenic.com>
parents: 8800
diff changeset
110 if not dest:
10099
f5e46dfb38c7 share: use defaultdest to compute unspecified destination
Brendan Cully <brendan@kublai.com>
parents: 9984
diff changeset
111 dest = defaultdest(source)
9344
eb6af02307ec Merge with i18n-stable
Matt Mackall <mpm@selenic.com>
parents: 9245
diff changeset
112 else:
eb6af02307ec Merge with i18n-stable
Matt Mackall <mpm@selenic.com>
parents: 9245
diff changeset
113 dest = ui.expandpath(dest)
8807
8bf6eb68ddaf share: allow dest to default to the basename of source
Matt Mackall <mpm@selenic.com>
parents: 8800
diff changeset
114
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
115 if isinstance(source, str):
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
116 origsource = ui.expandpath(source)
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
117 source, branches = parseurl(origsource)
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
118 srcrepo = repository(ui, source)
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
119 rev, checkout = addbranchrevs(srcrepo, srcrepo, branches, None)
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
120 else:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
121 srcrepo = source
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
122 origsource = source = srcrepo.url()
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
123 checkout = None
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
124
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
125 sharedpath = srcrepo.sharedpath # if our source is already sharing
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
126
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
127 root = os.path.realpath(dest)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
128 roothg = os.path.join(root, '.hg')
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
129
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
130 if os.path.exists(roothg):
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
131 raise util.Abort(_('destination already exists'))
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
132
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
133 if not os.path.isdir(root):
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
134 os.mkdir(root)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
135 os.mkdir(roothg)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
136
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
137 requirements = ''
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
138 try:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
139 requirements = srcrepo.opener('requires').read()
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
140 except IOError, inst:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
141 if inst.errno != errno.ENOENT:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
142 raise
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
143
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
144 requirements += 'shared\n'
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
145 file(os.path.join(roothg, 'requires'), 'w').write(requirements)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
146 file(os.path.join(roothg, 'sharedpath'), 'w').write(sharedpath)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
147
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
148 default = srcrepo.ui.config('paths', 'default')
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
149 if default:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
150 f = file(os.path.join(roothg, 'hgrc'), 'w')
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
151 f.write('[paths]\ndefault = %s\n' % default)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
152 f.close()
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
153
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
154 r = repository(ui, root)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
155
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
156 if update:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
157 r.ui.status(_("updating working directory\n"))
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
158 if update is not True:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
159 checkout = update
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
160 for test in (checkout, 'default', 'tip'):
9423
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
161 if test is None:
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
162 continue
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
163 try:
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
164 uprev = r.lookup(test)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
165 break
9423
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
166 except error.RepoLookupError:
8800
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
167 continue
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
168 _update(r, uprev)
971e38a9344b add helper function to create shared repos
Matt Mackall <mpm@selenic.com>
parents: 8649
diff changeset
169
2613
479e26afa10f clone: do not make streaming default. add --stream option instead.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
170 def clone(ui, source, dest=None, pull=False, rev=None, update=True,
10379
a78bfaf988e1 add -b/--branch option to clone, bundle, incoming, outgoing, pull, push
Sune Foldager <cryo@cyanite.org>
parents: 10365
diff changeset
171 stream=False, branch=None):
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
172 """Make a copy of an existing repository.
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
173
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
174 Create a copy of an existing repository in a new directory. The
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
175 source and destination are URLs, as passed to the repository
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
176 function. Returns a pair of repository objects, the source and
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
177 newly created destination.
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
178
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
179 The location of the source is added to the new repository's
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
180 .hg/hgrc file, as the default to be used for future pulls and
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
181 pushes.
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
182
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
183 If an exception is raised, the partly cloned/updated destination
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
184 repository will be deleted.
2600
c4325f0a9b91 clean up trailing white space.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2597
diff changeset
185
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
186 Arguments:
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
187
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
188 source: repository object or URL
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
189
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
190 dest: URL of destination repository to create (defaults to base
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
191 name of source repository)
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
192
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
193 pull: always pull from source repository, even in local case
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
194
2621
5a5852a417b1 clone: disable stream support on server side by default.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2613
diff changeset
195 stream: stream raw data uncompressed from repository (fast over
5a5852a417b1 clone: disable stream support on server side by default.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2613
diff changeset
196 LAN, slow over WAN)
2613
479e26afa10f clone: do not make streaming default. add --stream option instead.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
197
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
198 rev: revision to clone up to (implies pull=True)
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
199
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
200 update: update working directory after clone completes, if
6526
cfeeac24fc1e repo: add rjoin method
Bryan O'Sullivan <bos@serpentine.com>
parents: 6525
diff changeset
201 destination is local repository (True means update to default rev,
cfeeac24fc1e repo: add rjoin method
Bryan O'Sullivan <bos@serpentine.com>
parents: 6525
diff changeset
202 anything else is treated as a revision)
10379
a78bfaf988e1 add -b/--branch option to clone, bundle, incoming, outgoing, pull, push
Sune Foldager <cryo@cyanite.org>
parents: 10365
diff changeset
203
a78bfaf988e1 add -b/--branch option to clone, bundle, incoming, outgoing, pull, push
Sune Foldager <cryo@cyanite.org>
parents: 10365
diff changeset
204 branch: branches to clone
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
205 """
4478
b2b55acbacdd Add support for url#id syntax
Matt Mackall <mpm@selenic.com>
parents: 4477
diff changeset
206
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
207 if isinstance(source, str):
6089
28054773438c clone: make things work when source is a repo object
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6088
diff changeset
208 origsource = ui.expandpath(source)
10379
a78bfaf988e1 add -b/--branch option to clone, bundle, incoming, outgoing, pull, push
Sune Foldager <cryo@cyanite.org>
parents: 10365
diff changeset
209 source, branch = parseurl(origsource, branch)
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
210 src_repo = repository(ui, source)
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
211 else:
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
212 src_repo = source
10728
4a70178f9bde fix bug in hg.clone when passing in a repository object
Sune Foldager <cryo@cyanite.org>
parents: 10380
diff changeset
213 branch = None
6089
28054773438c clone: make things work when source is a repo object
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6088
diff changeset
214 origsource = source = src_repo.url()
10365
d757bc0c7865 interpret repo#name url syntax as branch instead of revision
Sune Foldager <cryo@cyanite.org>
parents: 10358
diff changeset
215 rev, checkout = addbranchrevs(src_repo, src_repo, branch, rev)
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
216
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
217 if dest is None:
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
218 dest = defaultdest(source)
3841
aaeb7f5d1052 Show the destionation for clone if not specified manually.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3791
diff changeset
219 ui.status(_("destination directory: %s\n") % dest)
9344
eb6af02307ec Merge with i18n-stable
Matt Mackall <mpm@selenic.com>
parents: 9245
diff changeset
220 else:
eb6af02307ec Merge with i18n-stable
Matt Mackall <mpm@selenic.com>
parents: 9245
diff changeset
221 dest = ui.expandpath(dest)
2719
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
222
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
223 dest = localpath(dest)
532809ba1db5 hg.py: add islocal() and defaultdest() functions, refactor
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2631
diff changeset
224 source = localpath(source)
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
225
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
226 if os.path.exists(dest):
7927
a218ba5f60df allow clone into existing but empty directories
Steve Borho <steve@borho.org>
parents: 7821
diff changeset
227 if not os.path.isdir(dest):
a218ba5f60df allow clone into existing but empty directories
Steve Borho <steve@borho.org>
parents: 7821
diff changeset
228 raise util.Abort(_("destination '%s' already exists") % dest)
a218ba5f60df allow clone into existing but empty directories
Steve Borho <steve@borho.org>
parents: 7821
diff changeset
229 elif os.listdir(dest):
a218ba5f60df allow clone into existing but empty directories
Steve Borho <steve@borho.org>
parents: 7821
diff changeset
230 raise util.Abort(_("destination '%s' is not empty") % dest)
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
231
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
232 class DirCleanup(object):
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
233 def __init__(self, dir_):
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
234 self.rmtree = shutil.rmtree
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
235 self.dir_ = dir_
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
236 def close(self):
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
237 self.dir_ = None
8110
b616f328af9f switch dircleanup in mercurial.hg.clone from gc based to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 8109
diff changeset
238 def cleanup(self):
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
239 if self.dir_:
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
240 self.rmtree(self.dir_, True)
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
241
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
242 src_lock = dest_lock = dir_cleanup = None
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
243 try:
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
244 if islocal(dest):
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
245 dir_cleanup = DirCleanup(dest)
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
246
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
247 abspath = origsource
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
248 copy = False
6315
5c96a4bca66b clone: use cancopy
Matt Mackall <mpm@selenic.com>
parents: 6217
diff changeset
249 if src_repo.cancopy() and islocal(dest):
5248
5517aa5aafb0 Merge with crew-stable
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5192 5225
diff changeset
250 abspath = os.path.abspath(util.drop_scheme('file', origsource))
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
251 copy = not pull and not rev
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
252
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
253 if copy:
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
254 try:
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
255 # we use a lock here because if we race with commit, we
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
256 # can end up with extra data in the cloned revlogs that's
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
257 # not pointed to by changesets, thus causing verify to
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
258 # fail
8649
2c097e22492c clone: fall back to pull source repo cannot be locked, 937ee88da3ef was a noop
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8417
diff changeset
259 src_lock = src_repo.lock(wait=False)
7640
7197812e8d44 error: move lock errors
Matt Mackall <mpm@selenic.com>
parents: 7546
diff changeset
260 except error.LockError:
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
261 copy = False
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
262
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
263 if copy:
8907
e9ef409e6399 Add (pre)outgoing hooks for local clones.
Fred Wulff <frew@cs.stanford.edu>
parents: 8814
diff changeset
264 src_repo.hook('preoutgoing', throw=True, source='clone')
7935
39566bb99a9c on clone failure, only remove directories we created
Steve Borho <steve@borho.org>
parents: 7927
diff changeset
265 hgdir = os.path.realpath(os.path.join(dest, ".hg"))
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
266 if not os.path.exists(dest):
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
267 os.mkdir(dest)
7935
39566bb99a9c on clone failure, only remove directories we created
Steve Borho <steve@borho.org>
parents: 7927
diff changeset
268 else:
39566bb99a9c on clone failure, only remove directories we created
Steve Borho <steve@borho.org>
parents: 7927
diff changeset
269 # only clean up directories we create ourselves
39566bb99a9c on clone failure, only remove directories we created
Steve Borho <steve@borho.org>
parents: 7927
diff changeset
270 dir_cleanup.dir_ = hgdir
5569
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
271 try:
7935
39566bb99a9c on clone failure, only remove directories we created
Steve Borho <steve@borho.org>
parents: 7927
diff changeset
272 dest_path = hgdir
5569
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
273 os.mkdir(dest_path)
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
274 except OSError, inst:
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
275 if inst.errno == errno.EEXIST:
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
276 dir_cleanup.close()
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
277 raise util.Abort(_("destination '%s' already exists")
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
278 % dest)
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
279 raise
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
280
11255
e4dbaa40096d clone: save hardlink state of util.copyfiles()
Adrian Buehlmann <adrian@cadifra.com>
parents: 11154
diff changeset
281 hardlink = None
6903
0642d9d7ec80 clone: get a list of files to clone from store
Matt Mackall <mpm@selenic.com>
parents: 6526
diff changeset
282 for f in src_repo.store.copylist():
9984
439d7ea6fe3a share: fix interaction with clone
Matt Mackall <mpm@selenic.com>
parents: 9788
diff changeset
283 src = os.path.join(src_repo.sharedpath, f)
6944
7e5f3480c45b fix regression on empty repo cloning introduced by 0642d9d7ec80
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6903
diff changeset
284 dst = os.path.join(dest_path, f)
7e5f3480c45b fix regression on empty repo cloning introduced by 0642d9d7ec80
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6903
diff changeset
285 dstbase = os.path.dirname(dst)
7e5f3480c45b fix regression on empty repo cloning introduced by 0642d9d7ec80
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6903
diff changeset
286 if dstbase and not os.path.exists(dstbase):
7e5f3480c45b fix regression on empty repo cloning introduced by 0642d9d7ec80
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6903
diff changeset
287 os.mkdir(dstbase)
6903
0642d9d7ec80 clone: get a list of files to clone from store
Matt Mackall <mpm@selenic.com>
parents: 6526
diff changeset
288 if os.path.exists(src):
0642d9d7ec80 clone: get a list of files to clone from store
Matt Mackall <mpm@selenic.com>
parents: 6526
diff changeset
289 if dst.endswith('data'):
0642d9d7ec80 clone: get a list of files to clone from store
Matt Mackall <mpm@selenic.com>
parents: 6526
diff changeset
290 # lock to avoid premature writing to the target
0642d9d7ec80 clone: get a list of files to clone from store
Matt Mackall <mpm@selenic.com>
parents: 6526
diff changeset
291 dest_lock = lock.lock(os.path.join(dstbase, "lock"))
11255
e4dbaa40096d clone: save hardlink state of util.copyfiles()
Adrian Buehlmann <adrian@cadifra.com>
parents: 11154
diff changeset
292 hardlink = util.copyfiles(src, dst, hardlink)
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
293
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
294 # we need to re-init the repo after manually copying the data
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
295 # into it
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
296 dest_repo = repository(ui, dest)
8907
e9ef409e6399 Add (pre)outgoing hooks for local clones.
Fred Wulff <frew@cs.stanford.edu>
parents: 8814
diff changeset
297 src_repo.hook('outgoing', source='clone', node='0'*40)
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
298 else:
5569
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
299 try:
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
300 dest_repo = repository(ui, dest, create=True)
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
301 except OSError, inst:
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
302 if inst.errno == errno.EEXIST:
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
303 dir_cleanup.close()
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
304 raise util.Abort(_("destination '%s' already exists")
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
305 % dest)
9e209193f18d clone: fix race with same target directory (issue716)
Matt Mackall <mpm@selenic.com>
parents: 5277
diff changeset
306 raise
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
307
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
308 revs = None
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
309 if rev:
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
310 if 'lookup' not in src_repo.capabilities:
9171
a458b9bf4f3a hg: better wrapping of string literal
Martin Geisler <mg@lazybytes.net>
parents: 8907
diff changeset
311 raise util.Abort(_("src repository does not support "
a458b9bf4f3a hg: better wrapping of string literal
Martin Geisler <mg@lazybytes.net>
parents: 8907
diff changeset
312 "revision lookup and so doesn't "
a458b9bf4f3a hg: better wrapping of string literal
Martin Geisler <mg@lazybytes.net>
parents: 8907
diff changeset
313 "support clone by revision"))
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
314 revs = [src_repo.lookup(r) for r in rev]
8417
39cf453da958 clone: try updating to the actual changeset specified in options
Brett Carter <brett@rdnzl.net>
parents: 8312
diff changeset
315 checkout = revs[0]
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
316 if dest_repo.local():
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
317 dest_repo.clone(src_repo, heads=revs, stream=stream)
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
318 elif src_repo.local():
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
319 src_repo.push(dest_repo, revs=revs)
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
320 else:
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
321 raise util.Abort(_("clone from remote to remote not supported"))
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
322
5186
0d5d03844927 merge with -stable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4917 5185
diff changeset
323 if dir_cleanup:
0d5d03844927 merge with -stable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4917 5185
diff changeset
324 dir_cleanup.close()
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
325
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
326 if dest_repo.local():
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
327 fp = dest_repo.opener("hgrc", "w", text=True)
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
328 fp.write("[paths]\n")
8179
0f3b8404051b clone: config escaping no longer needed
Matt Mackall <mpm@selenic.com>
parents: 8174
diff changeset
329 fp.write("default = %s\n" % abspath)
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
330 fp.close()
5185
156f4c8a12aa clone: do not delete the target if only the update fails
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4635
diff changeset
331
8814
ab668c92a036 subrepo: add update/merge logic
Matt Mackall <mpm@selenic.com>
parents: 8807
diff changeset
332 dest_repo.ui.setconfig('paths', 'default', abspath)
ab668c92a036 subrepo: add update/merge logic
Matt Mackall <mpm@selenic.com>
parents: 8807
diff changeset
333
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
334 if update:
6526
cfeeac24fc1e repo: add rjoin method
Bryan O'Sullivan <bos@serpentine.com>
parents: 6525
diff changeset
335 if update is not True:
cfeeac24fc1e repo: add rjoin method
Bryan O'Sullivan <bos@serpentine.com>
parents: 6525
diff changeset
336 checkout = update
9714
2f1ab7f77ddc clone: add option -u/--updaterev
Adrian Buehlmann <adrian@cadifra.com>
parents: 9611
diff changeset
337 if src_repo.local():
2f1ab7f77ddc clone: add option -u/--updaterev
Adrian Buehlmann <adrian@cadifra.com>
parents: 9611
diff changeset
338 checkout = src_repo.lookup(update)
7045
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
339 for test in (checkout, 'default', 'tip'):
9423
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
340 if test is None:
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
341 continue
5248
5517aa5aafb0 Merge with crew-stable
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5192 5225
diff changeset
342 try:
7045
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
343 uprev = dest_repo.lookup(test)
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
344 break
9423
1444a42f6052 Make distinct lookup error for localrepo.lookup
Matt Mackall <mpm@selenic.com>
parents: 9344
diff changeset
345 except error.RepoLookupError:
7045
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
346 continue
9611
a3d73b3e1f8a hg.clone: report branch name on update
Adrian Buehlmann <adrian@cadifra.com>
parents: 9468
diff changeset
347 bn = dest_repo[uprev].branch()
9788
75520c24feda Branch name printed since a3d73b3e1f8a now in local encoding.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 9714
diff changeset
348 dest_repo.ui.status(_("updating to branch %s\n")
75520c24feda Branch name printed since a3d73b3e1f8a now in local encoding.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 9714
diff changeset
349 % encoding.tolocal(bn))
7045
f82938c87b92 clone: honor -r even when pulling named branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7044
diff changeset
350 _update(dest_repo, uprev)
2597
5ba8be56fa8f clone: move code into hg module. make doc better.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2595
diff changeset
351
4915
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
352 return src_repo, dest_repo
97b734fb9c6f Use try/finally pattern to cleanup locks and transactions
Matt Mackall <mpm@selenic.com>
parents: 4853
diff changeset
353 finally:
8109
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7935
diff changeset
354 release(src_lock, dest_lock)
8110
b616f328af9f switch dircleanup in mercurial.hg.clone from gc based to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 8109
diff changeset
355 if dir_cleanup is not None:
b616f328af9f switch dircleanup in mercurial.hg.clone from gc based to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 8109
diff changeset
356 dir_cleanup.cleanup()
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents: 2774
diff changeset
357
3316
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
358 def _showstats(repo, stats):
9454
dafadd7ff07e hg: avoid combining translated strings
Martin Geisler <mg@lazybytes.net>
parents: 9425
diff changeset
359 repo.ui.status(_("%d files updated, %d files merged, "
dafadd7ff07e hg: avoid combining translated strings
Martin Geisler <mg@lazybytes.net>
parents: 9425
diff changeset
360 "%d files removed, %d files unresolved\n") % stats)
3316
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
361
2808
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
362 def update(repo, node):
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
363 """update the working directory to node, merging linear changes"""
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
364 stats = _merge.update(repo, node, False, False, None)
3316
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
365 _showstats(repo, stats)
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
366 if stats[3]:
6518
92ccccb55ba3 resolve: new command
Matt Mackall <mpm@selenic.com>
parents: 6338
diff changeset
367 repo.ui.status(_("use 'hg resolve' to retry unresolved file merges\n"))
5635
0c608a8d9c5f merge: make return codes more sensible
Matt Mackall <mpm@selenic.com>
parents: 5569
diff changeset
368 return stats[3] > 0
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents: 2774
diff changeset
369
7546
c7f48414f3ad add a comment about the need of hg._update()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7280
diff changeset
370 # naming conflict in clone()
c7f48414f3ad add a comment about the need of hg._update()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7280
diff changeset
371 _update = update
c7f48414f3ad add a comment about the need of hg._update()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7280
diff changeset
372
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
373 def clean(repo, node, show_stats=True):
2808
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
374 """forcibly switch the working directory to node, clobbering changes"""
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
375 stats = _merge.update(repo, node, False, True, None)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
376 if show_stats:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
377 _showstats(repo, stats)
5635
0c608a8d9c5f merge: make return codes more sensible
Matt Mackall <mpm@selenic.com>
parents: 5569
diff changeset
378 return stats[3] > 0
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents: 2774
diff changeset
379
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
380 def merge(repo, node, force=None, remind=True):
2808
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
381 """branch merge with node, resolving changes"""
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
382 stats = _merge.update(repo, node, True, force, False)
3316
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
383 _showstats(repo, stats)
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
384 if stats[3]:
7821
9fe7e6acf525 merge: better error messages to lead users to hg update --clean to abandon merges.
Augie Fackler <durin42@gmail.com>
parents: 7640
diff changeset
385 repo.ui.status(_("use 'hg resolve' to retry unresolved file merges "
9468
b2310903c462 hg: say 'hg update' instead of 'hg up' in merge status message
Stuart W Marks <smarks@smarks.org>
parents: 9454
diff changeset
386 "or 'hg update -C' to abandon\n"))
3316
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
387 elif remind:
39fd6e82ea38 merge: pull user messages out to hg.py
Matt Mackall <mpm@selenic.com>
parents: 3195
diff changeset
388 repo.ui.status(_("(branch merge, don't forget to commit)\n"))
5635
0c608a8d9c5f merge: make return codes more sensible
Matt Mackall <mpm@selenic.com>
parents: 5569
diff changeset
389 return stats[3] > 0
2808
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
390
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4915
diff changeset
391 def revert(repo, node, choose):
2808
30f59f4a327e Introduce update helper functions: update, merge, clean, and revert
Matt Mackall <mpm@selenic.com>
parents: 2778
diff changeset
392 """revert changes to revision in node without updating dirstate"""
5635
0c608a8d9c5f merge: make return codes more sensible
Matt Mackall <mpm@selenic.com>
parents: 5569
diff changeset
393 return _merge.update(repo, node, False, True, choose)[3] > 0
2778
fdc232d8a193 Move repo.verify
Matt Mackall <mpm@selenic.com>
parents: 2775
diff changeset
394
fdc232d8a193 Move repo.verify
Matt Mackall <mpm@selenic.com>
parents: 2775
diff changeset
395 def verify(repo):
fdc232d8a193 Move repo.verify
Matt Mackall <mpm@selenic.com>
parents: 2775
diff changeset
396 """verify the consistency of a repository"""
fdc232d8a193 Move repo.verify
Matt Mackall <mpm@selenic.com>
parents: 2775
diff changeset
397 return _verify.verify(repo)