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