##// END OF EJS Templates
Escape '%' when writing to hgrc (issue1199)...
Benoit Boissinot -
r7044:e51c0f41 default
parent child Browse files
Show More
@@ -1,312 +1,313 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
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 from i18n import _
10 10 import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo
11 11 import errno, lock, os, shutil, util, extensions
12 12 import merge as _merge
13 13 import verify as _verify
14 14
15 15 def _local(path):
16 16 return (os.path.isfile(util.drop_scheme('file', path)) and
17 17 bundlerepo or localrepo)
18 18
19 19 def parseurl(url, revs):
20 20 '''parse url#branch, returning url, branch + revs'''
21 21
22 22 if '#' not in url:
23 23 return url, (revs or None), None
24 24
25 25 url, rev = url.split('#', 1)
26 26 return url, revs + [rev], rev
27 27
28 28 schemes = {
29 29 'bundle': bundlerepo,
30 30 'file': _local,
31 31 'http': httprepo,
32 32 'https': httprepo,
33 33 'ssh': sshrepo,
34 34 'static-http': statichttprepo,
35 35 }
36 36
37 37 def _lookup(path):
38 38 scheme = 'file'
39 39 if path:
40 40 c = path.find(':')
41 41 if c > 0:
42 42 scheme = path[:c]
43 43 thing = schemes.get(scheme) or schemes['file']
44 44 try:
45 45 return thing(path)
46 46 except TypeError:
47 47 return thing
48 48
49 49 def islocal(repo):
50 50 '''return true if repo or path is local'''
51 51 if isinstance(repo, str):
52 52 try:
53 53 return _lookup(repo).islocal(repo)
54 54 except AttributeError:
55 55 return False
56 56 return repo.local()
57 57
58 58 def repository(ui, path='', create=False):
59 59 """return a repository object for the specified path"""
60 60 repo = _lookup(path).instance(ui, path, create)
61 61 ui = getattr(repo, "ui", ui)
62 62 for name, module in extensions.extensions():
63 63 hook = getattr(module, 'reposetup', None)
64 64 if hook:
65 65 hook(ui, repo)
66 66 return repo
67 67
68 68 def defaultdest(source):
69 69 '''return default destination of clone if none is given'''
70 70 return os.path.basename(os.path.normpath(source))
71 71
72 72 def clone(ui, source, dest=None, pull=False, rev=None, update=True,
73 73 stream=False):
74 74 """Make a copy of an existing repository.
75 75
76 76 Create a copy of an existing repository in a new directory. The
77 77 source and destination are URLs, as passed to the repository
78 78 function. Returns a pair of repository objects, the source and
79 79 newly created destination.
80 80
81 81 The location of the source is added to the new repository's
82 82 .hg/hgrc file, as the default to be used for future pulls and
83 83 pushes.
84 84
85 85 If an exception is raised, the partly cloned/updated destination
86 86 repository will be deleted.
87 87
88 88 Arguments:
89 89
90 90 source: repository object or URL
91 91
92 92 dest: URL of destination repository to create (defaults to base
93 93 name of source repository)
94 94
95 95 pull: always pull from source repository, even in local case
96 96
97 97 stream: stream raw data uncompressed from repository (fast over
98 98 LAN, slow over WAN)
99 99
100 100 rev: revision to clone up to (implies pull=True)
101 101
102 102 update: update working directory after clone completes, if
103 103 destination is local repository
104 104 """
105 105
106 106 if isinstance(source, str):
107 107 origsource = ui.expandpath(source)
108 108 source, rev, checkout = parseurl(origsource, rev)
109 109 src_repo = repository(ui, source)
110 110 else:
111 111 src_repo = source
112 112 origsource = source = src_repo.url()
113 113 checkout = None
114 114
115 115 if dest is None:
116 116 dest = defaultdest(source)
117 117 ui.status(_("destination directory: %s\n") % dest)
118 118
119 119 def localpath(path):
120 120 if path.startswith('file://localhost/'):
121 121 return path[16:]
122 122 if path.startswith('file://'):
123 123 return path[7:]
124 124 if path.startswith('file:'):
125 125 return path[5:]
126 126 return path
127 127
128 128 dest = localpath(dest)
129 129 source = localpath(source)
130 130
131 131 if os.path.exists(dest):
132 132 raise util.Abort(_("destination '%s' already exists") % dest)
133 133
134 134 class DirCleanup(object):
135 135 def __init__(self, dir_):
136 136 self.rmtree = shutil.rmtree
137 137 self.dir_ = dir_
138 138 def close(self):
139 139 self.dir_ = None
140 140 def __del__(self):
141 141 if self.dir_:
142 142 self.rmtree(self.dir_, True)
143 143
144 144 src_lock = dest_lock = dir_cleanup = None
145 145 try:
146 146 if islocal(dest):
147 147 dir_cleanup = DirCleanup(dest)
148 148
149 149 abspath = origsource
150 150 copy = False
151 151 if src_repo.cancopy() and islocal(dest):
152 152 abspath = os.path.abspath(util.drop_scheme('file', origsource))
153 153 copy = not pull and not rev
154 154
155 155 if copy:
156 156 try:
157 157 # we use a lock here because if we race with commit, we
158 158 # can end up with extra data in the cloned revlogs that's
159 159 # not pointed to by changesets, thus causing verify to
160 160 # fail
161 161 src_lock = src_repo.lock()
162 162 except lock.LockException:
163 163 copy = False
164 164
165 165 if copy:
166 166 def force_copy(src, dst):
167 167 if not os.path.exists(src):
168 168 # Tolerate empty source repository and optional files
169 169 return
170 170 util.copyfiles(src, dst)
171 171
172 172 src_store = os.path.realpath(src_repo.spath)
173 173 if not os.path.exists(dest):
174 174 os.mkdir(dest)
175 175 try:
176 176 dest_path = os.path.realpath(os.path.join(dest, ".hg"))
177 177 os.mkdir(dest_path)
178 178 except OSError, inst:
179 179 if inst.errno == errno.EEXIST:
180 180 dir_cleanup.close()
181 181 raise util.Abort(_("destination '%s' already exists")
182 182 % dest)
183 183 raise
184 184 if src_repo.spath != src_repo.path:
185 185 # XXX racy
186 186 dummy_changelog = os.path.join(dest_path, "00changelog.i")
187 187 # copy the dummy changelog
188 188 force_copy(src_repo.join("00changelog.i"), dummy_changelog)
189 189 dest_store = os.path.join(dest_path, "store")
190 190 os.mkdir(dest_store)
191 191 else:
192 192 dest_store = dest_path
193 193 # copy the requires file
194 194 force_copy(src_repo.join("requires"),
195 195 os.path.join(dest_path, "requires"))
196 196 # we lock here to avoid premature writing to the target
197 197 dest_lock = lock.lock(os.path.join(dest_store, "lock"))
198 198
199 199 files = ("data",
200 200 "00manifest.d", "00manifest.i",
201 201 "00changelog.d", "00changelog.i")
202 202 for f in files:
203 203 src = os.path.join(src_store, f)
204 204 dst = os.path.join(dest_store, f)
205 205 force_copy(src, dst)
206 206
207 207 # we need to re-init the repo after manually copying the data
208 208 # into it
209 209 dest_repo = repository(ui, dest)
210 210
211 211 else:
212 212 try:
213 213 dest_repo = repository(ui, dest, create=True)
214 214 except OSError, inst:
215 215 if inst.errno == errno.EEXIST:
216 216 dir_cleanup.close()
217 217 raise util.Abort(_("destination '%s' already exists")
218 218 % dest)
219 219 raise
220 220
221 221 revs = None
222 222 if rev:
223 223 if 'lookup' not in src_repo.capabilities:
224 224 raise util.Abort(_("src repository does not support revision "
225 225 "lookup and so doesn't support clone by "
226 226 "revision"))
227 227 revs = [src_repo.lookup(r) for r in rev]
228 228
229 229 if dest_repo.local():
230 230 dest_repo.clone(src_repo, heads=revs, stream=stream)
231 231 elif src_repo.local():
232 232 src_repo.push(dest_repo, revs=revs)
233 233 else:
234 234 raise util.Abort(_("clone from remote to remote not supported"))
235 235
236 236 if dir_cleanup:
237 237 dir_cleanup.close()
238 238
239 239 if dest_repo.local():
240 240 fp = dest_repo.opener("hgrc", "w", text=True)
241 241 fp.write("[paths]\n")
242 fp.write("default = %s\n" % abspath)
242 # percent needs to be escaped for ConfigParser
243 fp.write("default = %s\n" % abspath.replace('%', '%%'))
243 244 fp.close()
244 245
245 246 if update:
246 247 dest_repo.ui.status(_("updating working directory\n"))
247 248 if not checkout:
248 249 try:
249 250 checkout = dest_repo.lookup("default")
250 251 except:
251 252 checkout = dest_repo.changelog.tip()
252 253 _update(dest_repo, checkout)
253 254
254 255 return src_repo, dest_repo
255 256 finally:
256 257 del src_lock, dest_lock, dir_cleanup
257 258
258 259 def _showstats(repo, stats):
259 260 stats = ((stats[0], _("updated")),
260 261 (stats[1], _("merged")),
261 262 (stats[2], _("removed")),
262 263 (stats[3], _("unresolved")))
263 264 note = ", ".join([_("%d files %s") % s for s in stats])
264 265 repo.ui.status("%s\n" % note)
265 266
266 267 def _update(repo, node): return update(repo, node)
267 268
268 269 def update(repo, node):
269 270 """update the working directory to node, merging linear changes"""
270 271 pl = repo.parents()
271 272 stats = _merge.update(repo, node, False, False, None)
272 273 _showstats(repo, stats)
273 274 if stats[3]:
274 275 repo.ui.status(_("There are unresolved merges with"
275 276 " locally modified files.\n"))
276 277 if stats[1]:
277 278 repo.ui.status(_("You can finish the partial merge using:\n"))
278 279 else:
279 280 repo.ui.status(_("You can redo the full merge using:\n"))
280 281 # len(pl)==1, otherwise _merge.update() would have raised util.Abort:
281 282 repo.ui.status(_(" hg update %s\n hg update %s\n")
282 283 % (pl[0].rev(), repo.changectx(node).rev()))
283 284 return stats[3] > 0
284 285
285 286 def clean(repo, node, show_stats=True):
286 287 """forcibly switch the working directory to node, clobbering changes"""
287 288 stats = _merge.update(repo, node, False, True, None)
288 289 if show_stats: _showstats(repo, stats)
289 290 return stats[3] > 0
290 291
291 292 def merge(repo, node, force=None, remind=True):
292 293 """branch merge with node, resolving changes"""
293 294 stats = _merge.update(repo, node, True, force, False)
294 295 _showstats(repo, stats)
295 296 if stats[3]:
296 297 pl = repo.parents()
297 298 repo.ui.status(_("There are unresolved merges,"
298 299 " you can redo the full merge using:\n"
299 300 " hg update -C %s\n"
300 301 " hg merge %s\n")
301 302 % (pl[0].rev(), pl[1].rev()))
302 303 elif remind:
303 304 repo.ui.status(_("(branch merge, don't forget to commit)\n"))
304 305 return stats[3] > 0
305 306
306 307 def revert(repo, node, choose):
307 308 """revert changes to revision in node without updating dirstate"""
308 309 return _merge.update(repo, node, False, True, choose)[3] > 0
309 310
310 311 def verify(repo):
311 312 """verify the consistency of a repository"""
312 313 return _verify.verify(repo)
@@ -1,7 +1,18 b''
1 1 #!/bin/sh
2 2
3 3 mkdir t
4 4 cd t
5 5 hg init
6 6 echo "invalid" > .hg/hgrc
7 7 hg status 2>&1 |sed -e "s:/.*\(/t/.*\):...\1:"
8
9 #issue 1199, escaping
10
11 cd ..
12 hg init "foo%bar"
13 hg clone "foo%bar" foobar
14 p=`pwd`
15 cd foobar
16 cat .hg/hgrc |sed -e "s:$p:...:"
17 hg paths |sed -e "s:$p:...:"
18 hg showconfig |sed -e "s:$p:...:"
@@ -1,4 +1,16 b''
1 1 abort: Failed to parse .../t/.hg/hgrc
2 2 File contains no section headers.
3 3 file: .../t/.hg/hgrc, line: 1
4 4 'invalid\n'
5 updating working directory
6 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
7 [paths]
8 default = .../foo%%bar
9 default = .../foo%bar
10 bundle.mainreporoot=.../foobar
11 defaults.backout=-d "0 0"
12 defaults.commit=-d "0 0"
13 defaults.debugrawcommit=-d "0 0"
14 defaults.tag=-d "0 0"
15 paths.default=.../foo%bar
16 ui.slash=True
General Comments 0
You need to be logged in to leave comments. Login now