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',
71 'unapplied': 'unapplied',
72 'uncommit': 'uncommit'
75 # classification: repository, stack, patch, working copy
120 def _print_helpstring(cmd
):
121 print ' ' + cmd
+ ' ' * (12 - len(cmd
)) + commands
[cmd
].help
124 print 'usage: %s <command> [options]' % os
.path
.basename(sys
.argv
[0])
126 print 'Generic commands:'
127 print ' help print the detailed command usage'
128 print ' version display version information'
129 print ' copyright display copyright information'
130 # unclassified commands if any
131 cmds
= commands
.keys()
134 if not cmd
in repocommands
and not cmd
in stackcommands \
135 and not cmd
in patchcommands
and not cmd
in wccommands
:
136 _print_helpstring(cmd
)
139 print 'Repository commands:'
140 for cmd
in repocommands
:
141 _print_helpstring(cmd
)
144 print 'Stack commands:'
145 for cmd
in stackcommands
:
146 _print_helpstring(cmd
)
149 print 'Patch commands:'
150 for cmd
in patchcommands
:
151 _print_helpstring(cmd
)
154 print 'Working-copy commands:'
155 for cmd
in wccommands
:
156 _print_helpstring(cmd
)
159 # The main function (command dispatcher)
164 prog
= os
.path
.basename(sys
.argv
[0])
166 if len(sys
.argv
) < 2:
167 print >> sys
.stderr
, 'usage: %s <command>' % prog
168 print >> sys
.stderr
, \
169 ' Try "%s --help" for a list of supported commands' % prog
174 if cmd
in ['-h', '--help']:
175 if len(sys
.argv
) >= 3 and sys
.argv
[2] in commands
:
177 sys
.argv
[2] = '--help'
182 if len(sys
.argv
) == 3 and not sys
.argv
[2] in ['-h', '--help']:
184 if not cmd
in commands
:
185 print >> sys
.stderr
, '%s help: "%s" command unknown' \
189 sys
.argv
[0] += ' %s' % cmd
190 command
= commands
[cmd
]
191 parser
= OptionParser(usage
= command
.usage
,
192 option_list
= command
.options
)
197 if cmd
in ['-v', '--version', 'version']:
198 from stgit
.version
import version
199 print 'Stacked GIT %s' % version
200 os
.system('git --version')
201 print 'Python version %s' % sys
.version
203 if cmd
in ['copyright']:
206 if not cmd
in commands
:
207 print >> sys
.stderr
, 'Unknown command: %s' % cmd
208 print >> sys
.stderr
, ' Try "%s help" for a list of supported ' \
212 # re-build the command line arguments
213 sys
.argv
[0] += ' %s' % cmd
216 command
= commands
[cmd
]
217 usage
= command
.usage
.split('\n')[0].strip()
218 parser
= OptionParser(usage
= usage
, option_list
= command
.options
)
219 options
, args
= parser
.parse_args()
221 # These modules are only used from this point onwards and do not
222 # need to be imported earlier
223 from stgit
.config
import config_setup
224 from ConfigParser
import ParsingError
225 from stgit
.stack
import Series
, StackException
226 from stgit
.git
import GitException
227 from stgit
.commands
.common
import CmdException
228 from stgit
.gitmergeonefile
import GitMergeException
233 # 'clone' doesn't expect an already initialised GIT tree. A Series
234 # object will be created after the GIT tree is cloned
236 if hasattr(options
, 'branch') and options
.branch
:
237 command
.crt_series
= Series(options
.branch
)
239 command
.crt_series
= Series()
240 stgit
.commands
.common
.crt_series
= command
.crt_series
242 command
.func(parser
, options
, args
)
243 except (IOError, ParsingError
, CmdException
, StackException
, GitException
,
244 GitMergeException
), err
:
245 print >> sys
.stderr
, '%s %s: %s' %
(prog
, cmd
, err
)
247 except KeyboardInterrupt: