Show More
@@ -1,217 +1,237 b'' | |||
|
1 | 1 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> |
|
2 | 2 | # |
|
3 | 3 | # This software may be used and distributed according to the terms of the |
|
4 | 4 | # GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | 6 | '''share a common history between several working directories |
|
7 | 7 | |
|
8 | 8 | The share extension introduces a new command :hg:`share` to create a new |
|
9 | 9 | working directory. This is similar to :hg:`clone`, but doesn't involve |
|
10 | 10 | copying or linking the storage of the repository. This allows working on |
|
11 | 11 | different branches or changes in parallel without the associated cost in |
|
12 | 12 | terms of disk space. |
|
13 | 13 | |
|
14 | 14 | Note: destructive operations or extensions like :hg:`rollback` should be |
|
15 | 15 | used with care as they can result in confusing problems. |
|
16 | 16 | |
|
17 | 17 | Automatic Pooled Storage for Clones |
|
18 | 18 | ----------------------------------- |
|
19 | 19 | |
|
20 | 20 | When this extension is active, :hg:`clone` can be configured to |
|
21 | 21 | automatically share/pool storage across multiple clones. This |
|
22 | 22 | mode effectively converts :hg:`clone` to :hg:`clone` + :hg:`share`. |
|
23 | 23 | The benefit of using this mode is the automatic management of |
|
24 | 24 | store paths and intelligent pooling of related repositories. |
|
25 | 25 | |
|
26 | 26 | The following ``share.`` config options influence this feature: |
|
27 | 27 | |
|
28 | 28 | ``share.pool`` |
|
29 | 29 | Filesystem path where shared repository data will be stored. When |
|
30 | 30 | defined, :hg:`clone` will automatically use shared repository |
|
31 | 31 | storage instead of creating a store inside each clone. |
|
32 | 32 | |
|
33 | 33 | ``share.poolnaming`` |
|
34 | 34 | How directory names in ``share.pool`` are constructed. |
|
35 | 35 | |
|
36 | 36 | "identity" means the name is derived from the first changeset in the |
|
37 | 37 | repository. In this mode, different remotes share storage if their |
|
38 | 38 | root/initial changeset is identical. In this mode, the local shared |
|
39 | 39 | repository is an aggregate of all encountered remote repositories. |
|
40 | 40 | |
|
41 | 41 | "remote" means the name is derived from the source repository's |
|
42 | 42 | path or URL. In this mode, storage is only shared if the path or URL |
|
43 | 43 | requested in the :hg:`clone` command matches exactly to a repository |
|
44 | 44 | that was cloned before. |
|
45 | 45 | |
|
46 | 46 | The default naming mode is "identity". |
|
47 | ||
|
48 | .. container:: verbose | |
|
49 | ||
|
50 | Sharing requirements and configs of source repository with shares | |
|
51 | ----------------------------------------------------------------- | |
|
52 | ||
|
53 | By default creating a shared repository only enables sharing a common | |
|
54 | history and does not share requirements and configs between them. This | |
|
55 | may lead to problems in some cases, for example when you upgrade the | |
|
56 | storage format from one repository but does not set related configs | |
|
57 | in the shares. | |
|
58 | ||
|
59 | Setting `format.exp-share-safe = True` enables sharing configs and | |
|
60 | requirements. This only applies to shares which are done after enabling | |
|
61 | the config option. | |
|
62 | ||
|
63 | For enabling this in existing shares, enable the config option and reshare. | |
|
64 | ||
|
65 | For resharing existing shares, make sure your working directory is clean | |
|
66 | and there are no untracked files, delete that share and create a new share. | |
|
47 | 67 | ''' |
|
48 | 68 | |
|
49 | 69 | from __future__ import absolute_import |
|
50 | 70 | |
|
51 | 71 | import errno |
|
52 | 72 | from mercurial.i18n import _ |
|
53 | 73 | from mercurial import ( |
|
54 | 74 | bookmarks, |
|
55 | 75 | commands, |
|
56 | 76 | error, |
|
57 | 77 | extensions, |
|
58 | 78 | hg, |
|
59 | 79 | registrar, |
|
60 | 80 | txnutil, |
|
61 | 81 | util, |
|
62 | 82 | ) |
|
63 | 83 | |
|
64 | 84 | cmdtable = {} |
|
65 | 85 | command = registrar.command(cmdtable) |
|
66 | 86 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
67 | 87 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
68 | 88 | # be specifying the version(s) of Mercurial they are tested with, or |
|
69 | 89 | # leave the attribute unspecified. |
|
70 | 90 | testedwith = b'ships-with-hg-core' |
|
71 | 91 | |
|
72 | 92 | |
|
73 | 93 | @command( |
|
74 | 94 | b'share', |
|
75 | 95 | [ |
|
76 | 96 | (b'U', b'noupdate', None, _(b'do not create a working directory')), |
|
77 | 97 | (b'B', b'bookmarks', None, _(b'also share bookmarks')), |
|
78 | 98 | ( |
|
79 | 99 | b'', |
|
80 | 100 | b'relative', |
|
81 | 101 | None, |
|
82 | 102 | _(b'point to source using a relative path'), |
|
83 | 103 | ), |
|
84 | 104 | ], |
|
85 | 105 | _(b'[-U] [-B] SOURCE [DEST]'), |
|
86 | 106 | helpcategory=command.CATEGORY_REPO_CREATION, |
|
87 | 107 | norepo=True, |
|
88 | 108 | ) |
|
89 | 109 | def share( |
|
90 | 110 | ui, source, dest=None, noupdate=False, bookmarks=False, relative=False |
|
91 | 111 | ): |
|
92 | 112 | """create a new shared repository |
|
93 | 113 | |
|
94 | 114 | Initialize a new repository and working directory that shares its |
|
95 | 115 | history (and optionally bookmarks) with another repository. |
|
96 | 116 | |
|
97 | 117 | .. note:: |
|
98 | 118 | |
|
99 | 119 | using rollback or extensions that destroy/modify history (mq, |
|
100 | 120 | rebase, etc.) can cause considerable confusion with shared |
|
101 | 121 | clones. In particular, if two shared clones are both updated to |
|
102 | 122 | the same changeset, and one of them destroys that changeset |
|
103 | 123 | with rollback, the other clone will suddenly stop working: all |
|
104 | 124 | operations will fail with "abort: working directory has unknown |
|
105 | 125 | parent". The only known workaround is to use debugsetparents on |
|
106 | 126 | the broken clone to reset it to a changeset that still exists. |
|
107 | 127 | """ |
|
108 | 128 | |
|
109 | 129 | hg.share( |
|
110 | 130 | ui, |
|
111 | 131 | source, |
|
112 | 132 | dest=dest, |
|
113 | 133 | update=not noupdate, |
|
114 | 134 | bookmarks=bookmarks, |
|
115 | 135 | relative=relative, |
|
116 | 136 | ) |
|
117 | 137 | return 0 |
|
118 | 138 | |
|
119 | 139 | |
|
120 | 140 | @command(b'unshare', [], b'', helpcategory=command.CATEGORY_MAINTENANCE) |
|
121 | 141 | def unshare(ui, repo): |
|
122 | 142 | """convert a shared repository to a normal one |
|
123 | 143 | |
|
124 | 144 | Copy the store data to the repo and remove the sharedpath data. |
|
125 | 145 | """ |
|
126 | 146 | |
|
127 | 147 | if not repo.shared(): |
|
128 | 148 | raise error.Abort(_(b"this is not a shared repo")) |
|
129 | 149 | |
|
130 | 150 | hg.unshare(ui, repo) |
|
131 | 151 | |
|
132 | 152 | |
|
133 | 153 | # Wrap clone command to pass auto share options. |
|
134 | 154 | def clone(orig, ui, source, *args, **opts): |
|
135 | 155 | pool = ui.config(b'share', b'pool') |
|
136 | 156 | if pool: |
|
137 | 157 | pool = util.expandpath(pool) |
|
138 | 158 | |
|
139 | 159 | opts['shareopts'] = { |
|
140 | 160 | b'pool': pool, |
|
141 | 161 | b'mode': ui.config(b'share', b'poolnaming'), |
|
142 | 162 | } |
|
143 | 163 | |
|
144 | 164 | return orig(ui, source, *args, **opts) |
|
145 | 165 | |
|
146 | 166 | |
|
147 | 167 | def extsetup(ui): |
|
148 | 168 | extensions.wrapfunction(bookmarks, b'_getbkfile', getbkfile) |
|
149 | 169 | extensions.wrapfunction(bookmarks.bmstore, b'_recordchange', recordchange) |
|
150 | 170 | extensions.wrapfunction(bookmarks.bmstore, b'_writerepo', writerepo) |
|
151 | 171 | extensions.wrapcommand(commands.table, b'clone', clone) |
|
152 | 172 | |
|
153 | 173 | |
|
154 | 174 | def _hassharedbookmarks(repo): |
|
155 | 175 | """Returns whether this repo has shared bookmarks""" |
|
156 | 176 | if bookmarks.bookmarksinstore(repo): |
|
157 | 177 | # Kind of a lie, but it means that we skip our custom reads and writes |
|
158 | 178 | # from/to the source repo. |
|
159 | 179 | return False |
|
160 | 180 | try: |
|
161 | 181 | shared = repo.vfs.read(b'shared').splitlines() |
|
162 | 182 | except IOError as inst: |
|
163 | 183 | if inst.errno != errno.ENOENT: |
|
164 | 184 | raise |
|
165 | 185 | return False |
|
166 | 186 | return hg.sharedbookmarks in shared |
|
167 | 187 | |
|
168 | 188 | |
|
169 | 189 | def getbkfile(orig, repo): |
|
170 | 190 | if _hassharedbookmarks(repo): |
|
171 | 191 | srcrepo = hg.sharedreposource(repo) |
|
172 | 192 | if srcrepo is not None: |
|
173 | 193 | # just orig(srcrepo) doesn't work as expected, because |
|
174 | 194 | # HG_PENDING refers repo.root. |
|
175 | 195 | try: |
|
176 | 196 | fp, pending = txnutil.trypending( |
|
177 | 197 | repo.root, repo.vfs, b'bookmarks' |
|
178 | 198 | ) |
|
179 | 199 | if pending: |
|
180 | 200 | # only in this case, bookmark information in repo |
|
181 | 201 | # is up-to-date. |
|
182 | 202 | return fp |
|
183 | 203 | fp.close() |
|
184 | 204 | except IOError as inst: |
|
185 | 205 | if inst.errno != errno.ENOENT: |
|
186 | 206 | raise |
|
187 | 207 | |
|
188 | 208 | # otherwise, we should read bookmarks from srcrepo, |
|
189 | 209 | # because .hg/bookmarks in srcrepo might be already |
|
190 | 210 | # changed via another sharing repo |
|
191 | 211 | repo = srcrepo |
|
192 | 212 | |
|
193 | 213 | # TODO: Pending changes in repo are still invisible in |
|
194 | 214 | # srcrepo, because bookmarks.pending is written only into repo. |
|
195 | 215 | # See also https://www.mercurial-scm.org/wiki/SharedRepository |
|
196 | 216 | return orig(repo) |
|
197 | 217 | |
|
198 | 218 | |
|
199 | 219 | def recordchange(orig, self, tr): |
|
200 | 220 | # Continue with write to local bookmarks file as usual |
|
201 | 221 | orig(self, tr) |
|
202 | 222 | |
|
203 | 223 | if _hassharedbookmarks(self._repo): |
|
204 | 224 | srcrepo = hg.sharedreposource(self._repo) |
|
205 | 225 | if srcrepo is not None: |
|
206 | 226 | category = b'share-bookmarks' |
|
207 | 227 | tr.addpostclose(category, lambda tr: self._writerepo(srcrepo)) |
|
208 | 228 | |
|
209 | 229 | |
|
210 | 230 | def writerepo(orig, self, repo): |
|
211 | 231 | # First write local bookmarks file in case we ever unshare |
|
212 | 232 | orig(self, repo) |
|
213 | 233 | |
|
214 | 234 | if _hassharedbookmarks(self._repo): |
|
215 | 235 | srcrepo = hg.sharedreposource(self._repo) |
|
216 | 236 | if srcrepo is not None: |
|
217 | 237 | orig(self, srcrepo) |
General Comments 0
You need to be logged in to leave comments.
Login now