narrow: add a --narrowspec flag to clone command
authorPulkit Goyal <pulkit@yandex-team.ru>
Wed, 08 Aug 2018 13:56:53 +0300
changeset 39247 f4d4bd8c8911
parent 39246 61700d525a3b
child 39248 57d4754e44b8
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
hgext/narrow/narrowcommands.py
tests/test-narrow-clone-no-ellipsis.t
tests/test-narrow-clone.t
--- a/hgext/narrow/narrowcommands.py	Fri Aug 10 16:01:19 2018 -0700
+++ b/hgext/narrow/narrowcommands.py	Wed Aug 08 13:56:53 2018 +0300
@@ -7,6 +7,7 @@
 from __future__ import absolute_import
 
 import itertools
+import os
 
 from mercurial.i18n import _
 from mercurial import (
@@ -25,6 +26,7 @@
     repair,
     repository,
     repoview,
+    sparse,
     util,
 )
 
@@ -43,6 +45,8 @@
                      _("create a narrow clone of select files")))
     entry[1].append(('', 'depth', '',
                      _("limit the history fetched by distance from heads")))
+    entry[1].append(('', 'narrowspec', '',
+                     _("read narrowspecs from file")))
     # TODO(durin42): unify sparse/narrow --include/--exclude logic a bit
     if 'sparse' not in extensions.enabled():
         entry[1].append(('', 'include', [],
@@ -73,6 +77,27 @@
     opts = pycompat.byteskwargs(opts)
     wrappedextraprepare = util.nullcontextmanager()
     opts_narrow = opts['narrow']
+    narrowspecfile = opts['narrowspec']
+
+    if narrowspecfile:
+        filepath = os.path.join(pycompat.getcwd(), narrowspecfile)
+        ui.status(_("reading narrowspec from '%s'\n") % filepath)
+        try:
+            fp = open(filepath, 'rb')
+        except IOError:
+            raise error.Abort(_("file '%s' not found") % filepath)
+
+        includes, excludes, profiles = sparse.parseconfig(ui, fp.read(),
+                                                          'narrow')
+        if profiles:
+            raise error.Abort(_("cannot specify other files using '%include' in"
+                                " narrowspec"))
+
+        # narrowspec is passed so we should assume that user wants narrow clone
+        opts_narrow = True
+        opts['include'].extend(includes)
+        opts['exclude'].extend(excludes)
+
     if opts_narrow:
         def pullbundle2extraprepare_widen(orig, pullop, kwargs):
             # Create narrow spec patterns from clone flags
--- a/tests/test-narrow-clone-no-ellipsis.t	Fri Aug 10 16:01:19 2018 -0700
+++ b/tests/test-narrow-clone-no-ellipsis.t	Wed Aug 08 13:56:53 2018 +0300
@@ -123,3 +123,39 @@
   dir/src/f9
 
   $ cd ..
+
+Testing the --narrowspec flag to clone
+
+  $ cat >> narrowspecs <<EOF
+  > %include foo
+  > [include]
+  > path:dir/tests/
+  > file:dir/src/f12
+  > EOF
+
+  $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs
+  reading narrowspec from '$TESTTMP/narrowspecs'
+  abort: cannot specify other files using '%include' in narrowspec
+  [255]
+
+  $ cat > narrowspecs <<EOF
+  > [include]
+  > path:dir/tests/
+  > file:dir/src/f12
+  > EOF
+
+  $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs
+  reading narrowspec from '$TESTTMP/narrowspecs'
+  requesting all changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 40 changesets with 20 changes to 20 files
+  new changesets 681085829a73:26ce255d5b5d
+  updating to branch default
+  20 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd specfile
+  $ hg tracked
+  I path:dir/tests
+  I path:file:dir/src/f12
+  $ cd ..
--- a/tests/test-narrow-clone.t	Fri Aug 10 16:01:19 2018 -0700
+++ b/tests/test-narrow-clone.t	Wed Aug 08 13:56:53 2018 +0300
@@ -218,3 +218,39 @@
   dir/tests/t9
 
   $ cd ..
+
+Testing the --narrowspec flag to clone
+
+  $ cat >> narrowspecs <<EOF
+  > %include foo
+  > [include]
+  > path:dir/tests/
+  > dir/src/f12
+  > EOF
+
+  $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs
+  reading narrowspec from '$TESTTMP/narrowspecs'
+  abort: cannot specify other files using '%include' in narrowspec
+  [255]
+
+  $ cat > narrowspecs <<EOF
+  > [include]
+  > path:dir/tests/
+  > file:dir/src/f12
+  > EOF
+
+  $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs
+  reading narrowspec from '$TESTTMP/narrowspecs'
+  requesting all changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 21 changesets with 20 changes to 20 files
+  new changesets f93383bb3e99:26ce255d5b5d
+  updating to branch default
+  20 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd specfile
+  $ hg tracked
+  I path:dir/tests
+  I path:file:dir/src/f12
+  $ cd ..