[StGit PATCH 2/3] Add support for command aliases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch introduces support StGit command aliases with a few defaults:

stg add		-> git add
stg rm		-> git rm
stg mv		-> git mv
stg resolved	-> git add

Signed-off-by: Catalin Marinas <catalin.marinas@xxxxxxxxx>
---
 stgit/commands/__init__.py |    3 ++-
 stgit/config.py            |   11 ++++++++++-
 stgit/main.py              |   35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/stgit/commands/__init__.py b/stgit/commands/__init__.py
index f6cf3c3..f9de42b 100644
--- a/stgit/commands/__init__.py
+++ b/stgit/commands/__init__.py
@@ -28,7 +28,8 @@ def get_command(mod):
 _kinds = [('repo', 'Repository commands'),
           ('stack', 'Stack (branch) commands'),
           ('patch', 'Patch commands'),
-          ('wc', 'Index/worktree commands')]
+          ('wc', 'Index/worktree commands'),
+          ('alias', 'Alias commands')]
 _kind_order = [kind for kind, desc in _kinds]
 _kinds = dict(_kinds)
 
diff --git a/stgit/config.py b/stgit/config.py
index 811138d..dd194d3 100644
--- a/stgit/config.py
+++ b/stgit/config.py
@@ -36,7 +36,11 @@ class GitConfig:
         'stgit.autoimerge':     ['no'],
         'stgit.keepoptimized':  ['no'],
         'stgit.shortnr':        ['5'],
-        'stgit.pager':          ['less']
+        'stgit.pager':          ['less'],
+        'stgit.alias.add':      ['git add'],
+        'stgit.alias.rm':       ['git rm'],
+        'stgit.alias.mv':       ['git mv'],
+        'stgit.alias.resolved': ['git add']
         }
 
     __cache = None
@@ -76,6 +80,11 @@ class GitConfig:
         else:
             raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
 
+    def getstartswith(self, name):
+        self.load()
+        return ((n, v[-1]) for (n, v) in self.__cache.iteritems()
+                if n.startswith(name))
+
     def rename_section(self, from_name, to_name):
         """Rename a section in the config file. Silently do nothing if
         the section doesn't exist."""
diff --git a/stgit/main.py b/stgit/main.py
index e324179..d5be70b 100644
--- a/stgit/main.py
+++ b/stgit/main.py
@@ -23,6 +23,30 @@ import sys, os, traceback
 import stgit.commands
 from stgit.out import *
 from stgit import argparse, run, utils
+from stgit.config import config
+
+class CommandAlias(object):
+    def __init__(self, name, command):
+        self.__command = command
+        self.__name__ = name
+        self.usage = ['<arguments>']
+        self.help = 'Alias for "%s <arguments>".' % self.__command
+        self.options = []
+
+    def func(self, args):
+        cmd = self.__command.split() + args
+        p = run.Run(*cmd)
+        p.discard_exitcode().run()
+        return p.exitcode
+
+def is_cmd_alias(cmd):
+    return isinstance(cmd, CommandAlias)
+
+def append_alias_commands(cmd_list):
+    for (name, command) in config.getstartswith('stgit.alias.'):
+        name = utils.strip_prefix('stgit.alias.', name)
+        cmd_list[name] = (CommandAlias(name, command),
+                          'Alias commands', command)
 
 #
 # The commands map
@@ -49,9 +73,13 @@ class Commands(dict):
         
     def __getitem__(self, key):
         cmd_mod = self.get(key) or self.get(self.canonical_cmd(key))
-        return stgit.commands.get_command(cmd_mod)
+        if is_cmd_alias(cmd_mod):
+            return cmd_mod
+        else:
+            return stgit.commands.get_command(cmd_mod)
 
 cmd_list = stgit.commands.get_commands()
+append_alias_commands(cmd_list)
 commands = Commands((cmd, mod) for cmd, (mod, kind, help)
                     in cmd_list.iteritems())
 
@@ -100,6 +128,8 @@ def _main():
             sys.argv[0] += ' %s' % cmd
             command = commands[cmd]
             parser = argparse.make_option_parser(command)
+            if is_cmd_alias(command):
+                parser.remove_option('-h')
             from pydoc import pager
             pager(parser.format_help())
         else:
@@ -121,6 +151,9 @@ def _main():
     del(sys.argv[1])
 
     command = commands[cmd]
+    if is_cmd_alias(command):
+        sys.exit(command.func(sys.argv[1:]))
+
     parser = argparse.make_option_parser(command)
     options, args = parser.parse_args()
     directory = command.directory

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]