Add the ability to rename a git branch
[stgit] / stgit / main.py
CommitLineData
41a6d859
CM
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
873a27fd 24from stgit.utils import *
41a6d859
CM
25from stgit import stack, git
26from stgit.version import version
27from stgit.config import config
fcee87cf
CM
28from stgit.commands.common import *
29
30# The commands
31import stgit.commands.add
32import stgit.commands.applied
33import stgit.commands.delete
34import stgit.commands.diff
de4c9d27 35import stgit.commands.clean
1008fbce 36import stgit.commands.clone
6a093bbb 37import stgit.commands.commit
fcee87cf
CM
38import stgit.commands.export
39import stgit.commands.files
c14444b9 40import stgit.commands.fold
e1261152 41import stgit.commands.id
0d2cd1e4 42import stgit.commands.imprt
fcee87cf 43import stgit.commands.init
b4bddc06 44import stgit.commands.mail
fcee87cf 45import stgit.commands.new
0618ea9c 46import stgit.commands.pick
fcee87cf 47import stgit.commands.pop
f338c3c0 48import stgit.commands.pull
fcee87cf
CM
49import stgit.commands.push
50import stgit.commands.refresh
e55b53e0 51import stgit.commands.rename
fcee87cf
CM
52import stgit.commands.resolved
53import stgit.commands.rm
54import stgit.commands.series
55import stgit.commands.status
56import stgit.commands.top
57import stgit.commands.unapplied
41a6d859
CM
58
59
41a6d859
CM
60#
61# The commands map
62#
63commands = {
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
93def 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'
1e0bdf2a 98 print ' version display version information'
22ea9102 99 print ' copyright display copyright information'
1e0bdf2a 100 print
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#
110def 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)