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 canonical_cmd(self, key):
33 """Return the canonical name for a possibly-shortenned
36 candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
39 print >> sys.stderr, 'Unknown command: %s' % key
40 print >> sys.stderr, ' Try "%s help" for a list of ' \
41 'supported commands' % prog
43 elif len(candidates) > 1:
44 print >> sys.stderr, 'Ambiguous command: %s' % key
45 print >> sys.stderr, ' Candidates are: %s' \
46 % ', '.join(candidates)
51 def __getitem__(self, key):
52 """Return the command python module name based.
56 cmd_mod = self.get(key) or self.get(self.canonical_cmd(key))
58 __import__('stgit.commands.' + cmd_mod)
59 return getattr(stgit.commands, cmd_mod)
64 'assimilate': 'assimilate',
92 'resolved': 'resolved',
100 'unapplied': 'unapplied',
101 'uncommit': 'uncommit',
105 # classification: repository, stack, patch, working copy
156 def _print_helpstring(cmd):
157 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
160 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
162 print 'Generic commands:'
163 print ' help print the detailed command usage'
164 print ' version display version information'
165 print ' copyright display copyright information'
166 # unclassified commands if any
167 cmds = commands.keys()
170 if not cmd in repocommands and not cmd in stackcommands \
171 and not cmd in patchcommands and not cmd in wccommands:
172 _print_helpstring(cmd)
175 print 'Repository commands:'
176 for cmd in repocommands:
177 _print_helpstring(cmd)
180 print 'Stack commands:'
181 for cmd in stackcommands:
182 _print_helpstring(cmd)
185 print 'Patch commands:'
186 for cmd in patchcommands:
187 _print_helpstring(cmd)
190 print 'Working-copy commands:'
191 for cmd in wccommands:
192 _print_helpstring(cmd)
195 # The main function (command dispatcher)
202 prog = os.path.basename(sys.argv[0])
204 if len(sys.argv) < 2:
205 print >> sys.stderr, 'usage: %s <command>' % prog
206 print >> sys.stderr, \
207 ' Try "%s --help" for a list of supported commands' % prog
212 if cmd in ['-h', '--help']:
213 if len(sys.argv) >= 3:
214 cmd = commands.canonical_cmd(sys.argv[2])
215 sys.argv[2] = '--help'
220 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
221 cmd = commands.canonical_cmd(sys.argv[2])
222 if not cmd in commands:
223 print >> sys.stderr, '%s help: "%s" command unknown' \
227 sys.argv[0] += ' %s' % cmd
228 command = commands[cmd]
229 parser = OptionParser(usage = command.usage,
230 option_list = command.options)
231 from pydoc import pager
232 pager(parser.format_help())
236 if cmd in ['-v', '--version', 'version']:
237 from stgit.version import version
238 print 'Stacked GIT %s' % version
239 os.system('git --version')
240 print 'Python version %s' % sys.version
242 if cmd in ['copyright']:
246 # re-build the command line arguments
247 sys.argv[0] += ' %s' % commands.canonical_cmd(cmd)
250 command = commands[cmd]
251 usage = command.usage.split('\n')[0].strip()
252 parser = OptionParser(usage = usage, option_list = command.options)
253 options, args = parser.parse_args()
255 # These modules are only used from this point onwards and do not
256 # need to be imported earlier
257 from stgit.config import config_setup
258 from ConfigParser import ParsingError, NoSectionError
259 from stgit.stack import Series, StackException
260 from stgit.git import GitException
261 from stgit.commands.common import CmdException
262 from stgit.gitmergeonefile import GitMergeException
263 from stgit.utils import EditorException
266 debug_level = int(os.environ['STGIT_DEBUG_LEVEL'])
270 print >> sys.stderr, 'Invalid STGIT_DEBUG_LEVEL environment variable'
276 # 'clone' doesn't expect an already initialised GIT tree. A Series
277 # object will be created after the GIT tree is cloned
279 if hasattr(options, 'branch') and options.branch:
280 command.crt_series = Series(options.branch)
282 command.crt_series = Series()
283 stgit.commands.common.crt_series = command.crt_series
285 command.func(parser, options, args)
286 except (IOError, ParsingError, NoSectionError, CmdException,
287 StackException, GitException, GitMergeException,
288 EditorException), err:
289 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
294 except KeyboardInterrupt: