2017-11-03 08:34:41 +01:00
|
|
|
declare-option -docstring "name of the client in which documentation is to be displayed" \
|
2017-05-16 13:35:43 +02:00
|
|
|
str docsclient
|
2013-03-29 19:37:35 +01:00
|
|
|
|
2016-09-25 15:15:07 +02:00
|
|
|
hook -group git-log-highlight global WinSetOption filetype=git-log %{
|
2017-10-28 05:00:51 +02:00
|
|
|
add-highlighter window group git-log-highlight
|
|
|
|
add-highlighter window/git-log-highlight regex '^(commit) ([0-9a-f]+)$' 1:yellow 2:red
|
|
|
|
add-highlighter window/git-log-highlight regex '^([a-zA-Z_-]+:) (.*?)$' 1:green 2:magenta
|
|
|
|
add-highlighter window/git-log-highlight ref diff # highlight potential diffs from the -p option
|
2013-04-22 19:35:22 +02:00
|
|
|
}
|
2013-03-29 19:37:35 +01:00
|
|
|
|
2017-10-28 05:00:51 +02:00
|
|
|
hook -group git-log-highlight global WinSetOption filetype=(?!git-log).* %{ remove-highlighter window/git-log-highlight }
|
2013-03-31 14:52:09 +02:00
|
|
|
|
2016-09-25 15:15:07 +02:00
|
|
|
hook -group git-status-highlight global WinSetOption filetype=git-status %{
|
2017-10-28 05:00:51 +02:00
|
|
|
add-highlighter window group git-status-highlight
|
|
|
|
add-highlighter window/git-status-highlight regex '^\h+(?:((?:both )?modified:)|(added:|new file:)|(deleted(?: by \w+)?:)|(renamed:)|(copied:))(?:.*?)$' 1:yellow 2:green 3:red 4:cyan 5:blue 6:magenta
|
2014-04-23 23:21:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-28 05:00:51 +02:00
|
|
|
hook -group git-status-highlight global WinSetOption filetype=(?!git-status).* %{ remove-highlighter window/git-status-highlight }
|
2014-04-23 23:21:40 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden line-specs git_blame_flags
|
|
|
|
declare-option -hidden line-specs git_diff_flags
|
2013-04-22 19:35:22 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
set-face GitBlame default,magenta
|
|
|
|
set-face GitDiffFlags default,black
|
2015-08-23 13:13:14 +02:00
|
|
|
|
2017-11-03 08:34:41 +01:00
|
|
|
define-command -params 1.. \
|
2016-10-11 09:03:41 +02:00
|
|
|
-docstring %sh{printf '%%{git [<arguments>]: git wrapping helper
|
|
|
|
All the optional arguments are forwarded to the git utility
|
|
|
|
Available commands:\n-add\n-rm\n-blame\n-commit\n-checkout\n-diff\n-hide-blame\n-log\n-show\n-show-diff\n-status\n-update-diff}'} \
|
2016-08-30 02:01:18 +02:00
|
|
|
-shell-candidates %{
|
|
|
|
[ $kak_token_to_complete -eq 0 ] &&
|
|
|
|
printf "add\nrm\nblame\ncommit\ncheckout\ndiff\nhide-blame\nlog\nshow\nshow-diff\nstatus\nupdate-diff\n"
|
2014-08-14 20:42:24 +02:00
|
|
|
} \
|
2014-04-23 23:21:40 +02:00
|
|
|
git %{ %sh{
|
2013-04-22 19:35:22 +02:00
|
|
|
show_git_cmd_output() {
|
|
|
|
local filetype
|
|
|
|
case "$1" in
|
2013-04-25 18:52:06 +02:00
|
|
|
show|diff) filetype=diff ;;
|
2013-04-22 19:35:22 +02:00
|
|
|
log) filetype=git-log ;;
|
2014-04-23 23:21:40 +02:00
|
|
|
status) filetype=git-status ;;
|
2013-04-22 19:35:22 +02:00
|
|
|
esac
|
2017-06-09 13:05:31 +02:00
|
|
|
output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-git.XXXXXXXX)/fifo
|
2014-05-06 20:39:24 +02:00
|
|
|
mkfifo ${output}
|
|
|
|
( git "$@" > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null &
|
|
|
|
|
2016-05-09 14:52:54 +02:00
|
|
|
printf %s "eval -try-client '$kak_opt_docsclient' %{
|
2014-05-06 20:39:24 +02:00
|
|
|
edit! -fifo ${output} *git*
|
2017-11-03 08:34:41 +01:00
|
|
|
set-option buffer filetype '${filetype}'
|
2015-02-20 19:43:09 +01:00
|
|
|
hook -group fifo buffer BufCloseFifo .* %{
|
|
|
|
nop %sh{ rm -r $(dirname ${output}) }
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks buffer fifo
|
2015-02-20 19:43:09 +01:00
|
|
|
}
|
2014-05-06 20:39:24 +02:00
|
|
|
}"
|
2013-04-22 19:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run_git_blame() {
|
|
|
|
(
|
2016-05-09 14:52:54 +02:00
|
|
|
printf %s "eval -client '$kak_client' %{
|
2017-10-28 05:00:51 +02:00
|
|
|
try %{ add-highlighter window flag_lines GitBlame git_blame_flags }
|
2017-11-03 08:34:41 +01:00
|
|
|
set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp'
|
2014-03-02 03:05:38 +01:00
|
|
|
}" | kak -p ${kak_session}
|
2016-06-21 20:00:41 +02:00
|
|
|
git blame "$@" --incremental ${kak_buffile} | awk '
|
2014-03-25 10:25:37 +01:00
|
|
|
function send_flags(text, flag, i) {
|
|
|
|
if (line == "") { return; }
|
|
|
|
text=substr(sha,1,8) " " dates[sha] " " authors[sha]
|
|
|
|
gsub(":", "\\:", text)
|
2015-08-23 13:13:14 +02:00
|
|
|
# gsub("|", "\\|", text)
|
2015-12-13 00:16:07 +01:00
|
|
|
flag=line "|" text
|
2014-03-25 10:25:37 +01:00
|
|
|
for ( i=1; i < count; i++ ) {
|
2015-12-13 00:16:07 +01:00
|
|
|
flag=flag ":" line+i "|" text
|
2014-03-25 10:25:37 +01:00
|
|
|
}
|
|
|
|
cmd = "kak -p " ENVIRON["kak_session"]
|
2017-11-03 08:34:41 +01:00
|
|
|
print "set-option -add buffer=" ENVIRON["kak_bufname"] " git_blame_flags %{" flag "}" | cmd
|
2014-03-25 10:25:37 +01:00
|
|
|
close(cmd)
|
|
|
|
}
|
|
|
|
/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ {
|
|
|
|
send_flags()
|
|
|
|
sha=$1
|
|
|
|
line=$3
|
|
|
|
count=$4
|
|
|
|
}
|
|
|
|
/^author / { authors[sha]=substr($0,8) }
|
|
|
|
/^author-time ([0-9]*)/ {
|
|
|
|
cmd = "date -d @" $2 " +\"%F %T\""
|
|
|
|
cmd | getline dates[sha]
|
|
|
|
}
|
|
|
|
END { send_flags(); }'
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
2013-04-22 19:35:22 +02:00
|
|
|
}
|
2013-04-11 23:09:42 +02:00
|
|
|
|
2013-04-22 19:35:22 +02:00
|
|
|
update_diff() {
|
2016-06-21 20:00:41 +02:00
|
|
|
git diff -U0 $kak_buffile | awk '
|
2014-03-25 10:25:37 +01:00
|
|
|
BEGIN {
|
|
|
|
line=0
|
2015-12-12 12:45:45 +01:00
|
|
|
flags=ENVIRON["kak_timestamp"]
|
2014-03-25 10:25:37 +01:00
|
|
|
}
|
|
|
|
/^---.*/ {}
|
|
|
|
/^@@ -[0-9]+(,[0-9]+)? \+[0-9]+(,[0-9]+)? @@.*/ {
|
|
|
|
if ((x=index($3, ",")) > 0) {
|
2015-01-22 22:18:59 +01:00
|
|
|
line=substr($3, 2, x-2)
|
2014-03-25 10:25:37 +01:00
|
|
|
} else {
|
|
|
|
line=substr($3, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/^\+/ {
|
2015-12-13 00:16:07 +01:00
|
|
|
flags=flags ":" line "|{green}+"
|
2014-03-25 10:25:37 +01:00
|
|
|
line++
|
|
|
|
}
|
2015-12-13 00:16:07 +01:00
|
|
|
/^\-/ { flags=flags ":" line "|{red}-" }
|
2017-11-03 08:34:41 +01:00
|
|
|
END { print "set-option buffer git_diff_flags ", flags }
|
2014-03-25 10:25:37 +01:00
|
|
|
'
|
2013-04-22 19:35:22 +02:00
|
|
|
}
|
2013-04-17 19:15:15 +02:00
|
|
|
|
2015-01-15 20:58:43 +01:00
|
|
|
commit() {
|
2015-01-21 14:43:00 +01:00
|
|
|
# Handle case where message needs not to be edited
|
|
|
|
if grep -E -q -e "-m|-F|-C|--message=.*|--file=.*|--reuse-message=.*|--no-edit"; then
|
|
|
|
if git commit "$@" > /dev/null 2>&1; then
|
2017-07-19 17:18:52 +02:00
|
|
|
echo 'echo -markup "{Information}Commit succeeded"'
|
2015-01-21 14:43:00 +01:00
|
|
|
else
|
2017-07-19 17:18:52 +02:00
|
|
|
echo 'echo -markup "{Error}Commit failed"'
|
2015-01-21 14:43:00 +01:00
|
|
|
fi
|
|
|
|
exit
|
|
|
|
fi <<-EOF
|
|
|
|
$@
|
|
|
|
EOF
|
|
|
|
|
2015-01-15 20:58:43 +01:00
|
|
|
# fails, and generate COMMIT_EDITMSG
|
2016-10-24 21:45:57 +02:00
|
|
|
GIT_EDITOR='' EDITOR='' git commit "$@" > /dev/null 2>&1
|
2015-01-15 20:58:43 +01:00
|
|
|
msgfile="$(git rev-parse --git-dir)/COMMIT_EDITMSG"
|
2016-05-09 14:52:54 +02:00
|
|
|
printf %s "edit '$msgfile'
|
2015-01-15 20:58:43 +01:00
|
|
|
hook buffer BufWritePost '.*\Q$msgfile\E' %{ %sh{
|
|
|
|
if git commit -F '$msgfile' --cleanup=strip $@ > /dev/null; then
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s 'eval -client $kak_client echo -markup %{{Information}Commit succeeded}; delete-buffer'
|
2015-01-15 20:58:43 +01:00
|
|
|
else
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s 'eval -client $kak_client echo -markup %{{Error}Commit failed}'
|
2015-01-15 20:58:43 +01:00
|
|
|
fi
|
|
|
|
} }"
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:35:22 +02:00
|
|
|
case "$1" in
|
2014-04-23 23:21:40 +02:00
|
|
|
show|log|diff|status) show_git_cmd_output "$@" ;;
|
2014-11-25 14:52:41 +01:00
|
|
|
blame) shift; run_git_blame "$@" ;;
|
2015-05-10 18:20:42 +02:00
|
|
|
hide-blame)
|
2016-05-09 14:52:54 +02:00
|
|
|
printf %s "try %{
|
2017-11-03 08:34:41 +01:00
|
|
|
set-option buffer=$kak_bufname git_blame_flags ''
|
2017-10-28 05:00:51 +02:00
|
|
|
remove-highlighter window/hlflags_git_blame_flags
|
2015-05-10 18:20:42 +02:00
|
|
|
}"
|
|
|
|
;;
|
2013-04-22 19:35:22 +02:00
|
|
|
show-diff)
|
2017-10-28 05:00:51 +02:00
|
|
|
echo 'try %{ add-highlighter window flag_lines GitDiffFlags git_diff_flags }'
|
2013-04-22 19:35:22 +02:00
|
|
|
update_diff
|
|
|
|
;;
|
|
|
|
update-diff) update_diff ;;
|
2015-01-15 20:58:43 +01:00
|
|
|
commit) shift; commit "$@" ;;
|
2014-04-23 23:21:40 +02:00
|
|
|
checkout)
|
|
|
|
name="${2:-${kak_buffile}}"
|
2015-05-23 11:56:52 +02:00
|
|
|
git checkout "${name}" > /dev/null 2>&1
|
2014-04-23 23:21:40 +02:00
|
|
|
;;
|
2013-04-25 18:52:06 +02:00
|
|
|
add)
|
2014-03-02 02:05:01 +01:00
|
|
|
name="${2:-${kak_buffile}}"
|
2015-05-23 11:56:52 +02:00
|
|
|
if git add -- "${name}" > /dev/null 2>&1; then
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s "echo -markup '{Information}git: added ${name}'"
|
2013-04-25 18:52:06 +02:00
|
|
|
else
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s "echo -markup '{Error}git: unable to add ${name}'"
|
2013-04-25 18:52:06 +02:00
|
|
|
fi
|
|
|
|
;;
|
2015-05-23 11:56:52 +02:00
|
|
|
rm)
|
|
|
|
name="${2:-${kak_buffile}}"
|
|
|
|
if git rm -- "${name}" > /dev/null 2>&1; then
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s "echo -markup '{Information}git: removed ${name}'"
|
2015-05-23 11:56:52 +02:00
|
|
|
else
|
2017-07-19 17:18:52 +02:00
|
|
|
printf %s "echo -markup '{Error}git: unable to remove ${name}'"
|
2015-05-23 11:56:52 +02:00
|
|
|
fi
|
|
|
|
;;
|
2017-07-19 17:18:52 +02:00
|
|
|
*) printf %s "echo -markup %{{Error}unknown git command '$1'}"; exit ;;
|
2013-04-22 19:35:22 +02:00
|
|
|
esac
|
2013-04-11 23:09:42 +02:00
|
|
|
}}
|