/usr/lib/python2.7/dist-packages/hgnested/__init__.py is in mercurial-nested 0.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # This file is part of hgnested. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
'''commands to manage nested repositories.
This extension provides commands that apply to all the nested repositories.
It was inspired by the forest extension by Robin Farine.
'''
import os
import time
from functools import partial
from mercurial import hg
from mercurial import commands, localrepo, hgweb
from mercurial import util
from mercurial import cmdutil
from mercurial import extensions
from mercurial import wireproto
from mercurial.error import CapabilityError
from mercurial.scmutil import walkrepos
from mercurial.i18n import _
__version__ = '0.8'
testedwith = '3.6 3.7 3.8 3.9'
buglink = 'https://bitbucket.org/cedk/hgnested/issues'
cmdtable = {}
command = cmdutil.command(cmdtable)
_nested_cache = {}
_nested_refreshinterval = 20
def _capabilities(orig, repo, proto):
caps = orig(repo, proto)
caps.append('nested')
return caps
extensions.wrapfunction(wireproto, '_capabilities', _capabilities)
@wireproto.wireprotocommand('nested')
def nested(repo, proto):
'''Return a list of nested repositories.'''
return "\n".join(repo.nested)
def reposetup(ui, repo):
if isinstance(repo, localrepo.localrepository):
class nestedrepo(repo.__class__):
@property
def nested(self):
'''Return a list of nested repositories.'''
cache = _nested_cache.setdefault(self.root, {
'lastrefresh': 0,
'nested': None,
})
if (cache['lastrefresh'] + _nested_refreshinterval
> time.time()):
return cache['nested']
res = {}
paths = [self.root]
while paths:
path = paths.pop()
if os.path.realpath(path) in res:
continue
for root, dirs, files in os.walk(path):
realroot = os.path.realpath(root)
absroot = os.path.abspath(root)
for dir in dirs[:]:
if dir == '.hg':
res[realroot] = absroot
proot = os.path.join(root, '.hg', 'patches')
if os.path.isdir(os.path.join(proot, '.hg')):
realproot = os.path.realpath(proot)
absproot = os.path.abspath(proot)
res[realproot] = absproot
dirs.remove('.hg') # Don't visit .hg directory
else:
path = os.path.join(root, dir)
if (os.path.islink(path)
and os.path.realpath(path) not in res):
paths.append(path)
prefix = len(self.root) + 1
res = [x[prefix:] or '.' for x in res.itervalues()]
res.sort()
cache['nested'] = res
cache['lastrefresh'] = time.time()
return res
repo.__class__ = nestedrepo
if isinstance(repo, wireproto.wirepeer):
class nestedwirepeer(repo.__class__):
@property
def nested(self):
'''Return a list of nested repositories.'''
if not self.capable('nested'):
raise util.Abort(_("Remote repository doesn't support "
"the nested extension."))
return self._call('nested').splitlines()
repo.__class__ = nestedwirepeer
def _hgwebdir_refresh(orig, self):
refreshinterval = 20
if self.ui:
refreshinterval = self.ui.configint('web', 'refreshinterval',
refreshinterval)
if self.lastrefresh + refreshinterval > time.time():
return
orig(self)
for prefix, root in self.ui.configitems('collections'):
prefix = util.pconvert(prefix)
for path in walkrepos(root, followsym=True):
repo = hg.repository(self.ui, path)
for npath in repo.nested:
npath = os.path.normpath(os.path.join(path, npath))
name = util.pconvert(npath)
if name.startswith(prefix):
name = name[len(prefix):]
repo = (name.lstrip('/'), npath)
if repo not in self.repos:
self.repos.append(repo)
self.lastrefresh = time.time()
extensions.wrapfunction(
hgweb.hgwebdir_mod.hgwebdir, 'refresh', _hgwebdir_refresh)
def _nested_apply(ui, repo, function, status, *args, **kwargs):
'Apply function to all nested repositories'
for npath in repo.nested:
if npath == '.':
nrepo = repo
lui = ui
else:
lpath = os.path.join(repo.root, npath)
nrepo = hg.repository(ui, lpath)
lui = nrepo.ui
if function is _nested_diff:
kwargs['prefix'] = npath
if status:
lui.status('[%s]\n' % npath)
function(lui, nrepo, *args, **kwargs)
if status:
lui.status('\n')
def _nested_diff(ui, repo, *pats, **opts):
diffordiffstat = cmdutil.diffordiffstat
cmdutil.diffordiffstat = partial(diffordiffstat,
prefix=opts.get('prefix') or '')
commands.diff(ui, repo, *pats, **opts)
cmdutil.diffordiffstat = diffordiffstat
@command('^ndiff', cmdutil.findcmd('diff', commands.table)[1][1],
_('[OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...'))
def ndiff(ui, repo, *pats, **opts):
'''diff nested repositories (or selected files)
Show differences between revisions for the specified files.
Look at the help of diff command for more informations.'''
_nested_apply(ui, repo, _nested_diff, False, *pats, **opts)
@command('^nclone', cmdutil.findcmd('clone', commands.table)[1][1],
_('[OPTION]... SOURCE [DEST]'), norepo=True)
def nclone(ui, source, dest=None, **opts):
'''make a copy of an existing repository and all nested repositories
Create a copy of an existing repository in a new directory.
Look at the help of clone command for more informations.'''
origsource = ui.expandpath(source)
remotesource, remotebranch = hg.parseurl(origsource, opts.get('branch'))
if hasattr(hg, 'peer'):
remoterepo = hg.peer(ui, opts, remotesource)
localrepo = remoterepo.local()
if localrepo:
remoterepo = localrepo
else:
remoterepo = hg.repository(hg.remoteui(ui, opts), remotesource)
if dest is None:
dest = hg.defaultdest(source)
ui.status(_("destination directory: %s\n") % dest)
for npath in remoterepo.nested:
if npath == '.':
npath = ''
u = util.url(source)
if u.scheme:
nsource = '%s/%s' % (source, npath)
else:
nsource = os.path.join(source, npath)
ndest = os.path.join(dest, npath)
ui.status('[%s]\n' % os.path.normpath(
os.path.join(os.path.basename(dest),
ndest[len(dest) + 1:])))
commands.clone(ui, nsource, dest=ndest, **opts)
ui.status('\n')
@command('^nshare',
[('U', 'noupdate', None, _('do not create a working copy'))],
_('[-U] SOURCE [DEST]'), norepo=True)
def nshare(ui, source, dest=None, noupdate=False):
'''create a new shared repository and all nested repositories
'''
sourcerepo = hg.repository(ui, source)
if dest is None:
dest = hg.defaultdest(source)
ui.status(_("destination directory: %s\n") % dest)
for npath in sourcerepo.nested:
if npath == '.':
npath = ''
u = util.url(source)
if u.scheme:
nsource = '%s/%s' % (source, npath)
else:
nsource = os.path.join(source, npath)
ndest = os.path.join(dest, npath)
ui.status('[%s]\n' % os.path.normpath(
os.path.join(os.path.basename(dest),
ndest[len(dest) + 1:])))
hg.share(ui, nsource, ndest, not noupdate)
ui.status('\n')
@command('nincoming|nin', cmdutil.findcmd('incoming', commands.table)[1][1],
_('[-p] [-n] [-M] [-f] [-r REV]... [--bundle FILENAME] [SOURCE]'))
def nincoming(ui, repo, dest=None, **opts):
'''show changest not found in repository and all nested repositories
Look at the help of incoming command for more informations.'''
_nested_apply(ui, repo, commands.incoming, True, dest=dest, **opts)
@command('noutgoing|nout', cmdutil.findcmd('outgoing', commands.table)[1][1],
_('[-M] [-p] [-n] [-f] [-r REV]... [DEST]'))
def noutgoing(ui, repo, dest=None, **opts):
'''show changesest not found in the destination and all nested repositories
Look at the help of outgoing command for more informations.'''
_nested_apply(ui, repo, commands.outgoing, True, dest=dest, **opts)
@command('npull', cmdutil.findcmd('pull', commands.table)[1][1],
_('[-u] [-f] [-r REV]... [-e CMD] [--remotecmd CMD] [SOURCE]'))
def npull(ui, repo, source="default", **opts):
'''pull changes from the specified source and all nested repositories
Pull changes from a remote repository to a local one.
Look at the help of pull command for more informations.'''
def test(ui, repo, source, **opts):
source, branches = hg.parseurl(ui.expandpath(source),
opts.get('branch'))
other = hg.repository(hg.remoteui(repo, opts), source)
revs, checkout = hg.addbranchrevs(repo, other, branches,
opts.get('rev'))
if revs:
try:
revs = [other.lookup(rev) for rev in revs]
except CapabilityError:
err = _("Other repository doesn't support revision lookup, "
"so a rev cannot be specified.")
raise util.Abort(err)
if (source != 'default'
or opts.get('branch')
or opts.get('rev')):
_nested_apply(ui, repo, test, False, source=source, **opts)
_nested_apply(ui, repo, commands.pull, True, source=source, **opts)
@command('^npush', cmdutil.findcmd('push', commands.table)[1][1],
_('[-f] [-r REV]... [-e CMD] [--remotecmd CMD] [DEST]'))
def npush(ui, repo, dest=None, **opts):
'''push changes to the specified destination and all nested repositories
Push changes from the local repository to the specified destination.
Look at the help of status command for more informations.'''
def test(ui, repo, dest=None, **opts):
dest = ui.expandpath(dest or 'default-push', dest or 'default')
dest, branches = hg.parseurl(dest, opts.get('branch'))
revs, checkout = hg.addbranchrevs(repo, repo, branches,
opts.get('rev'))
if hasattr(hg, 'peer'):
other = hg.peer(ui, opts, dest)
localother = other.local()
if localother:
other = localother
else:
other = hg.repository(hg.remoteui(repo, opts), dest)
if revs:
revs = [other.lookup(rev) for rev in revs]
_nested_apply(ui, repo, test, False, dest=dest, **opts)
_nested_apply(ui, repo, commands.push, True, dest=dest, **opts)
@command('^nstatus|nst', cmdutil.findcmd('status', commands.table)[1][1],
_('[OPTION]... [FILE]...'))
def nstatus(ui, repo, *pats, **opts):
'''show changed files in the working directory and all nested repositories
Show status of files in the repository.
Look at the help of status command for more informations.'''
_nested_apply(ui, repo, commands.status, True, *pats, **opts)
@command('^nupdate|nup', cmdutil.findcmd('update', commands.table)[1][1],
_('[-c] [-C] [-d DATE] [[-r] REV]'))
def nupdate(ui, repo, node=None, rev=None, clean=False, date=None, check=False,
**opts):
'''update working directory and all nested repositories
Update the repository's working directory to the specified changeset.
Look at the help of update command for more informations.'''
def test(ui, repo, node=None, rev=None, date=None, check=False):
if rev and node:
raise util.Abort(_("please specify just one revision"))
if not rev:
rev = node
if check:
c = repo[None]
if c.modified() or c.added() or c.removed():
raise util.Abort(_("uncommitted local changes"))
if date:
rev = cmdutil.finddate(ui, repo, date)
_nested_apply(ui, repo, test, False, node=node, rev=rev, date=date,
check=check)
_nested_apply(ui, repo, commands.update, True, node=node, rev=rev,
clean=clean, date=date, check=check)
|