Treat "stg --help cmd" and "stg help cmd" like "stg cmd
[stgit] / stgit / main.py
1 """Basic quilt-like functionality
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
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.
10
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.
15
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
19 """
20
21 import sys, os
22 from optparse import OptionParser, make_option
23
24 from stgit.utils import *
25 from stgit import stack, git
26 from stgit.version import version
27 from stgit.config import config
28 from stgit.commands.common import *
29
30 # The commands
31 import stgit.commands.add
32 import stgit.commands.applied
33 import stgit.commands.branch
34 import stgit.commands.delete
35 import stgit.commands.diff
36 import stgit.commands.clean
37 import stgit.commands.clone
38 import stgit.commands.commit
39 import stgit.commands.export
40 import stgit.commands.files
41 import stgit.commands.fold
42 import stgit.commands.id
43 import stgit.commands.imprt
44 import stgit.commands.init
45 import stgit.commands.mail
46 import stgit.commands.new
47 import stgit.commands.patches
48 import stgit.commands.pick
49 import stgit.commands.pop
50 import stgit.commands.pull
51 import stgit.commands.push
52 import stgit.commands.refresh
53 import stgit.commands.rename
54 import stgit.commands.resolved
55 import stgit.commands.rm
56 import stgit.commands.series
57 import stgit.commands.status
58 import stgit.commands.top
59 import stgit.commands.unapplied
60
61
62 #
63 # The commands map
64 #
65 commands = {
66 'add': stgit.commands.add,
67 'applied': stgit.commands.applied,
68 'branch': stgit.commands.branch,
69 'delete': stgit.commands.delete,
70 'diff': stgit.commands.diff,
71 'clean': stgit.commands.clean,
72 'clone': stgit.commands.clone,
73 'commit': stgit.commands.commit,
74 'export': stgit.commands.export,
75 'files': stgit.commands.files,
76 'fold': stgit.commands.fold,
77 'id': stgit.commands.id,
78 'import': stgit.commands.imprt,
79 'init': stgit.commands.init,
80 'mail': stgit.commands.mail,
81 'new': stgit.commands.new,
82 'patches': stgit.commands.patches,
83 'pick': stgit.commands.pick,
84 'pop': stgit.commands.pop,
85 'pull': stgit.commands.pull,
86 'push': stgit.commands.push,
87 'refresh': stgit.commands.refresh,
88 'rename': stgit.commands.rename,
89 'resolved': stgit.commands.resolved,
90 'rm': stgit.commands.rm,
91 'series': stgit.commands.series,
92 'status': stgit.commands.status,
93 'top': stgit.commands.top,
94 'unapplied':stgit.commands.unapplied,
95 }
96
97 def print_help():
98 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
99 print
100 print 'commands:'
101 print ' help print this message'
102 print ' version display version information'
103 print ' copyright display copyright information'
104 print
105
106 cmds = commands.keys()
107 cmds.sort()
108 for cmd in cmds:
109 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
110
111 #
112 # The main function (command dispatcher)
113 #
114 def main():
115 """The main function
116 """
117 prog = os.path.basename(sys.argv[0])
118
119 if len(sys.argv) < 2:
120 print >> sys.stderr, 'Unknown command'
121 print >> sys.stderr, \
122 ' Try "%s help" for a list of supported commands' % prog
123 sys.exit(1)
124
125 cmd = sys.argv[1]
126
127 if cmd in ['-h', '--help', 'help']:
128 if len(sys.argv) == 3 and sys.argv[2] in commands:
129 cmd = sys.argv[2]
130 sys.argv[2] = '--help';
131 else:
132 print_help()
133 sys.exit(0)
134 if cmd in ['-v', '--version', 'version']:
135 print 'Stacked GIT %s' % version
136 os.system('git --version')
137 print 'Python version %s' % sys.version
138 sys.exit(0)
139 if cmd in ['copyright']:
140 print __copyright__
141 sys.exit(0)
142 if not cmd in commands:
143 print >> sys.stderr, 'Unknown command: %s' % cmd
144 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
145 'commands' % prog
146 sys.exit(1)
147
148 # re-build the command line arguments
149 sys.argv[0] += ' %s' % cmd
150 del(sys.argv[1])
151
152 command = commands[cmd]
153 parser = OptionParser(usage = command.usage,
154 option_list = command.options)
155 options, args = parser.parse_args()
156 try:
157 # 'clone' doesn't expect an already initialised GIT tree. A Series
158 # object will be created after the GIT tree is cloned
159 if cmd != 'clone':
160 if hasattr(options, 'branch') and options.branch:
161 command.crt_series = stack.Series(options.branch)
162 else:
163 command.crt_series = stack.Series()
164 stgit.commands.common.crt_series = command.crt_series
165
166 command.func(parser, options, args)
167 except (IOError, CmdException, stack.StackException, git.GitException), \
168 err:
169 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
170 sys.exit(2)
171 except KeyboardInterrupt:
172 sys.exit(1)
173
174 sys.exit(0)