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