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',
87 'resolved': 'resolved',
94 'unapplied': 'unapplied',
95 'uncommit': 'uncommit',
99 # classification: repository, stack, patch, working copy
148 def _print_helpstring(cmd):
149 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
152 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
154 print 'Generic commands:'
155 print ' help print the detailed command usage'
156 print ' version display version information'
157 print ' copyright display copyright information'
158 # unclassified commands if any
159 cmds = commands.keys()
162 if not cmd in repocommands and not cmd in stackcommands \
163 and not cmd in patchcommands and not cmd in wccommands:
164 _print_helpstring(cmd)
167 print 'Repository commands:'
168 for cmd in repocommands:
169 _print_helpstring(cmd)
172 print 'Stack commands:'
173 for cmd in stackcommands:
174 _print_helpstring(cmd)
177 print 'Patch commands:'
178 for cmd in patchcommands:
179 _print_helpstring(cmd)
182 print 'Working-copy commands:'
183 for cmd in wccommands:
184 _print_helpstring(cmd)
187 # The main function (command dispatcher)
194 prog = os.path.basename(sys.argv[0])
196 if len(sys.argv) < 2:
197 print >> sys.stderr, 'usage: %s <command>' % prog
198 print >> sys.stderr, \
199 ' Try "%s --help" for a list of supported commands' % prog
204 if cmd in ['-h', '--help']:
205 if len(sys.argv) >= 3 and sys.argv[2] in commands:
207 sys.argv[2] = '--help'
212 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
214 if not cmd in commands:
215 print >> sys.stderr, '%s help: "%s" command unknown' \
219 sys.argv[0] += ' %s' % cmd
220 command = commands[cmd]
221 parser = OptionParser(usage = command.usage,
222 option_list = command.options)
223 from pydoc import pager
224 pager(parser.format_help())
228 if cmd in ['-v', '--version', 'version']:
229 from stgit.version import version
230 print 'Stacked GIT %s' % version
231 os.system('git --version')
232 print 'Python version %s' % sys.version
234 if cmd in ['copyright']:
238 # re-build the command line arguments
239 sys.argv[0] += ' %s' % cmd
242 command = commands[cmd]
243 usage = command.usage.split('\n')[0].strip()
244 parser = OptionParser(usage = usage, option_list = command.options)
245 options, args = parser.parse_args()
247 # These modules are only used from this point onwards and do not
248 # need to be imported earlier
249 from stgit.config import config_setup
250 from ConfigParser import ParsingError, NoSectionError
251 from stgit.stack import Series, StackException
252 from stgit.git import GitException
253 from stgit.commands.common import CmdException
254 from stgit.gitmergeonefile import GitMergeException
259 # 'clone' doesn't expect an already initialised GIT tree. A Series
260 # object will be created after the GIT tree is cloned
262 if hasattr(options, 'branch') and options.branch:
263 command.crt_series = Series(options.branch)
265 command.crt_series = Series()
266 stgit.commands.common.crt_series = command.crt_series
268 command.func(parser, options, args)
269 except (IOError, ParsingError, NoSectionError, CmdException,
270 StackException, GitException, GitMergeException), err:
271 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
273 except KeyboardInterrupt: