Release 0.11
[stgit] / stgit / main.py
... / ...
CommitLineData
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
22from optparse import OptionParser, make_option
23
24from stgit.utils import *
25from stgit import stack, git, gitmergeonefile
26from stgit.version import version
27from stgit.config import config
28from stgit.commands.common import *
29
30# The commands
31import stgit.commands.add
32import stgit.commands.applied
33import stgit.commands.branch
34import stgit.commands.delete
35import stgit.commands.diff
36import stgit.commands.clean
37import stgit.commands.clone
38import stgit.commands.commit
39import stgit.commands.export
40import stgit.commands.files
41import stgit.commands.float
42import stgit.commands.fold
43import stgit.commands.goto
44import stgit.commands.id
45import stgit.commands.imprt
46import stgit.commands.init
47import stgit.commands.log
48import stgit.commands.mail
49import stgit.commands.new
50import stgit.commands.patches
51import stgit.commands.pick
52import stgit.commands.pop
53import stgit.commands.pull
54import stgit.commands.push
55import stgit.commands.refresh
56import stgit.commands.rename
57import stgit.commands.resolved
58import stgit.commands.rm
59import stgit.commands.series
60import stgit.commands.show
61import stgit.commands.status
62import stgit.commands.top
63import stgit.commands.unapplied
64import stgit.commands.uncommit
65
66
67#
68# The commands map
69#
70commands = {
71 'add': stgit.commands.add,
72 'applied': stgit.commands.applied,
73 'branch': stgit.commands.branch,
74 'delete': stgit.commands.delete,
75 'diff': stgit.commands.diff,
76 'clean': stgit.commands.clean,
77 'clone': stgit.commands.clone,
78 'commit': stgit.commands.commit,
79 'export': stgit.commands.export,
80 'files': stgit.commands.files,
81 'float': stgit.commands.float,
82 'fold': stgit.commands.fold,
83 'goto': stgit.commands.goto,
84 'id': stgit.commands.id,
85 'import': stgit.commands.imprt,
86 'init': stgit.commands.init,
87 'log': stgit.commands.log,
88 'mail': stgit.commands.mail,
89 'new': stgit.commands.new,
90 'patches': stgit.commands.patches,
91 'pick': stgit.commands.pick,
92 'pop': stgit.commands.pop,
93 'pull': stgit.commands.pull,
94 'push': stgit.commands.push,
95 'refresh': stgit.commands.refresh,
96 'rename': stgit.commands.rename,
97 'resolved': stgit.commands.resolved,
98 'rm': stgit.commands.rm,
99 'series': stgit.commands.series,
100 'show': stgit.commands.show,
101 'status': stgit.commands.status,
102 'top': stgit.commands.top,
103 'unapplied':stgit.commands.unapplied,
104 'uncommit': stgit.commands.uncommit,
105 }
106
107# classification: repository, stack, patch, working copy
108repocommands = (
109 'branch',
110 'clone',
111 'id',
112 'pull'
113 )
114stackcommands = (
115 'applied',
116 'clean',
117 'commit',
118 'float',
119 'goto',
120 'init',
121 'pop',
122 'push',
123 'series',
124 'top',
125 'unapplied',
126 'uncommit'
127 )
128patchcommands = (
129 'delete',
130 'export',
131 'files',
132 'fold',
133 'import',
134 'log',
135 'mail',
136 'new',
137 'pick',
138 'refresh',
139 'rename',
140 'show'
141 )
142wccommands = (
143 'add',
144 'diff',
145 'patches',
146 'resolved',
147 'rm',
148 'status'
149 )
150
151def _print_helpstring(cmd):
152 print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help
153
154def print_help():
155 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
156 print
157 print 'Generic commands:'
158 print ' help print the detailed command usage'
159 print ' version display version information'
160 print ' copyright display copyright information'
161 # unclassified commands if any
162 cmds = commands.keys()
163 cmds.sort()
164 for cmd in cmds:
165 if not cmd in repocommands and not cmd in stackcommands \
166 and not cmd in patchcommands and not cmd in wccommands:
167 _print_helpstring(cmd)
168 print
169
170 print 'Repository commands:'
171 for cmd in repocommands:
172 _print_helpstring(cmd)
173 print
174
175 print 'Stack commands:'
176 for cmd in stackcommands:
177 _print_helpstring(cmd)
178 print
179
180 print 'Patch commands:'
181 for cmd in patchcommands:
182 _print_helpstring(cmd)
183 print
184
185 print 'Working-copy commands:'
186 for cmd in wccommands:
187 _print_helpstring(cmd)
188
189#
190# The main function (command dispatcher)
191#
192def main():
193 """The main function
194 """
195 prog = os.path.basename(sys.argv[0])
196
197 if len(sys.argv) < 2:
198 print >> sys.stderr, 'Unknown command'
199 print >> sys.stderr, \
200 ' Try "%s --help" for a list of supported commands' % prog
201 sys.exit(1)
202
203 cmd = sys.argv[1]
204
205 if cmd in ['-h', '--help']:
206 if len(sys.argv) == 3 and sys.argv[2] in commands:
207 cmd = sys.argv[2]
208 sys.argv[2] = '--help'
209 else:
210 print_help()
211 sys.exit(0)
212 if cmd == 'help':
213 if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']:
214 cmd = sys.argv[2]
215 if not cmd in commands:
216 print >> sys.stderr, '%s help: "%s" command unknown' \
217 % (prog, cmd)
218 sys.exit(1)
219
220 sys.argv[0] += ' %s' % cmd
221 command = commands[cmd]
222 parser = OptionParser(usage = command.usage,
223 option_list = command.options)
224 parser.print_help()
225 else:
226 print 'usage: %s help <command>' % prog
227
228 sys.exit(0)
229 if cmd in ['-v', '--version', 'version']:
230 print 'Stacked GIT %s' % version
231 os.system('git --version')
232 print 'Python version %s' % sys.version
233 sys.exit(0)
234 if cmd in ['copyright']:
235 print __copyright__
236 sys.exit(0)
237 if not cmd in commands:
238 print >> sys.stderr, 'Unknown command: %s' % cmd
239 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
240 'commands' % prog
241 sys.exit(1)
242
243 # re-build the command line arguments
244 sys.argv[0] += ' %s' % cmd
245 del(sys.argv[1])
246
247 command = commands[cmd]
248 usage = command.usage.split('\n')[0].strip()
249 parser = OptionParser(usage = usage, option_list = command.options)
250 options, args = parser.parse_args()
251 try:
252 # 'clone' doesn't expect an already initialised GIT tree. A Series
253 # object will be created after the GIT tree is cloned
254 if cmd != 'clone':
255 if hasattr(options, 'branch') and options.branch:
256 command.crt_series = stack.Series(options.branch)
257 else:
258 command.crt_series = stack.Series()
259 stgit.commands.common.crt_series = command.crt_series
260
261 command.func(parser, options, args)
262 except (IOError, CmdException, stack.StackException, git.GitException,
263 gitmergeonefile.GitMergeException), err:
264 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
265 sys.exit(2)
266 except KeyboardInterrupt:
267 sys.exit(1)
268
269 sys.exit(0)