comparison hgext/narrow/narrowcommands.py @ 39247:f4d4bd8c8911

narrow: add a --narrowspec flag to clone command This patch adds a --narrowspec flag to `hg clone` command in narrow extension which can be used to read a file and parse narrowspecs from it and use it while cloning a repository. The --narrowspec flag assumes that the user wanted to narrow the clone. Tests are added both for ellipsis and non-ellipsis mode. Differential Revision: https://phab.mercurial-scm.org/D4156
author Pulkit Goyal <pulkit@yandex-team.ru>
date Wed, 08 Aug 2018 13:56:53 +0300
parents a232e6744ba3
children c8e4eae84808
comparison
equal deleted inserted replaced
39246:61700d525a3b 39247:f4d4bd8c8911
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 from __future__ import absolute_import 7 from __future__ import absolute_import
8 8
9 import itertools 9 import itertools
10 import os
10 11
11 from mercurial.i18n import _ 12 from mercurial.i18n import _
12 from mercurial import ( 13 from mercurial import (
13 cmdutil, 14 cmdutil,
14 commands, 15 commands,
23 pycompat, 24 pycompat,
24 registrar, 25 registrar,
25 repair, 26 repair,
26 repository, 27 repository,
27 repoview, 28 repoview,
29 sparse,
28 util, 30 util,
29 ) 31 )
30 32
31 from . import ( 33 from . import (
32 narrowbundle2, 34 narrowbundle2,
41 entry = extensions.wrapcommand(commands.table, 'clone', clonenarrowcmd) 43 entry = extensions.wrapcommand(commands.table, 'clone', clonenarrowcmd)
42 entry[1].append(('', 'narrow', None, 44 entry[1].append(('', 'narrow', None,
43 _("create a narrow clone of select files"))) 45 _("create a narrow clone of select files")))
44 entry[1].append(('', 'depth', '', 46 entry[1].append(('', 'depth', '',
45 _("limit the history fetched by distance from heads"))) 47 _("limit the history fetched by distance from heads")))
48 entry[1].append(('', 'narrowspec', '',
49 _("read narrowspecs from file")))
46 # TODO(durin42): unify sparse/narrow --include/--exclude logic a bit 50 # TODO(durin42): unify sparse/narrow --include/--exclude logic a bit
47 if 'sparse' not in extensions.enabled(): 51 if 'sparse' not in extensions.enabled():
48 entry[1].append(('', 'include', [], 52 entry[1].append(('', 'include', [],
49 _("specifically fetch this file/directory"))) 53 _("specifically fetch this file/directory")))
50 entry[1].append( 54 entry[1].append(
71 def clonenarrowcmd(orig, ui, repo, *args, **opts): 75 def clonenarrowcmd(orig, ui, repo, *args, **opts):
72 """Wraps clone command, so 'hg clone' first wraps localrepo.clone().""" 76 """Wraps clone command, so 'hg clone' first wraps localrepo.clone()."""
73 opts = pycompat.byteskwargs(opts) 77 opts = pycompat.byteskwargs(opts)
74 wrappedextraprepare = util.nullcontextmanager() 78 wrappedextraprepare = util.nullcontextmanager()
75 opts_narrow = opts['narrow'] 79 opts_narrow = opts['narrow']
80 narrowspecfile = opts['narrowspec']
81
82 if narrowspecfile:
83 filepath = os.path.join(pycompat.getcwd(), narrowspecfile)
84 ui.status(_("reading narrowspec from '%s'\n") % filepath)
85 try:
86 fp = open(filepath, 'rb')
87 except IOError:
88 raise error.Abort(_("file '%s' not found") % filepath)
89
90 includes, excludes, profiles = sparse.parseconfig(ui, fp.read(),
91 'narrow')
92 if profiles:
93 raise error.Abort(_("cannot specify other files using '%include' in"
94 " narrowspec"))
95
96 # narrowspec is passed so we should assume that user wants narrow clone
97 opts_narrow = True
98 opts['include'].extend(includes)
99 opts['exclude'].extend(excludes)
100
76 if opts_narrow: 101 if opts_narrow:
77 def pullbundle2extraprepare_widen(orig, pullop, kwargs): 102 def pullbundle2extraprepare_widen(orig, pullop, kwargs):
78 # Create narrow spec patterns from clone flags 103 # Create narrow spec patterns from clone flags
79 includepats = narrowspec.parsepatterns(opts['include']) 104 includepats = narrowspec.parsepatterns(opts['include'])
80 excludepats = narrowspec.parsepatterns(opts['exclude']) 105 excludepats = narrowspec.parsepatterns(opts['exclude'])