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 cmd_mod = self.get(key)
34 __import__('stgit.commands.' + cmd_mod)
35 return getattr(stgit.commands, cmd_mod)
40 'assimilate': 'assimilate',
65 'resolved': 'resolved',
72 'unapplied': 'unapplied',
73 'uncommit': 'uncommit'
76 # classification: repository, stack, patch, working copy
122 def _print_helpstring(cmd):
123 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
126 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
128 print 'Generic commands:'
129 print ' help print the detailed command usage'
130 print ' version display version information'
131 print ' copyright display copyright information'
132 # unclassified commands if any
133 cmds = commands.keys()
136 if not cmd in repocommands and not cmd in stackcommands \
137 and not cmd in patchcommands and not cmd in wccommands:
138 _print_helpstring(cmd)
141 print 'Repository commands:'
142 for cmd in repocommands:
143 _print_helpstring(cmd)
146 print 'Stack commands:'
147 for cmd in stackcommands:
148 _print_helpstring(cmd)
151 print 'Patch commands:'
152 for cmd in patchcommands:
153 _print_helpstring(cmd)
156 print 'Working-copy commands:'
157 for cmd in wccommands:
158 _print_helpstring(cmd)
161 # The main function (command dispatcher)
166 prog = os.path.basename(sys.argv[0])
168 if len(sys.argv) < 2:
169 print >> sys.stderr, 'usage: %s <command>' % prog
170 print >> sys.stderr, \
171 ' Try "%s --help" for a list of supported commands' % prog
176 if cmd in ['-h', '--help']:
177 if len(sys.argv) >= 3 and sys.argv[2] in commands:
179 sys.argv[2] = '--help'
184 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
186 if not cmd in commands:
187 print >> sys.stderr, '%s help: "%s" command unknown' \
191 sys.argv[0] += ' %s' % cmd
192 command = commands[cmd]
193 parser = OptionParser(usage = command.usage,
194 option_list = command.options)
195 from pydoc import pager
196 pager(parser.format_help())
200 if cmd in ['-v', '--version', 'version']:
201 from stgit.version import version
202 print 'Stacked GIT %s' % version
203 os.system('git --version')
204 print 'Python version %s' % sys.version
206 if cmd in ['copyright']:
209 if not cmd in commands:
210 print >> sys.stderr, 'Unknown command: %s' % cmd
211 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
215 # re-build the command line arguments
216 sys.argv[0] += ' %s' % cmd
219 command = commands[cmd]
220 usage = command.usage.split('\n')[0].strip()
221 parser = OptionParser(usage = usage, option_list = command.options)
222 options, args = parser.parse_args()
224 # These modules are only used from this point onwards and do not
225 # need to be imported earlier
226 from stgit.config import config_setup
227 from ConfigParser import ParsingError, NoSectionError
228 from stgit.stack import Series, StackException
229 from stgit.git import GitException
230 from stgit.commands.common import CmdException
231 from stgit.gitmergeonefile import GitMergeException
236 # 'clone' doesn't expect an already initialised GIT tree. A Series
237 # object will be created after the GIT tree is cloned
239 if hasattr(options, 'branch') and options.branch:
240 command.crt_series = Series(options.branch)
242 command.crt_series = Series()
243 stgit.commands.common.crt_series = command.crt_series
245 command.func(parser, options, args)
246 except (IOError, ParsingError, NoSectionError, CmdException,
247 StackException, GitException, GitMergeException), err:
248 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
250 except KeyboardInterrupt: