3 """git-cola: The highly caffeinated Git GUI
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.
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
):
40 sys
.path
.insert(1, prefix
)
43 install_lib
= os
.path
.join(prefix
, 'share', 'git-cola', 'lib')
44 sys
.path
.insert(1, install_lib
)
47 from cola
.app
import add_common_arguments
48 from cola
.app
import application_init
49 from cola
.app
import application_start
51 # NOTE: these must be imported *after* cola.app.
52 # PyQt4 may not be available until after cola.app has gotten a chance to
53 # install the homebrew modules in sys.path.
59 # we're using argparse with subparser, but argparse
60 # does not allow us to assign a default subparser
61 # when none has been specified. We fake it by injecting
62 # 'cola' into the command-line so that parse_args()
63 # routes them to the 'cola' parser by default.
64 if (len(sys
.argv
) < 2 or
65 sys
.argv
[1].startswith('-') and
66 sys
.argv
[1] not in ('-h', '--help')):
67 sys
.argv
.insert(1, 'cola')
69 return args
.func(args
)
73 parser
= ArgumentParser()
74 subparser
= parser
.add_subparsers(title
='valid commands')
76 add_cola_command(subparser
)
77 add_am_command(subparser
)
78 add_archive_command(subparser
)
79 add_branch_command(subparser
)
80 add_browse_command(subparser
)
81 add_config_command(subparser
)
82 add_dag_command(subparser
)
83 add_diff_command(subparser
)
84 add_fetch_command(subparser
)
85 add_grep_command(subparser
)
86 add_merge_command(subparser
)
87 add_pull_command(subparser
)
88 add_push_command(subparser
)
89 add_rebase_command(subparser
)
90 add_remote_command(subparser
)
91 add_search_command(subparser
)
92 add_stash_command(subparser
)
93 add_tag_command(subparser
)
94 add_version_command(subparser
)
96 return parser
.parse_args()
99 def add_command(parent
, name
, description
, func
):
100 parser
= parent
.add_parser(name
, help=description
)
101 parser
.set_defaults(func
=func
)
102 add_common_arguments(parser
)
106 def add_cola_command(subparser
):
107 parser
= add_command(subparser
, 'cola', 'start git-cola', cmd_cola
)
108 parser
.add_argument('--amend', default
=False, action
='store_true',
109 help='start in amend mode')
112 def add_am_command(parent
):
113 parser
= add_command(parent
, 'am', 'apply patches using "git am"', cmd_am
)
114 parser
.add_argument('patches', metavar
='<patches>', nargs
='*',
115 help='patches to apply')
118 def add_archive_command(parent
):
119 parser
= add_command(parent
, 'archive', 'save an archive', cmd_archive
)
120 parser
.add_argument('ref', metavar
='<ref>', nargs
='?', default
=None,
121 help='SHA-1 to archive')
124 def add_branch_command(subparser
):
125 add_command(subparser
, 'branch', 'create a branch', cmd_branch
)
128 def add_browse_command(subparser
):
129 add_command(subparser
, 'browse', 'browse repository', cmd_browse
)
130 add_command(subparser
, 'classic', 'browse repository', cmd_browse
)
133 def add_config_command(subparser
):
134 add_command(subparser
, 'config', 'edit configuration', cmd_config
)
137 def add_dag_command(subparser
):
138 parser
= add_command(subparser
, 'dag', 'start git-dag', cmd_dag
)
139 parser
.add_argument('-c', '--count', metavar
='<count>',
140 type=int, default
=1000,
141 help='number of commits to display')
142 parser
.add_argument('args', nargs
='*', metavar
='<args>',
143 help='git log arguments')
145 def add_diff_command(subparser
):
146 parser
= add_command(subparser
, 'diff', 'view diffs', cmd_diff
)
147 parser
.add_argument('args', nargs
='*', metavar
='<args>',
148 help='git diff arguments')
151 def add_fetch_command(subparser
):
152 add_command(subparser
, 'fetch', 'fetch remotes', cmd_fetch
)
155 def add_grep_command(subparser
):
156 parser
= add_command(subparser
, 'grep', 'grep source', cmd_grep
)
157 parser
.add_argument('args', nargs
='*', metavar
='<args>',
158 help='git grep arguments')
160 def add_merge_command(subparser
):
161 add_command(subparser
, 'merge', 'merge branches', cmd_merge
)
164 def add_pull_command(subparser
):
165 parser
= add_command(subparser
, 'pull', 'pull remote branches', cmd_pull
)
166 parser
.add_argument('--rebase', default
=False, action
='store_true',
167 help='rebase local branch when pulling')
170 def add_push_command(subparser
):
171 add_command(subparser
, 'push', 'push remote branches', cmd_push
)
174 def add_rebase_command(subparser
):
175 parser
= add_command(subparser
, 'rebase', 'interactive rebase', cmd_rebase
)
176 parser
.add_argument('branch', metavar
='<branch>',
177 help='New upstream branch')
180 def add_remote_command(subparser
):
181 add_command(subparser
, 'remote', 'edit remotes', cmd_remote
)
184 def add_search_command(subparser
):
185 add_command(subparser
, 'search', 'search commits', cmd_search
)
188 def add_stash_command(subparser
):
189 add_command(subparser
, 'stash', 'stash and unstash changes', cmd_stash
)
192 def add_tag_command(subparser
):
193 parser
= add_command(subparser
, 'tag', 'create tags', cmd_tag
)
194 parser
.add_argument('name', metavar
='<name>', nargs
='?', default
=None,
196 parser
.add_argument('ref', metavar
='<ref>', nargs
='?', default
=None,
198 parser
.add_argument('-s', '--sign', default
=False, action
='store_true',
199 help='annotated and GPG-signed tag')
201 def add_version_command(subparser
):
202 parser
= add_command(subparser
, 'version', 'print the version', cmd_version
)
203 parser
.add_argument('--brief', action
='store_true', default
=False,
204 help='print the version number only')
209 context
= application_init(args
)
210 from cola
.widgets
.main
import MainView
211 view
= MainView(context
.model
)
213 cmds
.do(cmds
.AmendMode
, True)
214 return application_start(context
, view
)
218 context
= application_init(args
)
219 from cola
.widgets
.patch
import new_apply_patches
220 view
= new_apply_patches(patches
=args
.patches
)
221 return application_start(context
, view
)
224 def cmd_archive(args
):
225 context
= application_init(args
, update
=True)
227 args
.ref
= context
.model
.currentbranch
229 from cola
.widgets
.archive
import GitArchiveDialog
230 view
= GitArchiveDialog(args
.ref
)
231 return application_start(context
, view
)
234 def cmd_branch(args
):
235 context
= application_init(args
, update
=True)
236 from cola
.widgets
.createbranch
import create_new_branch
237 view
= create_new_branch()
238 return application_start(context
, view
)
241 def cmd_browse(args
):
242 context
= application_init(args
)
243 from cola
.widgets
.browse
import worktree_browser
244 view
= worktree_browser(update
=False)
245 return application_start(context
, view
)
248 def cmd_config(args
):
249 context
= application_init(args
)
250 from cola
.widgets
.prefs
import preferences
252 return application_start(context
, view
)
256 context
= application_init(args
)
257 from cola
.widgets
.dag
import git_dag
258 view
= git_dag(context
.model
, args
=args
)
259 return application_start(context
, view
)
263 context
= application_init(args
)
264 from cola
.difftool
import diff_expression
265 expr
= subprocess
.list2cmdline(map(core
.decode
, args
.args
))
266 view
= diff_expression(None, expr
, create_widget
=True)
267 return application_start(context
, view
)
271 # TODO: the calls to update_status() can be done asynchronously
272 # by hooking into the message_updated notification.
273 context
= application_init(args
)
274 from cola
.widgets
import remote
275 context
.model
.update_status()
276 view
= remote
.fetch()
277 return application_start(context
, view
)
281 context
= application_init(args
)
282 from cola
.widgets
import grep
283 text
= subprocess
.list2cmdline(map(core
.decode
, args
.args
))
284 view
= grep
.new_grep(text
=text
, parent
=None)
285 return application_start(context
, view
)
289 context
= application_init(args
, update
=True)
290 from cola
.widgets
.merge
import MergeView
291 view
= MergeView(context
.model
, parent
=None)
292 return application_start(context
, view
)
295 def cmd_version(args
):
296 from cola
import version
297 version
.print_version(brief
=args
.brief
)
302 context
= application_init(args
, update
=True)
303 from cola
.widgets
import remote
306 view
.set_rebase(True)
307 return application_start(context
, view
)
311 context
= application_init(args
, update
=True)
312 from cola
.widgets
import remote
314 return application_start(context
, view
)
317 def cmd_rebase(args
):
318 status
, out
, err
= cmds
.do(cmds
.Rebase
, args
.branch
, capture_output
=False)
326 def cmd_remote(args
):
327 context
= application_init(args
)
328 from cola
.widgets
import editremotes
329 view
= editremotes
.new_remote_editor()
330 return application_start(context
, view
)
333 def cmd_search(args
):
334 context
= application_init(args
)
335 from cola
.widgets
.search
import search
337 return application_start(context
, view
)
341 context
= application_init(args
)
342 from cola
.widgets
.stash
import stash
344 return application_start(context
, view
)
348 context
= application_init(args
)
349 from cola
.widgets
.createtag
import new_create_tag
350 view
= new_create_tag(name
=args
.name
, ref
=args
.ref
, sign
=args
.sign
)
351 return application_start(context
, view
)
354 if __name__
== '__main__':