1 """Launcher and command line interface to git-cola"""
3 from __future__
import absolute_import
, division
, unicode_literals
9 from .app
import add_common_arguments
10 from .app
import application_init
11 from .app
import application_start
22 # we're using argparse with subparser, but argparse
23 # does not allow us to assign a default subparser
24 # when none has been specified. We fake it by injecting
25 # 'cola' into the command-line so that parse_args()
26 # routes them to the 'cola' parser by default.
28 argv
[0].startswith('-') and
29 '--help-commands' not in argv
):
30 argv
.insert(0, 'cola')
31 elif '--help-commands' in argv
:
33 args
= parse_args(argv
)
34 return args
.func(args
)
38 parser
= argparse
.ArgumentParser()
39 subparser
= parser
.add_subparsers(title
='valid commands')
41 add_cola_command(subparser
)
42 add_about_command(subparser
)
43 add_am_command(subparser
)
44 add_archive_command(subparser
)
45 add_branch_command(subparser
)
46 add_browse_command(subparser
)
47 add_config_command(subparser
)
48 add_dag_command(subparser
)
49 add_diff_command(subparser
)
50 add_fetch_command(subparser
)
51 add_find_command(subparser
)
52 add_grep_command(subparser
)
53 add_merge_command(subparser
)
54 add_pull_command(subparser
)
55 add_push_command(subparser
)
56 add_rebase_command(subparser
)
57 add_remote_command(subparser
)
58 add_search_command(subparser
)
59 add_stash_command(subparser
)
60 add_tag_command(subparser
)
61 add_version_command(subparser
)
63 return parser
.parse_args(argv
)
66 def add_command(parent
, name
, description
, func
):
67 parser
= parent
.add_parser(str(name
), help=description
)
68 parser
.set_defaults(func
=func
)
69 add_common_arguments(parser
)
73 def add_cola_command(subparser
):
74 parser
= add_command(subparser
, 'cola', 'start git-cola', cmd_cola
)
75 parser
.add_argument('--amend', default
=False, action
='store_true',
76 help='start in amend mode')
77 parser
.add_argument('--help-commands', default
=False, action
='store_true',
78 help='show available sub-commands')
79 parser
.add_argument('--status-filter', '-s', metavar
='<path>',
80 default
='', help='status path filter')
83 def add_about_command(parent
):
84 parser
= add_command(parent
, 'about', 'about git-cola', cmd_about
)
86 def add_am_command(parent
):
87 parser
= add_command(parent
, 'am', 'apply patches using "git am"', cmd_am
)
88 parser
.add_argument('patches', metavar
='<patches>', nargs
='*',
89 help='patches to apply')
92 def add_archive_command(parent
):
93 parser
= add_command(parent
, 'archive', 'save an archive', cmd_archive
)
94 parser
.add_argument('ref', metavar
='<ref>', nargs
='?', default
=None,
95 help='commit to archive')
98 def add_branch_command(subparser
):
99 add_command(subparser
, 'branch', 'create a branch', cmd_branch
)
102 def add_browse_command(subparser
):
103 add_command(subparser
, 'browse', 'browse repository', cmd_browse
)
104 add_command(subparser
, 'classic', 'browse repository', cmd_browse
)
107 def add_config_command(subparser
):
108 add_command(subparser
, 'config', 'edit configuration', cmd_config
)
111 def add_dag_command(subparser
):
112 parser
= add_command(subparser
, 'dag', 'start git-dag', cmd_dag
)
113 parser
.add_argument('-c', '--count', metavar
='<count>',
114 type=int, default
=1000,
115 help='number of commits to display')
116 parser
.add_argument('args', nargs
='*', metavar
='<args>',
117 help='git log arguments')
120 def add_diff_command(subparser
):
121 parser
= add_command(subparser
, 'diff', 'view diffs', cmd_diff
)
122 parser
.add_argument('args', nargs
='*', metavar
='<args>',
123 help='git diff arguments')
126 def add_fetch_command(subparser
):
127 add_command(subparser
, 'fetch', 'fetch remotes', cmd_fetch
)
130 def add_find_command(subparser
):
131 parser
= add_command(subparser
, 'find', 'find files', cmd_find
)
132 parser
.add_argument('paths', nargs
='*', metavar
='<path>',
133 help='filter by path')
136 def add_grep_command(subparser
):
137 parser
= add_command(subparser
, 'grep', 'grep source', cmd_grep
)
138 parser
.add_argument('args', nargs
='*', metavar
='<args>',
139 help='git grep arguments')
142 def add_merge_command(subparser
):
143 add_command(subparser
, 'merge', 'merge branches', cmd_merge
)
146 def add_pull_command(subparser
):
147 parser
= add_command(subparser
, 'pull', 'pull remote branches', cmd_pull
)
148 parser
.add_argument('--rebase', default
=False, action
='store_true',
149 help='rebase local branch when pulling')
152 def add_push_command(subparser
):
153 add_command(subparser
, 'push', 'push remote branches', cmd_push
)
156 def add_rebase_command(subparser
):
157 parser
= add_command(subparser
, 'rebase', 'interactive rebase', cmd_rebase
)
158 parser
.add_argument('-v', '--verbose', default
=False, action
='store_true',
159 help='display a diffstat of what changed upstream')
160 parser
.add_argument('-q', '--quiet', default
=False, action
='store_true',
161 help='be quiet. implies --no-stat')
162 parser
.add_argument('-i', '--interactive', default
=True,
163 action
='store_true', help=argparse
.SUPPRESS
)
164 parser
.add_argument('--autostash', default
=False, action
='store_true',
165 help='automatically stash/stash pop before and after')
166 parser
.add_argument('--fork-point', default
=False, action
='store_true',
167 help="use 'merge-base --fork-point' to refine upstream")
168 parser
.add_argument('--onto', default
=None, metavar
='<newbase>',
169 help='rebase onto given branch instead of upstream')
170 parser
.add_argument('-p', '--preserve-merges',
171 default
=False, action
='store_true',
172 help='try to recreate merges instead of ignoring them')
173 parser
.add_argument('-s', '--strategy', default
=None, metavar
='<strategy>',
174 help='use the given merge strategy')
175 parser
.add_argument('--no-ff', default
=False, action
='store_true',
176 help='cherry-pick all commits, even if unchanged')
177 parser
.add_argument('-m', '--merge', default
=False, action
='store_true',
178 help='use merging strategies to rebase')
179 parser
.add_argument('-x', '--exec', default
=None,
180 help='add exec lines after each commit of '
182 parser
.add_argument('-k', '--keep-empty', default
=False,
184 help='preserve empty commits during rebase')
185 parser
.add_argument('-f', '--force-rebase', default
=False,
187 help='force rebase even if branch is up to date')
188 parser
.add_argument('-X', '--strategy-option', default
=None,
190 help='pass the argument through to the merge strategy')
191 parser
.add_argument('--stat', default
=False, action
='store_true',
192 help='display a diffstat of what changed upstream')
193 parser
.add_argument('-n', '--no-stat', default
=False, action
='store_true',
194 help='do not show diffstat of what changed upstream')
195 parser
.add_argument('--verify', default
=False, action
='store_true',
196 help='allow pre-rebase hook to run')
197 parser
.add_argument('--rerere-autoupdate',
198 default
=False, action
='store_true',
199 help='allow rerere to update index with '
200 'resolved conflicts')
201 parser
.add_argument('--root', default
=False, action
='store_true',
202 help='rebase all reachable commits up to the root(s)')
203 parser
.add_argument('--autosquash', default
=True, action
='store_true',
204 help='move commits that begin with '
205 'squash!/fixup! under -i')
206 parser
.add_argument('--no-autosquash', default
=True, action
='store_false',
208 help='do not move commits that begin with '
209 'squash!/fixup! under -i')
210 parser
.add_argument('--committer-date-is-author-date',
211 default
=False, action
='store_true',
212 help="passed to 'git am' by 'git rebase'")
213 parser
.add_argument('--ignore-date', default
=False, action
='store_true',
214 help="passed to 'git am' by 'git rebase'")
215 parser
.add_argument('--whitespace', default
=False, action
='store_true',
216 help="passed to 'git apply' by 'git rebase'")
217 parser
.add_argument('--ignore-whitespace', default
=False,
219 help="passed to 'git apply' by 'git rebase'")
220 parser
.add_argument('-C', dest
='context_lines', default
=None, metavar
='<n>',
221 help="passed to 'git apply' by 'git rebase'")
223 actions
= parser
.add_argument_group('actions')
224 actions
.add_argument('--continue', default
=False, action
='store_true',
226 actions
.add_argument('--abort', default
=False, action
='store_true',
227 help='abort and check out the original branch')
228 actions
.add_argument('--skip', default
=False, action
='store_true',
229 help='skip current patch and continue')
230 actions
.add_argument('--edit-todo', default
=False, action
='store_true',
231 help='edit the todo list during an interactive rebase')
233 parser
.add_argument('upstream', nargs
='?', default
=None,
234 metavar
='<upstream>',
235 help='the upstream configured in branch.<name>.remote '
236 'and branch.<name>.merge options will be used '
237 'when <upstream> is omitted; see git-rebase(1) '
238 'for details. If you are currently not on any '
239 'branch or if the current branch does not have '
240 'a configured upstream, the rebase will abort')
241 parser
.add_argument('branch', nargs
='?', default
=None, metavar
='<branch>',
242 help='git rebase will perform an automatic '
243 '"git checkout <branch>" before doing anything '
244 'else when <branch> is specified')
247 def add_remote_command(subparser
):
248 add_command(subparser
, 'remote', 'edit remotes', cmd_remote
)
251 def add_search_command(subparser
):
252 add_command(subparser
, 'search', 'search commits', cmd_search
)
255 def add_stash_command(subparser
):
256 add_command(subparser
, 'stash', 'stash and unstash changes', cmd_stash
)
259 def add_tag_command(subparser
):
260 parser
= add_command(subparser
, 'tag', 'create tags', cmd_tag
)
261 parser
.add_argument('name', metavar
='<name>', nargs
='?', default
=None,
263 parser
.add_argument('ref', metavar
='<ref>', nargs
='?', default
=None,
264 help='commit to tag')
265 parser
.add_argument('-s', '--sign', default
=False, action
='store_true',
266 help='annotated and GPG-signed tag')
269 def add_version_command(subparser
):
270 parser
= add_command(subparser
, 'version', 'print the version', cmd_version
)
271 parser
.add_argument('--brief', action
='store_true', default
=False,
272 help='print the version number only')
277 from .widgets
.main
import MainView
278 status_filter
= args
.status_filter
280 status_filter
= core
.abspath(status_filter
)
282 context
= application_init(args
)
283 view
= MainView(context
.model
, settings
=args
.settings
)
285 cmds
.do(cmds
.AmendMode
, True)
288 view
.set_filter(core
.relpath(status_filter
))
290 return application_start(context
, view
)
294 from .widgets
import about
295 context
= application_init(args
)
296 view
= about
.about_dialog()
297 return application_start(context
, view
)
301 from .widgets
.patch
import new_apply_patches
302 context
= application_init(args
)
303 view
= new_apply_patches(patches
=args
.patches
)
304 return application_start(context
, view
)
307 def cmd_archive(args
):
308 from .widgets
.archive
import GitArchiveDialog
309 context
= application_init(args
, update
=True)
311 args
.ref
= context
.model
.currentbranch
312 view
= GitArchiveDialog(args
.ref
)
313 return application_start(context
, view
)
316 def cmd_branch(args
):
317 from .widgets
.createbranch
import create_new_branch
318 context
= application_init(args
, update
=True)
319 view
= create_new_branch(settings
=args
.settings
)
320 return application_start(context
, view
)
323 def cmd_browse(args
):
324 from .widgets
.browse
import worktree_browser
325 context
= application_init(args
)
326 view
= worktree_browser(update
=False, settings
=args
.settings
)
327 return application_start(context
, view
)
330 def cmd_config(args
):
331 from .widgets
.prefs
import preferences
332 context
= application_init(args
)
334 return application_start(context
, view
)
338 context
= application_init(args
)
339 from .widgets
.dag
import git_dag
340 view
= git_dag(context
.model
, args
=args
, settings
=args
.settings
)
341 return application_start(context
, view
)
345 context
= application_init(args
)
346 from .difftool
import diff_expression
347 expr
= core
.list2cmdline(args
.args
)
348 view
= diff_expression(None, expr
, create_widget
=True)
349 return application_start(context
, view
)
353 # TODO: the calls to update_status() can be done asynchronously
354 # by hooking into the message_updated notification.
355 context
= application_init(args
)
356 from .widgets
import remote
357 context
.model
.update_status()
358 view
= remote
.fetch()
359 return application_start(context
, view
)
363 context
= application_init(args
)
364 from .widgets
import finder
365 paths
= core
.list2cmdline(args
.paths
)
366 view
= finder
.finder(paths
=paths
)
367 return application_start(context
, view
)
371 context
= application_init(args
)
372 from .widgets
import grep
373 text
= core
.list2cmdline(args
.args
)
374 view
= grep
.new_grep(text
=text
, parent
=None)
375 return application_start(context
, view
)
379 context
= application_init(args
, update
=True)
380 from .widgets
.merge
import MergeView
381 view
= MergeView(context
.cfg
, context
.model
, parent
=None)
382 return application_start(context
, view
)
385 def cmd_version(args
):
386 from . import version
387 version
.print_version(brief
=args
.brief
)
392 from .widgets
import remote
393 context
= application_init(args
, update
=True)
396 view
.set_rebase(True)
397 return application_start(context
, view
)
401 from .widgets
import remote
402 context
= application_init(args
, update
=True)
404 return application_start(context
, view
)
407 def cmd_rebase(args
):
409 'verbose': args
.verbose
,
411 'autostash': args
.autostash
,
412 'fork_point': args
.fork_point
,
414 'preserve_merges': args
.preserve_merges
,
415 'strategy': args
.strategy
,
418 'exec': getattr(args
, 'exec', None), # python keyword
419 'keep_empty': args
.keep_empty
,
420 'force_rebase': args
.force_rebase
,
421 'strategy_option': args
.strategy_option
,
423 'no_stat': args
.no_stat
,
424 'verify': args
.verify
,
425 'rerere_autoupdate': args
.rerere_autoupdate
,
427 'autosquash': args
.autosquash
,
428 'committer_date_is_author_date': args
.committer_date_is_author_date
,
429 'ignore_date': args
.ignore_date
,
430 'whitespace': args
.whitespace
,
431 'ignore_whitespace': args
.ignore_whitespace
,
432 'C': args
.context_lines
,
433 'continue': getattr(args
, 'continue', False), # python keyword
436 'edit_todo': args
.edit_todo
,
437 'upstream': args
.upstream
,
438 'branch': args
.branch
,
439 'capture_output': False,
441 status
, out
, err
= cmds
.do(cmds
.Rebase
, **kwargs
)
449 def cmd_remote(args
):
450 from .widgets
import editremotes
451 context
= application_init(args
)
452 view
= editremotes
.new_remote_editor()
453 return application_start(context
, view
)
456 def cmd_search(args
):
457 from .widgets
.search
import search
458 context
= application_init(args
)
460 return application_start(context
, view
)
464 from .widgets
.stash
import stash
465 context
= application_init(args
)
467 return application_start(context
, view
)
471 from .widgets
.createtag
import new_create_tag
472 context
= application_init(args
)
473 view
= new_create_tag(name
=args
.name
, ref
=args
.ref
, sign
=args
.sign
,
474 settings
=args
.settings
)
475 return application_start(context
, view
)
478 # Windows shortcut launch features:
481 """Return the path of git.exe, or None if we can't find it."""
482 if not utils
.is_win32():
483 return None # UNIX systems have git in their $PATH
485 # If the user wants to use a Git/bin/ directory from a non-standard
486 # directory then they can write its location into
487 # ~/.config/git-cola/git-bindir
488 git_bindir
= os
.path
.expanduser(os
.path
.join('~', '.config', 'git-cola',
490 if core
.exists(git_bindir
):
491 custom_path
= core
.read(git_bindir
).strip()
492 if custom_path
and core
.exists(custom_path
):
495 # Try to find Git's bin/ directory in one of the typical locations
496 pf
= os
.environ
.get('ProgramFiles', 'C:\\Program Files')
497 pf32
= os
.environ
.get('ProgramFiles(x86)', 'C:\\Program Files (x86)')
498 for p
in [pf32
, pf
, 'C:\\']:
499 candidate
= os
.path
.join(p
, 'Git\\bin')
500 if os
.path
.isdir(candidate
):
506 def shortcut_launch():
507 """Launch from a shortcut
509 Prompt for the repository by default, and try to find git.
511 argv
= ['cola', '--prompt']
512 git_path
= find_git()
514 prepend_path(git_path
)
519 def prepend_path(path
):
520 # Adds git to the PATH. This is needed on Windows.
521 path
= core
.decode(path
)
522 path_entries
= core
.getenv('PATH', '').split(os
.pathsep
)
523 if path
not in path_entries
:
524 path_entries
.insert(0, path
)
525 compat
.setenv('PATH', os
.pathsep
.join(path_entries
))