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',
86 'resolved': 'resolved',
93 'unapplied': 'unapplied',
94 'uncommit': 'uncommit'
97 # classification: repository, stack, patch, working copy
144 def _print_helpstring(cmd):
145 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
148 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
150 print 'Generic commands:'
151 print ' help print the detailed command usage'
152 print ' version display version information'
153 print ' copyright display copyright information'
154 # unclassified commands if any
155 cmds = commands.keys()
158 if not cmd in repocommands and not cmd in stackcommands \
159 and not cmd in patchcommands and not cmd in wccommands:
160 _print_helpstring(cmd)
163 print 'Repository commands:'
164 for cmd in repocommands:
165 _print_helpstring(cmd)
168 print 'Stack commands:'
169 for cmd in stackcommands:
170 _print_helpstring(cmd)
173 print 'Patch commands:'
174 for cmd in patchcommands:
175 _print_helpstring(cmd)
178 print 'Working-copy commands:'
179 for cmd in wccommands:
180 _print_helpstring(cmd)
183 # The main function (command dispatcher)
190 prog = os.path.basename(sys.argv[0])
192 if len(sys.argv) < 2:
193 print >> sys.stderr, 'usage: %s <command>' % prog
194 print >> sys.stderr, \
195 ' Try "%s --help" for a list of supported commands' % prog
200 if cmd in ['-h', '--help']:
201 if len(sys.argv) >= 3 and sys.argv[2] in commands:
203 sys.argv[2] = '--help'
208 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
210 if not cmd in commands:
211 print >> sys.stderr, '%s help: "%s" command unknown' \
215 sys.argv[0] += ' %s' % cmd
216 command = commands[cmd]
217 parser = OptionParser(usage = command.usage,
218 option_list = command.options)
219 from pydoc import pager
220 pager(parser.format_help())
224 if cmd in ['-v', '--version', 'version']:
225 from stgit.version import version
226 print 'Stacked GIT %s' % version
227 os.system('git --version')
228 print 'Python version %s' % sys.version
230 if cmd in ['copyright']:
234 # re-build the command line arguments
235 sys.argv[0] += ' %s' % cmd
238 command = commands[cmd]
239 usage = command.usage.split('\n')[0].strip()
240 parser = OptionParser(usage = usage, option_list = command.options)
241 options, args = parser.parse_args()
243 # These modules are only used from this point onwards and do not
244 # need to be imported earlier
245 from stgit.config import config_setup
246 from ConfigParser import ParsingError, NoSectionError
247 from stgit.stack import Series, StackException
248 from stgit.git import GitException
249 from stgit.commands.common import CmdException
250 from stgit.gitmergeonefile import GitMergeException
255 # 'clone' doesn't expect an already initialised GIT tree. A Series
256 # object will be created after the GIT tree is cloned
258 if hasattr(options, 'branch') and options.branch:
259 command.crt_series = Series(options.branch)
261 command.crt_series = Series()
262 stgit.commands.common.crt_series = command.crt_series
264 command.func(parser, options, args)
265 except (IOError, ParsingError, NoSectionError, CmdException,
266 StackException, GitException, GitMergeException), err:
267 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
269 except KeyboardInterrupt: