Commits

Yujie Wu committed 7e6ef68

Implemented a mechanism to support common options, such as --user.

Comments (0)

Files changed (1)

 #
 # hgflow.py - Mercurial extension to support generalized Driessen's branching model
 # Copyright (C) 2011-2012, Yujie Wu
-
+#
 # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; either version 2 of the License or any later version.
 #
         self._cmd_history = []
         self._via_quiet   = False
         self._dryrun      = False
-        
+        self._common_opts = {}
+
         
     
     def __getattr__( self, name ) :
         cmd_str = "hg " + (self._cmd[:-1] if (self._cmd[-1] == "_") else self._cmd)
         arg     = self._branch2str(   arg )
         kwarg   = self._branch2str( kwarg )
+        cmd     = self._cmd
+
+        if (cmd[0] == "q") :
+            where = extensions.find( "mq" )
+            cmd   = cmd[1:]
+        elif (cmd == "strip" ) : where = extensions.find( "mq"     )
+        elif (cmd == "rebase") : where = extensions.find( "rebase" )
+        else                   : where = mercurial.commands
+
+        opts = self._filter_common_options( where, cmd )
+        opts.update( kwarg )
+        kwarg = opts
 
         for key, value in kwarg.items() :
             if (value is None) :
         
         try :
             ret = None
-            cmd = self._cmd
-            
-            if (cmd[0] == "q") :
-                where = extensions.find( "mq" )
-                cmd   = cmd[1:]
-            elif (cmd == "strip" ) : where = extensions.find( "mq"     )
-            elif (cmd == "rebase") : where = extensions.find( "rebase" )
-            else                   : where = mercurial.commands
-            
-            ret = getattr( where, cmd )( ui, repo, *arg, **kwarg )
+            ret = getattr( where, cmd )( ui, repo, *arg, **opts )
             if (ret and cmd not in ["commit", "rebase",]) :
                 raise AbortFlow( "Nonzero return code from hg command" )
         except Exception, e :
             return new_value
         return value
     
-            
+
+
+    def _filter_common_options( self, where, cmd ) :
+        """
+        If any common options are valid options of the command, return these options.
+
+        @type  where: module
+        @param where: Should be `mercurial.commands', or a plugin object.
+        @type  cmd  : C{str}
+        @param cmd  : command name
+        """
+        ret = {}
+        if (self._common_opts != {}) :
+            junk, table = mercurial.cmdutil.findcmd( cmd, where.table )
+            opts        = [e[1] for e in table[1]]
+            for e in self._common_opts :
+                if (e in opts) :
+                    ret[e] = self._common_opts[e]
+        return ret
+
+     
 
     def use_quiet_channel( self, via_quiet = True ) :
         """
 
 
 
+    def reg_common_options( self, opts ) :
+        """
+        Register common options.
+
+        @type  opts: C{dict}
+        @param opts: Common options. Key = option's flag, value = option's value. 
+        """
+        self._common_opts.update( opts )
+
+
+
     def dryrun( self, switch = None ) :
         """
         Switch the dry-run mode.
     if (kwarg.get( "dry_run" )) :
         _print( ui, "This is a dry run." )
         commands.use_quiet_channel( True )
-        
+
+    # Registers common options (such as "user").
+    common_opts = {}
+    for e in ["user",] :
+        v = kwarg.get( e )
+        if (v) :
+            common_opts[e] = v
+    commands.reg_common_options( common_opts )
+
     try :
         # If `cmd' is a command (instead of an action), checks the options for it.
         if (cmd in func) :
                 else :
                     ret[e] = not default_value
 
-    bad_opt = [e for e in     opt if (e not in (["history", "dry_run",] + ret.keys()) and opt[e])  ]
+    bad_opt = [e for e in     opt if (e not in (["history", "dry_run", "user"] + ret.keys()) and opt[e]) ]
     bad_opt = [e for e in bad_opt if (e in sys.argv) or (OPT_CONFLICT.get( e, [0,] )[0] not in rec_short)]
     
     if (bad_opt) :
     (flow_cmd,
      [("",  "history",   False, _("Print history of hg commands used in this workflow."),                          ),
       ("",  "dry-run",   None,  _("Do not perform actions, just print history."),                                  ),
+      ("u", "user",      '',    _("Use specified user instead of default user wherever valid."),         _('USER'),),
       ("f", "force",     False, _("Force reinitializing flow. [init]"),                                            ),
       ("r", "rev",       '',    _("Revision to start a new branch from. [start]"),                       _('REV'), ),
       ("",  "dirty",     False, _("Start a new branch from a dirty workspace, and move all"