diff hgext/narrow/narrowcommands.py @ 39440:ab20ee07b82d

narrow: add '--import-rules' flag to tracked command This patch adds a `--import-rules` flag to tracked command provided by narrow extension. Using the --import-rules flag, you can pass a filename from which narrowspecs should be read and added to main narrowspec. A lot of times, in automation or manually also, when you are working with big repo, specifying each path name on commandline using '--addinclude' and '--addexclude' is tedious and something which can scale. So we needed something where we can pass a file to extend the narrowspecs. Nice thing about this is that the automations which reads some file to change the sparse profile, can now read the same file for changing narrowspecs too. Tests are added for the new feature. Differential Revision: https://phab.mercurial-scm.org/D4125
author Pulkit Goyal <pulkit@yandex-team.ru>
date Mon, 06 Aug 2018 14:06:19 +0300
parents 2b8adb7ca39a
children 4062bbb1d10f
line wrap: on
line diff
--- a/hgext/narrow/narrowcommands.py	Thu Aug 23 13:11:13 2018 -0700
+++ b/hgext/narrow/narrowcommands.py	Mon Aug 06 14:06:19 2018 +0300
@@ -14,6 +14,7 @@
     cmdutil,
     commands,
     discovery,
+    encoding,
     error,
     exchange,
     extensions,
@@ -326,6 +327,7 @@
     [('', 'addinclude', [], _('new paths to include')),
      ('', 'removeinclude', [], _('old paths to no longer include')),
      ('', 'addexclude', [], _('new paths to exclude')),
+     ('', 'import-rules', '', _('import narrowspecs from a file')),
      ('', 'removeexclude', [], _('old paths to no longer exclude')),
      ('', 'clear', False, _('whether to replace the existing narrowspec')),
      ('', 'force-delete-local-changes', False,
@@ -369,6 +371,23 @@
         ui.warn(_('The --clear option is not yet supported.\n'))
         return 1
 
+    # import rules from a file
+    newrules = opts.get('import_rules')
+    if newrules:
+        try:
+            filepath = os.path.join(pycompat.getcwd(), newrules)
+            fdata = util.readfile(filepath)
+        except IOError as inst:
+            raise error.Abort(_("cannot read narrowspecs from '%s': %s") %
+                              (filepath, encoding.strtolocal(inst.strerror)))
+        includepats, excludepats, profiles = sparse.parseconfig(ui, fdata,
+                                                                'narrow')
+        if profiles:
+            raise error.Abort(_("including other spec files using '%include' "
+                                "is not supported in narrowspec"))
+        opts['addinclude'].extend(includepats)
+        opts['addexclude'].extend(excludepats)
+
     if narrowspec.needsexpansion(opts['addinclude'] + opts['addexclude']):
         raise error.Abort('Expansion not yet supported on widen/narrow')