rc git-tools: Change the CWD before running some git commands

When opening buffers from a directory that's not under versioning
(or even from a different repository), `git` commands will not work.

This commit temporarily changes the current working directory to that
of the current buffer to make sure the context is right.
This commit is contained in:
Frank LENORMAND 2018-08-12 09:41:45 +03:00
parent 983a8f759a
commit 55198032d2

View File

@ -39,6 +39,14 @@ Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-
fi fi
} \ } \
git %{ evaluate-commands %sh{ git %{ evaluate-commands %sh{
cd_bufdir() {
dirname_buffer="${kak_buffile%/*}"
cd "${dirname_buffer}" 2>/dev/null || {
printf 'echo -markup {Error}Unable to change the current working directory to: %s' "${dirname_buffer}"
exit 1
}
}
show_git_cmd_output() { show_git_cmd_output() {
local filetype local filetype
case "$1" in case "$1" in
@ -61,12 +69,13 @@ Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-
} }
run_git_blame() { run_git_blame() {
cd_bufdir
( (
printf %s "evaluate-commands -client '$kak_client' %{ printf %s "evaluate-commands -client '$kak_client' %{
try %{ add-highlighter window/git-blame flag-lines GitBlame git_blame_flags } try %{ add-highlighter window/git-blame flag-lines GitBlame git_blame_flags }
set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp' set-option 'buffer=${kak_bufname}' git_blame_flags '$kak_timestamp'
}" | kak -p ${kak_session} }" | kak -p "${kak_session}"
git blame "$@" --incremental ${kak_buffile} | awk ' git blame "$@" --incremental "${kak_buffile}" | awk '
function send_flags(text, flag, i) { function send_flags(text, flag, i) {
if (line == "") { return; } if (line == "") { return; }
text=substr(sha,1,8) " " dates[sha] " " authors[sha] text=substr(sha,1,8) " " dates[sha] " " authors[sha]
@ -97,6 +106,7 @@ Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-
} }
update_diff() { update_diff() {
cd_bufdir
git --no-pager diff -U0 "$kak_buffile" | perl -e ' git --no-pager diff -U0 "$kak_buffile" | perl -e '
$flags = $ENV{"kak_timestamp"}; $flags = $ENV{"kak_timestamp"};
foreach $line (<STDIN>) { foreach $line (<STDIN>) {