File: main.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites:
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (764 lines) | stat: -rw-r--r-- 22,506 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
"""Launcher and command line interface to git-cola"""
import argparse
import sys

from . import app
from . import cmds
from . import compat
from . import core
from . import version


def main(argv=None):
    app.initialize()
    if argv is None:
        argv = sys.argv[1:]
    # we're using argparse with subparsers, but argparse
    # does not allow us to assign a default subparser
    # when none has been specified.  We fake it by injecting
    # 'cola' into the command-line so that parse_args()
    # routes them to the 'cola' parser by default.
    help_commands = core.encode('--help-commands')
    args = [core.encode(arg) for arg in argv]
    if not argv or argv[0].startswith('-') and help_commands not in args:
        argv.insert(0, 'cola')
    elif help_commands in argv:
        argv.append('--help')
    args = parse_args(argv)
    return args.func(args)


def parse_args(argv):
    parser = argparse.ArgumentParser()
    # Newer versions of argparse (Python 3.6+) emit an error message for
    # "--help-commands" unless we register the flag on the main parser.
    if compat.PY_VERSION >= (3, 6):
        add_help_options(parser)
        parser.set_defaults(func=lambda _: parser.print_help())

    subparser = parser.add_subparsers(title='valid commands')
    add_cola_command(subparser)
    add_about_command(subparser)
    add_am_command(subparser)
    add_archive_command(subparser)
    add_branch_command(subparser)
    add_browse_command(subparser)
    add_clone_command(subparser)
    add_config_command(subparser)
    add_dag_command(subparser)
    add_diff_command(subparser)
    add_fetch_command(subparser)
    add_find_command(subparser)
    add_grep_command(subparser)
    add_merge_command(subparser)
    add_pull_command(subparser)
    add_push_command(subparser)
    add_rebase_command(subparser)
    add_recent_command(subparser)
    add_remote_command(subparser)
    add_search_command(subparser)
    add_stash_command(subparser)
    add_tag_command(subparser)
    add_version_command(subparser)

    return parser.parse_args(argv)


def add_help_options(parser):
    """Add the --help-commands flag to the parser"""
    parser.add_argument(
        '--help-commands',
        default=False,
        action='store_true',
        help='show available commands',
    )


def add_command(parent, name, description, func):
    """Add a "git cola" command with common arguments"""
    parser = parent.add_parser(str(name), help=description)
    parser.set_defaults(func=func)
    app.add_common_arguments(parser)
    return parser


def add_cola_command(subparser):
    """Add the main "git cola" command. "git cola cola" is valid"""
    parser = add_command(subparser, 'cola', 'launch git-cola', cmd_cola)
    parser.add_argument(
        '--amend', default=False, action='store_true', help='start in amend mode'
    )
    add_help_options(parser)
    parser.add_argument(
        '--status-filter', '-s', metavar='<path>', default='', help='status path filter'
    )


def add_about_command(parent):
    """Add the "git cola about" documentation command"""
    add_command(parent, 'about', 'about git-cola', cmd_about)


def add_am_command(parent):
    """Add the "git cola am" command for applying patches"""
    parser = add_command(parent, 'am', 'apply patches using "git am"', cmd_am)
    parser.add_argument(
        'patches', metavar='<patches>', nargs='*', help='patches to apply'
    )


def add_archive_command(parent):
    """Add the "git cola archive" tarball export command"""
    parser = add_command(parent, 'archive', 'save an archive', cmd_archive)
    parser.add_argument(
        'ref', metavar='<ref>', nargs='?', default=None, help='commit to archive'
    )


def add_branch_command(subparser):
    """Add the "git cola branch" branch creation command"""
    add_command(subparser, 'branch', 'create a branch', cmd_branch)


def add_browse_command(subparser):
    """Add the "git cola browse" repository browser command"""
    add_command(subparser, 'browse', 'browse repository', cmd_browse)


def add_clone_command(subparser):
    """Add the "git cola clone" command for cloning repositories"""
    add_command(subparser, 'clone', 'clone repository', cmd_clone)


def add_config_command(subparser):
    """Add the "git cola config" command for editing preferences"""
    add_command(subparser, 'config', 'edit configuration', cmd_config)


def add_dag_command(subparser):
    """Add the "git cola dag" command for visualizing history"""
    parser = add_command(subparser, 'dag', 'start git-dag', cmd_dag)
    parser.add_argument(
        '-c',
        '--count',
        metavar='<count>',
        type=int,
        default=1000,
        help='number of commits to display',
    )
    parser.add_argument(
        '--all',
        action='store_true',
        dest='show_all',
        help='visualize all branches',
        default=False,
    )
    parser.add_argument(
        'args', nargs=argparse.REMAINDER, metavar='<args>', help='git log arguments'
    )


def add_diff_command(subparser):
    """Add the "git cola diff" command for diffing changes"""
    parser = add_command(subparser, 'diff', 'view diffs', cmd_diff)
    parser.add_argument(
        'args', nargs=argparse.REMAINDER, metavar='<args>', help='git diff arguments'
    )


def add_fetch_command(subparser):
    """Add the "git cola fetch" command for fetching repositories"""
    add_command(subparser, 'fetch', 'fetch remotes', cmd_fetch)


def add_find_command(subparser):
    """Add the "git cola find" command for finding files"""
    parser = add_command(subparser, 'find', 'find files', cmd_find)
    parser.add_argument('paths', nargs='*', metavar='<path>', help='filter by path')


def add_grep_command(subparser):
    """Add the "git cola grep" command for searching files"""
    parser = add_command(subparser, 'grep', 'grep source', cmd_grep)
    parser.add_argument('args', nargs='*', metavar='<args>', help='git grep arguments')


def add_merge_command(subparser):
    """Add the "git cola merge" command for merging branches"""
    parser = add_command(subparser, 'merge', 'merge branches', cmd_merge)
    parser.add_argument(
        'ref', nargs='?', metavar='<ref>', help='branch, tag, or commit to merge'
    )


def add_pull_command(subparser):
    """Add the "git cola pull" command for pulling changes from remotes"""
    parser = add_command(subparser, 'pull', 'pull remote branches', cmd_pull)
    parser.add_argument(
        '--rebase',
        default=False,
        action='store_true',
        help='rebase local branch when pulling',
    )


def add_push_command(subparser):
    """Add the "git cola push" command for pushing branches to remotes"""
    add_command(subparser, 'push', 'push remote branches', cmd_push)


def add_rebase_command(subparser):
    """Add the "git cola rebase" command for rebasing the current branch"""
    parser = add_command(subparser, 'rebase', 'interactive rebase', cmd_rebase)
    parser.add_argument(
        '-v',
        '--verbose',
        default=False,
        action='store_true',
        help='display a diffstat of what changed upstream',
    )
    parser.add_argument(
        '-q',
        '--quiet',
        default=False,
        action='store_true',
        help='be quiet. implies --no-stat',
    )
    parser.add_argument(
        '-i', '--interactive', default=True, action='store_true', help=argparse.SUPPRESS
    )
    parser.add_argument(
        '--autostash',
        default=False,
        action='store_true',
        help='automatically stash/stash pop before and after',
    )
    parser.add_argument(
        '--fork-point',
        default=False,
        action='store_true',
        help="use 'merge-base --fork-point' to refine upstream",
    )
    parser.add_argument(
        '--onto',
        default=None,
        metavar='<newbase>',
        help='rebase onto given branch instead of upstream',
    )
    parser.add_argument(
        '-p',
        '--preserve-merges',
        default=False,
        action='store_true',
        help=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--rebase-merges',
        default=False,
        action='store_true',
        help='preserve branching structure when rebasing',
    )
    parser.add_argument(
        '-s',
        '--strategy',
        default=None,
        metavar='<strategy>',
        help='use the given merge strategy',
    )
    parser.add_argument(
        '--no-ff',
        default=False,
        action='store_true',
        help='cherry-pick all commits, even if unchanged',
    )
    parser.add_argument(
        '-m',
        '--merge',
        default=False,
        action='store_true',
        help='use merging strategies to rebase',
    )
    parser.add_argument(
        '-x',
        '--exec',
        default=None,
        help='add exec lines after each commit of ' 'the editable list',
    )
    parser.add_argument(
        '-k',
        '--keep-empty',
        default=False,
        action='store_true',
        help='preserve empty commits during rebase',
    )
    parser.add_argument(
        '-f',
        '--force-rebase',
        default=False,
        action='store_true',
        help='force rebase even if branch is up to date',
    )
    parser.add_argument(
        '-X',
        '--strategy-option',
        default=None,
        metavar='<arg>',
        help='pass the argument through to the merge strategy',
    )
    parser.add_argument(
        '--stat',
        default=False,
        action='store_true',
        help='display a diffstat of what changed upstream',
    )
    parser.add_argument(
        '-n',
        '--no-stat',
        default=False,
        action='store_true',
        help='do not show diffstat of what changed upstream',
    )
    parser.add_argument(
        '--verify',
        default=False,
        action='store_true',
        help='allow pre-rebase hook to run',
    )
    parser.add_argument(
        '--rerere-autoupdate',
        default=False,
        action='store_true',
        help='allow rerere to update index with ' 'resolved conflicts',
    )
    parser.add_argument(
        '--root',
        default=False,
        action='store_true',
        help='rebase all reachable commits up to the root(s)',
    )
    parser.add_argument(
        '--autosquash',
        default=True,
        action='store_true',
        help='move commits that begin with ' 'squash!/fixup! under -i',
    )
    parser.add_argument(
        '--no-autosquash',
        default=True,
        action='store_false',
        dest='autosquash',
        help='do not move commits that begin with ' 'squash!/fixup! under -i',
    )
    parser.add_argument(
        '--committer-date-is-author-date',
        default=False,
        action='store_true',
        help="passed to 'git am' by 'git rebase'",
    )
    parser.add_argument(
        '--ignore-date',
        default=False,
        action='store_true',
        help="passed to 'git am' by 'git rebase'",
    )
    parser.add_argument(
        '--whitespace',
        default=False,
        action='store_true',
        help="passed to 'git apply' by 'git rebase'",
    )
    parser.add_argument(
        '--ignore-whitespace',
        default=False,
        action='store_true',
        help="passed to 'git apply' by 'git rebase'",
    )
    parser.add_argument(
        '--update-refs',
        default=False,
        action='store_true',
        help='update branches that point to commits that are being rebased',
    )
    parser.add_argument(
        '-C',
        dest='context_lines',
        default=None,
        metavar='<n>',
        help="passed to 'git apply' by 'git rebase'",
    )

    actions = parser.add_argument_group('actions')
    actions.add_argument(
        '--continue', default=False, action='store_true', help='continue'
    )
    actions.add_argument(
        '--abort',
        default=False,
        action='store_true',
        help='abort and check out the original branch',
    )
    actions.add_argument(
        '--skip',
        default=False,
        action='store_true',
        help='skip current patch and continue',
    )
    actions.add_argument(
        '--edit-todo',
        default=False,
        action='store_true',
        help='edit the todo list during an interactive rebase',
    )

    parser.add_argument(
        'upstream',
        nargs='?',
        default=None,
        metavar='<upstream>',
        help='the upstream configured in branch.<name>.remote '
        'and branch.<name>.merge options will be used '
        'when <upstream> is omitted; see git-rebase(1) '
        'for details. If you are currently not on any '
        'branch or if the current branch does not have '
        'a configured upstream, the rebase will abort',
    )
    parser.add_argument(
        'branch',
        nargs='?',
        default=None,
        metavar='<branch>',
        help='git rebase will perform an automatic '
        '"git checkout <branch>" before doing anything '
        'else when <branch> is specified',
    )


def add_recent_command(subparser):
    """Add the "git cola recent" command for opening recently edited files"""
    add_command(subparser, 'recent', 'edit recent files', cmd_recent)


def add_remote_command(subparser):
    """Add the "git cola remote" command for editing remotes"""
    add_command(subparser, 'remote', 'edit remotes', cmd_remote)


def add_search_command(subparser):
    """Add the "git cola search" command for searching over commits"""
    add_command(subparser, 'search', 'search commits', cmd_search)


def add_stash_command(subparser):
    """Add the "git cola stash" command for creating and applying stashes"""
    add_command(subparser, 'stash', 'stash and unstash changes', cmd_stash)


def add_tag_command(subparser):
    """Add the "git cola tag" command for creating tags"""
    parser = add_command(subparser, 'tag', 'create tags', cmd_tag)
    parser.add_argument(
        'name', metavar='<name>', nargs='?', default=None, help='tag name'
    )
    parser.add_argument(
        'ref', metavar='<ref>', nargs='?', default=None, help='commit to tag'
    )
    parser.add_argument(
        '-s',
        '--sign',
        default=False,
        action='store_true',
        help='annotated and GPG-signed tag',
    )


def add_version_command(subparser):
    """Add the "git cola version" command for displaying Git Cola's version"""
    parser = add_command(subparser, 'version', 'print the version', cmd_version)
    parser.add_argument(
        '--builtin',
        action='store_true',
        default=False,
        help='print the builtin fallback version',
    )
    parser.add_argument(
        '--brief',
        action='store_true',
        default=False,
        help='print the version number only',
    )


# entry points
def cmd_cola(args):
    """The "git cola" entry point"""
    from .widgets.main import MainView

    status_filter = args.status_filter
    if status_filter:
        status_filter = core.abspath(status_filter)

    context = app.application_init(args)

    context.timer.start('view')
    view = MainView(context)
    if args.amend:
        cmds.do(cmds.AmendMode, context, amend=True)

    if status_filter:
        view.set_filter(core.relpath(status_filter))

    context.timer.stop('view')
    if args.perf:
        context.timer.display('view')

    return app.application_run(context, view, start=start_cola, stop=app.default_stop)


def start_cola(context, view):
    app.default_start(context, view)
    view.start(context)


def cmd_about(args):
    from .widgets import about

    context = app.application_init(args)
    view = about.about_dialog(context)
    return app.application_start(context, view)


def cmd_am(args):
    from .widgets.patch import new_apply_patches

    context = app.application_init(args)
    view = new_apply_patches(context, patches=args.patches)
    return app.application_start(context, view)


def cmd_archive(args):
    from .widgets import archive

    context = app.application_init(args, update=True)
    if args.ref is None:
        args.ref = context.model.currentbranch
    view = archive.Archive(context, args.ref)
    return app.application_start(context, view)


def cmd_branch(args):
    from .widgets.createbranch import create_new_branch

    context = app.application_init(args, update=True)
    view = create_new_branch(context)
    return app.application_start(context, view)


def cmd_browse(args):
    from .widgets.browse import worktree_browser

    context = app.application_init(args)
    view = worktree_browser(context, show=False, update=False)
    return app.application_start(context, view)


def cmd_clone(args):
    from .widgets import clone

    context = app.application_init(args)
    view = clone.clone(context)
    context.set_view(view)
    result = 0 if view.exec_() == view.Accepted else 1
    app.default_stop(context, view)
    return result


def cmd_config(args):
    from .widgets.prefs import preferences

    context = app.application_init(args)
    view = preferences(context)
    return app.application_start(context, view)


def cmd_dag(args):
    from .widgets import dag

    context = app.application_init(args, app_name='Git DAG')
    # cola.main() uses parse_args(), unlike dag.main() which uses
    # parse_known_args(), thus we aren't able to automatically forward
    # all unknown arguments.  Special-case support for "--all" since it's
    # used by the history viewer command on Windows.
    if args.show_all:
        args.args.insert(0, '--all')
    view = dag.git_dag(context, args=args, show=False)
    return app.application_start(context, view)


def cmd_diff(args):
    from .difftool import diff_expression

    context = app.application_init(args)
    expr = core.list2cmdline(args.args)
    view = diff_expression(context, None, expr, create_widget=True)
    return app.application_start(context, view)


def cmd_fetch(args):
    # TODO: the calls to update_status() can be done asynchronously
    # by hooking into the message_updated notification.
    from .widgets import remote

    context = app.application_init(args)
    context.model.update_status()
    view = remote.fetch(context)
    return app.application_start(context, view)


def cmd_find(args):
    from .widgets import finder

    context = app.application_init(args)
    paths = core.list2cmdline(args.paths)
    view = finder.finder(context, paths=paths)
    return app.application_start(context, view)


def cmd_grep(args):
    from .widgets import grep

    context = app.application_init(args)
    text = core.list2cmdline(args.args)
    view = grep.new_grep(context, text=text, parent=None)
    return app.application_start(context, view)


def cmd_merge(args):
    from .widgets.merge import Merge

    context = app.application_init(args, update=True)
    view = Merge(context, parent=None, ref=args.ref)
    return app.application_start(context, view)


def cmd_version(args):
    from . import version

    version.print_version(builtin=args.builtin, brief=args.brief)
    return 0


def cmd_pull(args):
    from .widgets import remote

    context = app.application_init(args, update=True)
    view = remote.pull(context)
    if args.rebase:
        view.set_rebase(True)
    return app.application_start(context, view)


def cmd_push(args):
    from .widgets import remote

    context = app.application_init(args, update=True)
    view = remote.push(context)
    return app.application_start(context, view)


def cmd_rebase(args):
    context = app.application_init(args)
    context.model.update_refs()
    kwargs = {
        'verbose': args.verbose,
        'quiet': args.quiet,
        'autostash': args.autostash,
        'fork_point': args.fork_point,
        'onto': args.onto,
        'strategy': args.strategy,
        'no_ff': args.no_ff,
        'merge': args.merge,
        'exec': getattr(args, 'exec', None),  # python keyword
        'keep_empty': args.keep_empty,
        'force_rebase': args.force_rebase,
        'strategy_option': args.strategy_option,
        'stat': args.stat,
        'no_stat': args.no_stat,
        'verify': args.verify,
        'rerere_autoupdate': args.rerere_autoupdate,
        'root': args.root,
        'autosquash': args.autosquash,
        'committer_date_is_author_date': args.committer_date_is_author_date,
        'ignore_date': args.ignore_date,
        'whitespace': args.whitespace,
        'ignore_whitespace': args.ignore_whitespace,
        'C': args.context_lines,
        'continue': getattr(args, 'continue', False),  # python keyword
        'abort': args.abort,
        'skip': args.skip,
        'edit_todo': args.edit_todo,
        'update_refs': args.update_refs,
        'upstream': args.upstream,
        'branch': args.branch,
    }
    # Backwards compatibility: --preserve-merges was replaced by --rebase-merges.
    rebase_merges = args.rebase_merges or args.preserve_merges
    if version.check_git(context, 'rebase-merges'):
        kwargs['rebase_merges'] = rebase_merges
    else:
        kwargs['preserve_merges'] = rebase_merges
    status, _, _ = cmds.do(cmds.Rebase, context, **kwargs)
    return status


def cmd_recent(args):
    from .widgets import recent

    context = app.application_init(args)
    view = recent.browse_recent_files(context)
    return app.application_start(context, view)


def cmd_remote(args):
    from .widgets import editremotes

    context = app.application_init(args)
    view = editremotes.editor(context, run=False)
    return app.application_start(context, view)


def cmd_search(args):
    from .widgets.search import search

    context = app.application_init(args)
    view = search(context)
    return app.application_start(context, view)


def cmd_stash(args):
    from .widgets import stash

    context = app.application_init(args)
    view = stash.view(context, show=False)
    return app.application_start(context, view)


def cmd_tag(args):
    from .widgets.createtag import new_create_tag

    context = app.application_init(args)
    context.model.update_status()
    view = new_create_tag(context, name=args.name, ref=args.ref, sign=args.sign)
    return app.application_start(context, view)


# Windows shortcut launch features:
def shortcut_launch():
    """Launch from a shortcut

    Prompt for the repository by default.

    """
    argv = sys.argv[1:]
    if not argv:
        argv = ['cola', '--prompt']
    return main(argv=argv)