git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas@arm.com>
To: "Karl Hasselström" <kha@treskal.com>
Cc: git@vger.kernel.org
Subject: [StGit PATCH 2/3] Add support for command aliases
Date: Fri, 08 Jan 2010 12:36:04 +0000	[thread overview]
Message-ID: <20100108123604.24161.25653.stgit@pc1117.cambridge.arm.com> (raw)
In-Reply-To: <20100108123403.24161.3495.stgit@pc1117.cambridge.arm.com>

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>
---
 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

  parent reply	other threads:[~2010-01-08 12:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-08 12:35 [StGit PATCH 0/3] Support for command aliases Catalin Marinas
2010-01-08 12:35 ` [StGit PATCH 1/3] Populate the cached config options with the defaults Catalin Marinas
2010-01-08 14:30   ` Karl Wiberg
2010-01-08 14:34     ` Karl Wiberg
2010-01-08 12:36 ` Catalin Marinas [this message]
2010-01-08 12:36 ` [StGit PATCH 3/3] Replace some git commands with stg aliases in test scripts Catalin Marinas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100108123604.24161.25653.stgit@pc1117.cambridge.arm.com \
    --to=catalin.marinas@arm.com \
    --cc=catalin.marinas@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=kha@treskal.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).