Add "stg bury" command, with the functionality of contrib/stg-sink.
[stgit] / stgit / main.py
index f9b07d7..9c319c6 100644 (file)
@@ -29,27 +29,31 @@ import stgit.commands
 class Commands(dict):
     """Commands class. It performs on-demand module loading
     """
+    def canonical_cmd(self, key):
+        """Return the canonical name for a possibly-shortenned
+        command name.
+        """
+        candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
+
+        if not candidates:
+            print >> sys.stderr, 'Unknown command: %s' % key
+            print >> sys.stderr, '  Try "%s help" for a list of ' \
+                  'supported commands' % prog
+            sys.exit(1)
+        elif len(candidates) > 1:
+            print >> sys.stderr, 'Ambiguous command: %s' % key
+            print >> sys.stderr, '  Candidates are: %s' \
+                  % ', '.join(candidates)
+            sys.exit(1)
+
+        return candidates[0]
+        
     def __getitem__(self, key):
         """Return the command python module name based.
         """
         global prog
 
-        cmd_mod = self.get(key)
-        if not cmd_mod:
-            candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
-
-            if not candidates:
-                print >> sys.stderr, 'Unknown command: %s' % key
-                print >> sys.stderr, '  Try "%s help" for a list of ' \
-                      'supported commands' % prog
-                sys.exit(1)
-            elif len(candidates) > 1:
-                print >> sys.stderr, 'Ambiguous command: %s' % key
-                print >> sys.stderr, '  Candidates are: %s' \
-                      % ', '.join(candidates)
-                sys.exit(1)
-
-            cmd_mod = self.get(candidates[0])
+        cmd_mod = self.get(key) or self.get(self.canonical_cmd(key))
             
         __import__('stgit.commands.' + cmd_mod)
         return getattr(stgit.commands, cmd_mod)
@@ -59,16 +63,19 @@ commands = Commands({
     'applied':          'applied',
     'assimilate':       'assimilate',
     'branch':           'branch',
+    'bury':             'bury',
     'delete':           'delete',
     'diff':             'diff',
     'clean':            'clean',
     'clone':            'clone',
     'commit':           'commit',
+    'cp':              'copy',
     'export':           'export',
     'files':            'files',
     'float':            'float',
     'fold':             'fold',
     'goto':             'goto',
+    'hide':             'hide',
     'id':               'id',
     'import':           'imprt',
     'init':             'init',
@@ -91,31 +98,36 @@ commands = Commands({
     'sync':             'sync',
     'top':              'top',
     'unapplied':        'unapplied',
-    'uncommit':         'uncommit'
+    'uncommit':         'uncommit',
+    'unhide':           'unhide'
     })
 
 # classification: repository, stack, patch, working copy
 repocommands = (
-    'branch',
     'clone',
     'id',
-    'pull'
     )
 stackcommands = (
     'applied',
     'assimilate',
+    'branch',
+    'bury',
     'clean',
     'commit',
     'float',
     'goto',
+    'hide',
     'init',
+    'patches',
     'pop',
+    'pull',
     'push',
     'rebase',
     'series',
     'top',
     'unapplied',
-    'uncommit'
+    'uncommit',
+    'unhide',
     )
 patchcommands = (
     'delete',
@@ -130,15 +142,15 @@ patchcommands = (
     'refresh',
     'rename',
     'show',
-    'sync'
+    'sync',
     )
 wccommands = (
     'add',
+    'cp',
     'diff',
-    'patches',
     'resolved',
     'rm',
-    'status'
+    'status',
     )
 
 def _print_helpstring(cmd):
@@ -198,15 +210,15 @@ def main():
     cmd = sys.argv[1]
 
     if cmd in ['-h', '--help']:
-        if len(sys.argv) >= 3 and sys.argv[2] in commands:
-            cmd = sys.argv[2]
+        if len(sys.argv) >= 3:
+            cmd = commands.canonical_cmd(sys.argv[2])
             sys.argv[2] = '--help'
         else:
             print_help()
             sys.exit(0)
     if cmd == 'help':
         if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
-            cmd = sys.argv[2]
+            cmd = commands.canonical_cmd(sys.argv[2])
             if not cmd in commands:
                 print >> sys.stderr, '%s help: "%s" command unknown' \
                       % (prog, cmd)
@@ -232,7 +244,7 @@ def main():
         sys.exit(0)
 
     # re-build the command line arguments
-    sys.argv[0] += ' %s' % cmd
+    sys.argv[0] += ' %s' % commands.canonical_cmd(cmd)
     del(sys.argv[1])
 
     command = commands[cmd]
@@ -248,6 +260,15 @@ def main():
     from stgit.git import GitException
     from stgit.commands.common import CmdException
     from stgit.gitmergeonefile import GitMergeException
+    from stgit.utils import EditorException
+
+    try:
+        debug_level = int(os.environ['STGIT_DEBUG_LEVEL'])
+    except KeyError:
+        debug_level = 0
+    except ValueError:
+        print >> sys.stderr, 'Invalid STGIT_DEBUG_LEVEL environment variable'
+        sys.exit(1)
 
     try:
         config_setup()
@@ -263,9 +284,13 @@ def main():
 
         command.func(parser, options, args)
     except (IOError, ParsingError, NoSectionError, CmdException,
-            StackException, GitException, GitMergeException), err:
+            StackException, GitException, GitMergeException,
+            EditorException), err:
         print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
-        sys.exit(2)
+        if debug_level:
+            raise
+        else:
+            sys.exit(2)
     except KeyboardInterrupt:
         sys.exit(1)