Add the --reset option to status
[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
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.delete
34import stgit.commands.diff
35import stgit.commands.clean
36import stgit.commands.clone
37import stgit.commands.commit
38import stgit.commands.export
39import stgit.commands.files
40import stgit.commands.fold
41import stgit.commands.id
42import stgit.commands.imprt
43import stgit.commands.init
44import stgit.commands.mail
45import stgit.commands.new
46import stgit.commands.pick
47import stgit.commands.pop
48import stgit.commands.pull
49import stgit.commands.push
50import stgit.commands.refresh
51import stgit.commands.rename
52import stgit.commands.resolved
53import stgit.commands.rm
54import stgit.commands.series
55import stgit.commands.status
56import stgit.commands.top
57import stgit.commands.unapplied
58
59
60#
61# The commands map
62#
63commands = {
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
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'
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#
110def 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)