aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCatalin Marinas <catalin.marinas@gmail.com>2010-02-05 13:45:01 +0000
committerCatalin Marinas <catalin.marinas@gmail.com>2010-02-05 13:45:01 +0000
commit3ecc9c014b8c7ac00d9f17230dfcff7438df03a9 (patch)
treee0ad9b00658b3a6ca60b7f9bbafb90cb9069db22
parentd0329a7785d86495fd02ca595d9cb94fc67cdf4d (diff)
downloadstgit-3ecc9c014b8c7ac00d9f17230dfcff7438df03a9.tar.gz
Add support for command aliases
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@gmail.com>
-rw-r--r--examples/gitconfig4
-rw-r--r--stgit/commands/__init__.py3
-rw-r--r--stgit/config.py11
-rw-r--r--stgit/main.py35
4 files changed, 50 insertions, 3 deletions
diff --git a/examples/gitconfig b/examples/gitconfig
index 4f56918..f6aab37 100644
--- a/examples/gitconfig
+++ b/examples/gitconfig
@@ -85,6 +85,10 @@
# Behave as if the --keep option is always passed
#autokeep = no
+[stgit "alias"]
+ # Command aliases.
+ #add = git add
+
[mail "alias"]
# E-mail aliases used with the 'mail' command
git = git@vger.kernel.org
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