git.kak

· ficd's pastes · raw

expires: 2026-06-20

  1# DANIEL PATCH:
  2# added completions for git diff
  3# added git push (with completions)
  4
  5declare-option -docstring "name of the client in which documentation is to be displayed" \
  6	str docsclient
  7
  8declare-option -docstring "git diff added character" \
  9	str git_diff_add_char "▊"
 10
 11declare-option -docstring "git diff modified character" \
 12	str git_diff_mod_char "▊"
 13
 14declare-option -docstring "git diff deleted character" \
 15	str git_diff_del_char "_"
 16
 17declare-option -docstring "git diff top deleted character" \
 18	str git_diff_top_char "‾"
 19
 20hook -group git-log-highlight global WinSetOption filetype=git-log %{
 21	add-highlighter window/git-log group
 22	add-highlighter window/git-log/ regex '^([*|\\ /_.-])*' 0:keyword
 23	add-highlighter window/git-log/ regex '^( ?[*|\\ /_.-])*\h{,3}(commit )?(\b[0-9a-f]{4,40}\b)' 2:keyword 3:comment
 24	add-highlighter window/git-log/ regex '^( ?[*|\\ /_.-])*\h{,3}([a-zA-Z_-]+:) (.*?)$' 2:variable 3:value
 25	hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-log }
 26}
 27
 28hook global WinSetOption filetype=diff %{
 29	try %{
 30		execute-keys -draft %{/^diff --git\b<ret>}
 31		evaluate-commands %sh{
 32			if [ -n "$(git ls-files -- "${kak_buffile}")" ]; then
 33				echo fail
 34			fi
 35		}
 36		set-option buffer filetype git-diff
 37	}
 38}
 39
 40hook -group git-diff-highlight global WinSetOption filetype=(git-diff|git-log) %{
 41	require-module diff
 42	add-highlighter %exp{window/%val{hook_param_capture_1}-ref-diff} ref diff
 43	hook -once -always window WinSetOption filetype=.* %exp{
 44		remove-highlighter window/%val{hook_param_capture_1}-ref-diff
 45	}
 46}
 47
 48hook global WinSetOption filetype=(?:git-diff|git-log) %{
 49	map buffer normal <ret> %exp{:git-diff-goto-source # %val{hook_param}<ret>} -docstring 'Jump to source from git diff'
 50	hook -once -always window WinSetOption filetype=.* %exp{
 51		unmap buffer normal <ret> %%{:git-diff-goto-source # %val{hook_param}<ret>}
 52	}
 53}
 54
 55hook -group git-status-highlight global WinSetOption filetype=git-status %{
 56	add-highlighter window/git-status group
 57	add-highlighter window/git-status/ regex '^## ' 0:comment
 58	add-highlighter window/git-status/ regex '^## (\S*[^\s\.@])' 1:green
 59	add-highlighter window/git-status/ regex '^## (\S*[^\s\.@])(\.\.+)(\S*[^\s\.@])' 1:green 2:comment 3:red
 60	add-highlighter window/git-status/ regex '^(##) (No commits yet on) (\S*[^\s\.@])' 1:comment 2:Default 3:green
 61	add-highlighter window/git-status/ regex '^## \S+ \[[^\n]*ahead (\d+)[^\n]*\]' 1:green
 62	add-highlighter window/git-status/ regex '^## \S+ \[[^\n]*behind (\d+)[^\n]*\]' 1:red
 63	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
 64	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
 65	add-highlighter window/git-status/ regex '^R[ !\?ACDMRTUacdmrtu] [^\n]+( -> )' 1:cyan
 66	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
 67
 68	hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-status }
 69}
 70
 71hook -group git-show-branch-highlight global WinSetOption filetype=git-show-branch %{
 72	add-highlighter window/git-show-branch group
 73	add-highlighter window/git-show-branch/ regex '(\*)|(\+)|(!)' 1:red 2:green 3:green
 74	add-highlighter window/git-show-branch/ regex '(!\D+\{0\}\])|(!\D+\{1\}\])|(!\D+\{2\}\])|(!\D+\{3\}\])' 1:red 2:green 3:yellow 4:blue
 75	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
 76
 77	hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-show-branch}
 78}
 79
 80declare-option -hidden line-specs git_blame_flags
 81declare-option -hidden line-specs git_blame_index
 82declare-option -hidden str git_blame
 83declare-option -hidden str git_blob
 84declare-option -hidden line-specs git_diff_flags
 85declare-option -hidden int-list git_hunk_list
 86
 87define-command -params 1.. \
 88	-docstring %{
 89		git [<arguments>]: git wrapping helper
 90		All the optional arguments are forwarded to the git utility
 91		Available commands:
 92			add
 93			apply      - run "patch git apply [<arguments>]"; if buffile is
 94						 tracked, use the changes to selected lines instead
 95			blame      - toggle blame annotations
 96			blame-jump - show the commit that added the line at cursor
 97			checkout
 98			commit
 99			diff
100			edit
101			grep
102			hide-diff
103			init
104			log
105			next-hunk
106			prev-hunk
107			push
108			reset
109			rm
110			show
111			show-branch
112			show-diff
113			status
114			update-diff
115	} -shell-script-candidates %{
116	if [ $kak_token_to_complete -eq 0 ]; then
117		printf %s\\n \
118			add \
119			apply \
120			blame \
121			blame-jump \
122			checkout \
123			commit \
124			diff \
125			edit \
126			grep \
127			hide-diff \
128			init \
129			log \
130			next-hunk \
131			prev-hunk \
132			push \
133			reset \
134			rm \
135			show \
136			show-branch \
137			show-diff \
138			status \
139			update-diff \
140		;
141	else
142		case "$1" in
143			commit) printf -- "--amend\n--no-edit\n--all\n--reset-author\n--fixup\n--squash\n"; git ls-files -m ;;
144			add) git ls-files -dmo --exclude-standard ;;
145			apply) printf -- "--reverse\n--cached\n--index\n--3way\n" ;;
146			grep|edit) git ls-files -c --recurse-submodules ;;
147			push) printf -- "--all\n--force\n--force-if-includes\n--force-with-lease\n--porcelain\n--prune\n--set-upstream\n";;
148			diff)
149			if [ "$kak_token_to_complete" -eq 1 ]; then
150				printf -- "--cached\n--staged\n--base\n--ours\n--theirs\n"
151				git diff --name-only
152				git log --oneline -n 5 --all | awk '{print $1}'
153				git for-each-ref --format='%(refname:short)'
154			else
155				case "$2" in
156					*--cached*|*--staged*)
157					git diff --cached --name-only
158					;;
159					*)
160					git diff --name-only
161					git log --oneline -n 5 --all | awk '{print $1}'
162					git for-each-ref --format='%(refname:short)'
163					;;
164				esac
165			fi
166				;;
167		esac
168	fi
169  } \
170  git %{ evaluate-commands %sh{
171	cd_bufdir() {
172		dirname_buffer="${kak_buffile%/*}"
173		if [ "${dirname_buffer}" = "${kak_buffile}" ]; then
174			printf 'fail git: cannot operate on scratch buffer: %s\n' "${kak_buffile}"
175			return 1
176		fi
177		cd "${dirname_buffer}" 2>/dev/null || {
178			printf 'fail git: unable to change the current working directory to: %s\n' "${dirname_buffer}"
179			return 1
180		}
181	}
182	kakquote() {
183		printf "%s" "$1" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"
184	}
185
186	show_git_cmd_output() {
187		local filetype
188
189		case "$1" in
190		   diff) filetype=git-diff ;;
191		   show) filetype=git-log ;;
192		   show-branch) filetype=git-show-branch ;;
193		   log)  filetype=git-log ;;
194		   status)  filetype=git-status ;;
195		   *) return 1 ;;
196		esac
197		output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-git.XXXXXXXX)/fifo
198		mkfifo ${output}
199		( trap - INT QUIT; git "$@" > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
200
201		printf %s "evaluate-commands -try-client '$kak_opt_docsclient' '
202				  edit! -fifo ${output} *git*
203				  set-option buffer filetype ${filetype}
204				  $(hide_blame)
205				  set-option buffer git_blob %{}
206				  hook -always -once buffer BufCloseFifo .* ''
207					  nop %sh{ rm -r $(dirname ${output}) }
208					  $(printf %s "${on_close_fifo}" | sed "s/'/''''/g")
209				  ''
210		'"
211	}
212
213	hide_blame() {
214		printf %s "
215			set-option buffer git_blame_flags $kak_timestamp
216			set-option buffer git_blame_index $kak_timestamp
217			set-option buffer git_blame %{}
218			try %{ remove-highlighter window/git-blame }
219			unmap window normal <ret> %{:git blame-jump<ret>}
220		"
221	}
222
223	diff_buffer_against_rev() {
224		if ! command -v diff >/dev/null; then
225			echo >${kak_command_fifo} "fail diff: command not found"
226		fi
227		rev=$1 # empty means index
228		shift
229		buffile_relative=${kak_buffile#"$(git rev-parse --show-toplevel)/"}
230		echo >${kak_command_fifo} "evaluate-commands -no-hooks write ${kak_response_fifo}"
231		git show "$rev:${buffile_relative}" |
232			diff - ${kak_response_fifo} "$@" |
233			awk -v buffile_relative="$buffile_relative" '
234				NR == 1 { print "--- a/" buffile_relative }
235				NR == 2 { print "+++ b/" buffile_relative }
236				NR > 2
237			'
238	}
239
240	blame_toggle() {
241		echo >${kak_command_fifo} "try %{
242			add-highlighter window/git-blame flag-lines Information git_blame_flags
243			echo -to-file ${kak_response_fifo}
244		} catch %{
245			echo -to-file ${kak_response_fifo} 'hide_blame; exit'
246		}"
247		eval $(cat ${kak_response_fifo})
248		if [ -z "${kak_opt_git_blob}" ] && {
249			[ "${kak_opt_filetype}" = git-diff ] || [ "${kak_opt_filetype}" = git-log ]
250		} then {
251			echo 'try %{ remove-highlighter window/git-blame }'
252			printf >${kak_command_fifo} %s '
253				evaluate-commands -client '${kak_client}' -draft %{
254					try %{
255						execute-keys <a-l><semicolon><a-?>^commit<ret><a-semicolon>
256					} catch %{
257						# Missing commit line, assume it is an uncommitted change.
258						execute-keys <a-l><semicolon>Gg<a-semicolon>
259					}
260					require-module diff
261					try %{
262						diff-parse BEGIN %{
263							$directory = qx(git rev-parse --show-toplevel);
264							chomp $directory;
265						} END %{
266							my $filename = $other_file;
267							my $line = $other_file_line;
268							if (not defined $commit) {
269								$commit = "HEAD";
270								if ($diff_line_text =~ m{^\+}) {
271									print "echo -to-file '${kak_response_fifo}' -quoting shell "
272										. "%{git blame: blame from HEAD does not work on added lines}";
273									exit;
274								}
275							} elsif ($diff_line_text =~ m{^[-]}) {
276								$commit = "$commit~";
277							} else {
278								$filename = $file;
279								$line = $file_line;
280							}
281							$line = $line or 1;
282							my $filename_relative = substr($filename, length "$directory/");
283							printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %s %d %d",
284							   $commit, quote($filename), quote($filename_relative),
285							   $line, ('${kak_cursor_column}' - 1);
286						}
287					} catch %{
288						echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error}
289					}
290				}
291			'
292			n=$#
293			eval set -- "$(cat ${kak_response_fifo})" "$@"
294			if [ $# -eq $((n+1)) ]; then
295				echo fail -- "$(kakquote "$1")"
296				exit
297			fi
298			commit=$1
299			file_absolute=$2
300			file_relative=$3
301			cursor_line=$4
302			cursor_column=$5
303			shift 5
304			# Log commit and file name because they are only echoed briefly
305			# and not shown elsewhere (we don't have a :messages buffer).
306			message="Blaming $file_relative as of $(git rev-parse --short $commit)"
307			echo "echo -debug -- $(kakquote "$message")"
308			on_close_fifo="
309				execute-keys -client ${kak_client} ${cursor_line}g<a-h>${cursor_column}lh
310				evaluate-commands -client ${kak_client} %{
311					set-option buffer git_blob $(kakquote "$commit:$file_absolute")
312					git blame $(for arg; do kakquote "$arg"; printf " "; done)
313					echo -markup -- $(kakquote "{Information}{\\}$message. Press <ret> to jump to blamed commit")
314					hook -once window NormalIdle .* %{ execute-keys vv }
315				}
316			" show_git_cmd_output show "$commit:$file_relative"
317			exit
318		} fi
319		if [ -n "${kak_opt_git_blob}" ]; then {
320			set -- "$@" "${kak_opt_git_blob%%:*}" -- "${kak_opt_git_blob#*:}"
321			blame_stdin=/dev/null
322		} else {
323			if ! error=$(cd_bufdir); then
324				echo 'remove-highlighter window/git-blame'
325				printf %s\\n "$error"
326				exit
327			fi
328			set -- "$@" --contents - -- "${kak_buffile}" # use stdin to work around git bug
329			blame_stdin=$(mktemp "${TMPDIR:-/tmp}"/kak-git.XXXXXX)
330			echo >${kak_command_fifo} "
331				evaluate-commands -no-hooks write -force ${blame_stdin}
332				echo -to-file ${kak_response_fifo}
333			"
334			: <${kak_response_fifo}
335		} fi
336		echo 'map window normal <ret> %{:git blame-jump<ret>}'
337		echo 'echo -markup {Information}Press <ret> to jump to blamed commit'
338		(
339			trap - INT QUIT
340			printf %s "evaluate-commands -client '$kak_client' %{
341					  set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp'
342					  set-option buffer=$kak_bufname git_blame_index '$kak_timestamp'
343					  set-option buffer=$kak_bufname git_blame ''
344				  }" | kak -p ${kak_session}
345			if ! stderr=$({ git blame --incremental "$@" <${blame_stdin} | perl -wne '
346				  BEGIN {
347				  use POSIX qw(strftime);
348				  sub quote {
349					  my $SQ = "'\''";
350					  my $token = shift;
351					  $token =~ s/$SQ/$SQ$SQ/g;
352					  return "$SQ$token$SQ";
353				  }
354				  sub send_flags {
355					  my $is_last_call = shift;
356					  if (not defined $line) {
357						  if ($is_last_call) { exit 1; }
358						  return;
359					  }
360					  my $text = substr($sha,0,7) . " " . $dates{$sha} . " " . $authors{$sha};
361					  $text =~ s/~/~~/g;
362					  for ( my $i = 0; $i < $count; $i++ ) {
363						  $flags .= " %~" . ($line+$i) . "|$text~";
364					  }
365					  $now = time();
366					  # Send roughly one update per second, to avoid creating too many kak processes.
367					  if (!$is_last_call && defined $last_sent && $now - $last_sent < 1) {
368						  return
369					  }
370					  open CMD, "|-", "kak -p $ENV{kak_session}";
371					  print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame_flags $flags;";
372					  print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame_index $index;";
373					  print CMD "set-option -add buffer=$ENV{kak_bufname} git_blame " . quote $raw_blame;
374					  close(CMD);
375					  $flags = "";
376					  $index = "";
377					  $raw_blame = "";
378					  $last_sent = $now;
379				  }
380				  }
381				  $raw_blame .= $_;
382				  chomp;
383				  if (m/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
384					  send_flags(0);
385					  $sha = $1;
386					  $line = $3;
387					  $count = $4;
388					  for ( my $i = 0; $i < $count; $i++ ) {
389						  $index .= " " . ($line+$i) . "|$.,$i";
390					  }
391				  }
392				  if (m/^author /) {
393					  $authors{$sha} = substr($_,7);
394					  $authors{$sha} = "Not Committed Yet" if $authors{$sha} eq "External file (--contents)";
395				  }
396				  if (m/^author-time ([0-9]*)/) { $author_time = $1; }
397				  if (m/^author-tz ([+-])(\d\d)/) {
398					  my $sign = $1 eq "+" ? "-" : "+";
399					  local $ENV{"TZ"} = "UTC${sign}$2";
400					  $dates{$sha} = strftime("%F %T", localtime $author_time);
401				  }
402				  END { send_flags(1); }'
403			} 2>&1); then
404				escape2() { printf %s "$*" | sed "s/'/''''/g"; }
405				echo "evaluate-commands -client ${kak_client} '
406					evaluate-commands -draft %{
407						buffer %{${kak_buffile}}
408						git hide-blame
409					}
410					echo -debug failed to run git blame
411					echo -debug git stderr: <<<
412					echo -debug ''$(escape2 "$stderr")>>>''
413					echo -markup %{{Error}failed to run git blame, see *debug* buffer}
414				'" | kak -p ${kak_session}
415			fi
416			if [ ${blame_stdin} != /dev/null ]; then
417				rm ${blame_stdin}
418			fi
419		) > /dev/null 2>&1 < /dev/null &
420	}
421
422	run_git_cmd() {
423		if git "${@}" > /dev/null 2>&1; then
424		  printf %s "echo -markup '{Information}git $1 succeeded'"
425		else
426		  printf 'fail git %s failed\n' "$1"
427		fi
428	}
429
430	update_diff() {
431		(
432			cd_bufdir || exit
433			diff_buffer_against_rev "" -U0 | perl -e '
434			use utf8;
435			$flags = $ENV{"kak_timestamp"};
436			$add_char = $ENV{"kak_opt_git_diff_add_char"};
437			$del_char = $ENV{"kak_opt_git_diff_del_char"};
438			$top_char = $ENV{"kak_opt_git_diff_top_char"};
439			$mod_char = $ENV{"kak_opt_git_diff_mod_char"};
440			foreach $line (<STDIN>) {
441				if ($line =~ /@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))?/) {
442					$from_line = $1;
443					$from_count = ($2 eq "" ? 1 : $2);
444					$to_line = $3;
445					$to_count = ($4 eq "" ? 1 : $4);
446
447					if ($from_count == 0 and $to_count > 0) {
448						for $i (0..$to_count - 1) {
449							$line = $to_line + $i;
450							$flags .= " $line|\{green\}$add_char";
451						}
452					}
453					elsif ($from_count > 0 and $to_count == 0) {
454						if ($to_line == 0) {
455							$flags .= " 1|\{red\}$top_char";
456						} else {
457							$flags .= " $to_line|\{red\}$del_char";
458						}
459					}
460					elsif ($from_count > 0 and $from_count == $to_count) {
461						for $i (0..$to_count - 1) {
462							$line = $to_line + $i;
463							$flags .= " $line|\{blue\}$mod_char";
464						}
465					}
466					elsif ($from_count > 0 and $from_count < $to_count) {
467						for $i (0..$from_count - 1) {
468							$line = $to_line + $i;
469							$flags .= " $line|\{blue\}$mod_char";
470						}
471						for $i ($from_count..$to_count - 1) {
472							$line = $to_line + $i;
473							$flags .= " $line|\{green\}$add_char";
474						}
475					}
476					elsif ($to_count > 0 and $from_count > $to_count) {
477						for $i (0..$to_count - 2) {
478							$line = $to_line + $i;
479							$flags .= " $line|\{blue\}$mod_char";
480						}
481						$last = $to_line + $to_count - 1;
482						$flags .= " $last|\{blue+u\}$mod_char";
483					}
484				}
485			}
486			print "set-option buffer git_diff_flags $flags\n"
487		' )
488	}
489
490	jump_hunk() {
491		direction=$1
492		set -- ${kak_opt_git_diff_flags}
493		shift
494
495		if [ $# -lt 1 ]; then
496			echo "fail 'no git hunks found, try \":git show-diff\" first'"
497			exit
498		fi
499
500		# Update hunk list if required
501		if [ "$kak_timestamp" != "${kak_opt_git_hunk_list%% *}" ]; then
502			hunks=$kak_timestamp
503
504			prev_line="-1"
505			for line in "$@"; do
506				line="${line%%|*}"
507				if [ "$((line - prev_line))" -gt 1 ]; then
508					hunks="$hunks $line"
509				fi
510				prev_line="$line"
511			done
512			echo "set-option buffer git_hunk_list $hunks"
513			hunks=${hunks#* }
514		else
515			hunks=${kak_opt_git_hunk_list#* }
516		fi
517
518		prev_hunk=""
519		next_hunk=""
520		for hunk in ${hunks}; do
521			if   [ "$hunk" -lt "$kak_cursor_line" ]; then
522				prev_hunk=$hunk
523			elif [ "$hunk" -gt "$kak_cursor_line" ]; then
524				next_hunk=$hunk
525				break
526			fi
527		done
528
529		wrapped=false
530		if [ "$direction" = "next" ]; then
531			if [ -z "$next_hunk" ]; then
532				next_hunk=${hunks%% *}
533				wrapped=true
534			fi
535			if [ -n "$next_hunk" ]; then
536				echo "select $next_hunk.1,$next_hunk.1"
537			fi
538		elif [ "$direction" = "prev" ]; then
539			if [ -z "$prev_hunk" ]; then
540				wrapped=true
541				prev_hunk=${hunks##* }
542			fi
543			if [ -n "$prev_hunk" ]; then
544				echo "select $prev_hunk.1,$prev_hunk.1"
545			fi
546		fi
547
548		if [ "$wrapped" = true ]; then
549			echo "echo -markup '{Information}git hunk search wrapped around buffer'"
550		fi
551	}
552
553	commit() {
554		# Handle case where message needs not to be edited
555		if grep -E -q -e "-m|-F|-C|--message=.*|--file=.*|--reuse-message=.*|--no-edit|--fixup.*|--squash.*"; then
556			if git commit "$@" > /dev/null 2>&1; then
557				echo 'echo -markup "{Information}Commit succeeded"'
558			else
559				echo 'fail Commit failed'
560			fi
561			exit
562		fi <<-EOF
563			$@
564		EOF
565
566		# fails, and generate COMMIT_EDITMSG
567		GIT_EDITOR='' EDITOR='' git commit "$@" > /dev/null 2>&1
568		msgfile="$(git rev-parse --git-dir)/COMMIT_EDITMSG"
569		printf %s "edit '$msgfile'
570			  hook buffer BufWritePost '.*\Q$msgfile\E' %{ evaluate-commands %sh{
571				  if git commit -F '$msgfile' --cleanup=strip $* > /dev/null; then
572					 printf %s 'evaluate-commands -client $kak_client echo -markup %{{Information}Commit succeeded}; delete-buffer'
573				  else
574					 printf 'evaluate-commands -client %s fail Commit failed\n' "$kak_client"
575				  fi
576			  } }"
577	}
578
579	blame_jump() {
580		if [ -z "${kak_client}" ]; then
581			echo fail git blame-jump: no client in context
582			exit
583		fi
584		echo >${kak_command_fifo} "echo -to-file ${kak_response_fifo} -- %opt{git_blame}"
585		blame_info=$(cat < ${kak_response_fifo})
586		blame_index=
587		cursor_column=${kak_cursor_column}
588		cursor_line=${kak_cursor_line}
589		if [ -n "$blame_info" ]; then {
590			echo >${kak_command_fifo} "
591				update-option buffer git_blame_index
592				echo -to-file ${kak_response_fifo} -- %opt{git_blame_index}
593			"
594			blame_index=$(cat < ${kak_response_fifo})
595		} elif [ "${kak_opt_filetype}" = git-diff ] || [ "${kak_opt_filetype}" = git-log ]; then {
596			printf >${kak_command_fifo} %s '
597				evaluate-commands -draft %{
598					try %{
599						execute-keys <a-l><semicolon><a-?>^commit<ret><a-semicolon>
600					} catch %{
601						# Missing commit line, assume it is an uncommitted change.
602						execute-keys <a-l><semicolon><a-?>\A<ret><a-semicolon>
603					}
604					require-module diff
605					try %{
606						diff-parse BEGIN %{
607							$version = "-";
608							$directory = qx(git rev-parse --show-toplevel);
609							chomp $directory;
610						} END %{
611							if ($diff_line_text !~ m{^[ -]}) {
612								print quote "git blame-jump: recursive blame only works on context or deleted lines";
613								exit 1;
614							}
615							if (not defined $commit) {
616								$commit = "HEAD";
617							} else {
618								$commit = "$commit~";
619							}
620							printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d",
621									$commit, quote($file), $file_line, ('$cursor_column' - 1);
622						}
623					} catch %{
624						echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error}
625					}
626				}
627			'
628			eval set -- "$(cat ${kak_response_fifo})"
629			if [ $# -eq 1 ]; then
630				echo fail -- "$(kakquote "$1")"
631				exit
632			fi
633			starting_commit=$1
634			file=$2
635			cursor_line=$3
636			cursor_column=$4
637			blame_info=$(git blame --porcelain "$starting_commit" -L"$cursor_line,$cursor_line" -- "$file")
638			if [ $? -ne 0 ]; then
639				echo 'echo -markup %{{Error}failed to run git blame, see *debug* buffer}'
640				exit
641			fi
642		} else {
643			if [ -n "${kak_opt_git_blob}" ]; then {
644				set -- "${kak_opt_git_blob%%:*}" -- "${kak_opt_git_blob#*:}"
645				blame_stdin=/dev/null
646			} else {
647				set -- --contents - -- "${kak_buffile}" # use stdin to work around git bug
648				blame_stdin=${kak_response_fifo}
649				echo >${kak_command_fifo} "evaluate-commands -no-hooks write ${kak_response_fifo}"
650			} fi
651			if ! blame_info=$(
652				git blame --porcelain -L"$cursor_line,$cursor_line" "$@" <${blame_stdin})
653			then
654				echo 'echo -markup %{{Error}failed to run git blame, see *debug* buffer}'
655				exit
656			fi
657		} fi
658		eval "$(printf '%s\n---\n%s' "$blame_index" "$blame_info" |
659				client=${kak_opt_docsclient:-$kak_client} \
660				cursor_line=$cursor_line cursor_column=$cursor_column \
661				perl -wne '
662			BEGIN {
663				use POSIX qw(strftime);
664				our $SQ = "'\''";
665				sub escape {
666				   return shift =~ s/$SQ/$SQ$SQ/gr
667				}
668				sub quote {
669					my $token = escape shift;
670					return "$SQ$token$SQ";
671				}
672				sub shellquote {
673					my $token = shift;
674					$token =~ s/$SQ/$SQ\\$SQ$SQ/g;
675					return "$SQ$token$SQ";
676				}
677				sub perlquote {
678					my $token = shift;
679					$token =~ s/\\/\\\\/g;
680					$token =~ s/$SQ/\\$SQ/g;
681					return "$SQ$token$SQ";
682				}
683				$target = $ENV{"cursor_line"};
684				$state = "index";
685			}
686			chomp;
687			if ($state eq "index") {
688				if ($_ eq "---") {
689					$state = "blame";
690					next;
691				}
692				@blame_index = split;
693				next unless @blame_index;
694				shift @blame_index;
695				foreach (@blame_index) {
696					$_ =~ m{(\d+)\|(\d+),(\d+)} or die "bad blame index flag: $_";
697					my $buffer_line = $1;
698					if ($buffer_line == $target) {
699						$target_in_blame = $2;
700						$target_offset = $3;
701						last;
702					}
703				}
704				defined $target_in_blame and next, or last;
705			}
706			if (m/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
707				if ($done) {
708					last;
709				}
710				$sha = $1;
711				$old_line = $2;
712				$new_line = $3;
713				$count = $4;
714				if (defined $target_in_blame) {
715					if ($target_in_blame == $. - 2) {
716						$old_line += $target_offset;
717						$done = 1;
718					}
719				} else {
720					if ($new_line <= $target and $target < $new_line + $count) {
721						$old_line += $target - $new_line;
722						$done = 1;
723					}
724				}
725			}
726			if (m/^filename /) { $old_filenames{$sha} = substr($_,9) }
727			if (m/^author /) { $authors{$sha} = substr($_,7) }
728			if (m/^author-time ([0-9]*)/) { $author_time = $1; }
729			if (m/^author-tz ([+-])(\d\d)/) {
730				my $sign = $1 eq "+" ? "-" : "+";
731				local $ENV{"TZ"} = "UTC${sign}$2";
732				$dates{$sha} = strftime("%F", localtime $author_time);
733			}
734			if (m/^summary /) { $summaries{$sha} = substr($_,8) }
735			END {
736				if (@blame_index and not defined $target_in_blame) {
737					print "echo fail git blame-jump: line has no blame information;";
738					exit;
739				}
740				if (not defined $sha) {
741					print "echo fail git blame-jump: missing blame info";
742					exit;
743				}
744				if (not $done) {
745					print "echo \"fail git blame-jump: line not found in annotations (blame still loading?)\"";
746					exit;
747				}
748				$info = "{Information}{\\}";
749				if ($sha =~ m{^0+$}) {
750					$old_filename = $ENV{"kak_buffile"};
751					$old_filename = substr $old_filename, length($ENV{"PWD"}) + 1;
752					$show_diff = "diff HEAD";
753					$info .= "Not committed yet";
754				} else {
755					$old_filename = $old_filenames{$sha};
756					$author = $authors{$sha};
757					$date = $dates{$sha};
758					$summary = $summaries{$sha};
759					$show_diff = "show $sha";
760					$info .= "$date $author \"$summary\"";
761				}
762				$on_close_fifo = "
763					evaluate-commands -draft $SQ
764						execute-keys <percent>
765						require-module diff
766						diff-parse BEGIN %{
767							\$in_file = " . escape(perlquote($old_filename)) . ";
768							\$in_file_line = $old_line;
769						} END $SQ$SQ
770							print \"execute-keys -client $ENV{client} \${diff_line}g<a-h>$ENV{cursor_column}l;\";
771							printf \"evaluate-commands -client $ENV{client} $SQ$SQ$SQ$SQ
772								hook -once window NormalIdle .* $SQ$SQ$SQ$SQ$SQ$SQ$SQ$SQ
773									execute-keys vv
774									echo -markup -- %s
775								$SQ$SQ$SQ$SQ$SQ$SQ$SQ$SQ
776							$SQ$SQ$SQ$SQ ;\"," . escape(escape(perlquote(escape(escape(quote($info)))))) . ";
777						$SQ$SQ
778					$SQ
779				";
780				printf "on_close_fifo=%s show_git_cmd_output %s",
781					shellquote($on_close_fifo), $show_diff;
782			}
783		')"
784	}
785
786	apply_selections() {
787		if [ -z "$(cd_bufdir >/dev/null 2>&1 && git ls-files -- ":(literal)${kak_buffile}")" ]; then {
788			enquoted="$(printf '"%s" ' "$@")"
789			echo "require-module patch"
790			echo "patch git apply $enquoted"
791			return
792		} fi
793		base_rev=HEAD
794		index_only=false
795		index=false
796		reverse=false
797		for arg; do
798			case "$arg" in
799				(--cached) index_only=true ; base_rev= ;;
800				(--index) index=true ;;
801				(--reverse|-R) reverse=true ;;
802			esac
803		done
804		if ! $reverse && ! $index_only; then
805			echo "fail %{git apply on buffer contents doesn't make sense without --reverse or --cached}"
806			exit
807		fi
808		cd_bufdir || exit
809		num_inserted=0
810		num_deleted=0
811		for selection_desc in $kak_selections_desc; do {
812			IFS=' .,' read anchor_line _ cursor_line _ <<-EOF
813				$selection_desc
814		EOF
815			if [ $anchor_line -lt $cursor_line ]; then
816				min_line=$anchor_line
817				max_line=$cursor_line
818			else
819				min_line=$cursor_line
820				max_line=$anchor_line
821			fi
822			intended_diff='diff_buffer_against_rev "$base_rev" -u'
823			if $index; then {
824				git update-index --refresh "${kak_buffile}" >/dev/null
825				intended_diff='git diff --no-ext-diff HEAD -- ":(literal)${kak_buffile}"'
826			} elif $index_only && $reverse; then {
827				diff=$(eval "$intended_diff")
828				if [ -n "$diff" ]; then {
829					# Convert from buffile lines to index lines.
830					for line in min_line max_line; do {
831						if ! index_line_or_error_message=$(
832							eval file_line=\$$line
833							printf %s "$diff" |
834								perl "${kak_runtime}/rc/filetype/diff-parse.pl" \
835									BEGIN '
836										$in_file = ""; # no need to check filename, there is only one
837										$in_file_line = '"$file_line"';
838									' END '
839										$other_file_line++ if $diff_line_text =~ m{^\+};
840										$other_file_line += $in_file_line - $file_line;
841										print "$other_file_line\n";
842									'
843						); then
844							echo fail "git apply: $index_line_or_error_message"
845							exit
846						fi
847						eval $line=$index_line_or_error_message
848					} done
849				} fi
850				intended_diff='git diff --no-ext-diff --cached -- ":(literal)${kak_buffile}"'
851			} fi
852			diff=$(eval "$intended_diff" |
853					perl "${kak_runtime}"/rc/tools/patch-range.pl -line-numbers-from-new-file \
854						$min_line $max_line sh -c cat -- "$@" # forward any --reverse arg
855				   printf .) # avoid stripping newline
856			diff=${diff%.}
857			if ! printf %s "$diff" | git apply "$@"; then
858				printf >&2 "git apply: error running:\n\$ git apply %s << EOF\n" "$*"
859				printf >&2 %s "$diff"
860				printf >&2 'EOF\n'
861				echo "fail 'git apply: failed to apply selections, see *debug* buffer'"
862				exit
863			fi
864			count() {
865				printf %s "$diff" | awk '
866					BEGIN { n = 0 }
867					/^@@/,/^$/ { if ($0 ~ /^'"$1"'/) { n++ } }
868					END { print n }'
869			}
870			num_inserted=$(( $num_inserted + $(count +) ))
871			num_deleted=$(( $num_deleted + $(count -) ))
872		} done
873		if ! $index_only && ! $kak_modified; then
874			echo edit!
875			echo git update-diff
876		else
877			update_diff
878		fi
879		msg=
880		case $index_only,$reverse,$index in
881			(true,false,*) msg=Staged ;;
882			(true,true,*) msg=Unstaged ;;
883			(false,true,false) msg=Reverted ;;
884			(false,true,true) msg='Unstaged and reverted' ;;
885		esac
886		case $num_inserted,$num_deleted in
887			(*,0) msg="$msg $num_inserted inserted line(s)";;
888			(0,*) msg="$msg $num_deleted deleted line(s)";;
889			(*,*) msg="$msg $num_inserted inserted and $num_deleted deleted lines";;
890		esac
891		echo "echo -markup '{Information}{\\}$msg'"
892	}
893
894	case "$1" in
895		apply)
896			shift
897			apply_selections "$@"
898			;;
899		show|show-branch|log|diff|status)
900			show_git_cmd_output "$@"
901			;;
902		blame)
903			shift
904			blame_toggle "$@"
905			;;
906		blame-jump)
907			blame_jump
908			;;
909		hide-blame)
910			hide_blame
911			;;
912		show-diff)
913			echo 'try %{ add-highlighter window/git-diff flag-lines Default git_diff_flags }'
914			update_diff
915			;;
916		hide-diff)
917			echo 'try %{ remove-highlighter window/git-diff }'
918			;;
919		update-diff) update_diff ;;
920		next-hunk) jump_hunk next ;;
921		prev-hunk) jump_hunk prev ;;
922		commit)
923			shift
924			commit "$@"
925			;;
926		init)
927			shift
928			git init "$@" > /dev/null 2>&1
929			;;
930		add|rm)
931			cmd="$1"
932			shift
933			run_git_cmd $cmd "${@:-"${kak_buffile}"}"
934			;;
935		reset|checkout)
936			run_git_cmd "$@"
937			;;
938		grep)
939			shift
940			enquoted="$(printf '"%s" ' "$@")"
941			printf %s "try %{
942				set-option current grepcmd 'git grep -n --column'
943				grep $enquoted
944				set-option current grepcmd '$kak_opt_grepcmd'
945			}"
946			;;
947		edit)
948			shift
949			enquoted="$(printf '"%s" ' "$@")"
950			printf %s "edit -existing -- $enquoted"
951			;;
952		push)
953			run_git_cmd "$@"
954			;;
955		*)
956			printf "fail unknown git command '%s'\n" "$1"
957			exit
958			;;
959	esac
960}}
961
962# Works within :git diff and :git show
963define-command git-diff-goto-source \
964	-docstring 'Navigate to source by pressing the enter key in hunks when git diff is displayed. Works within :git diff and :git show' %{
965	require-module diff
966	diff-jump %sh{ git rev-parse --show-toplevel }
967}