Commit | Line | Data |
---|---|---|
41a6d859 CM |
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 | ||
873a27fd | 24 | from stgit.utils import * |
41a6d859 CM |
25 | from stgit import stack, git |
26 | from stgit.version import version | |
27 | from stgit.config import config | |
fcee87cf CM |
28 | from stgit.commands.common import * |
29 | ||
30 | # The commands | |
31 | import stgit.commands.add | |
32 | import stgit.commands.applied | |
33 | import stgit.commands.delete | |
34 | import stgit.commands.diff | |
de4c9d27 | 35 | import stgit.commands.clean |
1008fbce | 36 | import stgit.commands.clone |
6a093bbb | 37 | import stgit.commands.commit |
fcee87cf CM |
38 | import stgit.commands.export |
39 | import stgit.commands.files | |
c14444b9 | 40 | import stgit.commands.fold |
e1261152 | 41 | import stgit.commands.id |
0d2cd1e4 | 42 | import stgit.commands.imprt |
fcee87cf | 43 | import stgit.commands.init |
b4bddc06 | 44 | import stgit.commands.mail |
fcee87cf | 45 | import stgit.commands.new |
0618ea9c | 46 | import stgit.commands.pick |
fcee87cf | 47 | import stgit.commands.pop |
f338c3c0 | 48 | import stgit.commands.pull |
fcee87cf CM |
49 | import stgit.commands.push |
50 | import stgit.commands.refresh | |
e55b53e0 | 51 | import stgit.commands.rename |
fcee87cf CM |
52 | import stgit.commands.resolved |
53 | import stgit.commands.rm | |
54 | import stgit.commands.series | |
55 | import stgit.commands.status | |
56 | import stgit.commands.top | |
57 | import stgit.commands.unapplied | |
41a6d859 CM |
58 | |
59 | ||
41a6d859 CM |
60 | # |
61 | # The commands map | |
62 | # | |
63 | commands = { | |
fcee87cf CM |
64 | 'add': stgit.commands.add, |
65 | 'applied': stgit.commands.applied, | |
66 | 'delete': stgit.commands.delete, | |
67 | 'diff': stgit.commands.diff, | |
de4c9d27 | 68 | 'clean': stgit.commands.clean, |
1008fbce | 69 | 'clone': stgit.commands.clone, |
6a093bbb | 70 | 'commit': stgit.commands.commit, |
fcee87cf CM |
71 | 'export': stgit.commands.export, |
72 | 'files': stgit.commands.files, | |
c14444b9 | 73 | 'fold': stgit.commands.fold, |
e1261152 | 74 | 'id': stgit.commands.id, |
0d2cd1e4 | 75 | 'import': stgit.commands.imprt, |
fcee87cf | 76 | 'init': stgit.commands.init, |
b4bddc06 | 77 | 'mail': stgit.commands.mail, |
fcee87cf | 78 | 'new': stgit.commands.new, |
0618ea9c | 79 | 'pick': stgit.commands.pick, |
fcee87cf | 80 | 'pop': stgit.commands.pop, |
f338c3c0 | 81 | 'pull': stgit.commands.pull, |
fcee87cf CM |
82 | 'push': stgit.commands.push, |
83 | 'refresh': stgit.commands.refresh, | |
e55b53e0 | 84 | 'rename': stgit.commands.rename, |
fcee87cf CM |
85 | 'resolved': stgit.commands.resolved, |
86 | 'rm': stgit.commands.rm, | |
87 | 'series': stgit.commands.series, | |
88 | 'status': stgit.commands.status, | |
89 | 'top': stgit.commands.top, | |
90 | 'unapplied':stgit.commands.unapplied, | |
41a6d859 CM |
91 | } |
92 | ||
93 | def print_help(): | |
94 | print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0]) | |
95 | ||
96 | print 'commands:' | |
97 | print ' help print this message' | |
1e0bdf2a | 98 | print ' version display version information' |
22ea9102 | 99 | print ' copyright display copyright information' |
1e0bdf2a | 100 | |
41a6d859 CM |
101 | |
102 | cmds = commands.keys() | |
103 | cmds.sort() | |
104 | for cmd in cmds: | |
105 | print ' ' + cmd + ' ' * (12 - len(cmd)) + commands[cmd].help | |
106 | ||
107 | # | |
108 | # The main function (command dispatcher) | |
109 | # | |
110 | def main(): | |
111 | """The main function | |
112 | """ | |
41a6d859 CM |
113 | prog = os.path.basename(sys.argv[0]) |
114 | ||
115 | if len(sys.argv) < 2: | |
116 | print >> sys.stderr, 'Unknown command' | |
117 | print >> sys.stderr, \ | |
118 | ' Try "%s help" for a list of supported commands' % prog | |
119 | sys.exit(1) | |
120 | ||
121 | cmd = sys.argv[1] | |
122 | ||
123 | if cmd in ['-h', '--help', 'help']: | |
124 | print_help() | |
125 | sys.exit(0) | |
1e0bdf2a | 126 | if cmd in ['-v', '--version', 'version']: |
4df2f866 | 127 | print 'Stacked GIT %s' % version |
1e0bdf2a | 128 | print 'Python version %s' % sys.version |
41a6d859 | 129 | sys.exit(0) |
22ea9102 CM |
130 | if cmd in ['copyright']: |
131 | print __copyright__ | |
132 | sys.exit(0) | |
41a6d859 CM |
133 | if not cmd in commands: |
134 | print >> sys.stderr, 'Unknown command: %s' % cmd | |
2f7c8b0b CM |
135 | print >> sys.stderr, ' Try "%s help" for a list of supported ' \ |
136 | 'commands' % prog | |
41a6d859 CM |
137 | sys.exit(1) |
138 | ||
139 | # re-build the command line arguments | |
140 | sys.argv[0] += ' %s' % cmd | |
141 | del(sys.argv[1]) | |
142 | ||
143 | command = commands[cmd] | |
144 | parser = OptionParser(usage = command.usage, | |
fcee87cf | 145 | option_list = command.options) |
41a6d859 CM |
146 | options, args = parser.parse_args() |
147 | try: | |
1008fbce CM |
148 | # 'clone' doesn't expect an already initialised GIT tree |
149 | if cmd == 'clone': | |
150 | stgit.commands.common.crt_series = stack.Series('master') | |
2f7c8b0b CM |
151 | elif hasattr(options, 'branch') and options.branch: |
152 | stgit.commands.common.crt_series = stack.Series(options.branch) | |
1008fbce CM |
153 | else: |
154 | stgit.commands.common.crt_series = stack.Series() | |
155 | # the line below is a simple way to avoid an exception when | |
9ac09909 | 156 | # stgit is run outside an initialised tree |
9ac09909 CM |
157 | setattr(command, 'crt_series', stgit.commands.common.crt_series) |
158 | ||
41a6d859 | 159 | command.func(parser, options, args) |
fcee87cf | 160 | except (IOError, CmdException, stack.StackException, git.GitException), \ |
41a6d859 CM |
161 | err: |
162 | print >> sys.stderr, '%s %s: %s' % (prog, cmd, err) | |
163 | sys.exit(2) | |
164 | ||
165 | sys.exit(0) |