py3: handle opts uniformly in commands.py
authorPulkit Goyal <7895pulkit@gmail.com>
Wed, 03 May 2017 15:25:06 +0530
changeset 32142 d90ffee93df6
parent 32141 4fadea09feca
child 32143 964c6be36590
py3: handle opts uniformly in commands.py Since keyword arguments can't be bytes on Python 3, we converted then to unicodes before passing into different command functions. We need to adopt a certain pattern to convert opts back to bytes. Following are some of the functions which are called from inside these command functions and should always be feeded bytes to follow the right behaviour. ui.fomattter() scmutil.match() patch.diffallopts() hg.peer() cmdutil.{show_changeset|copy|graphrevs|checkunsupportedflag} server.{createservice|runservice} There are few commands which are left out where opts is again passed to a function as keyword arguments or converting opts back to bytes is kind of not necessary. Those are cat, revert, help, unbundle and rollback. Following patches will deal with them. This patch apart from these five commands, convert opts back to bytes for rest of the commands. This fixes a lot of things which are hidden like --git works now. Similarly more flags of commands which run on Python 3 currently get fixed.
mercurial/commands.py
--- a/mercurial/commands.py	Thu May 04 00:26:55 2017 -0400
+++ b/mercurial/commands.py	Wed May 03 15:25:06 2017 +0530
@@ -255,6 +255,7 @@
     Returns 0 if all files are successfully added.
     """
 
+    opts = pycompat.byteskwargs(opts)
     m = scmutil.match(repo[None], pats, opts)
     rejected = cmdutil.add(ui, repo, m, "", False, **opts)
     return rejected and 1 or 0
@@ -325,6 +326,7 @@
 
     Returns 0 if all files are successfully added.
     """
+    opts = pycompat.byteskwargs(opts)
     try:
         sim = float(opts.get('similarity') or 100)
     except ValueError:
@@ -368,6 +370,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     if not pats:
         raise error.Abort(_('at least one filename or pattern is required'))
 
@@ -532,6 +535,7 @@
     Returns 0 on success.
     '''
 
+    opts = pycompat.byteskwargs(opts)
     ctx = scmutil.revsingle(repo, opts.get('rev'))
     if not ctx:
         raise error.Abort(_('no working directory: please specify a revision'))
@@ -627,6 +631,7 @@
         release(lock, wlock)
 
 def _dobackout(ui, repo, node=None, rev=None, **opts):
+    opts = pycompat.byteskwargs(opts)
     if opts.get('commit') and opts.get('no_commit'):
         raise error.Abort(_("cannot use --commit with --no-commit"))
     if opts.get('merge') and opts.get('no_commit'):
@@ -1001,6 +1006,7 @@
 
           hg book -f @
     '''
+    opts = pycompat.byteskwargs(opts)
     force = opts.get('force')
     rev = opts.get('rev')
     delete = opts.get('delete')
@@ -1177,6 +1183,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     if label:
         label = label.strip()
 
@@ -1225,6 +1232,7 @@
     Returns 0.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('branches')
     fm = ui.formatter('branches', opts)
     hexfunc = fm.hexfunc
@@ -1308,6 +1316,7 @@
 
     Returns 0 on success, 1 if no changes found.
     """
+    opts = pycompat.byteskwargs(opts)
     revs = None
     if 'rev' in opts:
         revstrings = opts['rev']
@@ -1548,6 +1557,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('noupdate') and opts.get('updaterev'):
         raise error.Abort(_("cannot specify both --noupdate and --updaterev"))
 
@@ -1638,16 +1648,16 @@
         release(lock, wlock)
 
 def _docommit(ui, repo, *pats, **opts):
-    opts = pycompat.byteskwargs(opts)
-    if opts.get('interactive'):
-        opts.pop('interactive')
+    if opts.get(r'interactive'):
+        opts.pop(r'interactive')
         ret = cmdutil.dorecord(ui, repo, commit, None, False,
                                cmdutil.recordfilter, *pats,
-                               **pycompat.strkwargs(opts))
+                               **opts)
         # ret can be 0 (no changes to record) or the value returned by
         # commit(), 1 if nothing changed or None on success.
         return 1 if ret == 0 else ret
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('subrepos'):
         if opts.get('amend'):
             raise error.Abort(_('cannot amend with --subrepos'))
@@ -1768,6 +1778,7 @@
 
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('edit') or opts.get('local') or opts.get('global'):
         if opts.get('local') and opts.get('global'):
             raise error.Abort(_("can't use --local and --global together"))
@@ -1870,6 +1881,7 @@
 
     Returns 0 on success, 1 if errors are encountered.
     """
+    opts = pycompat.byteskwargs(opts)
     with repo.wlock(False):
         return cmdutil.copy(ui, repo, pats, opts)
 
@@ -1937,6 +1949,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     revs = opts.get('rev')
     change = opts.get('change')
     stat = opts.get('stat')
@@ -2093,7 +2106,9 @@
     Returns 0 if a match is found, 1 otherwise.
 
     """
-    ctx = scmutil.revsingle(repo, opts.get(r'rev'), None)
+
+    opts = pycompat.byteskwargs(opts)
+    ctx = scmutil.revsingle(repo, opts.get('rev'), None)
 
     end = '\n'
     if opts.get('print0'):
@@ -2135,6 +2150,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not pats:
         raise error.Abort(_('no files specified'))
 
@@ -2219,6 +2235,7 @@
         return _dograft(ui, repo, *revs, **opts)
 
 def _dograft(ui, repo, *revs, **opts):
+    opts = pycompat.byteskwargs(opts)
     if revs and opts.get('rev'):
         ui.warn(_('warning: inconsistent use of --rev might give unexpected '
                   'revision ordering!\n'))
@@ -2436,6 +2453,7 @@
 
     Returns 0 if a match is found, 1 otherwise.
     """
+    opts = pycompat.byteskwargs(opts)
     reflags = re.M
     if opts.get('ignore_case'):
         reflags |= re.I
@@ -2682,6 +2700,7 @@
     Returns 0 if matching heads are found, 1 if not.
     """
 
+    opts = pycompat.byteskwargs(opts)
     start = None
     if 'rev' in opts:
         start = scmutil.revsingle(repo, opts['rev'], None).node()
@@ -2807,6 +2826,7 @@
     Returns 0 if successful.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not repo and not source:
         raise error.Abort(_("there is no Mercurial repository here "
                            "(.hg not found)"))
@@ -3030,6 +3050,7 @@
     Returns 0 on success, 1 on partial success (see --partial).
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not patch1:
         raise error.Abort(_('need at least one patch to import'))
 
@@ -3234,6 +3255,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     hg.peer(ui, opts, ui.expandpath(dest), create=True)
 
 @command('locate',
@@ -3264,6 +3286,7 @@
 
     Returns 0 if a match is found, 1 otherwise.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('print0'):
         end = '\0'
     else:
@@ -3470,6 +3493,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     fm = ui.formatter('manifest', opts)
 
     if opts.get('all'):
@@ -3548,6 +3572,7 @@
     Returns 0 on success, 1 if there are unresolved files.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('rev') and node:
         raise error.Abort(_("please specify just one revision"))
     if not node:
@@ -3627,6 +3652,7 @@
 
     Returns 0 if there are outgoing changes, 1 otherwise.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('graph'):
         cmdutil.checkunsupportedgraphflags([], opts)
         o, other = hg._outgoing(ui, repo, dest, opts)
@@ -3684,6 +3710,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ctx = scmutil.revsingle(repo, opts.get('rev'), None)
 
     if file_:
@@ -3746,6 +3773,8 @@
 
     Returns 0 on success.
     """
+
+    opts = pycompat.byteskwargs(opts)
     ui.pager('paths')
     if search:
         pathitems = [(name, path) for name, path in ui.paths.iteritems()
@@ -3808,6 +3837,7 @@
 
     (For more information about the phases concept, see :hg:`help phases`.)
     """
+    opts = pycompat.byteskwargs(opts)
     # search for a unique phase argument
     targetphase = None
     for idx, name in enumerate(phases.phasenames):
@@ -3940,6 +3970,7 @@
     Returns 0 on success, 1 if an update had unresolved files.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if ui.configbool('commands', 'update.requiredest') and opts.get('update'):
         msg = _('update destination required by configuration')
         hint = _('use hg pull followed by hg update DEST')
@@ -4070,6 +4101,7 @@
     Returns 0 if push was successful, 1 if nothing to push.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('bookmark'):
         ui.setconfig('bookmarks', 'pushing', opts['bookmark'], 'push')
         for b in opts['bookmark']:
@@ -4195,6 +4227,7 @@
     Returns 0 on success, 1 if any warnings encountered.
     """
 
+    opts = pycompat.byteskwargs(opts)
     after, force = opts.get('after'), opts.get('force')
     if not pats and not after:
         raise error.Abort(_('no files specified'))
@@ -4224,6 +4257,7 @@
 
     Returns 0 on success, 1 if errors are encountered.
     """
+    opts = pycompat.byteskwargs(opts)
     with repo.wlock(False):
         return cmdutil.copy(ui, repo, pats, opts, rename=True)
 
@@ -4278,6 +4312,7 @@
     Returns 0 on success, 1 if any files fail a resolve attempt.
     """
 
+    opts = pycompat.byteskwargs(opts)
     flaglist = 'all mark unmark list no_status'.split()
     all, mark, unmark, show, nostatus = \
         [opts.get(o) for o in flaglist]
@@ -4649,6 +4684,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts["stdio"] and opts["cmdserver"]:
         raise error.Abort(_("cannot use --stdio with --cmdserver"))
 
@@ -4814,6 +4850,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('summary')
     ctx = repo[None]
     parents = ctx.parents()
@@ -5122,6 +5159,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     wlock = lock = None
     try:
         wlock = repo.wlock()
@@ -5209,6 +5247,7 @@
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('tags')
     fm = ui.formatter('tags', opts)
     hexfunc = fm.hexfunc
@@ -5253,6 +5292,7 @@
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     displayer = cmdutil.show_changeset(ui, repo, opts)
     displayer.show(repo['tip'])
     displayer.close()
@@ -5427,6 +5467,7 @@
 @command('version', [] + formatteropts, norepo=True)
 def version_(ui, **opts):
     """output version and copyright information"""
+    opts = pycompat.byteskwargs(opts)
     if ui.verbose:
         ui.pager('version')
     fm = ui.formatter("version", opts)