2017-11-03 10:34:41 +03:00
|
|
|
declare-option -docstring "name of the client in which documentation is to be displayed" \
|
2017-05-16 14:35:43 +03:00
|
|
|
str docsclient
|
2013-03-29 22:37:35 +04:00
|
|
|
|
2016-09-25 16:15:07 +03:00
|
|
|
hook -group git-log-highlight global WinSetOption filetype=git-log %{
|
2018-08-08 20:11:55 +03:00
|
|
|
add-highlighter window/git-log group
|
2020-01-29 15:48:08 +03:00
|
|
|
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
|
2018-07-07 02:51:18 +03:00
|
|
|
add-highlighter window/git-log/ ref diff # highlight potential diffs from the -p option
|
2018-11-28 12:39:18 +03:00
|
|
|
|
2018-12-11 02:11:35 +03:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-log }
|
2013-04-22 21:35:22 +04:00
|
|
|
}
|
2013-03-29 22:37:35 +04:00
|
|
|
|
2016-09-25 16:15:07 +03:00
|
|
|
hook -group git-status-highlight global WinSetOption filetype=git-status %{
|
2018-08-08 20:11:55 +03:00
|
|
|
add-highlighter window/git-status group
|
2018-07-07 02:51:18 +03:00
|
|
|
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
|
2018-11-28 12:39:18 +03:00
|
|
|
|
2018-12-11 02:11:35 +03:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/git-status }
|
2014-04-24 01:21:40 +04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
declare-option -hidden line-specs git_blame_flags
|
|
|
|
declare-option -hidden line-specs git_diff_flags
|
2019-11-13 18:52:29 +03:00
|
|
|
declare-option -hidden int-list git_hunk_list
|
2013-04-22 21:35:22 +04:00
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -params 1.. \
|
2018-05-07 00:29:52 +03:00
|
|
|
-docstring %sh{printf 'git [<arguments>]: git wrapping helper
|
2016-10-11 10:03:41 +03:00
|
|
|
All the optional arguments are forwarded to the git utility
|
2019-11-13 18:52:29 +03:00
|
|
|
Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-blame\n hide-diff\n init\n log\n next-hunk\n previous-hunk\n show\n show-diff\n status\n update-diff'} \
|
2018-09-27 00:42:01 +03:00
|
|
|
-shell-script-candidates %{
|
2018-04-21 05:43:59 +03:00
|
|
|
if [ $kak_token_to_complete -eq 0 ]; then
|
2019-11-13 18:52:29 +03:00
|
|
|
printf "add\nrm\nblame\ncommit\ncheckout\ndiff\nhide-blame\nhide-diff\nlog\nnext-hunk\nprev-hunk\nshow\nshow-diff\ninit\nstatus\nupdate-diff\n"
|
2018-04-21 05:43:59 +03:00
|
|
|
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 ;;
|
|
|
|
rm) git ls-files -c ;;
|
|
|
|
esac
|
|
|
|
fi
|
2014-08-14 22:42:24 +04:00
|
|
|
} \
|
2018-05-07 00:29:52 +03:00
|
|
|
git %{ evaluate-commands %sh{
|
2018-08-12 09:41:45 +03:00
|
|
|
cd_bufdir() {
|
|
|
|
dirname_buffer="${kak_buffile%/*}"
|
|
|
|
cd "${dirname_buffer}" 2>/dev/null || {
|
2019-11-14 10:32:55 +03:00
|
|
|
printf 'fail Unable to change the current working directory to: %s\n' "${dirname_buffer}"
|
2018-08-12 09:41:45 +03:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 21:35:22 +04:00
|
|
|
show_git_cmd_output() {
|
|
|
|
local filetype
|
|
|
|
case "$1" in
|
2013-04-25 20:52:06 +04:00
|
|
|
show|diff) filetype=diff ;;
|
2013-04-22 21:35:22 +04:00
|
|
|
log) filetype=git-log ;;
|
2014-04-24 01:21:40 +04:00
|
|
|
status) filetype=git-status ;;
|
2013-04-22 21:35:22 +04:00
|
|
|
esac
|
2017-06-09 14:05:31 +03:00
|
|
|
output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-git.XXXXXXXX)/fifo
|
2014-05-06 22:39:24 +04:00
|
|
|
mkfifo ${output}
|
2019-06-10 19:00:11 +03:00
|
|
|
( git "$@" > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
|
2014-05-06 22:39:24 +04:00
|
|
|
|
2018-04-27 12:44:21 +03:00
|
|
|
printf %s "evaluate-commands -try-client '$kak_opt_docsclient' %{
|
2014-05-06 22:39:24 +04:00
|
|
|
edit! -fifo ${output} *git*
|
2017-11-03 10:34:41 +03:00
|
|
|
set-option buffer filetype '${filetype}'
|
2018-08-19 01:04:31 +03:00
|
|
|
hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } }
|
2014-05-06 22:39:24 +04:00
|
|
|
}"
|
2013-04-22 21:35:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
run_git_blame() {
|
|
|
|
(
|
2019-09-22 02:29:19 +03:00
|
|
|
cd_bufdir
|
2017-11-03 11:09:45 +03:00
|
|
|
printf %s "evaluate-commands -client '$kak_client' %{
|
2018-08-09 12:27:40 +03:00
|
|
|
try %{ add-highlighter window/git-blame flag-lines Information git_blame_flags }
|
2017-11-03 10:34:41 +03:00
|
|
|
set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp'
|
2014-03-02 06:05:38 +04:00
|
|
|
}" | kak -p ${kak_session}
|
2016-06-21 21:00:41 +03:00
|
|
|
git blame "$@" --incremental ${kak_buffile} | awk '
|
2014-03-25 13:25:37 +04:00
|
|
|
function send_flags(text, flag, i) {
|
|
|
|
if (line == "") { return; }
|
|
|
|
text=substr(sha,1,8) " " dates[sha] " " authors[sha]
|
2015-08-23 14:13:14 +03:00
|
|
|
# gsub("|", "\\|", text)
|
2018-05-30 16:28:50 +03:00
|
|
|
gsub("~", "~~", text)
|
|
|
|
flag="%~" line "|" text "~"
|
2014-03-25 13:25:37 +04:00
|
|
|
for ( i=1; i < count; i++ ) {
|
2018-05-30 16:28:50 +03:00
|
|
|
flag=flag " %~" line+i "|" text "~"
|
2014-03-25 13:25:37 +04:00
|
|
|
}
|
|
|
|
cmd = "kak -p " ENVIRON["kak_session"]
|
2018-05-30 16:28:50 +03:00
|
|
|
print "set-option -add buffer=" ENVIRON["kak_bufname"] " git_blame_flags " flag | cmd
|
2014-03-25 13:25:37 +04:00
|
|
|
close(cmd)
|
|
|
|
}
|
2019-05-02 22:27:25 +03:00
|
|
|
/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/ {
|
2014-03-25 13:25:37 +04:00
|
|
|
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]
|
2018-06-19 00:43:13 +03:00
|
|
|
close(cmd)
|
2014-03-25 13:25:37 +04:00
|
|
|
}
|
|
|
|
END { send_flags(); }'
|
|
|
|
) > /dev/null 2>&1 < /dev/null &
|
2013-04-22 21:35:22 +04:00
|
|
|
}
|
2013-04-12 01:09:42 +04:00
|
|
|
|
2019-10-09 09:39:58 +03:00
|
|
|
run_git_cmd() {
|
|
|
|
if git "${@}" > /dev/null 2>&1; then
|
|
|
|
printf %s "echo -markup '{Information}git $1 succeeded'"
|
|
|
|
else
|
2019-11-14 10:32:55 +03:00
|
|
|
printf 'fail git %s failed\n' "$1"
|
2019-10-09 09:39:58 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-04-22 21:35:22 +04:00
|
|
|
update_diff() {
|
2019-09-22 02:29:19 +03:00
|
|
|
(
|
|
|
|
cd_bufdir
|
|
|
|
git --no-pager diff -U0 "$kak_buffile" | perl -e '
|
2018-07-01 03:06:47 +03:00
|
|
|
$flags = $ENV{"kak_timestamp"};
|
|
|
|
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;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $line|\{green\}+";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($from_count > 0 and $to_count == 0) {
|
|
|
|
if ($to_line == 0) {
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " 1|\{red\}‾";
|
2018-07-01 03:06:47 +03:00
|
|
|
} else {
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $to_line|\{red\}_";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($from_count > 0 and $from_count == $to_count) {
|
|
|
|
for $i (0..$to_count - 1) {
|
|
|
|
$line = $to_line + $i;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $line|\{blue\}~";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($from_count > 0 and $from_count < $to_count) {
|
|
|
|
for $i (0..$from_count - 1) {
|
|
|
|
$line = $to_line + $i;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $line|\{blue\}~";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
for $i ($from_count..$to_count - 1) {
|
|
|
|
$line = $to_line + $i;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $line|\{green\}+";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($to_count > 0 and $from_count > $to_count) {
|
|
|
|
for $i (0..$to_count - 2) {
|
|
|
|
$line = $to_line + $i;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $line|\{blue\}~";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
$last = $to_line + $to_count - 1;
|
2018-05-30 16:28:50 +03:00
|
|
|
$flags .= " $last|\{blue+u\}~";
|
2018-07-01 03:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 13:25:37 +04:00
|
|
|
}
|
2018-07-01 03:06:47 +03:00
|
|
|
print "set-option buffer git_diff_flags $flags"
|
2019-09-15 22:29:33 +03:00
|
|
|
' )
|
2013-04-22 21:35:22 +04:00
|
|
|
}
|
2013-04-17 21:15:15 +04:00
|
|
|
|
2019-11-13 18:52:29 +03:00
|
|
|
jump_hunk() {
|
|
|
|
direction=$1
|
|
|
|
set -- ${kak_opt_git_diff_flags}
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
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"
|
|
|
|
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
|
|
|
|
|
|
|
|
if [ "$direction" = "next" ] && [ -n "$next_hunk" ]; then
|
|
|
|
echo "select $next_hunk.1,$next_hunk.1"
|
|
|
|
elif [ "$direction" = "prev" ] && [ -n "$prev_hunk" ]; then
|
|
|
|
echo "select $prev_hunk.1,$prev_hunk.1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-01-15 22:58:43 +03:00
|
|
|
commit() {
|
2015-01-21 16:43:00 +03:00
|
|
|
# Handle case where message needs not to be edited
|
2018-04-21 05:43:59 +03:00
|
|
|
if grep -E -q -e "-m|-F|-C|--message=.*|--file=.*|--reuse-message=.*|--no-edit|--fixup.*|--squash.*"; then
|
2015-01-21 16:43:00 +03:00
|
|
|
if git commit "$@" > /dev/null 2>&1; then
|
2017-07-19 18:18:52 +03:00
|
|
|
echo 'echo -markup "{Information}Commit succeeded"'
|
2015-01-21 16:43:00 +03:00
|
|
|
else
|
2019-11-14 10:32:55 +03:00
|
|
|
echo 'fail Commit failed'
|
2015-01-21 16:43:00 +03:00
|
|
|
fi
|
|
|
|
exit
|
|
|
|
fi <<-EOF
|
|
|
|
$@
|
|
|
|
EOF
|
|
|
|
|
2015-01-15 22:58:43 +03:00
|
|
|
# fails, and generate COMMIT_EDITMSG
|
2016-10-24 22:45:57 +03:00
|
|
|
GIT_EDITOR='' EDITOR='' git commit "$@" > /dev/null 2>&1
|
2015-01-15 22:58:43 +03:00
|
|
|
msgfile="$(git rev-parse --git-dir)/COMMIT_EDITMSG"
|
2016-05-09 15:52:54 +03:00
|
|
|
printf %s "edit '$msgfile'
|
2018-05-07 00:29:52 +03:00
|
|
|
hook buffer BufWritePost '.*\Q$msgfile\E' %{ evaluate-commands %sh{
|
2018-07-04 12:42:03 +03:00
|
|
|
if git commit -F '$msgfile' --cleanup=strip $* > /dev/null; then
|
2017-11-03 11:09:45 +03:00
|
|
|
printf %s 'evaluate-commands -client $kak_client echo -markup %{{Information}Commit succeeded}; delete-buffer'
|
2015-01-15 22:58:43 +03:00
|
|
|
else
|
2019-11-14 10:32:55 +03:00
|
|
|
printf 'evaluate-commands -client %s fail Commit failed\n' "$kak_client"
|
2015-01-15 22:58:43 +03:00
|
|
|
fi
|
|
|
|
} }"
|
|
|
|
}
|
|
|
|
|
2013-04-22 21:35:22 +04:00
|
|
|
case "$1" in
|
2019-10-09 09:39:58 +03:00
|
|
|
show|log|diff|status)
|
|
|
|
show_git_cmd_output "$@"
|
|
|
|
;;
|
|
|
|
blame)
|
|
|
|
shift
|
|
|
|
run_git_blame "$@"
|
|
|
|
;;
|
|
|
|
hide-blame)
|
2016-05-09 15:52:54 +03:00
|
|
|
printf %s "try %{
|
2018-08-08 20:11:55 +03:00
|
|
|
set-option buffer=$kak_bufname git_blame_flags $kak_timestamp
|
2018-07-07 02:51:18 +03:00
|
|
|
remove-highlighter window/git-blame
|
2015-05-10 19:20:42 +03:00
|
|
|
}"
|
|
|
|
;;
|
2019-10-09 09:39:58 +03:00
|
|
|
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 ;;
|
2019-11-13 18:52:29 +03:00
|
|
|
next-hunk) jump_hunk next ;;
|
|
|
|
prev-hunk) jump_hunk prev ;;
|
2019-10-09 09:39:58 +03:00
|
|
|
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 "$@"
|
|
|
|
;;
|
|
|
|
*)
|
2019-11-14 10:32:55 +03:00
|
|
|
printf "fail unknown git command '%s'\n" "$1"
|
2019-10-09 09:39:58 +03:00
|
|
|
exit
|
|
|
|
;;
|
2013-04-22 21:35:22 +04:00
|
|
|
esac
|
2013-04-12 01:09:42 +04:00
|
|
|
}}
|