1 """Basic quilt-like functionality
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 import sys, os, traceback
24 from stgit.out import *
25 from stgit import argparse, run, utils
31 """Commands class. It performs on-demand module loading
33 def canonical_cmd(self, key):
34 """Return the canonical name for a possibly-shortenned
37 candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
40 out.error('Unknown command: %s' % key,
41 'Try "%s help" for a list of supported commands' % prog)
42 sys.exit(utils.STGIT_GENERAL_ERROR)
43 elif len(candidates) > 1:
44 out.error('Ambiguous command: %s' % key,
45 'Candidates are: %s' % ', '.join(candidates))
46 sys.exit(utils.STGIT_GENERAL_ERROR)
50 def __getitem__(self, key):
51 """Return the command python module name based.
55 cmd_mod = self.get(key) or self.get(self.canonical_cmd(key))
57 __import__('stgit.commands.' + cmd_mod)
58 return getattr(stgit.commands, cmd_mod)
66 'coalesce': 'coalesce',
90 'resolved': 'resolved',
97 'uncommit': 'uncommit',
101 # classification: repository, stack, patch, working copy
149 def _print_helpstring(cmd):
150 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
153 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
155 print 'Generic commands:'
156 print ' help print the detailed command usage'
157 print ' version display version information'
158 print ' copyright display copyright information'
159 # unclassified commands if any
160 cmds = commands.keys()
163 if not cmd in repocommands and not cmd in stackcommands \
164 and not cmd in patchcommands and not cmd in wccommands:
165 _print_helpstring(cmd)
168 print 'Repository commands:'
169 for cmd in repocommands:
170 _print_helpstring(cmd)
173 print 'Stack commands:'
174 for cmd in stackcommands:
175 _print_helpstring(cmd)
178 print 'Patch commands:'
179 for cmd in patchcommands:
180 _print_helpstring(cmd)
183 print 'Working-copy commands:'
184 for cmd in wccommands:
185 _print_helpstring(cmd)
188 # The main function (command dispatcher)
195 prog = os.path.basename(sys.argv[0])
197 if len(sys.argv) < 2:
198 print >> sys.stderr, 'usage: %s <command>' % prog
199 print >> sys.stderr, \
200 ' Try "%s --help" for a list of supported commands' % prog
201 sys.exit(utils.STGIT_GENERAL_ERROR)
205 if cmd in ['-h', '--help']:
206 if len(sys.argv) >= 3:
207 cmd = commands.canonical_cmd(sys.argv[2])
208 sys.argv[2] = '--help'
211 sys.exit(utils.STGIT_SUCCESS)
213 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
214 cmd = commands.canonical_cmd(sys.argv[2])
215 if not cmd in commands:
216 out.error('%s help: "%s" command unknown' % (prog, cmd))
217 sys.exit(utils.STGIT_GENERAL_ERROR)
219 sys.argv[0] += ' %s' % cmd
220 command = commands[cmd]
221 parser = argparse.make_option_parser(command)
222 from pydoc import pager
223 pager(parser.format_help())
226 sys.exit(utils.STGIT_SUCCESS)
227 if cmd in ['-v', '--version', 'version']:
228 from stgit.version import version
229 print 'Stacked GIT %s' % version
230 os.system('git --version')
231 print 'Python version %s' % sys.version
232 sys.exit(utils.STGIT_SUCCESS)
233 if cmd in ['copyright']:
235 sys.exit(utils.STGIT_SUCCESS)
237 # re-build the command line arguments
238 cmd = commands.canonical_cmd(cmd)
239 sys.argv[0] += ' %s' % cmd
242 command = commands[cmd]
243 parser = argparse.make_option_parser(command)
244 options, args = parser.parse_args()
245 directory = command.directory
247 # These modules are only used from this point onwards and do not
248 # need to be imported earlier
249 from stgit.exception import StgException
250 from stgit.config import config_setup
251 from ConfigParser import ParsingError, NoSectionError
252 from stgit.stack import Series
255 debug_level = int(os.environ.get('STGIT_DEBUG_LEVEL', 0))
257 out.error('Invalid STGIT_DEBUG_LEVEL environment variable')
258 sys.exit(utils.STGIT_GENERAL_ERROR)
264 # Some commands don't (always) need an initialized series.
265 if directory.needs_current_series:
266 if hasattr(options, 'branch') and options.branch:
267 command.crt_series = Series(options.branch)
269 command.crt_series = Series()
271 ret = command.func(parser, options, args)
272 except (StgException, IOError, ParsingError, NoSectionError), err:
273 out.error(str(err), title = '%s %s' % (prog, cmd))
275 traceback.print_exc()
276 sys.exit(utils.STGIT_COMMAND_ERROR)
278 # Triggered by the option parser when it finds bad commandline
280 sys.exit(utils.STGIT_COMMAND_ERROR)
281 except KeyboardInterrupt:
282 sys.exit(utils.STGIT_GENERAL_ERROR)
284 out.error('Unhandled exception:')
285 traceback.print_exc()
286 sys.exit(utils.STGIT_BUG_ERROR)
288 sys.exit(ret or utils.STGIT_SUCCESS)