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