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
25 from stgit.stack import Series, StackException
26 from stgit.git import GitException
27 from stgit.commands.common import CmdException
28 from stgit.gitmergeonefile import GitMergeException
34 """Commands class. It performs on-demand module loading
36 def __getitem__(self, key):
37 cmd_mod = self.get(key)
38 __import__('stgit.commands.' + cmd_mod)
39 return getattr(stgit.commands, cmd_mod)
44 'assimilate': 'assimilate',
69 'resolved': 'resolved',
75 'unapplied': 'unapplied',
76 'uncommit': 'uncommit'
79 # classification: repository, stack, patch, working copy
124 def _print_helpstring(cmd):
125 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
128 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
130 print 'Generic commands:'
131 print ' help print the detailed command usage'
132 print ' version display version information'
133 print ' copyright display copyright information'
134 # unclassified commands if any
135 cmds = commands.keys()
138 if not cmd in repocommands and not cmd in stackcommands \
139 and not cmd in patchcommands and not cmd in wccommands:
140 _print_helpstring(cmd)
143 print 'Repository commands:'
144 for cmd in repocommands:
145 _print_helpstring(cmd)
148 print 'Stack commands:'
149 for cmd in stackcommands:
150 _print_helpstring(cmd)
153 print 'Patch commands:'
154 for cmd in patchcommands:
155 _print_helpstring(cmd)
158 print 'Working-copy commands:'
159 for cmd in wccommands:
160 _print_helpstring(cmd)
163 # The main function (command dispatcher)
168 prog = os.path.basename(sys.argv[0])
170 if len(sys.argv) < 2:
171 print >> sys.stderr, 'usage: %s <command>' % prog
172 print >> sys.stderr, \
173 ' Try "%s --help" for a list of supported commands' % prog
178 if cmd in ['-h', '--help']:
179 if len(sys.argv) >= 3 and sys.argv[2] in commands:
181 sys.argv[2] = '--help'
186 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
188 if not cmd in commands:
189 print >> sys.stderr, '%s help: "%s" command unknown' \
193 sys.argv[0] += ' %s' % cmd
194 command = commands[cmd]
195 parser = OptionParser(usage = command.usage,
196 option_list = command.options)
201 if cmd in ['-v', '--version', 'version']:
202 from stgit.version import version
203 print 'Stacked GIT %s' % version
204 os.system('git --version')
205 print 'Python version %s' % sys.version
207 if cmd in ['copyright']:
210 if not cmd in commands:
211 print >> sys.stderr, 'Unknown command: %s' % cmd
212 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
216 # re-build the command line arguments
217 sys.argv[0] += ' %s' % cmd
220 command = commands[cmd]
221 usage = command.usage.split('\n')[0].strip()
222 parser = OptionParser(usage = usage, option_list = command.options)
223 options, args = parser.parse_args()
225 # 'clone' doesn't expect an already initialised GIT tree. A Series
226 # object will be created after the GIT tree is cloned
228 if hasattr(options, 'branch') and options.branch:
229 command.crt_series = Series(options.branch)
231 command.crt_series = Series()
232 stgit.commands.common.crt_series = command.crt_series
234 command.func(parser, options, args)
235 except (IOError, CmdException, StackException, GitException,
236 GitMergeException), err:
237 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
239 except KeyboardInterrupt: