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