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
22 from optparse import OptionParser
30 """Commands class. It performs on-demand module loading
32 def __getitem__(self, key):
33 """Return the command python module name based.
37 cmd_mod = self.get(key)
39 candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
42 print >> sys.stderr, 'Unknown command: %s' % key
43 print >> sys.stderr, ' Try "%s help" for a list of ' \
44 'supported commands' % prog
46 elif len(candidates) > 1:
47 print >> sys.stderr, 'Ambiguous command: %s' % key
48 print >> sys.stderr, ' Candidates are: %s' \
49 % ', '.join(candidates)
52 cmd_mod = self.get(candidates[0])
54 __import__('stgit.commands.' + cmd_mod)
55 return getattr(stgit.commands, cmd_mod)
60 'assimilate': 'assimilate',
85 'resolved': 'resolved',
92 'unapplied': 'unapplied',
93 'uncommit': 'uncommit'
96 # classification: repository, stack, patch, working copy
142 def _print_helpstring(cmd):
143 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
146 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
148 print 'Generic commands:'
149 print ' help print the detailed command usage'
150 print ' version display version information'
151 print ' copyright display copyright information'
152 # unclassified commands if any
153 cmds = commands.keys()
156 if not cmd in repocommands and not cmd in stackcommands \
157 and not cmd in patchcommands and not cmd in wccommands:
158 _print_helpstring(cmd)
161 print 'Repository commands:'
162 for cmd in repocommands:
163 _print_helpstring(cmd)
166 print 'Stack commands:'
167 for cmd in stackcommands:
168 _print_helpstring(cmd)
171 print 'Patch commands:'
172 for cmd in patchcommands:
173 _print_helpstring(cmd)
176 print 'Working-copy commands:'
177 for cmd in wccommands:
178 _print_helpstring(cmd)
181 # The main function (command dispatcher)
188 prog = os.path.basename(sys.argv[0])
190 if len(sys.argv) < 2:
191 print >> sys.stderr, 'usage: %s <command>' % prog
192 print >> sys.stderr, \
193 ' Try "%s --help" for a list of supported commands' % prog
198 if cmd in ['-h', '--help']:
199 if len(sys.argv) >= 3 and sys.argv[2] in commands:
201 sys.argv[2] = '--help'
206 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
208 if not cmd in commands:
209 print >> sys.stderr, '%s help: "%s" command unknown' \
213 sys.argv[0] += ' %s' % cmd
214 command = commands[cmd]
215 parser = OptionParser(usage = command.usage,
216 option_list = command.options)
217 from pydoc import pager
218 pager(parser.format_help())
222 if cmd in ['-v', '--version', 'version']:
223 from stgit.version import version
224 print 'Stacked GIT %s' % version
225 os.system('git --version')
226 print 'Python version %s' % sys.version
228 if cmd in ['copyright']:
232 # re-build the command line arguments
233 sys.argv[0] += ' %s' % cmd
236 command = commands[cmd]
237 usage = command.usage.split('\n')[0].strip()
238 parser = OptionParser(usage = usage, option_list = command.options)
239 options, args = parser.parse_args()
241 # These modules are only used from this point onwards and do not
242 # need to be imported earlier
243 from stgit.config import config_setup
244 from ConfigParser import ParsingError, NoSectionError
245 from stgit.stack import Series, StackException
246 from stgit.git import GitException
247 from stgit.commands.common import CmdException
248 from stgit.gitmergeonefile import GitMergeException
253 # 'clone' doesn't expect an already initialised GIT tree. A Series
254 # object will be created after the GIT tree is cloned
256 if hasattr(options, 'branch') and options.branch:
257 command.crt_series = Series(options.branch)
259 command.crt_series = Series()
260 stgit.commands.common.crt_series = command.crt_series
262 command.func(parser, options, args)
263 except (IOError, ParsingError, NoSectionError, CmdException,
264 StackException, GitException, GitMergeException), err:
265 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
267 except KeyboardInterrupt: