git-cola: avoid symbol conflicts
[git-cola.git] / bin / git-cola
blob4c4b436babdf810ad1f68183801581d3dd5368bf
1 #!/usr/bin/env python
2 # -*- python-mode -*-
3 """git-cola: The highly caffeinated Git GUI
4 """
6 __copyright__ = """
7 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013
8 David Aguilar <davvid@gmail.com> and contributors
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 """
21 import os
22 import sys
23 import subprocess
24 from argparse import ArgumentParser
25 from os.path import abspath
26 from os.path import dirname
27 from os.path import realpath
30 def setup_environment():
31 """Provides access to the cola modules"""
32 # Try to detect where it is run from and set prefix and the search path.
33 # It is assumed that the user installed Cola using the --prefix= option
34 prefix = dirname(dirname(realpath(abspath(__file__))))
36 # Look for modules in the source or install trees
37 cola_mod = os.path.join(prefix, 'cola', '__init__.py')
38 if os.path.exists(cola_mod):
39 # Source tree
40 sys.path.insert(1, prefix)
41 else:
42 # Install tree
43 install_lib = os.path.join(prefix, 'share', 'git-cola', 'lib')
44 sys.path.insert(1, install_lib)
45 setup_environment()
47 from cola import core
48 from cola import cmds
49 from cola.app import add_common_arguments
50 from cola.app import application_init
51 from cola.app import application_start
54 def main():
55 # we're using argparse with subparser, but argparse
56 # does not allow us to assign a default subparser
57 # when none has been specified. We fake it by injecting
58 # 'cola' into the command-line so that parse_args()
59 # routes them to the 'cola' parser by default.
60 if (len(sys.argv) < 2 or
61 sys.argv[1].startswith('-') and
62 sys.argv[1] not in ('-h', '--help')):
63 sys.argv.insert(1, 'cola')
64 args = parse_args()
65 return args.func(args)
68 def parse_args():
69 parser = ArgumentParser()
70 subparser = parser.add_subparsers(title='valid commands')
72 add_cola_command(subparser)
73 add_archive_command(subparser)
74 add_branch_command(subparser)
75 add_browse_command(subparser)
76 add_config_command(subparser)
77 add_dag_command(subparser)
78 add_diff_command(subparser)
79 add_fetch_command(subparser)
80 add_grep_command(subparser)
81 add_merge_command(subparser)
82 add_pull_command(subparser)
83 add_push_command(subparser)
84 add_rebase_command(subparser)
85 add_remote_command(subparser)
86 add_search_command(subparser)
87 add_stash_command(subparser)
88 add_tag_command(subparser)
89 add_version_command(subparser)
91 return parser.parse_args()
94 def add_command(parent, name, description, func):
95 parser = parent.add_parser(name, help=description)
96 parser.set_defaults(func=func)
97 add_common_arguments(parser)
98 return parser
101 def add_cola_command(subparser):
102 parser = add_command(subparser, 'cola', 'start git-cola', cmd_cola)
105 def add_archive_command(parent):
106 parser = add_command(parent, 'archive', 'save an archive', cmd_archive)
107 parser.add_argument('ref', metavar='<ref>', nargs='?', default=None,
108 help='SHA-1 to archive')
111 def add_branch_command(subparser):
112 add_command(subparser, 'branch', 'create a branch', cmd_branch)
115 def add_browse_command(subparser):
116 add_command(subparser, 'browse', 'browse repository', cmd_browse)
117 add_command(subparser, 'classic', 'browse repository', cmd_browse)
120 def add_config_command(subparser):
121 add_command(subparser, 'config', 'edit configuration', cmd_config)
124 def add_dag_command(subparser):
125 parser = add_command(subparser, 'dag', 'start git-dag', cmd_dag)
126 parser.add_argument('-c', '--count', metavar='<count>',
127 type=int, default=1000,
128 help='number of commits to display')
129 parser.add_argument('args', nargs='*', metavar='<args>',
130 help='git log arguments')
132 def add_diff_command(subparser):
133 parser = add_command(subparser, 'diff', 'view diffs', cmd_diff)
134 parser.add_argument('args', nargs='*', metavar='<args>',
135 help='git diff arguments')
138 def add_fetch_command(subparser):
139 add_command(subparser, 'fetch', 'fetch remotes', cmd_fetch)
142 def add_grep_command(subparser):
143 parser = add_command(subparser, 'grep', 'grep source', cmd_grep)
144 parser.add_argument('args', nargs='*', metavar='<args>',
145 help='git grep arguments')
147 def add_merge_command(subparser):
148 add_command(subparser, 'merge', 'merge branches', cmd_merge)
151 def add_pull_command(subparser):
152 parser = add_command(subparser, 'pull', 'pull remote branches', cmd_pull)
153 parser.add_argument('--rebase', default=False, action='store_true',
154 help='rebase local branch when pulling')
157 def add_push_command(subparser):
158 add_command(subparser, 'push', 'push remote branches', cmd_push)
161 def add_rebase_command(subparser):
162 parser = add_command(subparser, 'rebase', 'interactive rebase', cmd_rebase)
163 parser.add_argument('branch', metavar='<branch>',
164 help='New upstream branch')
167 def add_remote_command(subparser):
168 add_command(subparser, 'remote', 'edit remotes', cmd_remote)
171 def add_search_command(subparser):
172 add_command(subparser, 'search', 'search commits', cmd_search)
175 def add_stash_command(subparser):
176 add_command(subparser, 'stash', 'stash and unstash changes', cmd_stash)
179 def add_tag_command(subparser):
180 parser = add_command(subparser, 'tag', 'create tags', cmd_tag)
181 parser.add_argument('name', metavar='<name>', nargs='?', default=None,
182 help='tag name')
183 parser.add_argument('ref', metavar='<ref>', nargs='?', default=None,
184 help='SHA-1 to tag')
185 parser.add_argument('-s', '--sign', default=False, action='store_true',
186 help='annotated and GPG-signed tag')
188 def add_version_command(subparser):
189 parser = add_command(subparser, 'version', 'print the version', cmd_version)
190 parser.add_argument('--brief', action='store_true', default=False,
191 help='print the version number only')
193 # entry points
195 def cmd_cola(args):
196 context = application_init(args)
197 from cola.widgets.main import MainView
198 view = MainView(context.model)
199 return application_start(context, view)
202 def cmd_archive(args):
203 context = application_init(args, update=True)
204 if args.ref is None:
205 args.ref = context.model.currentbranch
207 from cola.widgets.archive import GitArchiveDialog
208 view = GitArchiveDialog(args.ref)
209 return application_start(context, view)
212 def cmd_branch(args):
213 context = application_init(args, update=True)
214 from cola.widgets.createbranch import create_new_branch
215 view = create_new_branch()
216 return application_start(context, view)
219 def cmd_browse(args):
220 context = application_init(args)
221 from cola.widgets.browse import worktree_browser
222 view = worktree_browser(update=False)
223 return application_start(context, view)
226 def cmd_config(args):
227 context = application_init(args)
228 from cola.widgets.prefs import preferences
229 view = preferences()
230 return application_start(context, view)
233 def cmd_dag(args):
234 context = application_init(args)
235 from cola.widgets.dag import git_dag
236 view = git_dag(context.model, args=args)
237 return application_start(context, view)
240 def cmd_diff(args):
241 context = application_init(args)
242 from cola.difftool import diff_expression
243 expr = subprocess.list2cmdline(map(core.decode, args.args))
244 view = diff_expression(None, expr, create_widget=True)
245 return application_start(context, view)
248 def cmd_fetch(args):
249 # TODO: the calls to update_status() can be done asynchronously
250 # by hooking into the message_updated notification.
251 context = application_init(args)
252 from cola.widgets import remote
253 context.model.update_status()
254 view = remote.fetch()
255 return application_start(context, view)
258 def cmd_grep(args):
259 context = application_init(args)
260 from cola.widgets import grep
261 text = subprocess.list2cmdline(map(core.decode, args.args))
262 view = grep.run_grep(text=text, parent=None)
263 return application_start(context, view)
266 def cmd_merge(args):
267 context = application_init(args, update=True)
268 from cola.widgets.merge import MergeView
269 view = MergeView(context.model, parent=None)
270 return application_start(context, view)
273 def cmd_version(args):
274 from cola import version
275 version.print_version(brief=args.brief)
276 return 0
279 def cmd_pull(args):
280 context = application_init(args, update=True)
281 from cola.widgets import remote
282 view = remote.pull()
283 if args.rebase:
284 view.set_rebase(True)
285 return application_start(context, view)
288 def cmd_push(args):
289 context = application_init(args, update=True)
290 from cola.widgets import remote
291 view = remote.push()
292 return application_start(context, view)
295 def cmd_rebase(args):
296 status, out, err = cmds.do(cmds.Rebase, args.branch)
297 if out:
298 core.stdout(out)
299 if err:
300 core.stderr(err)
301 return status
304 def cmd_remote(args):
305 context = application_init(args)
306 from cola.widgets import editremotes
307 view = editremotes.edit()
308 return application_start(context, view)
311 def cmd_search(args):
312 context = application_init(args)
313 from cola.widgets.search import search
314 view = search()
315 return application_start(context, view)
318 def cmd_stash(args):
319 context = application_init(args)
320 from cola.widgets.stash import stash
321 view = stash()
322 return application_start(context, view)
325 def cmd_tag(args):
326 context = application_init(args)
327 from cola.widgets.createtag import create_tag
328 view = create_tag(name=args.name, ref=args.ref, sign=args.sign)
329 return application_start(context, view)
332 if __name__ == '__main__':
333 sys.exit(main())