Pipe the 'stg help <command>' output through the pager
[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):
33 cmd_mod = self.get(key)
34 __import__('stgit.commands.' + cmd_mod)
35 return getattr(stgit.commands, cmd_mod)
36
37commands = Commands({
38 'add': 'add',
39 'applied': 'applied',
40 'assimilate': 'assimilate',
41 'branch': 'branch',
42 'delete': 'delete',
43 'diff': 'diff',
44 'clean': 'clean',
45 'clone': 'clone',
46 'commit': 'commit',
47 'export': 'export',
48 'files': 'files',
49 'float': 'float',
50 'fold': 'fold',
51 'goto': 'goto',
52 'id': 'id',
53 'import': 'imprt',
54 'init': 'init',
55 'log': 'log',
56 'mail': 'mail',
57 'new': 'new',
58 'patches': 'patches',
59 'pick': 'pick',
60 'pop': 'pop',
61 'pull': 'pull',
62 'push': 'push',
63 'refresh': 'refresh',
64 'rename': 'rename',
65 'resolved': 'resolved',
66 'rm': 'rm',
67 'series': 'series',
68 'show': 'show',
69 'status': 'status',
70 'top': 'top',
71 'unapplied': 'unapplied',
72 'uncommit': 'uncommit'
73 })
41a6d859 74
5dc51fea
YD
75# classification: repository, stack, patch, working copy
76repocommands = (
77 'branch',
78 'clone',
79 'id',
80 'pull'
81 )
82stackcommands = (
83 'applied',
4d0ba818 84 'assimilate',
5dc51fea
YD
85 'clean',
86 'commit',
d98a499c 87 'float',
5dc51fea
YD
88 'goto',
89 'init',
90 'pop',
91 'push',
92 'series',
93 'top',
94 'unapplied',
95 'uncommit'
96 )
97patchcommands = (
98 'delete',
99 'export',
100 'files',
101 'fold',
102 'import',
64354a2d 103 'log',
5dc51fea
YD
104 'mail',
105 'new',
106 'pick',
107 'refresh',
108 'rename',
109 'show'
110 )
111wccommands = (
112 'add',
113 'diff',
114 'patches',
115 'resolved',
116 'rm',
117 'status'
118 )
119
120def _print_helpstring(cmd):
121 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
122
41a6d859
CM
123def print_help():
124 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
125 print
5dc51fea 126 print 'Generic commands:'
f0699cc7 127 print ' help print the detailed command usage'
1e0bdf2a 128 print ' version display version information'
22ea9102 129 print ' copyright display copyright information'
5dc51fea 130 # unclassified commands if any
41a6d859
CM
131 cmds = commands.keys()
132 cmds.sort()
133 for cmd in cmds:
5dc51fea
YD
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)
137 print
138
139 print 'Repository commands:'
140 for cmd in repocommands:
141 _print_helpstring(cmd)
142 print
143
144 print 'Stack commands:'
145 for cmd in stackcommands:
146 _print_helpstring(cmd)
147 print
148
149 print 'Patch commands:'
150 for cmd in patchcommands:
151 _print_helpstring(cmd)
152 print
153
154 print 'Working-copy commands:'
155 for cmd in wccommands:
156 _print_helpstring(cmd)
41a6d859
CM
157
158#
159# The main function (command dispatcher)
160#
161def main():
162 """The main function
163 """
41a6d859
CM
164 prog = os.path.basename(sys.argv[0])
165
166 if len(sys.argv) < 2:
d00e708a 167 print >> sys.stderr, 'usage: %s <command>' % prog
41a6d859 168 print >> sys.stderr, \
f0699cc7 169 ' Try "%s --help" for a list of supported commands' % prog
41a6d859
CM
170 sys.exit(1)
171
172 cmd = sys.argv[1]
173
f0699cc7 174 if cmd in ['-h', '--help']:
a9d82c36 175 if len(sys.argv) >= 3 and sys.argv[2] in commands:
31c5abf2 176 cmd = sys.argv[2]
f0699cc7 177 sys.argv[2] = '--help'
31c5abf2
PR
178 else:
179 print_help()
180 sys.exit(0)
f0699cc7
CM
181 if cmd == 'help':
182 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
183 cmd = sys.argv[2]
184 if not cmd in commands:
185 print >> sys.stderr, '%s help: "%s" command unknown' \
186 % (prog, cmd)
187 sys.exit(1)
188
189 sys.argv[0] += ' %s' % cmd
190 command = commands[cmd]
191 parser = OptionParser(usage = command.usage,
192 option_list = command.options)
0d4cd7ce
CM
193 from pydoc import pager
194 pager(parser.format_help())
f0699cc7 195 else:
4fe42e63 196 print_help()
f0699cc7 197 sys.exit(0)
1e0bdf2a 198 if cmd in ['-v', '--version', 'version']:
c333afcf 199 from stgit.version import version
4df2f866 200 print 'Stacked GIT %s' % version
50725547 201 os.system('git --version')
1e0bdf2a 202 print 'Python version %s' % sys.version
41a6d859 203 sys.exit(0)
22ea9102
CM
204 if cmd in ['copyright']:
205 print __copyright__
206 sys.exit(0)
41a6d859
CM
207 if not cmd in commands:
208 print >> sys.stderr, 'Unknown command: %s' % cmd
2f7c8b0b
CM
209 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
210 'commands' % prog
41a6d859
CM
211 sys.exit(1)
212
213 # re-build the command line arguments
214 sys.argv[0] += ' %s' % cmd
215 del(sys.argv[1])
216
217 command = commands[cmd]
f0699cc7
CM
218 usage = command.usage.split('\n')[0].strip()
219 parser = OptionParser(usage = usage, option_list = command.options)
41a6d859 220 options, args = parser.parse_args()
22d87516
CM
221
222 # These modules are only used from this point onwards and do not
223 # need to be imported earlier
eee7283e 224 from stgit.config import config_setup
9e3f506f 225 from ConfigParser import ParsingError, NoSectionError
22d87516
CM
226 from stgit.stack import Series, StackException
227 from stgit.git import GitException
228 from stgit.commands.common import CmdException
229 from stgit.gitmergeonefile import GitMergeException
230
41a6d859 231 try:
eee7283e
CM
232 config_setup()
233
98290387
CM
234 # 'clone' doesn't expect an already initialised GIT tree. A Series
235 # object will be created after the GIT tree is cloned
236 if cmd != 'clone':
237 if hasattr(options, 'branch') and options.branch:
c333afcf 238 command.crt_series = Series(options.branch)
98290387 239 else:
c333afcf 240 command.crt_series = Series()
98290387 241 stgit.commands.common.crt_series = command.crt_series
9ac09909 242
41a6d859 243 command.func(parser, options, args)
9e3f506f
KH
244 except (IOError, ParsingError, NoSectionError, CmdException,
245 StackException, GitException, GitMergeException), err:
41a6d859
CM
246 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
247 sys.exit(2)
4d9fc826
CM
248 except KeyboardInterrupt:
249 sys.exit(1)
41a6d859
CM
250
251 sys.exit(0)