Show More
@@ -1,312 +1,311 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 node import * |
|
10 | 10 | from repo import * |
|
11 | 11 | from i18n import _ |
|
12 | 12 | import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo |
|
13 | 13 | import errno, lock, os, shutil, util, extensions |
|
14 | 14 | import merge as _merge |
|
15 | 15 | import verify as _verify |
|
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 | 25 | return url, (revs or None), None |
|
26 | 26 | |
|
27 | 27 | url, rev = url.split('#', 1) |
|
28 | 28 | return url, revs + [rev], rev |
|
29 | 29 | |
|
30 | 30 | schemes = { |
|
31 | 31 | 'bundle': bundlerepo, |
|
32 | 32 | 'file': _local, |
|
33 | 33 | 'http': httprepo, |
|
34 | 34 | 'https': httprepo, |
|
35 | 35 | 'ssh': sshrepo, |
|
36 | 36 | 'static-http': statichttprepo, |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | def _lookup(path): |
|
40 | 40 | scheme = 'file' |
|
41 | 41 | if path: |
|
42 | 42 | c = path.find(':') |
|
43 | 43 | if c > 0: |
|
44 | 44 | scheme = path[:c] |
|
45 | 45 | thing = schemes.get(scheme) or schemes['file'] |
|
46 | 46 | try: |
|
47 | 47 | return thing(path) |
|
48 | 48 | except TypeError: |
|
49 | 49 | return thing |
|
50 | 50 | |
|
51 | 51 | def islocal(repo): |
|
52 | 52 | '''return true if repo or path is local''' |
|
53 | 53 | if isinstance(repo, str): |
|
54 | 54 | try: |
|
55 | 55 | return _lookup(repo).islocal(repo) |
|
56 | 56 | except AttributeError: |
|
57 | 57 | return False |
|
58 | 58 | return repo.local() |
|
59 | 59 | |
|
60 | 60 | def repository(ui, path='', create=False): |
|
61 | 61 | """return a repository object for the specified path""" |
|
62 | 62 | repo = _lookup(path).instance(ui, path, create) |
|
63 | 63 | ui = getattr(repo, "ui", ui) |
|
64 | 64 | for name, module in extensions.extensions(): |
|
65 | 65 | hook = getattr(module, 'reposetup', None) |
|
66 | 66 | if hook: |
|
67 | 67 | hook(ui, repo) |
|
68 | 68 | return repo |
|
69 | 69 | |
|
70 | 70 | def defaultdest(source): |
|
71 | 71 | '''return default destination of clone if none is given''' |
|
72 | 72 | return os.path.basename(os.path.normpath(source)) |
|
73 | 73 | |
|
74 | 74 | def clone(ui, source, dest=None, pull=False, rev=None, update=True, |
|
75 | 75 | stream=False): |
|
76 | 76 | """Make a copy of an existing repository. |
|
77 | 77 | |
|
78 | 78 | Create a copy of an existing repository in a new directory. The |
|
79 | 79 | source and destination are URLs, as passed to the repository |
|
80 | 80 | function. Returns a pair of repository objects, the source and |
|
81 | 81 | newly created destination. |
|
82 | 82 | |
|
83 | 83 | The location of the source is added to the new repository's |
|
84 | 84 | .hg/hgrc file, as the default to be used for future pulls and |
|
85 | 85 | pushes. |
|
86 | 86 | |
|
87 | 87 | If an exception is raised, the partly cloned/updated destination |
|
88 | 88 | repository will be deleted. |
|
89 | 89 | |
|
90 | 90 | Arguments: |
|
91 | 91 | |
|
92 | 92 | source: repository object or URL |
|
93 | 93 | |
|
94 | 94 | dest: URL of destination repository to create (defaults to base |
|
95 | 95 | name of source repository) |
|
96 | 96 | |
|
97 | 97 | pull: always pull from source repository, even in local case |
|
98 | 98 | |
|
99 | 99 | stream: stream raw data uncompressed from repository (fast over |
|
100 | 100 | LAN, slow over WAN) |
|
101 | 101 | |
|
102 | 102 | rev: revision to clone up to (implies pull=True) |
|
103 | 103 | |
|
104 | 104 | update: update working directory after clone completes, if |
|
105 | 105 | destination is local repository |
|
106 | 106 | """ |
|
107 | 107 | |
|
108 | 108 | origsource = source |
|
109 | 109 | source, rev, checkout = parseurl(ui.expandpath(source), rev) |
|
110 | 110 | |
|
111 | 111 | if isinstance(source, str): |
|
112 | 112 | src_repo = repository(ui, source) |
|
113 | 113 | else: |
|
114 | 114 | src_repo = source |
|
115 | 115 | source = src_repo.url() |
|
116 | 116 | |
|
117 | 117 | if dest is None: |
|
118 | 118 | dest = defaultdest(source) |
|
119 | 119 | ui.status(_("destination directory: %s\n") % dest) |
|
120 | 120 | |
|
121 | 121 | def localpath(path): |
|
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.local() 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 | # Tolerate empty source repository and optional files | |
|
169 | return | |
|
168 | 170 |
|
|
169 | except OSError, inst: | |
|
170 | if inst.errno != errno.ENOENT: | |
|
171 | raise | |
|
172 | 171 | |
|
173 | 172 | src_store = os.path.realpath(src_repo.spath) |
|
174 | 173 | if not os.path.exists(dest): |
|
175 | 174 | os.mkdir(dest) |
|
176 | 175 | try: |
|
177 | 176 | dest_path = os.path.realpath(os.path.join(dest, ".hg")) |
|
178 | 177 | os.mkdir(dest_path) |
|
179 | 178 | except OSError, inst: |
|
180 | 179 | if inst.errno == errno.EEXIST: |
|
181 | 180 | dir_cleanup.close() |
|
182 | 181 | raise util.Abort(_("destination '%s' already exists") |
|
183 | 182 | % dest) |
|
184 | 183 | raise |
|
185 | 184 | if src_repo.spath != src_repo.path: |
|
186 | 185 | # XXX racy |
|
187 | 186 | dummy_changelog = os.path.join(dest_path, "00changelog.i") |
|
188 | 187 | # copy the dummy changelog |
|
189 | 188 | force_copy(src_repo.join("00changelog.i"), dummy_changelog) |
|
190 | 189 | dest_store = os.path.join(dest_path, "store") |
|
191 | 190 | os.mkdir(dest_store) |
|
192 | 191 | else: |
|
193 | 192 | dest_store = dest_path |
|
194 | 193 | # copy the requires file |
|
195 | 194 | force_copy(src_repo.join("requires"), |
|
196 | 195 | os.path.join(dest_path, "requires")) |
|
197 | 196 | # we lock here to avoid premature writing to the target |
|
198 | 197 | dest_lock = lock.lock(os.path.join(dest_store, "lock")) |
|
199 | 198 | |
|
200 | 199 | files = ("data", |
|
201 | 200 | "00manifest.d", "00manifest.i", |
|
202 | 201 | "00changelog.d", "00changelog.i") |
|
203 | 202 | for f in files: |
|
204 | 203 | src = os.path.join(src_store, f) |
|
205 | 204 | dst = os.path.join(dest_store, f) |
|
206 | 205 | force_copy(src, dst) |
|
207 | 206 | |
|
208 | 207 | # we need to re-init the repo after manually copying the data |
|
209 | 208 | # into it |
|
210 | 209 | dest_repo = repository(ui, dest) |
|
211 | 210 | |
|
212 | 211 | else: |
|
213 | 212 | try: |
|
214 | 213 | dest_repo = repository(ui, dest, create=True) |
|
215 | 214 | except OSError, inst: |
|
216 | 215 | if inst.errno == errno.EEXIST: |
|
217 | 216 | dir_cleanup.close() |
|
218 | 217 | raise util.Abort(_("destination '%s' already exists") |
|
219 | 218 | % dest) |
|
220 | 219 | raise |
|
221 | 220 | |
|
222 | 221 | revs = None |
|
223 | 222 | if rev: |
|
224 | 223 | if 'lookup' not in src_repo.capabilities: |
|
225 | 224 | raise util.Abort(_("src repository does not support revision " |
|
226 | 225 | "lookup and so doesn't support clone by " |
|
227 | 226 | "revision")) |
|
228 | 227 | revs = [src_repo.lookup(r) for r in rev] |
|
229 | 228 | |
|
230 | 229 | if dest_repo.local(): |
|
231 | 230 | dest_repo.clone(src_repo, heads=revs, stream=stream) |
|
232 | 231 | elif src_repo.local(): |
|
233 | 232 | src_repo.push(dest_repo, revs=revs) |
|
234 | 233 | else: |
|
235 | 234 | raise util.Abort(_("clone from remote to remote not supported")) |
|
236 | 235 | |
|
237 | 236 | if dir_cleanup: |
|
238 | 237 | dir_cleanup.close() |
|
239 | 238 | |
|
240 | 239 | if dest_repo.local(): |
|
241 | 240 | fp = dest_repo.opener("hgrc", "w", text=True) |
|
242 | 241 | fp.write("[paths]\n") |
|
243 | 242 | fp.write("default = %s\n" % abspath) |
|
244 | 243 | fp.close() |
|
245 | 244 | |
|
246 | 245 | if update: |
|
247 | 246 | if not checkout: |
|
248 | 247 | try: |
|
249 | 248 | checkout = dest_repo.lookup("default") |
|
250 | 249 | except: |
|
251 | 250 | checkout = dest_repo.changelog.tip() |
|
252 | 251 | _update(dest_repo, checkout) |
|
253 | 252 | |
|
254 | 253 | return src_repo, dest_repo |
|
255 | 254 | finally: |
|
256 | 255 | del src_lock, dest_lock, dir_cleanup |
|
257 | 256 | |
|
258 | 257 | def _showstats(repo, stats): |
|
259 | 258 | stats = ((stats[0], _("updated")), |
|
260 | 259 | (stats[1], _("merged")), |
|
261 | 260 | (stats[2], _("removed")), |
|
262 | 261 | (stats[3], _("unresolved"))) |
|
263 | 262 | note = ", ".join([_("%d files %s") % s for s in stats]) |
|
264 | 263 | repo.ui.status("%s\n" % note) |
|
265 | 264 | |
|
266 | 265 | def _update(repo, node): return update(repo, node) |
|
267 | 266 | |
|
268 | 267 | def update(repo, node): |
|
269 | 268 | """update the working directory to node, merging linear changes""" |
|
270 | 269 | pl = repo.parents() |
|
271 | 270 | stats = _merge.update(repo, node, False, False, None) |
|
272 | 271 | _showstats(repo, stats) |
|
273 | 272 | if stats[3]: |
|
274 | 273 | repo.ui.status(_("There are unresolved merges with" |
|
275 | 274 | " locally modified files.\n")) |
|
276 | 275 | if stats[1]: |
|
277 | 276 | repo.ui.status(_("You can finish the partial merge using:\n")) |
|
278 | 277 | else: |
|
279 | 278 | repo.ui.status(_("You can redo the full merge using:\n")) |
|
280 | 279 | # len(pl)==1, otherwise _merge.update() would have raised util.Abort: |
|
281 | 280 | repo.ui.status(_(" hg update %s\n hg update %s\n") |
|
282 | 281 | % (pl[0].rev(), repo.changectx(node).rev())) |
|
283 | 282 | return stats[3] > 0 |
|
284 | 283 | |
|
285 | 284 | def clean(repo, node, show_stats=True): |
|
286 | 285 | """forcibly switch the working directory to node, clobbering changes""" |
|
287 | 286 | stats = _merge.update(repo, node, False, True, None) |
|
288 | 287 | if show_stats: _showstats(repo, stats) |
|
289 | 288 | return stats[3] > 0 |
|
290 | 289 | |
|
291 | 290 | def merge(repo, node, force=None, remind=True): |
|
292 | 291 | """branch merge with node, resolving changes""" |
|
293 | 292 | stats = _merge.update(repo, node, True, force, False) |
|
294 | 293 | _showstats(repo, stats) |
|
295 | 294 | if stats[3]: |
|
296 | 295 | pl = repo.parents() |
|
297 | 296 | repo.ui.status(_("There are unresolved merges," |
|
298 | 297 | " you can redo the full merge using:\n" |
|
299 | 298 | " hg update -C %s\n" |
|
300 | 299 | " hg merge %s\n") |
|
301 | 300 | % (pl[0].rev(), pl[1].rev())) |
|
302 | 301 | elif remind: |
|
303 | 302 | repo.ui.status(_("(branch merge, don't forget to commit)\n")) |
|
304 | 303 | return stats[3] > 0 |
|
305 | 304 | |
|
306 | 305 | def revert(repo, node, choose): |
|
307 | 306 | """revert changes to revision in node without updating dirstate""" |
|
308 | 307 | return _merge.update(repo, node, False, True, choose)[3] > 0 |
|
309 | 308 | |
|
310 | 309 | def verify(repo): |
|
311 | 310 | """verify the consistency of a repository""" |
|
312 | 311 | return _verify.verify(repo) |
General Comments 0
You need to be logged in to leave comments.
Login now