File: commitmsg.py

package info (click to toggle)
git-cola 4.14.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,812 kB
  • sloc: python: 37,625; sh: 298; makefile: 223; xml: 102; tcl: 62
file content (1263 lines) | stat: -rw-r--r-- 48,355 bytes parent folder | download
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
import datetime
from functools import partial

from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets
from qtpy.QtCore import Qt
from qtpy.QtCore import Signal

from .. import actions
from .. import cmds
from .. import core
from .. import display
from .. import gitcmds
from .. import hotkeys
from .. import icons
from .. import textwrap
from .. import qtutils
from .. import spellcheck
from .. import utils
from ..interaction import Interaction
from ..gitcmds import commit_message_path
from ..i18n import N_
from ..models import dag
from ..models import prefs
from ..qtutils import get
from . import defs
from . import standard
from .selectcommits import select_commits
from .spellcheck import SpellCheckLineEdit, SpellCheckTextEdit
from .text import event_anchor_mode, is_shift_pressed


class CommitMessageEditor(QtWidgets.QFrame):
    commit_finished = Signal(object)
    cursor_changed = Signal(int, int)
    down = Signal()
    up = Signal()

    def __init__(self, context, parent):
        QtWidgets.QFrame.__init__(self, parent)
        cfg = context.cfg
        self.context = context
        self.model = model = context.model
        self.spellcheck_initialized = False
        self.spellcheck = spellcheck.NorvigSpellCheck()
        self.spellcheck.set_dictionary(cfg.get('cola.dictionary', None))

        self._linebreak = None
        self._textwidth = None
        self._tabwidth = None
        self._last_commit_datetime = None  # The most recently selected commit date.
        self._last_commit_datetime_backup = None  # Used when amending.
        self._git_commit_date = None  # Overrides the commit date when committing.
        self._commit_authors = []  # Recently used author override values.
        self._last_git_commit_author = None  # The most recently selected author.
        self._git_commit_author = None  # Overrides the commit author when comitting.
        self._git_commit_author_backup = None  # Used when amending.

        self._widgets_initialized = False  # Defer setting the cursor position height.

        # Actions
        self.signoff_action = qtutils.add_action(
            self, cmds.SignOff.name(), cmds.run(cmds.SignOff, context), hotkeys.SIGNOFF
        )
        self.signoff_action.setIcon(icons.style_dialog_apply())
        self.signoff_action.setToolTip(N_('Sign off on this commit'))

        self.commit_action = qtutils.add_action(
            self, N_('Commit@@verb'), self.commit, hotkeys.APPLY
        )
        self.commit_action.setIcon(icons.commit())
        self.commit_action.setToolTip(N_('Commit staged changes'))
        self.clear_action = qtutils.add_action(self, N_('Clear...'), self.clear)

        self.launch_editor = actions.launch_editor_at_line(context, self)
        self.launch_difftool = actions.launch_difftool(context, self)

        self.move_up = actions.move_up(self)
        self.move_down = actions.move_down(self)

        # Menu actions
        self.menu_actions = menu_actions = [
            self.signoff_action,
            self.commit_action,
            None,
            self.launch_editor,
            self.launch_difftool,
            None,
            self.move_up,
            self.move_down,
            None,
        ]

        # Widgets
        self.summary = CommitSummaryLineEdit(context, check=self.spellcheck)
        self.summary.menu_actions.extend(menu_actions)
        self.summary.addAction(self.commit_action)
        self.summary.addAction(self.move_up)
        self.summary.addAction(self.move_down)
        self.summary.addAction(self.signoff_action)

        self.description = CommitMessageTextEdit(
            context, check=self.spellcheck, parent=self
        )
        self.description.menu_actions.extend(menu_actions)

        commit_button_tooltip = N_('Commit staged changes\nShortcut: Ctrl+Enter')
        self.commit_button = qtutils.create_button(
            text=N_('Commit@@verb'), tooltip=commit_button_tooltip, icon=icons.commit()
        )
        self.commit_group = utils.Group(self.commit_action, self.commit_button)
        self.commit_progress_bar = standard.progress_bar(
            self,
            disable=(self.commit_button, self.summary, self.description),
        )

        # make the position label fixed size to avoid layout issues
        font = qtutils.default_monospace_font()
        font.setPixelSize(defs.action_text)
        text_width = qtutils.text_width(font, '999:999')
        cursor_position_label = self.cursor_position_label = QtWidgets.QLabel(self)
        cursor_position_label.setFont(font)
        cursor_position_label.setMinimumWidth(text_width)
        cursor_position_label.setAlignment(Qt.AlignCenter)

        self.actions_menu = qtutils.create_menu(N_('Actions'), self)
        self.actions_button = qtutils.create_toolbutton(
            icon=icons.configure(), tooltip=N_('Actions...')
        )
        self.actions_button.setMenu(self.actions_menu)

        self.actions_menu.addAction(self.signoff_action)
        self.actions_menu.addAction(self.commit_action)
        self.actions_menu.addSeparator()

        # Amend checkbox
        self.amend_action = qtutils.add_action_bool(
            self,
            N_('Amend Last Commit'),
            partial(cmds.run(cmds.AmendMode), context),
            False,
            *hotkeys.AMEND,
        )
        self.amend_action.setIcon(icons.edit())
        self.amend_action.setCheckable(True)
        self.amend_action.setShortcutContext(Qt.ApplicationShortcut)
        self.actions_menu.addAction(self.amend_action)

        # Commit Date
        self.commit_date_action = self.actions_menu.addAction(N_('Set Commit Date'))
        self.commit_date_action.setCheckable(True)
        self.commit_date_action.setChecked(False)
        qtutils.connect_action_bool(self.commit_date_action, self.set_commit_date)

        # Commit Author
        self.commit_author_action = self.actions_menu.addAction(N_('Set Commit Author'))
        self.commit_author_action.setCheckable(True)
        self.commit_author_action.setChecked(False)
        qtutils.connect_action_bool(self.commit_author_action, self.set_commit_author)

        # Bypass hooks
        self.bypass_commit_hooks_action = self.actions_menu.addAction(
            N_('Bypass Commit Hooks')
        )
        self.bypass_commit_hooks_action.setCheckable(True)
        self.bypass_commit_hooks_action.setChecked(False)

        # Sign commits
        self.sign_action = self.actions_menu.addAction(N_('Create Signed Commit'))
        self.sign_action.setCheckable(True)
        signcommits = cfg.get('cola.signcommits', default=False)
        self.sign_action.setChecked(signcommits)

        # Spell checker
        self.check_spelling_action = self.actions_menu.addAction(N_('Check Spelling'))
        self.check_spelling_action.setCheckable(True)
        spell_check = prefs.spellcheck(context)
        self.check_spelling_action.setChecked(spell_check)
        self.toggle_check_spelling(spell_check)

        # Line wrapping
        self.autowrap_action = self.actions_menu.addAction(N_('Auto-Wrap Lines'))
        self.autowrap_action.setCheckable(True)
        self.autowrap_action.setChecked(prefs.linebreak(context))

        # Commit message
        self.actions_menu.addSeparator()
        self.load_commitmsg_menu = self.actions_menu.addMenu(
            N_('Load Previous Commit Message')
        )
        self.load_commitmsg_menu.aboutToShow.connect(self.build_commitmsg_menu)

        self.fixup_commit_menu = self.actions_menu.addMenu(N_('Fixup Previous Commit'))
        self.fixup_commit_menu.aboutToShow.connect(self.build_fixup_menu)

        # Display commit author overrides.
        self._author_close = qtutils.create_action_button(
            icon=icons.close(),
            tooltip=N_('Cancel the commit author override.'),
        )
        tooltip = N_('A commit will be recorded with this author')
        self._author_image = qtutils.pixmap_label(
            icons.person(), defs.default_icon, tooltip=tooltip, parent=self
        )
        self._author_label = qtutils.plain_text_label(tooltip=tooltip, parent=self)
        tooltip = N_('A commit will be recorded with this date and time')

        # Display commit date overrides.
        self._date_close = qtutils.create_action_button(
            icon=icons.close(),
            tooltip=N_('Cancel the commit date override.'),
        )
        self._date_image = qtutils.pixmap_label(
            icons.clock(), defs.default_icon, tooltip=tooltip, parent=self
        )
        self._date_label = qtutils.plain_text_label(tooltip=tooltip, parent=self)

        self.bottomlayout = qtutils.hbox(
            defs.no_margin,
            defs.spacing,
            self._author_close,
            self._author_image,
            self._author_label,
            qtutils.STRETCH,
            self._date_close,
            self._date_image,
            self._date_label,
        )
        self.toplayout = qtutils.hbox(
            defs.no_margin,
            defs.spacing,
            self.actions_button,
            self.summary,
            self.commit_progress_bar,
            self.commit_button,
            self.cursor_position_label,
        )
        self.topwidget = QtWidgets.QWidget()
        self.topwidget.setLayout(self.toplayout)

        self.mainlayout = qtutils.vbox(
            defs.no_margin, defs.spacing, self.description, self.bottomlayout
        )
        self.setLayout(self.mainlayout)

        qtutils.connect_button(self.commit_button, self.commit)
        qtutils.connect_button(
            self._author_close, lambda: self.set_commit_author(False)
        )
        qtutils.connect_button(self._date_close, lambda: self.set_commit_date(False))
        qtutils.connect_action_bool(
            self.check_spelling_action, self.toggle_check_spelling
        )
        # Handle the one-off auto-wrapping
        qtutils.connect_action_bool(self.autowrap_action, self.set_linebreak)

        self.summary.accepted.connect(self.focus_description)
        self.summary.down_pressed.connect(self.summary_cursor_down)

        self.model.commit_message_changed.connect(
            self.set_commit_message, type=Qt.QueuedConnection
        )
        self.commit_finished.connect(self._commit_finished, type=Qt.QueuedConnection)

        self.summary.cursor_changed.connect(self.cursor_changed.emit)
        self.description.cursor_changed.connect(
            # description starts at line 2
            lambda row, col: self.cursor_changed.emit(row + 2, col)
        )
        self.summary.textChanged.connect(self.commit_summary_changed)
        self.description.textChanged.connect(self._commit_message_changed)
        self.description.leave.connect(self.focus_summary)
        self.cursor_changed.connect(self.show_cursor_position)
        # Set initial position.
        self.show_cursor_position(1, 0)

        self.commit_group.setEnabled(False)

        self.set_expandtab(prefs.expandtab(context))
        self.set_tabwidth(prefs.tabwidth(context))
        self.set_textwidth(prefs.textwidth(context))
        self.set_linebreak(prefs.linebreak(context))

        # Loading message
        commit_msg = ''
        commit_msg_path = commit_message_path(context)
        if commit_msg_path:
            commit_msg = core.read(commit_msg_path)
        model.set_commitmsg(commit_msg)

        # Allow tab to jump from the summary to the description
        self.setFont(qtutils.diff_font(context))
        self.setFocusProxy(self.summary)

        cfg.user_config_changed.connect(self.config_changed)

    def config_changed(self, key, value):
        if key != prefs.SPELL_CHECK:
            return
        if get(self.check_spelling_action) == value:
            return
        self.check_spelling_action.setChecked(value)
        self.toggle_check_spelling(value)

    def set_initial_size(self):
        self.setMaximumHeight(133)
        QtCore.QTimer.singleShot(1, self.restore_size)

    def export_state(self, state):
        """Save persistent UI state on shutdown"""
        # Set a limit on the number of recent author values.
        max_recent = prefs.maxrecent(self.context)
        state['authors'] = self._commit_authors[:max_recent]
        return state

    def apply_state(self, state):
        """Apply persistent UI state on startup"""
        self._commit_authors = state.get('authors', [])
        return True

    def restore_size(self):
        self.setMaximumHeight(2**13)

    def focus_summary(self):
        self.summary.setFocus()

    def focus_description(self):
        self.description.setFocus()

    def summary_cursor_down(self):
        """Handle the down key in the summary field

        If the cursor is at the end of the line then focus the description.
        Otherwise, move the cursor to the end of the line so that a
        subsequence "down" press moves to the end of the line.
        """
        self.focus_description()

    def commit_message(self, raw=True):
        """Return the commit message as a Unicode string"""
        summary = get(self.summary)
        if raw:
            description = get(self.description)
        else:
            description = self.formatted_description()
        if summary and description:
            return summary + '\n\n' + description
        if summary:
            return summary
        if description:
            return '\n\n' + description
        return ''

    def formatted_description(self):
        text = get(self.description)
        if not self._linebreak:
            return text
        return textwrap.word_wrap(text, self._tabwidth, self._textwidth)

    def commit_summary_changed(self):
        """Respond to changes to the `summary` field

        Newlines can enter the `summary` field when pasting, which is
        undesirable.  Break the pasted value apart into the separate
        (summary, description) values and move the description over to the
        "extended description" field.

        """
        value = self.summary.value()
        if '\n' in value:
            summary, description = value.split('\n', 1)
            description = description.lstrip('\n')
            cur_description = get(self.description)
            if cur_description:
                description = description + '\n' + cur_description
            # this callback is triggered by changing `summary`
            # so disable signals for `summary` only.
            self.summary.set_value(summary, block=True)
            self.description.set_value(description)
        self._commit_message_changed()
        self.summary.cursor_position.emit()

    def _commit_message_changed(self, _value=None):
        """Update the model when values change"""
        message = self.commit_message()
        self.model.set_commitmsg(message, notify=False)
        self.update_actions()

    def clear(self):
        if not Interaction.confirm(
            N_('Clear commit message?'),
            N_('The commit message will be cleared.'),
            N_('This cannot be undone.  Clear commit message?'),
            N_('Clear commit message'),
            default=True,
            icon=icons.discard(),
        ):
            return
        self.model.set_commitmsg('')

    def update_actions(self):
        commit_enabled = bool(get(self.summary))
        self.commit_group.setEnabled(commit_enabled)

    def set_commit_message(self, message):
        """Set the commit message to match the observed model"""
        # Parse the "summary" and "description" fields
        lines = message.splitlines()
        num_lines = len(lines)
        if num_lines == 0:
            # Message is empty
            summary = ''
            description = ''
        elif num_lines == 1:
            # Message has a summary only
            summary = lines[0]
            description = ''
        elif num_lines == 2:
            # Message has two lines; this is not a common case
            summary = lines[0]
            description = lines[1]
        else:
            # Summary and several description lines
            summary = lines[0]
            if lines[1]:
                # We usually skip this line but check just in case
                description_lines = lines[1:]
            else:
                description_lines = lines[2:]
            description = '\n'.join(description_lines)

        focus_summary = not summary
        focus_description = not description
        # Update summary
        self.summary.set_value(summary, block=True)
        # Update description
        self.description.set_value(description, block=True)
        # Focus the empty summary or description
        if focus_summary:
            self.summary.setFocus()
        elif focus_description:
            self.description.setFocus()
        else:
            self.summary.cursor_position.emit()
        self.update_actions()

    def set_expandtab(self, value):
        self.description.set_expandtab(value)

    def set_tabwidth(self, width):
        self._tabwidth = width
        self.description.set_tabwidth(width)

    def set_textwidth(self, width):
        self._textwidth = width
        self.description.set_textwidth(width)

    def set_linebreak(self, brk):
        self._linebreak = brk
        self.description.set_linebreak(brk)
        with qtutils.BlockSignals(self.autowrap_action):
            self.autowrap_action.setChecked(brk)

    def setFont(self, font):
        """Pass the setFont() calls down to the text widgets"""
        self.summary.setFont(font)
        self.description.setFont(font)

    def set_mode(self, mode):
        can_amend = not self.model.is_merging
        checked = mode == self.model.mode_amend
        with qtutils.BlockSignals(self.amend_action):
            self.amend_action.setEnabled(can_amend)
            self.amend_action.setChecked(checked)
        # Store/restore the commit date and author when amending.
        if checked:
            self._last_commit_datetime_backup = self._last_commit_datetime
            self._last_commit_datetime = _get_latest_commit_datetime(self.context)
            self._git_commit_author_backup = self._git_commit_author
            self._git_commit_author = None
        else:
            self._last_commit_datetime = self._last_commit_datetime_backup
            self._last_commit_datetime_backup = None
            if self._git_commit_author_backup:
                self._git_commit_author = self._git_commit_author_backup
                self._git_commit_author_backup = None
                with qtutils.BlockSignals(self.commit_author_action):
                    self.commit_author_action.setChecked(True)

        self.update_author_and_date()

    def update_author_and_date(self):
        """Hide or display the author field"""
        amending = self.model.mode == self.model.mode_amend
        # Display the author when amending or when overridden.
        if self._git_commit_author:
            author = self._git_commit_author
        elif amending:
            author = self.model.commit_author
        else:
            author = None
        author_visible = bool(author)
        if author:
            self._author_label.setText(author)
        author_checked = self.commit_author_action.isChecked()
        self._author_close.setVisible(author_visible and author_checked)
        self._author_image.setVisible(author_visible)
        self._author_label.setVisible(author_visible)

        # Display the commit date when amending or when overridden.
        if self._git_commit_date:
            date = self._git_commit_date
        elif amending and self._last_commit_datetime:
            date = display.git_commit_date(self._last_commit_datetime)
        else:
            date = ''
        date_visible = bool(date)
        if date:
            self._date_label.setText(date)
        self._date_close.setVisible(bool(self._git_commit_date))
        self._date_image.setVisible(date_visible)
        self._date_label.setVisible(date_visible)

        if author_visible or date_visible:
            self.actions_button.setIcon(icons.gear_solid())
        else:
            self.actions_button.setIcon(icons.configure())

    def commit(self):
        """Attempt to create a commit from the index and commit message."""
        context = self.context
        if not bool(get(self.summary)):
            # Describe a good commit message
            error_msg = N_(
                'Please supply a commit message.\n\n'
                'A good commit message has the following format:\n\n'
                '- First line: Describe in one sentence what you did.\n'
                '- Second line: Blank\n'
                '- Remaining lines: Describe why this change is good.\n'
            )
            Interaction.log(error_msg)
            Interaction.information(N_('Missing Commit Message'), error_msg)
            return

        msg = self.commit_message(raw=False)

        # We either need to have something staged, or be merging.
        # If there was a merge conflict resolved, there may not be anything
        # to stage, but we still need to commit to complete the merge.
        if not (self.model.staged or self.model.is_merging):
            error_msg = N_(
                'No changes to commit.\n\n'
                'You must stage at least 1 file before you can commit.'
            )
            if self.model.modified:
                informative_text = N_(
                    'Would you like to stage and commit all modified files?'
                )
                if not Interaction.confirm(
                    N_('Stage and commit?'),
                    error_msg,
                    informative_text,
                    N_('Stage and Commit'),
                    default=True,
                    icon=icons.save(),
                ):
                    return
            else:
                Interaction.information(N_('Nothing to commit'), error_msg)
                return
            cmds.do(cmds.StageModified, context)

        # Warn that amending published commits is generally bad
        amend = get(self.amend_action)
        check_published = prefs.check_published_commits(context)
        if (
            amend
            and check_published
            and self.model.is_commit_published()
            and not Interaction.confirm(
                N_('Rewrite Published Commit?'),
                N_(
                    'This commit has already been published.\n'
                    'This operation will rewrite published history.\n'
                    "You probably don't want to do this."
                ),
                N_('Amend the published commit?'),
                N_('Amend Commit'),
                default=False,
                icon=icons.save(),
            )
        ):
            return

        sign = get(self.sign_action)
        no_verify = get(self.bypass_commit_hooks_action)
        if self.commit_date_action.isChecked():
            date = self._git_commit_date
        else:
            date = None

        if self.commit_author_action.isChecked():
            author = self._git_commit_author
        else:
            author = None

        task = qtutils.SimpleTask(
            cmds.run(
                cmds.Commit,
                context,
                amend,
                msg,
                sign,
                no_verify=no_verify,
                author=author,
                date=date,
            )
        )
        self.context.runtask.start(
            task,
            finish=self.commit_finished.emit,
            progress=self.commit_progress_bar,
        )

    def _commit_finished(self, task):
        """Reset widget state on completion of the commit task"""
        title = N_('Commit failed')
        status, out, err = task.result
        Interaction.command(title, 'git commit', status, out, err)
        self.bypass_commit_hooks_action.setChecked(False)
        # Author and date settings are not cleared unless the commit operation succeeds.
        # These settings are consumed when a commit is produced with their values.
        if status == 0:
            self.set_commit_author(False, update=False)
            self.set_commit_date(False, update=True)
        self.setFocus()

    def build_fixup_menu(self):
        self.build_commits_menu(
            cmds.LoadFixupMessage,
            self.fixup_commit_menu,
            self.choose_fixup_commit,
            prefix='fixup! ',
        )

    def build_commitmsg_menu(self):
        self.build_commits_menu(
            cmds.LoadCommitMessageFromOID,
            self.load_commitmsg_menu,
            self.choose_commit_message,
        )

    def build_commits_menu(self, cmd, menu, chooser, prefix=''):
        context = self.context
        params = dag.DAG('HEAD', 6)
        commits = dag.RepoReader(context, params)

        menu_commits = []
        for idx, commit in enumerate(commits.get()):
            menu_commits.insert(0, commit)
            if idx > 5:
                continue

        menu.clear()
        for commit in menu_commits:
            menu.addAction(prefix + commit.summary, cmds.run(cmd, context, commit.oid))

        if len(commits) == 6:
            menu.addSeparator()
            menu.addAction(N_('More...'), chooser)

    def choose_commit(self, cmd):
        context = self.context
        revs, summaries = gitcmds.log_helper(context)
        oids = select_commits(
            context, N_('Select Commit'), revs, summaries, multiselect=False
        )
        if not oids:
            return
        oid = oids[0]
        cmds.do(cmd, context, oid)

    def choose_commit_message(self):
        self.choose_commit(cmds.LoadCommitMessageFromOID)

    def choose_fixup_commit(self):
        self.choose_commit(cmds.LoadFixupMessage)

    def toggle_check_spelling(self, enabled):
        spell_check = self.spellcheck
        cfg = self.context.cfg

        if prefs.spellcheck(self.context) != enabled:
            cfg.set_user(prefs.SPELL_CHECK, enabled)
        if enabled and not self.spellcheck_initialized:
            # Add our name to the dictionary
            self.spellcheck_initialized = True
            user_name = cfg.get('user.name')
            if user_name:
                for part in user_name.split():
                    spell_check.add_word(part)

            # Add our email address to the dictionary
            user_email = cfg.get('user.email')
            if user_email:
                for part in user_email.split('@'):
                    for elt in part.split('.'):
                        spell_check.add_word(elt)

            # git jargon
            spell_check.add_word('Acked')
            spell_check.add_word('Signed')
            spell_check.add_word('Closes')
            spell_check.add_word('Fixes')

        self.summary.highlighter.enable(enabled)
        self.description.highlighter.enable(enabled)

    def show_cursor_position(self, rows, cols):
        """Display the cursor position with warnings and error colors for long lines"""
        display_content = '%02d:%02d' % (rows, cols)
        try:
            max_width = max(
                len(line) for line in self.commit_message(raw=False).splitlines()
            )
        except ValueError:
            max_width = 0
        if max_width > 78:
            color = 'red'
        elif max_width > 72:
            color = '#ff8833'
        elif max_width > 64:
            color = 'yellow'
        else:
            color = ''
        if color:
            radius = defs.small_icon // 2
            stylesheet = f"""
                color: black;
                background-color: {color};
                border-radius: {radius}px;
            """
        else:
            stylesheet = ''
        self.cursor_position_label.setStyleSheet(stylesheet)
        self.cursor_position_label.setText(display_content)

    def set_commit_date(self, enabled, update=True):
        """Choose the date and time that is used when authoring commits"""
        if not enabled:
            self._git_commit_date = None
            if self.commit_date_action.isChecked():
                with qtutils.BlockSignals(self.commit_date_action):
                    self.commit_date_action.setChecked(False)
            if update:
                self.update_author_and_date()
            return
        widget = CommitDateDialog(
            self, self.context, commit_datetime=self._last_commit_datetime
        )
        if widget.exec_() == QtWidgets.QDialog.Accepted:
            commit_date = widget.commit_date()
            Interaction.log(N_('Setting commit date to %s') % commit_date)
            self._git_commit_date = commit_date
            self._last_commit_datetime = CommitDateDialog.tick_time(widget.datetime())
        else:
            self.commit_date_action.setChecked(False)
        if update:
            self.update_author_and_date()

    def set_commit_author(self, enabled, update=True):
        """Choose a commit author to override the author value when authoring commits"""
        if not enabled:
            if self._git_commit_author:
                self._last_git_commit_author = self._git_commit_author
            self._git_commit_author = None
            if self.commit_author_action.isChecked():
                with qtutils.BlockSignals(self.commit_author_action):
                    self.commit_author_action.setChecked(False)
            if update:
                self.update_author_and_date()
            return
        widget = CommitAuthorDialog(
            self,
            self.context,
            commit_author=self._git_commit_author or self._last_git_commit_author,
            commit_authors=self._commit_authors,
        )
        if widget.exec_() == QtWidgets.QDialog.Accepted:
            commit_author = widget.commit_author()
            default_author = _get_default_author(self.context)
            Interaction.log(N_('Setting commit author to %s') % commit_author)
            self._git_commit_author = commit_author
            if (
                commit_author
                and commit_author != default_author
                and commit_author not in self._commit_authors
            ):
                self._commit_authors.insert(0, commit_author)
        else:
            self._git_commit_author = None
            self.commit_author_action.setChecked(False)
        if update:
            self.update_author_and_date()

    # Qt overrides
    def showEvent(self, event):
        """Resize the position label once the sizes are known"""
        super().showEvent(event)
        if not self._widgets_initialized:
            self._widgets_initialized = True
            height = self.summary.height()
            self.commit_button.setMinimumHeight(height)
            self.cursor_position_label.setMaximumHeight(defs.small_icon + defs.spacing)
            self.commit_progress_bar.setMaximumHeight(height - 2)
            self.commit_progress_bar.setMaximumWidth(self.commit_button.width())


def _get_latest_commit_datetime(context):
    """Query the commit time from Git or fallback to the current time when unavailable"""
    commit_datetime = datetime.datetime.now()
    status, out, _ = context.git.log('-1', '--format=%aI', 'HEAD')
    if status != 0 or not out:
        return commit_datetime
    try:
        commit_datetime = datetime.datetime.fromisoformat(out)
    except ValueError:
        pass
    return commit_datetime


class CommitDateDialog(QtWidgets.QDialog):
    """Choose the date and time used when authoring commits"""

    slider_range = 500

    def __init__(self, parent, context, commit_datetime=None):
        QtWidgets.QDialog.__init__(self, parent)
        slider_range = self.slider_range
        self.context = context
        self._calendar_widget = QtWidgets.QCalendarWidget()
        self._time_widget = QtWidgets.QTimeEdit()
        self._time_widget.setDisplayFormat('hh:mm:ss AP')

        # Horizontal slider moves the date and time backwards and forwards.
        self._slider = QtWidgets.QSlider(Qt.Horizontal)
        self._slider.setRange(0, slider_range)  # Mapped from 00:00:00 to 23:59:59

        self._tick_backward = qtutils.create_toolbutton_with_callback(
            partial(self._adjust_slider, -1),
            '-',
            None,
            N_('Decrement'),
            repeat=True,
        )
        self._tick_forward = qtutils.create_toolbutton_with_callback(
            partial(self._adjust_slider, 1),
            '+',
            None,
            N_('Increment'),
            repeat=True,
        )
        self._reset_to_commit_time = qtutils.create_toolbutton_with_callback(
            self._reset_time_to_latest_commit,
            None,
            icons.sync(),
            N_('Reset time to latest commit'),
        )
        self._reset_to_current_time = qtutils.create_toolbutton_with_callback(
            lambda: self._reset_time_to_datetime(datetime.datetime.now()),
            None,
            icons.style_dialog_reset(),
            N_('Reset time to current time'),
        )

        self._cancel_button = QtWidgets.QPushButton(N_('Cancel'))
        self._cancel_button.setIcon(icons.close())

        self._set_commit_time_button = QtWidgets.QPushButton(N_('Set Date and Time'))
        self._set_commit_time_button.setDefault(True)
        self._set_commit_time_button.setIcon(icons.ok())

        button_layout = qtutils.hbox(
            defs.no_margin,
            defs.button_spacing,
            self._cancel_button,
            qtutils.STRETCH,
            self._set_commit_time_button,
        )
        slider_layout = qtutils.hbox(
            defs.no_margin,
            defs.spacing,
            self._tick_backward,
            self._slider,
            self._tick_forward,
            self._reset_to_commit_time,
            self._reset_to_current_time,
            self._time_widget,
        )
        layout = qtutils.vbox(
            defs.small_margin,
            defs.spacing,
            self._calendar_widget,
            slider_layout,
            defs.button_spacing,
            button_layout,
        )
        self.setLayout(layout)
        self.setWindowTitle(N_('Set Commit Date'))
        self.setWindowModality(Qt.ApplicationModal)

        if commit_datetime is None:
            commit_datetime = self.tick_time(_get_latest_commit_datetime(context))
        self._time_widget.setTime(commit_datetime.time())
        self._calendar_widget.setSelectedDate(commit_datetime.date())
        self._update_slider_from_datetime(commit_datetime)

        self._right_action = qtutils.add_action(
            self, N_('Increment'), partial(self._adjust_slider, 1), hotkeys.CTRL_RIGHT
        )
        self._left_action = qtutils.add_action(
            self, N_('Decrement'), partial(self._adjust_slider, -1), hotkeys.CTRL_LEFT
        )

        self._time_widget.timeChanged.connect(self._update_slider_from_time_signal)
        self._slider.valueChanged.connect(self._update_time_from_slider)
        self._calendar_widget.activated.connect(lambda _: self.accept())

        self._cancel_button.clicked.connect(self.reject)
        self._set_commit_time_button.clicked.connect(self.accept)

    @classmethod
    def tick_time(cls, commit_datetime):
        """Tick time forward"""
        seconds_per_day = 86400
        seconds_range = seconds_per_day - 1
        one_tick = seconds_range // cls.slider_range  # 172 seconds (2m52s)
        return commit_datetime + datetime.timedelta(seconds=one_tick)

    def datetime(self):
        """Return the calculated datetime value"""
        # Combine the calendar widget's date with the time widget's time.
        time_value = self._time_widget.time().toPyTime()
        date_value = self._calendar_widget.selectedDate().toPyDate()
        date_time = datetime.datetime(
            date_value.year,
            date_value.month,
            date_value.day,
            time_value.hour,
            time_value.minute,
            time_value.second,
        )
        return date_time.astimezone()

    def commit_date(self):
        """Return the selected datetime as a string for use by Git"""
        return display.git_commit_date(self.datetime())

    def _update_time_from_slider(self, value):
        """Map the slider value to an offset corresponding to the current time.

        The passed-in value will be between 0 and range.
        """
        seconds_per_day = 86400
        seconds_range = seconds_per_day - 1
        ratio = value / self.slider_range
        delta = datetime.timedelta(seconds=int(ratio * seconds_range))
        midnight = datetime.datetime(1999, 12, 31)
        new_time = (midnight + delta).time()
        with qtutils.BlockSignals(self._time_widget):
            self._time_widget.setTime(new_time)

    def _adjust_slider(self, amount):
        """Adjust the slider forward or backwards"""
        new_value = self._slider.value() + amount
        self._slider.setValue(new_value)

    def _update_slider_from_time_signal(self, new_time):
        """Update the time slider to match the new time"""
        self._update_slider_from_time(new_time.toPyTime())

    def _update_slider_from_datetime(self, commit_datetime):
        """Update the time slider to match the specified datetime"""
        commit_time = commit_datetime.time()
        self._update_slider_from_time(commit_time)

    def _update_slider_from_time(self, commit_time):
        """Update the slider to match the specified time."""
        seconds_since_midnight = (
            60 * 60 * commit_time.hour + 60 * commit_time.minute + commit_time.second
        )
        seconds_per_day = 86400
        seconds_range = seconds_per_day - 1
        ratio = seconds_since_midnight / seconds_range
        value = int(self.slider_range * ratio)
        with qtutils.BlockSignals(self._slider):
            self._slider.setValue(value)

    def _reset_time_to_latest_commit(self):
        """Reset the commit time to match the most recent commit"""
        commit_datetime = _get_latest_commit_datetime(self.context)
        self._reset_time_to_datetime(commit_datetime)

    def _reset_time_to_datetime(self, commit_datetime):
        """Reset the commit time to match the specified datetime"""
        with qtutils.BlockSignals(self._time_widget):
            self._time_widget.setTime(commit_datetime.time())
        with qtutils.BlockSignals(self._calendar_widget):
            self._calendar_widget.setSelectedDate(commit_datetime.date())
        self._update_slider_from_datetime(commit_datetime)


def _get_default_author(context):
    """Get the default author value"""
    name, email = context.cfg.get_author()
    return f'{name} <{email}>'


def _get_latest_commit_author(context):
    """Query the commit author from Git"""
    status, out, _ = context.git.log('-1', '--format=%aN <%aE>', 'HEAD')
    if status != 0 or not out:
        return None
    return out


class CommitAuthorDialog(QtWidgets.QDialog):
    """Override the commit author when authoring commits"""

    def __init__(self, parent, context, commit_author=None, commit_authors=None):
        QtWidgets.QDialog.__init__(self, parent)
        main_tooltip = """Override the commit author.

Specify an explicit author using the standard "A U Thor <author@example.com>" format.

Otherwise <author> is assumed to be a pattern and is used to search for an
existing commit by that author (i.e. rev-list --all -i --author=<author>);"""
        self.context = context
        self._current_author = commit_author or _get_default_author(context)

        authors = self._get_authors(commit_author, commit_authors)
        tooltip = N_('Override the commit author when authoring commits')
        self._author_combobox = qtutils.combo(authors, editable=True, tooltip=tooltip)
        self._author_combobox.setToolTip(main_tooltip)
        if authors:
            self._author_combobox.set_value(authors[0])

        self._reset_to_commit_author_button = qtutils.create_toolbutton_with_callback(
            self._reset_to_commit_author,
            None,
            icons.sync(),
            N_('Set author to match the latest commit'),
        )
        self._reset_to_current_author_button = qtutils.create_toolbutton_with_callback(
            self._reset_to_current_author,
            None,
            icons.undo(),
            N_('Reset author to the current author'),
        )
        self._reset_to_default_author_button = qtutils.create_toolbutton_with_callback(
            self._reset_to_default_author,
            None,
            icons.style_dialog_reset(),
            N_('Reset to default author'),
        )

        self._cancel_button = QtWidgets.QPushButton(N_('Cancel'))
        self._cancel_button.setIcon(icons.close())

        self._apply_button = QtWidgets.QPushButton(N_('Set Author'))
        self._apply_button.setToolTip(main_tooltip)
        self._apply_button.setDefault(True)
        self._apply_button.setIcon(icons.ok())

        button_layout = qtutils.hbox(
            defs.no_margin,
            defs.button_spacing,
            self._cancel_button,
            qtutils.STRETCH,
            self._apply_button,
        )
        input_layout = qtutils.hbox(
            defs.no_margin,
            defs.spacing,
            self._author_combobox,
            self._reset_to_commit_author_button,
            self._reset_to_default_author_button,
            self._reset_to_current_author_button,
        )
        layout = qtutils.vbox(
            defs.small_margin,
            defs.spacing,
            input_layout,
            qtutils.STRETCH,
            defs.button_spacing,
            button_layout,
        )
        self.setLayout(layout)
        self.setWindowTitle(N_('Set Commit Author'))
        self.setWindowModality(Qt.ApplicationModal)

        self._apply_button.clicked.connect(self.accept)
        self._cancel_button.clicked.connect(self.reject)
        self._author_combobox.currentTextChanged.connect(lambda _: self._validate())
        self._validate()

    def commit_author(self):
        """Return the selected author value"""
        return self._author_combobox.current_value().strip()

    def _validate(self):
        """Validate the author value and disable the apply button when invalid"""
        author = self.commit_author()
        self._apply_button.setEnabled(bool(author))

    def _get_authors(self, commit_author, commit_authors):
        """Build a list of authors for the combo box"""
        seen = set()
        all_authors = [commit_author]
        if commit_authors:
            all_authors.extend(commit_authors)
        all_authors.append(_get_default_author(self.context))
        # Create a final unique list of authors.
        authors = []
        for author in all_authors:
            if author and author not in seen:
                seen.add(author)
                authors.append(author)
        return authors

    def _reset_to_commit_author(self):
        """Reset the author value to the author of the most recent commit"""
        commit_author = _get_latest_commit_author(self.context)
        if commit_author:
            self._author_combobox.set_current_value(commit_author)
        else:
            self._reset_to_default_author()  # Fallback to the current author.

    def _reset_to_current_author(self):
        """Reset the author value to the current author"""
        current_author = self._current_author
        if current_author:
            self._author_combobox.set_current_value(current_author)

    def _reset_to_default_author(self):
        """Reset the author value to the default author"""
        self._author_combobox.set_current_value(_get_default_author(self.context))


class CommitSummaryLineEdit(SpellCheckLineEdit):
    """Text input field for the commit summary"""

    down_pressed = Signal()
    accepted = Signal()

    def __init__(self, context, check=None, parent=None):
        hint = N_('Commit summary')
        SpellCheckLineEdit.__init__(self, context, hint, check=check, parent=parent)
        self._comment_char = None
        self._refresh_config()

        self.textChanged.connect(self._update_summary_text, Qt.QueuedConnection)
        context.cfg.updated.connect(self._refresh_config, type=Qt.QueuedConnection)

    def _refresh_config(self):
        """Update comment char in response to config changes"""
        self._comment_char = prefs.comment_char(self.context)

    def _update_summary_text(self):
        """Prevent commit messages from starting with comment characters"""
        value = self.get()
        if self._comment_char and value.lstrip().startswith(self._comment_char):
            cursor = self.textCursor()
            position = cursor.position()

            value = value.lstrip()
            if self._comment_char:
                value = value.lstrip(self._comment_char).lstrip()

            self.set_value(value, block=True)
            value = self.get()

            if position > 1:
                position = utils.clamp_zero(position - 1, len(value))
                cursor.setPosition(position)
                self.setTextCursor(cursor)

    def keyPressEvent(self, event):
        """Allow "Enter" to focus into the extended description field"""
        event_key = event.key()
        if event_key in (
            Qt.Key_Enter,
            Qt.Key_Return,
        ):
            self.accepted.emit()
            return
        SpellCheckLineEdit.keyPressEvent(self, event)


class CommitMessageTextEdit(SpellCheckTextEdit):
    leave = Signal()

    def __init__(self, context, check=None, parent=None):
        hint = N_('Extended description...')
        SpellCheckTextEdit.__init__(self, context, hint, check=check, parent=parent)

        self.action_emit_leave = qtutils.add_action(
            self, 'Shift Tab', self.leave.emit, hotkeys.LEAVE
        )

    def keyPressEvent(self, event):
        """Update the cursor display and move the cursor to the end and beginning"""
        if event.key() == Qt.Key_Up:
            cursor = self.textCursor()
            position = cursor.position()
            if position == 0:
                # The cursor is at the beginning of the line.
                # If we have a selection and shift is not held then reset the
                # cursor to clear the selection. Otherwise, emit a signal so
                # that the parent can change focus.
                shifted = is_shift_pressed(event)
                if cursor.hasSelection() and not shifted:
                    self.set_cursor_position(0)
                else:
                    self.leave.emit()
                event.accept()
                return
            text_before = self.toPlainText()[:position]
            lines_before = text_before.count('\n')
            if lines_before == 0:
                # If we're on the first line, but not at the
                # beginning, then move the cursor to the beginning
                # of the line.
                mode = event_anchor_mode(event)
                cursor.movePosition(QtGui.QTextCursor.Up, mode)
                new_position = cursor.position()
                if position == new_position:
                    cursor.setPosition(0, mode)
                self.setTextCursor(cursor)
                event.accept()
                return
        elif event.key() == Qt.Key_Down:
            cursor = self.textCursor()
            position = cursor.position()
            all_text = self.toPlainText()
            text_after = all_text[position:]
            lines_after = text_after.count('\n')
            if lines_after == 0:
                mode = event_anchor_mode(event)
                cursor.movePosition(QtGui.QTextCursor.Down, mode)
                new_position = cursor.position()
                if position == new_position:
                    cursor.setPosition(len(all_text), mode)
                self.setTextCursor(cursor)
                event.accept()
                return
        SpellCheckTextEdit.keyPressEvent(self, event)

    def setFont(self, font):
        SpellCheckTextEdit.setFont(self, font)
        width, height = qtutils.text_size(font, 'MMMM')
        self.setMinimumSize(QtCore.QSize(width, height * 2))