narrowcommands.py
697 lines
| 24.1 KiB
| text/x-python
|
PythonLexer
Augie Fackler
|
r36096 | # narrowcommands.py - command modifications for narrowhg extension | ||
# | ||||
# Copyright 2017 Google, Inc. | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
import itertools | ||||
Pulkit Goyal
|
r39283 | import os | ||
Augie Fackler
|
r36096 | |||
from mercurial.i18n import _ | ||||
Joerg Sonnenberger
|
r46729 | from mercurial.node import ( | ||
hex, | ||||
short, | ||||
) | ||||
Augie Fackler
|
r36096 | from mercurial import ( | ||
Pulkit Goyal
|
r40106 | bundle2, | ||
Augie Fackler
|
r36096 | cmdutil, | ||
commands, | ||||
discovery, | ||||
Pulkit Goyal
|
r39471 | encoding, | ||
Augie Fackler
|
r36096 | error, | ||
exchange, | ||||
extensions, | ||||
hg, | ||||
Gregory Szorc
|
r36178 | narrowspec, | ||
r43923 | pathutil, | |||
Augie Fackler
|
r36172 | pycompat, | ||
Augie Fackler
|
r36096 | registrar, | ||
repair, | ||||
repoview, | ||||
Pulkit Goyal
|
r45932 | requirements, | ||
Pulkit Goyal
|
r39283 | sparse, | ||
Augie Fackler
|
r36096 | util, | ||
Pulkit Goyal
|
r40110 | wireprototypes, | ||
Augie Fackler
|
r36096 | ) | ||
r47669 | from mercurial.utils import ( | |||
urlutil, | ||||
) | ||||
Augie Fackler
|
r36096 | |||
table = {} | ||||
command = registrar.command(table) | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def setup(): | ||
"""Wraps user-facing mercurial commands with narrow-aware versions.""" | ||||
Augie Fackler
|
r43347 | entry = extensions.wrapcommand(commands.table, b'clone', clonenarrowcmd) | ||
Augie Fackler
|
r43346 | entry[1].append( | ||
Augie Fackler
|
r43347 | (b'', b'narrow', None, _(b"create a narrow clone of select files")) | ||
Augie Fackler
|
r43346 | ) | ||
entry[1].append( | ||||
Augie Fackler
|
r43347 | ( | ||
b'', | ||||
b'depth', | ||||
b'', | ||||
_(b"limit the history fetched by distance from heads"), | ||||
) | ||||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r43347 | entry[1].append((b'', b'narrowspec', b'', _(b"read narrowspecs from file"))) | ||
Augie Fackler
|
r36096 | # TODO(durin42): unify sparse/narrow --include/--exclude logic a bit | ||
Augie Fackler
|
r43347 | if b'sparse' not in extensions.enabled(): | ||
Augie Fackler
|
r43346 | entry[1].append( | ||
Augie Fackler
|
r43347 | (b'', b'include', [], _(b"specifically fetch this file/directory")) | ||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | entry[1].append( | ||
Augie Fackler
|
r43346 | ( | ||
Augie Fackler
|
r43347 | b'', | ||
b'exclude', | ||||
Augie Fackler
|
r43346 | [], | ||
Augie Fackler
|
r43347 | _(b"do not fetch this file/directory, even if included"), | ||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43347 | entry = extensions.wrapcommand(commands.table, b'pull', pullnarrowcmd) | ||
Augie Fackler
|
r43346 | entry[1].append( | ||
Augie Fackler
|
r43347 | ( | ||
b'', | ||||
b'depth', | ||||
b'', | ||||
_(b"limit the history fetched by distance from heads"), | ||||
) | ||||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43347 | extensions.wrapcommand(commands.table, b'archive', archivenarrowcmd) | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def clonenarrowcmd(orig, ui, repo, *args, **opts): | ||
"""Wraps clone command, so 'hg clone' first wraps localrepo.clone().""" | ||||
wrappedextraprepare = util.nullcontextmanager() | ||||
Matt Harbison
|
r51774 | narrowspecfile = opts['narrowspec'] | ||
Pulkit Goyal
|
r39283 | |||
if narrowspecfile: | ||||
Matt Harbison
|
r39843 | filepath = os.path.join(encoding.getcwd(), narrowspecfile) | ||
Augie Fackler
|
r43347 | ui.status(_(b"reading narrowspec from '%s'\n") % filepath) | ||
Pulkit Goyal
|
r39283 | try: | ||
Pulkit Goyal
|
r39501 | fdata = util.readfile(filepath) | ||
except IOError as inst: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b"cannot read narrowspecs from '%s': %s") | ||
Augie Fackler
|
r43346 | % (filepath, encoding.strtolocal(inst.strerror)) | ||
) | ||||
Pulkit Goyal
|
r39283 | |||
Augie Fackler
|
r43347 | includes, excludes, profiles = sparse.parseconfig(ui, fdata, b'narrow') | ||
Pulkit Goyal
|
r39283 | if profiles: | ||
Martin von Zweigbergk
|
r46733 | raise error.ConfigError( | ||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b"cannot specify other files using '%include' in" | ||
b" narrowspec" | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pulkit Goyal
|
r39283 | |||
Gregory Szorc
|
r39577 | narrowspec.validatepatterns(includes) | ||
narrowspec.validatepatterns(excludes) | ||||
Pulkit Goyal
|
r39283 | # narrowspec is passed so we should assume that user wants narrow clone | ||
Matt Harbison
|
r51774 | opts['narrow'] = True | ||
opts['include'].extend(includes) | ||||
opts['exclude'].extend(excludes) | ||||
Pulkit Goyal
|
r39283 | |||
Matt Harbison
|
r51774 | if opts['narrow']: | ||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def pullbundle2extraprepare_widen(orig, pullop, kwargs): | ||
orig(pullop, kwargs) | ||||
Matt Harbison
|
r51774 | if opts.get('depth'): | ||
# TODO: fix exchange._pullbundle2extraprepare() | ||||
kwargs[b'depth'] = opts['depth'] | ||||
Augie Fackler
|
r43346 | |||
wrappedextraprepare = extensions.wrappedfunction( | ||||
r51680 | exchange, '_pullbundle2extraprepare', pullbundle2extraprepare_widen | |||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
Gregory Szorc
|
r39590 | with wrappedextraprepare: | ||
Matt Harbison
|
r51774 | return orig(ui, repo, *args, **opts) | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def pullnarrowcmd(orig, ui, repo, *args, **opts): | ||
"""Wraps pull command to allow modifying narrow spec.""" | ||||
wrappedextraprepare = util.nullcontextmanager() | ||||
Pulkit Goyal
|
r45932 | if requirements.NARROW_REQUIREMENT in repo.requirements: | ||
Augie Fackler
|
r36096 | |||
def pullbundle2extraprepare_widen(orig, pullop, kwargs): | ||||
orig(pullop, kwargs) | ||||
Augie Fackler
|
r43906 | if opts.get('depth'): | ||
kwargs[b'depth'] = opts['depth'] | ||||
Augie Fackler
|
r43346 | |||
wrappedextraprepare = extensions.wrappedfunction( | ||||
r51680 | exchange, '_pullbundle2extraprepare', pullbundle2extraprepare_widen | |||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
with wrappedextraprepare: | ||||
return orig(ui, repo, *args, **opts) | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def archivenarrowcmd(orig, ui, repo, *args, **opts): | ||
"""Wraps archive command to narrow the default includes.""" | ||||
Pulkit Goyal
|
r45932 | if requirements.NARROW_REQUIREMENT in repo.requirements: | ||
Augie Fackler
|
r36096 | repo_includes, repo_excludes = repo.narrowpats | ||
Augie Fackler
|
r43906 | includes = set(opts.get('include', [])) | ||
excludes = set(opts.get('exclude', [])) | ||||
Augie Fackler
|
r36117 | includes, excludes, unused_invalid = narrowspec.restrictpatterns( | ||
Augie Fackler
|
r43346 | includes, excludes, repo_includes, repo_excludes | ||
) | ||||
Augie Fackler
|
r36096 | if includes: | ||
Augie Fackler
|
r43906 | opts['include'] = includes | ||
Augie Fackler
|
r36096 | if excludes: | ||
Augie Fackler
|
r43906 | opts['exclude'] = excludes | ||
Augie Fackler
|
r36096 | return orig(ui, repo, *args, **opts) | ||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | def pullbundle2extraprepare(orig, pullop, kwargs): | ||
repo = pullop.repo | ||||
Pulkit Goyal
|
r45932 | if requirements.NARROW_REQUIREMENT not in repo.requirements: | ||
Augie Fackler
|
r36096 | return orig(pullop, kwargs) | ||
Pulkit Goyal
|
r40110 | if wireprototypes.NARROWCAP not in pullop.remote.capabilities(): | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b"server does not support narrow clones")) | ||
Augie Fackler
|
r36096 | orig(pullop, kwargs) | ||
Augie Fackler
|
r43347 | kwargs[b'narrow'] = True | ||
Augie Fackler
|
r36096 | include, exclude = repo.narrowpats | ||
Augie Fackler
|
r43347 | kwargs[b'oldincludepats'] = include | ||
kwargs[b'oldexcludepats'] = exclude | ||||
Pulkit Goyal
|
r40528 | if include: | ||
Augie Fackler
|
r43347 | kwargs[b'includepats'] = include | ||
Pulkit Goyal
|
r40528 | if exclude: | ||
Augie Fackler
|
r43347 | kwargs[b'excludepats'] = exclude | ||
Pulkit Goyal
|
r39560 | # calculate known nodes only in ellipses cases because in non-ellipses cases | ||
# we have all the nodes | ||||
Pulkit Goyal
|
r42605 | if wireprototypes.ELLIPSESCAP1 in pullop.remote.capabilities(): | ||
Augie Fackler
|
r43347 | kwargs[b'known'] = [ | ||
Joerg Sonnenberger
|
r46729 | hex(ctx.node()) | ||
Augie Fackler
|
r43347 | for ctx in repo.set(b'::%ln', pullop.common) | ||
Joerg Sonnenberger
|
r47771 | if ctx.node() != repo.nullid | ||
Augie Fackler
|
r43346 | ] | ||
Augie Fackler
|
r43347 | if not kwargs[b'known']: | ||
Pulkit Goyal
|
r39560 | # Mercurial serializes an empty list as '' and deserializes it as | ||
# [''], so delete it instead to avoid handling the empty string on | ||||
# the server. | ||||
Augie Fackler
|
r43347 | del kwargs[b'known'] | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | |||
extensions.wrapfunction( | ||||
r51680 | exchange, '_pullbundle2extraprepare', pullbundle2extraprepare | |||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | def _narrow( | ||
ui, | ||||
repo, | ||||
remote, | ||||
commoninc, | ||||
oldincludes, | ||||
oldexcludes, | ||||
newincludes, | ||||
newexcludes, | ||||
force, | ||||
Martin von Zweigbergk
|
r47206 | backup, | ||
Augie Fackler
|
r43346 | ): | ||
Augie Fackler
|
r36096 | oldmatch = narrowspec.match(repo.root, oldincludes, oldexcludes) | ||
newmatch = narrowspec.match(repo.root, newincludes, newexcludes) | ||||
# This is essentially doing "hg outgoing" to find all local-only | ||||
# commits. We will then check that the local-only commits don't | ||||
# have any changes to files that will be untracked. | ||||
unfi = repo.unfiltered() | ||||
Augie Fackler
|
r43346 | outgoing = discovery.findcommonoutgoing(unfi, remote, commoninc=commoninc) | ||
Augie Fackler
|
r43347 | ui.status(_(b'looking for local changes to affected paths\n')) | ||
Martin von Zweigbergk
|
r47799 | progress = ui.makeprogress( | ||
topic=_(b'changesets'), | ||||
unit=_(b'changesets'), | ||||
total=len(outgoing.missing) + len(outgoing.excluded), | ||||
) | ||||
Augie Fackler
|
r36096 | localnodes = [] | ||
Martin von Zweigbergk
|
r47799 | with progress: | ||
for n in itertools.chain(outgoing.missing, outgoing.excluded): | ||||
progress.increment() | ||||
if any(oldmatch(f) and not newmatch(f) for f in unfi[n].files()): | ||||
localnodes.append(n) | ||||
Augie Fackler
|
r43347 | revstostrip = unfi.revs(b'descendants(%ln)', localnodes) | ||
hiddenrevs = repoview.filterrevs(repo, b'visible') | ||||
Augie Fackler
|
r43346 | visibletostrip = list( | ||
repo.changelog.node(r) for r in (revstostrip - hiddenrevs) | ||||
) | ||||
Augie Fackler
|
r36096 | if visibletostrip: | ||
Augie Fackler
|
r43346 | ui.status( | ||
_( | ||||
Augie Fackler
|
r43347 | b'The following changeset(s) or their ancestors have ' | ||
b'local changes not on the remote:\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Augie Fackler
|
r36096 | maxnodes = 10 | ||
if ui.verbose or len(visibletostrip) <= maxnodes: | ||||
for n in visibletostrip: | ||||
Joerg Sonnenberger
|
r46729 | ui.status(b'%s\n' % short(n)) | ||
Augie Fackler
|
r36096 | else: | ||
for n in visibletostrip[:maxnodes]: | ||||
Joerg Sonnenberger
|
r46729 | ui.status(b'%s\n' % short(n)) | ||
Augie Fackler
|
r43346 | ui.status( | ||
Augie Fackler
|
r43347 | _(b'...and %d more, use --verbose to list all\n') | ||
Augie Fackler
|
r43346 | % (len(visibletostrip) - maxnodes) | ||
) | ||||
Augie Fackler
|
r36096 | if not force: | ||
Martin von Zweigbergk
|
r46733 | raise error.StateError( | ||
Augie Fackler
|
r43347 | _(b'local changes found'), | ||
Martin von Zweigbergk
|
r43387 | hint=_(b'use --force-delete-local-changes to ignore'), | ||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
Kyle Lippincott
|
r41106 | with ui.uninterruptible(): | ||
Augie Fackler
|
r38547 | if revstostrip: | ||
tostrip = [unfi.changelog.node(r) for r in revstostrip] | ||||
Augie Fackler
|
r43347 | if repo[b'.'].node() in tostrip: | ||
Augie Fackler
|
r38547 | # stripping working copy, so move to a different commit first | ||
Augie Fackler
|
r43346 | urev = max( | ||
repo.revs( | ||||
Augie Fackler
|
r43347 | b'(::%n) - %ln + null', | ||
repo[b'.'].node(), | ||||
visibletostrip, | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Augie Fackler
|
r38547 | hg.clean(repo, urev) | ||
Augie Fackler
|
r43347 | overrides = {(b'devel', b'strip-obsmarkers'): False} | ||
Martin von Zweigbergk
|
r47800 | if backup: | ||
ui.status(_(b'moving unwanted changesets to backup\n')) | ||||
else: | ||||
ui.status(_(b'deleting unwanted changesets\n')) | ||||
Augie Fackler
|
r43347 | with ui.configoverride(overrides, b'narrow'): | ||
Martin von Zweigbergk
|
r47206 | repair.strip(ui, unfi, tostrip, topic=b'narrow', backup=backup) | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r38547 | todelete = [] | ||
r51397 | for entry in repo.store.data_entries(): | |||
r51377 | if not entry.is_revlog: | |||
continue | ||||
r51392 | if entry.is_filelog: | |||
r51377 | if not newmatch(entry.target_id): | |||
r51365 | for file_ in entry.files(): | |||
todelete.append(file_.unencoded_path) | ||||
r51392 | elif entry.is_manifestlog: | |||
Martin von Zweigbergk
|
r52169 | dir = entry.target_id[:-1] | ||
r43923 | dirs = sorted(pathutil.dirs({dir})) + [dir] | |||
Augie Fackler
|
r38547 | include = True | ||
for d in dirs: | ||||
visit = newmatch.visitdir(d) | ||||
if not visit: | ||||
include = False | ||||
break | ||||
Augie Fackler
|
r43347 | if visit == b'all': | ||
Augie Fackler
|
r38547 | break | ||
if not include: | ||||
r51365 | for file_ in entry.files(): | |||
todelete.append(file_.unencoded_path) | ||||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r38547 | repo.destroying() | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43347 | with repo.transaction(b'narrowing'): | ||
Martin von Zweigbergk
|
r41057 | # Update narrowspec before removing revlogs, so repo won't be | ||
# corrupt in case of crash | ||||
repo.setnarrowpats(newincludes, newexcludes) | ||||
Augie Fackler
|
r38547 | for f in todelete: | ||
Augie Fackler
|
r43347 | ui.status(_(b'deleting %s\n') % f) | ||
Augie Fackler
|
r38547 | util.unlinkpath(repo.svfs.join(f)) | ||
repo.store.markremoved(f) | ||||
Augie Fackler
|
r36096 | |||
Martin von Zweigbergk
|
r47800 | ui.status(_(b'deleting unwanted files from working copy\n')) | ||
r50855 | with repo.dirstate.changing_parents(repo): | |||
r48404 | narrowspec.updateworkingcopy(repo, assumeclean=True) | |||
narrowspec.copytoworkingcopy(repo) | ||||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r38547 | repo.destroyed() | ||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | |||
def _widen( | ||||
ui, | ||||
repo, | ||||
remote, | ||||
commoninc, | ||||
oldincludes, | ||||
oldexcludes, | ||||
newincludes, | ||||
newexcludes, | ||||
): | ||||
Pulkit Goyal
|
r39999 | # for now we assume that if a server has ellipses enabled, we will be | ||
# exchanging ellipses nodes. In future we should add ellipses as a client | ||||
# side requirement (maybe) to distinguish a client is shallow or not and | ||||
# then send that information to server whether we want ellipses or not. | ||||
# Theoretically a non-ellipses repo should be able to use narrow | ||||
# functionality from an ellipses enabled server | ||||
Pulkit Goyal
|
r42605 | remotecap = remote.capabilities() | ||
Augie Fackler
|
r43346 | ellipsesremote = any( | ||
cap in remotecap for cap in wireprototypes.SUPPORTED_ELLIPSESCAP | ||||
) | ||||
Pulkit Goyal
|
r42605 | |||
# check whether we are talking to a server which supports old version of | ||||
# ellipses capabilities | ||||
Augie Fackler
|
r43346 | isoldellipses = ( | ||
ellipsesremote | ||||
and wireprototypes.ELLIPSESCAP1 in remotecap | ||||
and wireprototypes.ELLIPSESCAP not in remotecap | ||||
) | ||||
Pulkit Goyal
|
r39999 | |||
Augie Fackler
|
r36096 | def pullbundle2extraprepare_widen(orig, pullop, kwargs): | ||
orig(pullop, kwargs) | ||||
# The old{in,ex}cludepats have already been set by orig() | ||||
Augie Fackler
|
r43347 | kwargs[b'includepats'] = newincludes | ||
kwargs[b'excludepats'] = newexcludes | ||||
Augie Fackler
|
r43346 | |||
wrappedextraprepare = extensions.wrappedfunction( | ||||
r51680 | exchange, '_pullbundle2extraprepare', pullbundle2extraprepare_widen | |||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | |||
# define a function that narrowbundle2 can call after creating the | ||||
# backup bundle, but before applying the bundle from the server | ||||
def setnewnarrowpats(): | ||||
repo.setnarrowpats(newincludes, newexcludes) | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | repo.setnewnarrowpats = setnewnarrowpats | ||
Pulkit Goyal
|
r39709 | # silence the devel-warning of applying an empty changegroup | ||
Augie Fackler
|
r43347 | overrides = {(b'devel', b'all-warnings'): False} | ||
Augie Fackler
|
r36096 | |||
Pulkit Goyal
|
r42606 | common = commoninc[0] | ||
Kyle Lippincott
|
r41106 | with ui.uninterruptible(): | ||
Pulkit Goyal
|
r39999 | if ellipsesremote: | ||
ds = repo.dirstate | ||||
p1, p2 = ds.p1(), ds.p2() | ||||
r50855 | with ds.changing_parents(repo): | |||
Joerg Sonnenberger
|
r47771 | ds.setparents(repo.nullid, repo.nullid) | ||
Pulkit Goyal
|
r42606 | if isoldellipses: | ||
Augie Fackler
|
r41926 | with wrappedextraprepare: | ||
Pulkit Goyal
|
r42606 | exchange.pull(repo, remote, heads=common) | ||
Pulkit Goyal
|
r39999 | else: | ||
Pulkit Goyal
|
r42606 | known = [] | ||
if ellipsesremote: | ||||
Augie Fackler
|
r43346 | known = [ | ||
ctx.node() | ||||
Augie Fackler
|
r43347 | for ctx in repo.set(b'::%ln', common) | ||
Joerg Sonnenberger
|
r47771 | if ctx.node() != repo.nullid | ||
Augie Fackler
|
r43346 | ] | ||
Pulkit Goyal
|
r40106 | with remote.commandexecutor() as e: | ||
Augie Fackler
|
r43346 | bundle = e.callcommand( | ||
Augie Fackler
|
r43347 | b'narrow_widen', | ||
Augie Fackler
|
r43346 | { | ||
Augie Fackler
|
r43347 | b'oldincludes': oldincludes, | ||
b'oldexcludes': oldexcludes, | ||||
b'newincludes': newincludes, | ||||
b'newexcludes': newexcludes, | ||||
b'cgversion': b'03', | ||||
b'commonheads': common, | ||||
b'known': known, | ||||
b'ellipses': ellipsesremote, | ||||
Augie Fackler
|
r43346 | }, | ||
).result() | ||||
Pulkit Goyal
|
r40106 | |||
Augie Fackler
|
r43347 | trmanager = exchange.transactionmanager( | ||
repo, b'widen', remote.url() | ||||
) | ||||
with trmanager, repo.ui.configoverride(overrides, b'widen'): | ||||
Augie Fackler
|
r43346 | op = bundle2.bundleoperation( | ||
Augie Fackler
|
r43347 | repo, trmanager.transaction, source=b'widen' | ||
Augie Fackler
|
r43346 | ) | ||
Pulkit Goyal
|
r42606 | # TODO: we should catch error.Abort here | ||
r50659 | bundle2.processbundle(repo, bundle, op=op, remote=remote) | |||
Pulkit Goyal
|
r42606 | |||
if ellipsesremote: | ||||
r50855 | with ds.changing_parents(repo): | |||
Pulkit Goyal
|
r42606 | ds.setparents(p1, p2) | ||
Augie Fackler
|
r36096 | |||
r50855 | with repo.transaction(b'widening'), repo.dirstate.changing_parents( | |||
repo | ||||
): | ||||
Martin von Zweigbergk
|
r41273 | repo.setnewnarrowpats() | ||
narrowspec.updateworkingcopy(repo) | ||||
narrowspec.copytoworkingcopy(repo) | ||||
Augie Fackler
|
r36096 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36096 | # TODO(rdamazio): Make new matcher format and update description | ||
Augie Fackler
|
r43346 | @command( | ||
Augie Fackler
|
r43347 | b'tracked', | ||
Augie Fackler
|
r43346 | [ | ||
Augie Fackler
|
r43347 | (b'', b'addinclude', [], _(b'new paths to include')), | ||
(b'', b'removeinclude', [], _(b'old paths to no longer include')), | ||||
Augie Fackler
|
r43346 | ( | ||
Augie Fackler
|
r43347 | b'', | ||
b'auto-remove-includes', | ||||
Augie Fackler
|
r43346 | False, | ||
Augie Fackler
|
r43347 | _(b'automatically choose unused includes to remove'), | ||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
r43347 | (b'', b'addexclude', [], _(b'new paths to exclude')), | ||
(b'', b'import-rules', b'', _(b'import narrowspecs from a file')), | ||||
(b'', b'removeexclude', [], _(b'old paths to no longer exclude')), | ||||
Augie Fackler
|
r43346 | ( | ||
Augie Fackler
|
r43347 | b'', | ||
b'clear', | ||||
Augie Fackler
|
r43346 | False, | ||
Augie Fackler
|
r43347 | _(b'whether to replace the existing narrowspec'), | ||
Augie Fackler
|
r43346 | ), | ||
( | ||||
Augie Fackler
|
r43347 | b'', | ||
b'force-delete-local-changes', | ||||
Augie Fackler
|
r43346 | False, | ||
Augie Fackler
|
r43347 | _(b'forces deletion of local changes when narrowing'), | ||
), | ||||
( | ||||
b'', | ||||
Martin von Zweigbergk
|
r47206 | b'backup', | ||
True, | ||||
_(b'back up local changes when narrowing'), | ||||
), | ||||
( | ||||
b'', | ||||
Augie Fackler
|
r43347 | b'update-working-copy', | ||
False, | ||||
_(b'update working copy when the store has changed'), | ||||
Augie Fackler
|
r43346 | ), | ||
] | ||||
+ commands.remoteopts, | ||||
Augie Fackler
|
r43347 | _(b'[OPTIONS]... [REMOTE]'), | ||
Augie Fackler
|
r43346 | inferrepo=True, | ||
Rodrigo Damazio
|
r43451 | helpcategory=command.CATEGORY_MAINTENANCE, | ||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r36096 | def trackedcmd(ui, repo, remotepath=None, *pats, **opts): | ||
"""show or change the current narrowspec | ||||
With no argument, shows the current narrowspec entries, one per line. Each | ||||
line will be prefixed with 'I' or 'X' for included or excluded patterns, | ||||
respectively. | ||||
The narrowspec is comprised of expressions to match remote files and/or | ||||
directories that should be pulled into your client. | ||||
The narrowspec has *include* and *exclude* expressions, with excludes always | ||||
trumping includes: that is, if a file matches an exclude expression, it will | ||||
be excluded even if it also matches an include expression. | ||||
Excluding files that were never included has no effect. | ||||
Each included or excluded entry is in the format described by | ||||
'hg help patterns'. | ||||
The options allow you to add or remove included and excluded expressions. | ||||
If --clear is specified, then all previous includes and excludes are DROPPED | ||||
and replaced by the new ones specified to --addinclude and --addexclude. | ||||
If --clear is specified without any further options, the narrowspec will be | ||||
empty and will not match any files. | ||||
Pulkit Goyal
|
r42146 | |||
Martin von Zweigbergk
|
r43215 | If --auto-remove-includes is specified, then those includes that don't match | ||
any files modified by currently visible local commits (those not shared by | ||||
the remote) will be added to the set of explicitly specified includes to | ||||
remove. | ||||
Pulkit Goyal
|
r42146 | --import-rules accepts a path to a file containing rules, allowing you to | ||
add --addinclude, --addexclude rules in bulk. Like the other include and | ||||
exclude switches, the changes are applied immediately. | ||||
Augie Fackler
|
r36096 | """ | ||
Pulkit Goyal
|
r45932 | if requirements.NARROW_REQUIREMENT not in repo.requirements: | ||
Martin von Zweigbergk
|
r46733 | raise error.InputError( | ||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b'the tracked command is only supported on ' | ||
b'repositories cloned with --narrow' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Augie Fackler
|
r36096 | |||
# Before supporting, decide whether it "hg tracked --clear" should mean | ||||
# tracking no paths or all paths. | ||||
Matt Harbison
|
r51774 | if opts['clear']: | ||
Martin von Zweigbergk
|
r46733 | raise error.InputError(_(b'the --clear option is not yet supported')) | ||
Augie Fackler
|
r36096 | |||
Pulkit Goyal
|
r39471 | # import rules from a file | ||
Matt Harbison
|
r51774 | newrules = opts.get('import_rules') | ||
Pulkit Goyal
|
r39471 | if newrules: | ||
Matt Harbison
|
r51775 | filepath = os.path.join(encoding.getcwd(), newrules) | ||
Pulkit Goyal
|
r39471 | try: | ||
fdata = util.readfile(filepath) | ||||
except IOError as inst: | ||||
Martin von Zweigbergk
|
r46733 | raise error.StorageError( | ||
Augie Fackler
|
r43347 | _(b"cannot read narrowspecs from '%s': %s") | ||
Augie Fackler
|
r43346 | % (filepath, encoding.strtolocal(inst.strerror)) | ||
) | ||||
includepats, excludepats, profiles = sparse.parseconfig( | ||||
Augie Fackler
|
r43347 | ui, fdata, b'narrow' | ||
Augie Fackler
|
r43346 | ) | ||
Pulkit Goyal
|
r39471 | if profiles: | ||
Martin von Zweigbergk
|
r46733 | raise error.InputError( | ||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b"including other spec files using '%include' " | ||
b"is not supported in narrowspec" | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Matt Harbison
|
r51774 | opts['addinclude'].extend(includepats) | ||
opts['addexclude'].extend(excludepats) | ||||
Pulkit Goyal
|
r39471 | |||
Matt Harbison
|
r51774 | addedincludes = narrowspec.parsepatterns(opts['addinclude']) | ||
removedincludes = narrowspec.parsepatterns(opts['removeinclude']) | ||||
addedexcludes = narrowspec.parsepatterns(opts['addexclude']) | ||||
removedexcludes = narrowspec.parsepatterns(opts['removeexclude']) | ||||
autoremoveincludes = opts['auto_remove_includes'] | ||||
Pulkit Goyal
|
r40462 | |||
Matt Harbison
|
r51774 | update_working_copy = opts['update_working_copy'] | ||
Augie Fackler
|
r43346 | only_show = not ( | ||
addedincludes | ||||
or removedincludes | ||||
or addedexcludes | ||||
or removedexcludes | ||||
or newrules | ||||
or autoremoveincludes | ||||
or update_working_copy | ||||
) | ||||
Pulkit Goyal
|
r40462 | |||
Augie Fackler
|
r36096 | # Only print the current narrowspec. | ||
if only_show: | ||||
r51078 | oldincludes, oldexcludes = repo.narrowpats | |||
Augie Fackler
|
r43347 | ui.pager(b'tracked') | ||
Matt Harbison
|
r51774 | fm = ui.formatter(b'narrow', pycompat.byteskwargs(opts)) | ||
Pulkit Goyal
|
r40802 | for i in sorted(oldincludes): | ||
Augie Fackler
|
r36096 | fm.startitem() | ||
Augie Fackler
|
r43347 | fm.write(b'status', b'%s ', b'I', label=b'narrow.included') | ||
fm.write(b'pat', b'%s\n', i, label=b'narrow.included') | ||||
Pulkit Goyal
|
r40802 | for i in sorted(oldexcludes): | ||
Augie Fackler
|
r36096 | fm.startitem() | ||
Augie Fackler
|
r43347 | fm.write(b'status', b'%s ', b'X', label=b'narrow.excluded') | ||
fm.write(b'pat', b'%s\n', i, label=b'narrow.excluded') | ||||
Augie Fackler
|
r36096 | fm.end() | ||
return 0 | ||||
r51079 | with repo.wlock(), repo.lock(): | |||
oldincludes, oldexcludes = repo.narrowpats | ||||
r51078 | ||||
r51079 | # filter the user passed additions and deletions into actual additions and | |||
# deletions of excludes and includes | ||||
addedincludes -= oldincludes | ||||
removedincludes &= oldincludes | ||||
addedexcludes -= oldexcludes | ||||
removedexcludes &= oldexcludes | ||||
r51078 | ||||
r51079 | widening = addedincludes or removedexcludes | |||
narrowing = removedincludes or addedexcludes | ||||
r51078 | ||||
r51079 | if update_working_copy: | |||
with repo.transaction(b'narrow-wc'), repo.dirstate.changing_parents( | ||||
repo | ||||
): | ||||
narrowspec.updateworkingcopy(repo) | ||||
narrowspec.copytoworkingcopy(repo) | ||||
return 0 | ||||
Martin von Zweigbergk
|
r41072 | |||
r51079 | if not (widening or narrowing or autoremoveincludes): | |||
ui.status(_(b"nothing to widen or narrow\n")) | ||||
return 0 | ||||
Pulkit Goyal
|
r40462 | |||
Augie Fackler
|
r36096 | cmdutil.bailifchanged(repo) | ||
# Find the revisions we have in common with the remote. These will | ||||
# be used for finding local-only changes for narrowing. They will | ||||
# also define the set of revisions to update for widening. | ||||
r50627 | path = urlutil.get_unique_pull_path_obj(b'tracked', ui, remotepath) | |||
ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(path.loc)) | ||||
Matt Harbison
|
r51774 | remote = hg.peer(repo, pycompat.byteskwargs(opts), path) | ||
Pulkit Goyal
|
r40000 | |||
Valentin Gatien-Baron
|
r47419 | try: | ||
# check narrow support before doing anything if widening needs to be | ||||
# performed. In future we should also abort if client is ellipses and | ||||
# server does not support ellipses | ||||
if ( | ||||
widening | ||||
and wireprototypes.NARROWCAP not in remote.capabilities() | ||||
): | ||||
raise error.Abort(_(b"server does not support narrow clones")) | ||||
Pulkit Goyal
|
r40000 | |||
Valentin Gatien-Baron
|
r47419 | commoninc = discovery.findcommonincoming(repo, remote) | ||
Augie Fackler
|
r36096 | |||
Valentin Gatien-Baron
|
r47419 | if autoremoveincludes: | ||
outgoing = discovery.findcommonoutgoing( | ||||
repo, remote, commoninc=commoninc | ||||
) | ||||
ui.status(_(b'looking for unused includes to remove\n')) | ||||
localfiles = set() | ||||
for n in itertools.chain(outgoing.missing, outgoing.excluded): | ||||
localfiles.update(repo[n].files()) | ||||
suggestedremovals = [] | ||||
for include in sorted(oldincludes): | ||||
match = narrowspec.match(repo.root, [include], oldexcludes) | ||||
if not any(match(f) for f in localfiles): | ||||
suggestedremovals.append(include) | ||||
if suggestedremovals: | ||||
for s in suggestedremovals: | ||||
ui.status(b'%s\n' % s) | ||||
if ( | ||||
ui.promptchoice( | ||||
_( | ||||
Jason R. Coombs
|
r51607 | b'remove these unused includes (Yn)?' | ||
Valentin Gatien-Baron
|
r47419 | b'$$ &Yes $$ &No' | ||
) | ||||
Augie Fackler
|
r43347 | ) | ||
Valentin Gatien-Baron
|
r47419 | == 0 | ||
): | ||||
removedincludes.update(suggestedremovals) | ||||
narrowing = True | ||||
else: | ||||
ui.status(_(b'found no unused includes\n')) | ||||
Martin von Zweigbergk
|
r43215 | |||
Valentin Gatien-Baron
|
r47419 | if narrowing: | ||
newincludes = oldincludes - removedincludes | ||||
newexcludes = oldexcludes | addedexcludes | ||||
_narrow( | ||||
ui, | ||||
repo, | ||||
remote, | ||||
commoninc, | ||||
oldincludes, | ||||
oldexcludes, | ||||
newincludes, | ||||
newexcludes, | ||||
Matt Harbison
|
r51774 | opts['force_delete_local_changes'], | ||
opts['backup'], | ||||
Valentin Gatien-Baron
|
r47419 | ) | ||
# _narrow() updated the narrowspec and _widen() below needs to | ||||
# use the updated values as its base (otherwise removed includes | ||||
# and addedexcludes will be lost in the resulting narrowspec) | ||||
oldincludes = newincludes | ||||
oldexcludes = newexcludes | ||||
Augie Fackler
|
r36096 | |||
Valentin Gatien-Baron
|
r47419 | if widening: | ||
newincludes = oldincludes | addedincludes | ||||
newexcludes = oldexcludes - removedexcludes | ||||
_widen( | ||||
ui, | ||||
repo, | ||||
remote, | ||||
commoninc, | ||||
oldincludes, | ||||
oldexcludes, | ||||
newincludes, | ||||
newexcludes, | ||||
) | ||||
finally: | ||||
remote.close() | ||||
Augie Fackler
|
r36096 | |||
return 0 | ||||