Add a commit command to merge the current patches into base
[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.delete
34 import stgit.commands.diff
35 import stgit.commands.clean
36 import stgit.commands.clone
37 import stgit.commands.commit
38 import stgit.commands.export
39 import stgit.commands.files
40 import stgit.commands.fold
41 import stgit.commands.id
42 import stgit.commands.imprt
43 import stgit.commands.init
44 import stgit.commands.mail
45 import stgit.commands.new
46 import stgit.commands.pick
47 import stgit.commands.pop
48 import stgit.commands.pull
49 import stgit.commands.push
50 import stgit.commands.refresh
51 import stgit.commands.rename
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
58
59
60 #
61 # The commands map
62 #
63 commands = {
64 'add': stgit.commands.add,
65 'applied': stgit.commands.applied,
66 'delete': stgit.commands.delete,
67 'diff': stgit.commands.diff,
68 'clean': stgit.commands.clean,
69 'clone': stgit.commands.clone,
70 'commit': stgit.commands.commit,
71 'export': stgit.commands.export,
72 'files': stgit.commands.files,
73 'fold': stgit.commands.fold,
74 'id': stgit.commands.id,
75 'import': stgit.commands.imprt,
76 'init': stgit.commands.init,
77 'mail': stgit.commands.mail,
78 'new': stgit.commands.new,
79 'pick': stgit.commands.pick,
80 'pop': stgit.commands.pop,
81 'pull': stgit.commands.pull,
82 'push': stgit.commands.push,
83 'refresh': stgit.commands.refresh,
84 'rename': stgit.commands.rename,
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,
91 }
92
93 def print_help():
94 print 'usage: %s <command> [options]' % os.path.basename(sys.argv[0])
95 print
96 print 'commands:'
97 print ' help print this message'
98 print ' version display version information'
99 print ' copyright display copyright information'
100 print
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 """
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)
126 if cmd in ['-v', '--version', 'version']:
127 print 'Stacked GIT %s' % version
128 print 'Python version %s' % sys.version
129 sys.exit(0)
130 if cmd in ['copyright']:
131 print __copyright__
132 sys.exit(0)
133 if not cmd in commands:
134 print >> sys.stderr, 'Unknown command: %s' % cmd
135 print >> sys.stderr, ' Try "%s help" for a list of supported ' \
136 'commands' % prog
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,
145 option_list = command.options)
146 options, args = parser.parse_args()
147 try:
148 # 'clone' doesn't expect an already initialised GIT tree
149 if cmd == 'clone':
150 stgit.commands.common.crt_series = stack.Series('master')
151 elif hasattr(options, 'branch') and options.branch:
152 stgit.commands.common.crt_series = stack.Series(options.branch)
153 else:
154 stgit.commands.common.crt_series = stack.Series()
155 # the line below is a simple way to avoid an exception when
156 # stgit is run outside an initialised tree
157 setattr(command, 'crt_series', stgit.commands.common.crt_series)
158
159 command.func(parser, options, args)
160 except (IOError, CmdException, stack.StackException, git.GitException), \
161 err:
162 print >> sys.stderr, '%s %s: %s' % (prog, cmd, err)
163 sys.exit(2)
164
165 sys.exit(0)