chiark / gitweb /
Do not raise an exception if no FETCH_HEAD
[stgit] / stgit / main.py
index 294b7035f35c5db1a6ca431c1832de3c708799be..e8242c2783bddd18122b345512f9e155495538f7 100644 (file)
@@ -22,7 +22,7 @@ import sys, os
 from optparse import OptionParser
 
 import stgit.commands
-from stgit.utils import out
+from stgit.out import *
 
 #
 # The commands map
@@ -68,6 +68,7 @@ commands = Commands({
     'clone':            'clone',
     'commit':           'commit',
     'cp':              'copy',
+    'edit':             'edit',
     'export':           'export',
     'files':            'files',
     'float':            'float',
@@ -130,6 +131,7 @@ stackcommands = (
     )
 patchcommands = (
     'delete',
+    'edit',
     'export',
     'files',
     'fold',
@@ -250,43 +252,36 @@ def main():
     usage = command.usage.split('\n')[0].strip()
     parser = OptionParser(usage = usage, option_list = command.options)
     options, args = parser.parse_args()
+    directory = command.directory
 
     # These modules are only used from this point onwards and do not
     # need to be imported earlier
+    from stgit.exception import StgException
     from stgit.config import config_setup
     from ConfigParser import ParsingError, NoSectionError
-    from stgit.stack import Series, StackException
-    from stgit.git import GitException
-    from stgit.commands.common import CmdException
-    from stgit.gitmergeonefile import GitMergeException
-    from stgit.utils import EditorException
+    from stgit.stack import Series
 
     try:
-        debug_level = int(os.environ['STGIT_DEBUG_LEVEL'])
-    except KeyError:
-        debug_level = 0
+        debug_level = int(os.environ.get('STGIT_DEBUG_LEVEL', 0))
     except ValueError:
         out.error('Invalid STGIT_DEBUG_LEVEL environment variable')
         sys.exit(1)
 
     try:
+        directory.setup()
         config_setup()
 
-        # 'clone' doesn't expect an already initialised GIT tree. A Series
-        # object will be created after the GIT tree is cloned
-        if cmd != 'clone':
+        # Some commands don't (always) need an initialized series.
+        if directory.needs_current_series:
             if hasattr(options, 'branch') and options.branch:
                 command.crt_series = Series(options.branch)
             else:
                 command.crt_series = Series()
-            stgit.commands.common.crt_series = command.crt_series
 
         command.func(parser, options, args)
-    except (IOError, ParsingError, NoSectionError, CmdException,
-            StackException, GitException, GitMergeException,
-            EditorException), err:
-        print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
-        if debug_level:
+    except (StgException, IOError, ParsingError, NoSectionError), err:
+        out.error(str(err), title = '%s %s' % (prog, cmd))
+        if debug_level > 0:
             raise
         else:
             sys.exit(2)