Add new 'rebase' command.
[stgit] / stgit / main.py
CommitLineData
41a6d859
CM
1"""Basic quilt-like functionality
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
21import sys, os
c333afcf 22from optparse import OptionParser
41a6d859 23
c333afcf 24import stgit.commands
41a6d859 25
41a6d859
CM
26#
27# The commands map
28#
c333afcf
CM
29class Commands(dict):
30 """Commands class. It performs on-demand module loading
31 """
32 def __getitem__(self, key):
514dd4f2
CM
33 """Return the command python module name based.
34 """
35 global prog
36
c333afcf 37 cmd_mod = self.get(key)
514dd4f2
CM
38 if not cmd_mod:
39 candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
40
41 if not candidates:
42 print >> sys.stderr, 'Unknown command: %s' % key
43 print >> sys.stderr, ' Try "%s help" for a list of ' \
44 'supported commands' % prog
45 sys.exit(1)
46 elif len(candidates) > 1:
47 print >> sys.stderr, 'Ambiguous command: %s' % key
48 print >> sys.stderr, ' Candidates are: %s' \
49 % ', '.join(candidates)
50 sys.exit(1)
51
52 cmd_mod = self.get(candidates[0])
53
c333afcf
CM
54 __import__('stgit.commands.' + cmd_mod)
55 return getattr(stgit.commands, cmd_mod)
56
57commands = Commands({
58 'add': 'add',
59 'applied': 'applied',
60 'assimilate': 'assimilate',
61 'branch': 'branch',
62 'delete': 'delete',
63 'diff': 'diff',
64 'clean': 'clean',
65 'clone': 'clone',
66 'commit': 'commit',
67 'export': 'export',
68 'files': 'files',
69 'float': 'float',
70 'fold': 'fold',
71 'goto': 'goto',
72 'id': 'id',
73 'import': 'imprt',
74 'init': 'init',
75 'log': 'log',
76 'mail': 'mail',
77 'new': 'new',
78 'patches': 'patches',
79 'pick': 'pick',
80 'pop': 'pop',
81 'pull': 'pull',
82 'push': 'push',
22037590 83 'rebase': 'rebase',
c333afcf
CM
84 'refresh': 'refresh',
85 'rename': 'rename',
86 'resolved': 'resolved',
87 'rm': 'rm',
88 'series': 'series',
89 'show': 'show',
90 'status': 'status',
06848fab 91 'sync': 'sync',
c333afcf
CM
92 'top': 'top',
93 'unapplied': 'unapplied',
94 'uncommit': 'uncommit'
95 })
41a6d859 96
5dc51fea
YD
97# classification: repository, stack, patch, working copy
98repocommands = (
99 'branch',
100 'clone',
101 'id',
102 'pull'
103 )
104stackcommands = (
105 'applied',
4d0ba818 106 'assimilate',
5dc51fea
YD
107 'clean',
108 'commit',
d98a499c 109 'float',
5dc51fea
YD
110 'goto',
111 'init',
112 'pop',
113 'push',
22037590 114 'rebase',
5dc51fea
YD
115 'series',
116 'top',
117 'unapplied',
118 'uncommit'
119 )
120patchcommands = (
121 'delete',
122 'export',
123 'files',
124 'fold',
125 'import',
64354a2d 126 'log',
5dc51fea
YD
127 'mail',
128 'new',
129 'pick',
130 'refresh',
131 'rename',
06848fab
CM
132 'show',
133 'sync'
5dc51fea
YD
134 )
135wccommands = (
136 'add',
137 'diff',
138 'patches',
139 'resolved',
140 'rm',
141 'status'
142 )
143
144def _print_helpstring(cmd):
145 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
146
41a6d859
CM
147def print_help():
148 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
149 print
5dc51fea 150 print 'Generic commands:'
f0699cc7 151 print ' help print the detailed command usage'
1e0bdf2a 152 print ' version display version information'
22ea9102 153 print ' copyright display copyright information'
5dc51fea 154 # unclassified commands if any
41a6d859
CM
155 cmds = commands.keys()
156 cmds.sort()
157 for cmd in cmds:
5dc51fea
YD
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)
161 print
162
163 print 'Repository commands:'
164 for cmd in repocommands:
165 _print_helpstring(cmd)
166 print
167
168 print 'Stack commands:'
169 for cmd in stackcommands:
170 _print_helpstring(cmd)
171 print
172
173 print 'Patch commands:'
174 for cmd in patchcommands:
175 _print_helpstring(cmd)
176 print
177
178 print 'Working-copy commands:'
179 for cmd in wccommands:
180 _print_helpstring(cmd)
41a6d859
CM
181
182#
183# The main function (command dispatcher)
184#
185def main():
186 """The main function
187 """
514dd4f2
CM
188 global prog
189
41a6d859
CM
190 prog = os.path.basename(sys.argv[0])
191
192 if len(sys.argv) < 2:
d00e708a 193 print >> sys.stderr, 'usage: %s <command>' % prog
41a6d859 194 print >> sys.stderr, \
f0699cc7 195 ' Try "%s --help" for a list of supported commands' % prog
41a6d859
CM
196 sys.exit(1)
197
198 cmd = sys.argv[1]
199
f0699cc7 200 if cmd in ['-h', '--help']:
a9d82c36 201 if len(sys.argv) >= 3 and sys.argv[2] in commands:
31c5abf2 202 cmd = sys.argv[2]
f0699cc7 203 sys.argv[2] = '--help'
31c5abf2
PR
204 else:
205 print_help()
206 sys.exit(0)
f0699cc7
CM
207 if cmd == 'help':
208 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
209 cmd = sys.argv[2]
210 if not cmd in commands:
211 print >> sys.stderr, '%s help: "%s" command unknown' \
212 % (prog, cmd)
213 sys.exit(1)
214
215 sys.argv[0] += ' %s' % cmd
216 command = commands[cmd]
217 parser = OptionParser(usage = command.usage,
218 option_list = command.options)
0d4cd7ce
CM
219 from pydoc import pager
220 pager(parser.format_help())
f0699cc7 221 else:
4fe42e63 222 print_help()
f0699cc7 223 sys.exit(0)
1e0bdf2a 224 if cmd in ['-v', '--version', 'version']:
c333afcf 225 from stgit.version import version
4df2f866 226 print 'Stacked GIT %s' % version
50725547 227 os.system('git --version')
1e0bdf2a 228 print 'Python version %s' % sys.version
41a6d859 229 sys.exit(0)
22ea9102
CM
230 if cmd in ['copyright']:
231 print __copyright__
232 sys.exit(0)
41a6d859
CM
233
234 # re-build the command line arguments
235 sys.argv[0] += ' %s' % cmd
236 del(sys.argv[1])
237
238 command = commands[cmd]
f0699cc7
CM
239 usage = command.usage.split('\n')[0].strip()
240 parser = OptionParser(usage = usage, option_list = command.options)
41a6d859 241 options, args = parser.parse_args()
22d87516
CM
242
243 # These modules are only used from this point onwards and do not
244 # need to be imported earlier
eee7283e 245 from stgit.config import config_setup
9e3f506f 246 from ConfigParser import ParsingError, NoSectionError
22d87516
CM
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
251
41a6d859 252 try:
eee7283e
CM
253 config_setup()
254
98290387
CM
255 # 'clone' doesn't expect an already initialised GIT tree. A Series
256 # object will be created after the GIT tree is cloned
257 if cmd != 'clone':
258 if hasattr(options, 'branch') and options.branch:
c333afcf 259 command.crt_series = Series(options.branch)
98290387 260 else:
c333afcf 261 command.crt_series = Series()
98290387 262 stgit.commands.common.crt_series = command.crt_series
9ac09909 263
41a6d859 264 command.func(parser, options, args)
9e3f506f
KH
265 except (IOError, ParsingError, NoSectionError, CmdException,
266 StackException, GitException, GitMergeException), err:
41a6d859
CM
267 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
268 sys.exit(2)
4d9fc826
CM
269 except KeyboardInterrupt:
270 sys.exit(1)
41a6d859
CM
271
272 sys.exit(0)