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