summaryrefslogtreecommitdiff
path: root/rc/tools/git.kak
blob: 05e6ff1440bdb1f27485b3f6ab1fdafd35a8ba60 (plain)
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
declare-option -docstring "name of the client in which documentation is to be displayed" \
    str docsclient

declare-option -docstring "git diff added character" \
    str git_diff_add_char "▊"

declare-option -docstring "git diff modified character" \
    str git_diff_mod_char "▊"

declare-option -docstring "git diff deleted character" \
    str git_diff_del_char "_"

declare-option -docstring "git diff top deleted character" \
    str git_diff_top_char "‾"

hook -group git-log-highlight global WinSetOption filetype=git-log %{
    add-highlighter window/git-log group
    add-highlighter window/git-log/ regex '^([*|\\ /_.-])*' 0:keyword
    add-highlighter window/git-log/ regex '^( ?[*|\\ /_.-])*\h{,3}(commit )?(\b[0-9a-f]{4,40}\b)' 2:keyword 3:comment
    add-highlighter window/git-log/ regex '^( ?[*|\\ /_.-])*\h{,3}([a-zA-Z_-]+:) (.*?)$' 2:variable 3:value
    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-log }
}

hook global WinSetOption filetype=diff %{
    try %{
        execute-keys -draft %{/^diff --git\b<ret>}
        evaluate-commands %sh{
            if [ -n "$(git ls-files -- "${kak_buffile}")" ]; then
                echo fail
            fi
        }
        set-option buffer filetype git-diff
    }
}

hook -group git-diff-highlight global WinSetOption filetype=(git-diff|git-log) %{
    require-module diff
    add-highlighter %exp{window/%val{hook_param_capture_1}-ref-diff} ref diff
    hook -once -always window WinSetOption filetype=.* %exp{
        remove-highlighter window/%val{hook_param_capture_1}-ref-diff
    }
}

hook global WinSetOption filetype=(?:git-diff|git-log) %{
    map buffer normal <ret> %exp{:git-diff-goto-source # %val{hook_param}<ret>} -docstring 'Jump to source from git diff'
    hook -once -always window WinSetOption filetype=.* %exp{
        unmap buffer normal <ret> %%{:git-diff-goto-source # %val{hook_param}<ret>}
    }
}

hook -group git-status-highlight global WinSetOption filetype=git-status %{
    add-highlighter window/git-status group
    add-highlighter window/git-status/ regex '^## ' 0:comment
    add-highlighter window/git-status/ regex '^## (\S*[^\s\.@])' 1:green
    add-highlighter window/git-status/ regex '^## (\S*[^\s\.@])(\.\.+)(\S*[^\s\.@])' 1:green 2:comment 3:red
    add-highlighter window/git-status/ regex '^(##) (No commits yet on) (\S*[^\s\.@])' 1:comment 2:Default 3:green
    add-highlighter window/git-status/ regex '^## \S+ \[[^\n]*ahead (\d+)[^\n]*\]' 1:green
    add-highlighter window/git-status/ regex '^## \S+ \[[^\n]*behind (\d+)[^\n]*\]' 1:red
    add-highlighter window/git-status/ regex '^(?:([Aa])|([Cc])|([Dd!?])|([MUmu])|([Rr])|([Tt]))[ !\?ACDMRTUacdmrtu]\h' 1:green 2:blue 3:red 4:yellow 5:cyan 6:cyan
    add-highlighter window/git-status/ regex '^[ !\?ACDMRTUacdmrtu](?:([Aa])|([Cc])|([Dd!?])|([MUmu])|([Rr])|([Tt]))\h' 1:green 2:blue 3:red 4:yellow 5:cyan 6:cyan
    add-highlighter window/git-status/ regex '^R[ !\?ACDMRTUacdmrtu] [^\n]+( -> )' 1:cyan
    add-highlighter window/git-status/ regex '^\h+(?:((?:both )?modified:)|(added:|new file:)|(deleted(?: by \w+)?:)|(renamed:)|(copied:))(?:.*?)$' 1:yellow 2:green 3:red 4:cyan 5:blue 6:magenta

    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-status }
}

hook -group git-show-branch-highlight global WinSetOption filetype=git-show-branch %{
    add-highlighter window/git-show-branch group
    add-highlighter window/git-show-branch/ regex '(\*)|(\+)|(!)' 1:red 2:green 3:green
    add-highlighter window/git-show-branch/ regex '(!\D+\{0\}\])|(!\D+\{1\}\])|(!\D+\{2\}\])|(!\D+\{3\}\])' 1:red 2:green 3:yellow 4:blue
    add-highlighter window/git-show-branch/ regex '(\B\+\D+\{0\}\])|(\B\+\D+\{1\}\])|(\B\+\D+\{2\}\])|(\B\+\D+\{3\}\])|(\B\+\D+\{1\}\^\])' 1:red 2:green 3:yellow 4:blue 5:magenta

    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-show-branch}
}

declare-option -hidden line-specs git_blame_flags
declare-option -hidden line-specs git_blame_index
declare-option -hidden str git_blame
declare-option -hidden str git_blob
declare-option -hidden line-specs git_diff_flags
declare-option -hidden int-list git_hunk_list

define-command -params 1.. \
    -docstring %{
        git [<arguments>]: git wrapping helper
        All the optional arguments are forwarded to the git utility
        Available commands:
            add
            apply      - run "patch git apply [<arguments>]"; if buffile is
                         tracked, use the changes to selected lines instead
            blame      - toggle blame annotations
            blame-jump - show the commit that added the line at cursor
            checkout
            commit
            diff
            edit
            grep
            hide-diff
            init
            log
            next-hunk
            prev-hunk
            reset
            rm
            show
            show-branch
            show-diff
            status
            update-diff
    } -shell-script-candidates %{
    if [ $kak_token_to_complete -eq 0 ]; then
        printf %s\\n \
            add \
            apply \
            blame \
            blame-jump \
            checkout \
            commit \
            diff \
            edit \
            grep \
            hide-diff \
            init \
            log \
            next-hunk \
            prev-hunk \
            reset \
            rm \
            show \
            show-branch \
            show-diff \
            status \
            update-diff \
        ;
    else
        case "$1" in
            commit) printf -- "--amend\n--no-edit\n--all\n--reset-author\n--fixup\n--squash\n"; git ls-files -m ;;
            add) git ls-files -dmo --exclude-standard ;;
            apply) printf -- "--reverse\n--cached\n--index\n--3way\n" ;;
            grep|edit) git ls-files -c --recurse-submodules ;;
        esac
    fi
  } \
  git %{ evaluate-commands %sh{
    cd_bufdir() {
        dirname_buffer="${kak_buffile%/*}"
        cd "${dirname_buffer}" 2>/dev/null || {
            printf 'fail Unable to change the current working directory to: %s\n' "${dirname_buffer}"
            exit 1
        }
    }
    kakquote() {
        printf "%s" "$1" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"
    }

    show_git_cmd_output() {
        local filetype

        case "$1" in
           diff) filetype=git-diff ;;
           show) filetype=git-log ;;
           show-branch) filetype=git-show-branch ;;
           log)  filetype=git-log ;;
           status)  filetype=git-status ;;
           *) return 1 ;;
        esac
        output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-git.XXXXXXXX)/fifo
        mkfifo ${output}
        ( trap - INT QUIT; git "$@" > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null

        printf %s "evaluate-commands -try-client '$kak_opt_docsclient' '
                  edit! -fifo ${output} *git*
                  set-option buffer filetype ${filetype}
                  $(hide_blame)
                  set-option buffer git_blob %{}
                  hook -always -once buffer BufCloseFifo .* ''
                      nop %sh{ rm -r $(dirname ${output}) }
                      $(printf %s "${on_close_fifo}" | sed "s/'/''''/g")
                  ''
        '"
    }

    hide_blame() {
        printf %s "
            set-option buffer git_blame_flags $kak_timestamp
            set-option buffer git_blame_index $kak_timestamp
            set-option buffer git_blame %{}
            try %{ remove-highlighter window/git-blame }
            unmap window normal <ret> %{:git blame-jump<ret>}
        "
    }

    diff_buffer_against_rev() {
        rev=$1 # empty means index
        shift
        buffile_relative=${kak_buffile#"$(git rev-parse --show-toplevel)/"}
        echo >${kak_command_fifo} "evaluate-commands -save-regs | %{
            set-register | %{ cat >${kak_response_fifo} }
            execute-keys -draft %{%<a-|><ret>}
        }"
        git show "$rev:${buffile_relative}" |
            diff - ${kak_response_fifo} "$@" |
            awk -v buffile_relative="$buffile_relative" '
                NR == 1 { print "--- a/" buffile_relative }
                NR == 2 { print "+++ b/" buffile_relative }
                NR > 2
            '
    }

    blame_toggle() {
        echo >${kak_command_fifo} "try %{
            add-highlighter window/git-blame flag-lines Information git_blame_flags
            echo -to-file ${kak_response_fifo}
        } catch %{
            echo -to-file ${kak_response_fifo} 'hide_blame; exit'
        }"
        eval $(cat ${kak_response_fifo})
        if [ -z "${kak_opt_git_blob}" ] && {
            [ "${kak_opt_filetype}" = git-diff ] || [ "${kak_opt_filetype}" = git-log ]
        } then {
            echo 'try %{ remove-highlighter window/git-blame }'
            printf >${kak_command_fifo} %s '
                evaluate-commands -client '${kak_client}' -draft %{
                    try %{
                        execute-keys <a-l><semicolon><a-?>^commit<ret><a-semicolon>
                    } catch %{
                        # Missing commit line, assume it is an uncommitted change.
                        execute-keys <a-l><semicolon>Gg<a-semicolon>
                    }
                    require-module diff
                    try %{
                        diff-parse END %{
                            my $filename = $other_file;
                            my $line = $other_file_line;
                            if (not defined $commit) {
                                $commit = "HEAD";
                                if ($diff_line_text =~ m{^\+}) {
                                    print "echo -to-file '${kak_response_fifo}' -quoting shell "
                                        . "%{git blame: blame from HEAD does not work on added lines}";
                                    exit;
                                }
                            } elsif ($diff_line_text =~ m{^[-]}) {
                                $commit = "$commit~";
                            } else {
                                $filename = $file;
                                $line = $file_line;
                            }
                            $line = $line or 1;
                            printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d",
                                $commit, quote($filename), $line, ('${kak_cursor_column}' - 1);
                        }
                    } catch %{
                        echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error}
                    }
                }
            '
            n=$#
            eval set -- "$(cat ${kak_response_fifo})" "$@"
            if [ $# -eq $((n+1)) ]; then
                echo fail -- "$(kakquote "$1")"
                exit
            fi
            commit=$1
            file=${2#"$PWD/"}
            cursor_line=$3
            cursor_column=$4
            shift 4
            # Log commit and file name because they are only echoed briefly
            # and not shown elsewhere (we don't have a :messages buffer).
            message="Blaming $file as of $(git rev-parse --short $commit)"
            echo "echo -debug -- $(kakquote "$message")"
            on_close_fifo="
                execute-keys -client ${kak_client} ${cursor_line}g<a-h>${cursor_column}lh
                evaluate-commands -client ${kak_client} %{
                    set-option buffer git_blob $(kakquote "$commit:$file")
                    git blame $(for arg; do kakquote "$arg"; printf " "; done)
                    hook -once window NormalIdle .* %{
                        execute-keys vv
                        echo -markup -- $(kakquote "{Information}{\\}$message. Press <ret> to jump to blamed commit")
                    }
                }
            " show_git_cmd_output show "$commit:$file"
            exit
        } fi
        if [ -n "${kak_opt_git_blob}" ]; then {
            set -- "$@" "${kak_opt_git_blob%%:*}" -- "${kak_opt_git_blob#*:}"
            blame_stdin=/dev/null
        } else {
            set -- "$@" --contents - -- "${kak_buffile}" # use stdin to work around git bug
            blame_stdin=$(mktemp "${TMPDIR:-/tmp}"/kak-git.XXXXXX)
            echo >${kak_command_fifo} "evaluate-commands -save-regs | %{
                set-register | %{
                    cat >${blame_stdin}
                    : >${kak_response_fifo}
                }
                execute-keys -client ${kak_client} -draft %{%<a-|><ret>}
            }"
            : <${kak_response_fifo}
        } fi
        echo 'map window normal <ret> %{:git blame-jump<ret>}'
        echo 'echo -markup {Information}Press <ret> to jump to blamed commit'
        (
            trap - INT QUIT
            if [ -z "${kak_opt_git_blob}" ]; then
                cd_bufdir
            fi
            printf %s "evaluate-commands -client '$kak_client' %{
                      set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp'
                      set-option buffer=$kak_bufname git_blame_index '$kak_timestamp'
                      set-option buffer=$kak_bufname git_blame ''
                  }" | kak -p ${kak_session}
            if ! stderr=$({ git blame --incremental "$@" <${blame_stdin} | perl -wne '
                  BEGIN {
                  use POSIX qw(strftime);
                  sub quote {
                      my $SQ = "'\''";
                      my $token = shift;
                      $token =~ s/$SQ/$SQ$SQ/g;
                      return "$SQ$token$SQ";
                  }
                  sub send_flags {
                      my $is_last_call = shift;
                      if (not defined $line) {
                          if ($is_last_call) { exit 1; }
                          return;
                      }
                      my $text = substr($sha,0,7) . " " . $dates{$sha} . " " . $authors{$sha};
                      $text =~ s/~/~~/g;
                      for ( my $i = 0; $i < $count; $i++ ) {
                          $flags .= " %~" . ($line+$i) . "|$text~";
                      }
                      $now = time();
                      # Send roughly one update per second, to avoid creating too many kak processes.
                      if (!$is_last_call && defined $last_sent && $now - $last_sent < 1) {
                          return
                      }
                      open CMD, "|-", "kak -p $ENV{kak_session}";
                      print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame_flags $flags;";
                      print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame_index $index;";
                      print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame " . quote $raw_blame;
                      close(CMD);
                      $flags = "";
                      $index = "";
                      $raw_blame = "";
                      $last_sent = $now;
                  }
                  }
                  $raw_blame .= $_;
                  chomp;
                  if (m/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
                      send_flags(0);
                      $sha = $1;
                      $line = $3;
                      $count = $4;
                      for ( my $i = 0; $i < $count; $i++ ) {
                          $index .= " " . ($line+$i) . "|$.,$i";
                      }
                  }
                  if (m/^author /) {
                      $authors{$sha} = substr($_,7);
                      $authors{$sha} = "Not Committed Yet" if $authors{$sha} eq "External file (--contents)";
                  }
                  if (m/^author-time ([0-9]*)/) { $author_time = $1; }
                  if (m/^author-tz ([+-])(\d\d)/) {
                      my $sign = $1 eq "+" ? "-" : "+";
                      local $ENV{"TZ"} = "UTC${sign}$2";
                      $dates{$sha} = strftime("%F %T", localtime $author_time);
                  }
                  END { send_flags(1); }'
            } 2>&1); then
                escape2() { printf %s "$*" | sed "s/'/''''/g"; }
                echo "evaluate-commands -client ${kak_client} '
                    evaluate-commands -draft %{
                        buffer %{${kak_buffile}}
                        git hide-blame
                    }
                    echo -debug failed to run git blame
                    echo -debug git stderr: <<<
                    echo -debug ''$(escape2 "$stderr")>>>''
                    hook -once buffer NormalIdle .* %{
                        echo -markup %{{Error}failed to run git blame, see *debug* buffer}
                    }
                '" | kak -p ${kak_session}
            fi
            if [ ${blame_stdin} != /dev/null ]; then
                rm ${blame_stdin}
            fi
        ) > /dev/null 2>&1 < /dev/null &
    }

    run_git_cmd() {
        if git "${@}" > /dev/null 2>&1; then
          printf %s "echo -markup '{Information}git $1 succeeded'"
        else
          printf 'fail git %s failed\n' "$1"
        fi
    }

    update_diff() {
        (
            cd_bufdir
            diff_buffer_against_rev "" -U0 | perl -e '
            use utf8;
            $flags = $ENV{"kak_timestamp"};
            $add_char = $ENV{"kak_opt_git_diff_add_char"};
            $del_char = $ENV{"kak_opt_git_diff_del_char"};
            $top_char = $ENV{"kak_opt_git_diff_top_char"};
            $mod_char = $ENV{"kak_opt_git_diff_mod_char"};
            foreach $line (<STDIN>) {
                if ($line =~ /@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))?/) {
                    $from_line = $1;
                    $from_count = ($2 eq "" ? 1 : $2);
                    $to_line = $3;
                    $to_count = ($4 eq "" ? 1 : $4);

                    if ($from_count == 0 and $to_count > 0) {
                        for $i (0..$to_count - 1) {
                            $line = $to_line + $i;
                            $flags .= " $line|\{green\}$add_char";
                        }
                    }
                    elsif ($from_count > 0 and $to_count == 0) {
                        if ($to_line == 0) {
                            $flags .= " 1|\{red\}$top_char";
                        } else {
                            $flags .= " $to_line|\{red\}$del_char";
                        }
                    }
                    elsif ($from_count > 0 and $from_count == $to_count) {
                        for $i (0..$to_count - 1) {
                            $line = $to_line + $i;
                            $flags .= " $line|\{blue\}$mod_char";
                        }
                    }
                    elsif ($from_count > 0 and $from_count < $to_count) {
                        for $i (0..$from_count - 1) {
                            $line = $to_line + $i;
                            $flags .= " $line|\{blue\}$mod_char";
                        }
                        for $i ($from_count..$to_count - 1) {
                            $line = $to_line + $i;
                            $flags .= " $line|\{green\}$add_char";
                        }
                    }
                    elsif ($to_count > 0 and $from_count > $to_count) {
                        for $i (0..$to_count - 2) {
                            $line = $to_line + $i;
                            $flags .= " $line|\{blue\}$mod_char";
                        }
                        $last = $to_line + $to_count - 1;
                        $flags .= " $last|\{blue+u\}$mod_char";
                    }
                }
            }
            print "set-option buffer git_diff_flags $flags\n"
        ' )
    }

    jump_hunk() {
        direction=$1
        set -- ${kak_opt_git_diff_flags}
        shift

        if [ $# -lt 1 ]; then
            echo "fail 'no git hunks found, try \":git show-diff\" first'"
            exit
        fi

        # Update hunk list if required
        if [ "$kak_timestamp" != "${kak_opt_git_hunk_list%% *}" ]; then
            hunks=$kak_timestamp

            prev_line="-1"
            for line in "$@"; do
                line="${line%%|*}"
                if [ "$((line - prev_line))" -gt 1 ]; then
                    hunks="$hunks $line"
                fi
                prev_line="$line"
            done
            echo "set-option buffer git_hunk_list $hunks"
            hunks=${hunks#* }
        else
            hunks=${kak_opt_git_hunk_list#* }
        fi

        prev_hunk=""
        next_hunk=""
        for hunk in ${hunks}; do
            if   [ "$hunk" -lt "$kak_cursor_line" ]; then
                prev_hunk=$hunk
            elif [ "$hunk" -gt "$kak_cursor_line" ]; then
                next_hunk=$hunk
                break
            fi
        done

        wrapped=false
        if [ "$direction" = "next" ]; then
            if [ -z "$next_hunk" ]; then
                next_hunk=${hunks%% *}
                wrapped=true
            fi
            if [ -n "$next_hunk" ]; then
                echo "select $next_hunk.1,$next_hunk.1"
            fi
        elif [ "$direction" = "prev" ]; then
            if [ -z "$prev_hunk" ]; then
                wrapped=true
                prev_hunk=${hunks##* }
            fi
            if [ -n "$prev_hunk" ]; then
                echo "select $prev_hunk.1,$prev_hunk.1"
            fi
        fi

        if [ "$wrapped" = true ]; then
            echo "echo -markup '{Information}git hunk search wrapped around buffer'"
        fi
    }

    commit() {
        # Handle case where message needs not to be edited
        if grep -E -q -e "-m|-F|-C|--message=.*|--file=.*|--reuse-message=.*|--no-edit|--fixup.*|--squash.*"; then
            if git commit "$@" > /dev/null 2>&1; then
                echo 'echo -markup "{Information}Commit succeeded"'
            else
                echo 'fail Commit failed'
            fi
            exit
        fi <<-EOF
			$@
		EOF

        # fails, and generate COMMIT_EDITMSG
        GIT_EDITOR='' EDITOR='' git commit "$@" > /dev/null 2>&1
        msgfile="$(git rev-parse --git-dir)/COMMIT_EDITMSG"
        printf %s "edit '$msgfile'
              hook buffer BufWritePost '.*\Q$msgfile\E' %{ evaluate-commands %sh{
                  if git commit -F '$msgfile' --cleanup=strip $* > /dev/null; then
                     printf %s 'evaluate-commands -client $kak_client echo -markup %{{Information}Commit succeeded}; delete-buffer'
                  else
                     printf 'evaluate-commands -client %s fail Commit failed\n' "$kak_client"
                  fi
              } }"
    }

    blame_jump() {
        echo >${kak_command_fifo} "echo -to-file ${kak_response_fifo} -- %opt{git_blame}"
        blame_info=$(cat < ${kak_response_fifo})
        blame_index=
        cursor_column=${kak_cursor_column}
        cursor_line=${kak_cursor_line}
        if [ -n "$blame_info" ]; then {
            echo >${kak_command_fifo} "
                update-option buffer git_blame_index
                echo -to-file ${kak_response_fifo} -- %opt{git_blame_index}
            "
            blame_index=$(cat < ${kak_response_fifo})
        } elif [ "${kak_opt_filetype}" = git-diff ] || [ "${kak_opt_filetype}" = git-log ]; then {
            printf >${kak_command_fifo} %s '
                evaluate-commands -draft %{
                    try %{
                        execute-keys <a-l><semicolon><a-?>^commit<ret><a-semicolon>
                    } catch %{
                        # Missing commit line, assume it is an uncommitted change.
                        execute-keys <a-l><semicolon><a-?>\A<ret><a-semicolon>
                    }
                    require-module diff
                    try %{
                        diff-parse BEGIN %{
                            $version = "-";
                        } END %{
                            if ($diff_line_text !~ m{^[ -]}) {
                                print quote "git blame-jump: recursive blame only works on context or deleted lines";
                                exit 1;
                            }
                            if (not defined $commit) {
                                $commit = "HEAD";
                            } else {
                                $commit = "$commit~";
                            }
                            printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d",
                                    $commit, quote($file), $file_line, ('$cursor_column' - 1);
                        }
                    } catch %{
                        echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error}
                    }
                }
            '
            eval set -- "$(cat ${kak_response_fifo})"
            if [ $# -eq 1 ]; then
                echo fail -- "$(kakquote "$1")"
                exit
            fi
            starting_commit=$1
            file=$2
            cursor_line=$3
            cursor_column=$4
            blame_info=$(git blame --porcelain "$starting_commit" -L"$cursor_line,$cursor_line" -- "$file")
            if [ $? -ne 0 ]; then
                echo 'echo -markup %{{Error}failed to run git blame, see *debug* buffer}'
                exit
            fi
        } else {
            if [ -n "${kak_opt_git_blob}" ]; then {
                set -- "${kak_opt_git_blob%%:*}" -- "${kak_opt_git_blob#*:}"
                blame_stdin=/dev/null
            } else {
                set -- --contents - -- "${kak_buffile}" # use stdin to work around git bug
                blame_stdin=${kak_response_fifo}
                echo >${kak_command_fifo} "evaluate-commands -save-regs | %{
                    set-register | %{ cat >${kak_response_fifo} }
                    execute-keys -client ${kak_client} -draft %{%<a-|><ret>}
                }"
            } fi
            if ! blame_info=$(
                git blame --porcelain -L"$cursor_line,$cursor_line" "$@" <${blame_stdin})
            then
                echo 'echo -markup %{{Error}failed to run git blame, see *debug* buffer}'
                exit
            fi
        } fi
        eval "$(printf '%s\n---\n%s' "$blame_index" "$blame_info" |
                client=${kak_opt_docsclient:-$kak_client} \
                cursor_line=$cursor_line cursor_column=$cursor_column \
                perl -wne '
            BEGIN {
                use POSIX qw(strftime);
                our $SQ = "'\''";
                sub escape {
                   return shift =~ s/$SQ/$SQ$SQ/gr
                }
                sub quote {
                    my $token = escape shift;
                    return "$SQ$token$SQ";
                }
                sub shellquote {
                    my $token = shift;
                    $token =~ s/$SQ/$SQ\\$SQ$SQ/g;
                    return "$SQ$token$SQ";
                }
                sub perlquote {
                    my $token = shift;
                    $token =~ s/\\/\\\\/g;
                    $token =~ s/$SQ/\\$SQ/g;
                    return "$SQ$token$SQ";
                }
                $target = $ENV{"cursor_line"};
                $state = "index";
            }
            chomp;
            if ($state eq "index") {
                if ($_ eq "---") {
                    $state = "blame";
                    next;
                }
                @blame_index = split;
                next unless @blame_index;
                shift @blame_index;
                foreach (@blame_index) {
                    $_ =~ m{(\d+)\|(\d+),(\d+)} or die "bad blame index flag: $_";
                    my $buffer_line = $1;
                    if ($buffer_line == $target) {
                        $target_in_blame = $2;
                        $target_offset = $3;
                        last;
                    }
                }
                defined $target_in_blame and next, or last;
            }
            if (m/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
                if ($done) {
                    last;
                }
                $sha = $1;
                $old_line = $2;
                $new_line = $3;
                $count = $4;
                if (defined $target_in_blame) {
                    if ($target_in_blame == $. - 2) {
                        $old_line += $target_offset;
                        $done = 1;
                    }
                } else {
                    if ($new_line <= $target and $target < $new_line + $count) {
                        $old_line += $target - $new_line;
                        $done = 1;
                    }
                }
            }
            if (m/^filename /) { $old_filenames{$sha} = substr($_,9) }
            if (m/^author /) { $authors{$sha} = substr($_,7) }
            if (m/^author-time ([0-9]*)/) { $author_time = $1; }
            if (m/^author-tz ([+-])(\d\d)/) {
                my $sign = $1 eq "+" ? "-" : "+";
                local $ENV{"TZ"} = "UTC${sign}$2";
                $dates{$sha} = strftime("%F", localtime $author_time);
            }
            if (m/^summary /) { $summaries{$sha} = substr($_,8) }
            END {
                if (@blame_index and not defined $target_in_blame) {
                    print "echo fail git blame-jump: line has no blame information;";
                    exit;
                }
                if (not defined $sha) {
                    print "echo fail git blame-jump: missing blame info";
                    exit;
                }
                if (not $done) {
                    print "echo \"fail git blame-jump: line not found in annotations (blame still loading?)\"";
                    exit;
                }
                $info = "{Information}{\\}";
                if ($sha =~ m{^0+$}) {
                    $old_filename = $ENV{"kak_buffile"};
                    $old_filename = substr $old_filename, length($ENV{"PWD"}) + 1;
                    $show_diff = "diff HEAD";
                    $info .= "Not committed yet";
                } else {
                    $old_filename = $old_filenames{$sha};
                    $author = $authors{$sha};
                    $date = $dates{$sha};
                    $summary = $summaries{$sha};
                    $show_diff = "show $sha";
                    $info .= "$date $author \"$summary\"";
                }
                $on_close_fifo = "
                    evaluate-commands -draft $SQ
                        execute-keys <percent>
                        require-module diff
                        diff-parse BEGIN %{
                            \$in_file = " . escape(perlquote($old_filename)) . ";
                            \$in_file_line = $old_line;
                        } END $SQ$SQ
                            print \"execute-keys -client $ENV{client} \${diff_line}g<a-h>$ENV{cursor_column}l;\";
                            printf \"evaluate-commands -client $ENV{client} $SQ$SQ$SQ$SQ
                                hook -once window NormalIdle .* $SQ$SQ$SQ$SQ$SQ$SQ$SQ$SQ
                                    execute-keys vv
                                    echo -markup -- %s
                                $SQ$SQ$SQ$SQ$SQ$SQ$SQ$SQ
                            $SQ$SQ$SQ$SQ ;\"," . escape(escape(perlquote(escape(escape(quote($info)))))) . ";
                        $SQ$SQ
                    $SQ
                ";
                printf "on_close_fifo=%s show_git_cmd_output %s",
                    shellquote($on_close_fifo), $show_diff;
            }
        ')"
    }

    apply_selections() {
        if [ -z "$(cd_bufdir >/dev/null 2>&1; git ls-files -- ":(literal)${kak_buffile}")" ]; then {
            enquoted="$(printf '"%s" ' "$@")"
            echo "require-module patch"
            echo "patch git apply $enquoted"
            return
        } fi
        base_rev=HEAD
        index_only=false
        index=false
        reverse=false
        for arg; do
            case "$arg" in
                (--cached) index_only=true ; base_rev= ;;
                (--index) index=true ;;
                (--reverse|-R) reverse=true ;;
            esac
        done
        if ! $reverse && ! $index_only; then
            echo "fail %{git apply on buffer contents doesn't make sense without --reverse or --cached}"
            exit
        fi
        cd_bufdir
        num_inserted=0
        num_deleted=0
        for selection_desc in $kak_selections_desc; do {
            IFS=' .,' read anchor_line _ cursor_line _ <<-EOF
                $selection_desc
		EOF
            if [ $anchor_line -lt $cursor_line ]; then
                min_line=$anchor_line
                max_line=$cursor_line
            else
                min_line=$cursor_line
                max_line=$anchor_line
            fi
            intended_diff='diff_buffer_against_rev "$base_rev" -u'
            if $index; then {
                git update-index --refresh "${kak_buffile}" >/dev/null
                intended_diff='git diff --no-ext-diff HEAD -- ":(literal)${kak_buffile}"'
            } elif $index_only && $reverse; then {
                diff=$(eval "$intended_diff")
                if [ -n "$diff" ]; then {
                    # Convert from buffile lines to index lines.
                    for line in min_line max_line; do {
                        if ! index_line_or_error_message=$(
                            eval file_line=\$$line
                            printf %s "$diff" |
                                perl "${kak_runtime}/rc/filetype/diff-parse.pl" \
                                    BEGIN '
                                        $in_file = ""; # no need to check filename, there is only one
                                        $in_file_line = '"$file_line"';
                                    ' END '
                                        $other_file_line++ if $diff_line_text =~ m{^\+};
                                        $other_file_line += $in_file_line - $file_line;
                                        print "$other_file_line\n";
                                    '
                        ); then
                            echo fail "git apply: $index_line_or_error_message"
                            exit
                        fi
                        eval $line=$index_line_or_error_message
                    } done
                } fi
                intended_diff='git diff --no-ext-diff --cached -- ":(literal)${kak_buffile}"'
            } fi
            diff=$(eval "$intended_diff" |
                    perl "${kak_runtime}"/rc/tools/patch-range.pl -line-numbers-from-new-file \
                        $min_line $max_line sh -c cat -- "$@" # forward any --reverse arg
                   printf .) # avoid stripping newline
            diff=${diff%.}
            if ! printf %s "$diff" | git apply "$@"; then
                printf >&2 "git apply: error running:\n\$ git apply %s << EOF\n" "$*"
                printf >&2 %s "$diff"
                printf >&2 'EOF\n'
                echo "fail 'git apply: failed to apply selections, see *debug* buffer'"
                exit
            fi
            count() {
                printf %s "$diff" | awk '
                    BEGIN { n = 0 }
                    /^@@/,/^$/ { if ($0 ~ /^'"$1"'/) { n++ } }
                    END { print n }'
            }
            num_inserted=$(( $num_inserted + $(count +) ))
            num_deleted=$(( $num_deleted + $(count -) ))
        } done
        if ! $index_only && ! $kak_modified; then
            echo edit!
            echo git update-diff
        else
            update_diff
        fi
        msg=
        case $index_only,$reverse,$index in
            (true,false,*) msg=Staged ;;
            (true,true,*) msg=Unstaged ;;
            (false,true,false) msg=Reverted ;;
            (false,true,true) msg='Unstaged and reverted' ;;
        esac
        case $num_inserted,$num_deleted in
            (*,0) msg="$msg $num_inserted inserted line(s)";;
            (0,*) msg="$msg $num_deleted deleted line(s)";;
            (*,*) msg="$msg $num_inserted inserted and $num_deleted deleted lines";;
        esac
        echo "echo -markup '{Information}{\\}$msg'"
    }

    case "$1" in
        apply)
            shift
            apply_selections "$@"
            ;;
        show|show-branch|log|diff|status)
            show_git_cmd_output "$@"
            ;;
        blame)
            shift
            blame_toggle "$@"
            ;;
        blame-jump)
            blame_jump
            ;;
        hide-blame)
            hide_blame
            ;;
        show-diff)
            echo 'try %{ add-highlighter window/git-diff flag-lines Default git_diff_flags }'
            update_diff
            ;;
        hide-diff)
            echo 'try %{ remove-highlighter window/git-diff }'
            ;;
        update-diff) update_diff ;;
        next-hunk) jump_hunk next ;;
        prev-hunk) jump_hunk prev ;;
        commit)
            shift
            commit "$@"
            ;;
        init)
            shift
            git init "$@" > /dev/null 2>&1
            ;;
        add|rm)
            cmd="$1"
            shift
            run_git_cmd $cmd "${@:-"${kak_buffile}"}"
            ;;
        reset|checkout)
            run_git_cmd "$@"
            ;;
        grep)
            shift
            enquoted="$(printf '"%s" ' "$@")"
            printf %s "try %{
                set-option current grepcmd 'git grep -n --column'
                grep $enquoted
                set-option current grepcmd '$kak_opt_grepcmd'
            }"
            ;;
        edit)
            shift
            enquoted="$(printf '"%s" ' "$@")"
            printf %s "edit -existing -- $enquoted"
            ;;
        *)
            printf "fail unknown git command '%s'\n" "$1"
            exit
            ;;
    esac
}}

# Works within :git diff and :git show
define-command git-diff-goto-source \
    -docstring 'Navigate to source by pressing the enter key in hunks when git diff is displayed. Works within :git diff and :git show' %{
    require-module diff
    diff-jump %sh{ git rev-parse --show-toplevel }
}