2021-06-05 18:21:05 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
source $current_dir/utils.sh
|
2021-06-05 18:21:05 +02:00
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
IFS=' ' read -r -a hide_status <<< $(get_tmux_option "@dracula-git-disable-status" "false")
|
|
|
|
IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@dracula-git-show-current-symbol" "✓")
|
|
|
|
IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@dracula-git-show-diff-symbol" "!")
|
|
|
|
IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@dracula-git-no-repo-message" "")
|
2022-09-05 15:47:55 +02:00
|
|
|
IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@dracula-git-no-untracked-files" "false")
|
2022-09-20 14:29:45 +02:00
|
|
|
IFS=' ' read -r -a show_remote_status <<< $(get_tmux_option "@dracula-git-show-remote-status" "false")
|
2021-07-27 04:57:16 +02:00
|
|
|
|
|
|
|
# Get added, modified, updated and deleted files from git status
|
|
|
|
getChanges()
|
|
|
|
{
|
|
|
|
declare -i added=0;
|
|
|
|
declare -i modified=0;
|
|
|
|
declare -i updated=0;
|
|
|
|
declare -i deleted=0;
|
|
|
|
|
2022-09-05 15:57:11 +02:00
|
|
|
for i in $(git -C $path --no-optional-locks status -s)
|
2021-07-27 04:57:16 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
case $i in
|
|
|
|
'A')
|
|
|
|
added+=1
|
|
|
|
;;
|
|
|
|
'M')
|
|
|
|
modified+=1
|
|
|
|
;;
|
|
|
|
'U')
|
|
|
|
updated+=1
|
|
|
|
;;
|
|
|
|
'D')
|
|
|
|
deleted+=1
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
output=""
|
|
|
|
[ $added -gt 0 ] && output+="${added}A"
|
|
|
|
[ $modified -gt 0 ] && output+=" ${modified}M"
|
|
|
|
[ $updated -gt 0 ] && output+=" ${updated}U"
|
|
|
|
[ $deleted -gt 0 ] && output+=" ${deleted}D"
|
|
|
|
|
|
|
|
echo $output
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# getting the #{pane_current_path} from dracula.sh is no longer possible
|
|
|
|
getPaneDir()
|
|
|
|
{
|
|
|
|
nextone="false"
|
|
|
|
for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}");
|
|
|
|
do
|
|
|
|
if [ "$nextone" == "true" ]; then
|
|
|
|
echo $i
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ "$i" == "1" ]; then
|
|
|
|
nextone="true"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# check if the current or diff symbol is empty to remove ugly padding
|
|
|
|
checkEmptySymbol()
|
|
|
|
{
|
|
|
|
symbol=$1
|
|
|
|
if [ "$symbol" == "" ]; then
|
|
|
|
echo "true"
|
|
|
|
else
|
|
|
|
echo "false"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# check to see if the current repo is not up to date with HEAD
|
2021-06-05 18:21:05 +02:00
|
|
|
checkForChanges()
|
|
|
|
{
|
2022-09-05 15:47:55 +02:00
|
|
|
[ $no_untracked_files == "false" ] && no_untracked="" || no_untracked="-uno"
|
2021-06-05 18:21:05 +02:00
|
|
|
if [ "$(checkForGitDir)" == "true" ]; then
|
2022-09-05 15:57:11 +02:00
|
|
|
if [ "$(git -C $path --no-optional-locks status -s $no_untracked)" != "" ]; then
|
2021-06-05 18:21:05 +02:00
|
|
|
echo "true"
|
|
|
|
else
|
|
|
|
echo "false"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "false"
|
|
|
|
fi
|
2021-07-27 04:57:16 +02:00
|
|
|
}
|
2021-06-05 18:21:05 +02:00
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
# check if a git repo exists in the directory
|
2021-06-05 18:21:05 +02:00
|
|
|
checkForGitDir()
|
|
|
|
{
|
|
|
|
if [ "$(git -C $path rev-parse --abbrev-ref HEAD)" != "" ]; then
|
|
|
|
echo "true"
|
|
|
|
else
|
|
|
|
echo "false"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
# return branch name if there is one
|
|
|
|
getBranch()
|
|
|
|
{
|
|
|
|
if [ $(checkForGitDir) == "true" ]; then
|
|
|
|
echo $(git -C $path rev-parse --abbrev-ref HEAD)
|
2021-06-05 18:21:05 +02:00
|
|
|
else
|
2021-07-27 04:57:16 +02:00
|
|
|
echo $no_repo_message
|
2021-06-05 18:21:05 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-09-20 14:29:45 +02:00
|
|
|
getRemoteInfo()
|
|
|
|
{
|
|
|
|
base=$(git -C $path for-each-ref --format='%(upstream:short) %(upstream:track)' "$(git -C $path symbolic-ref -q HEAD)")
|
|
|
|
remote=$(echo "$base" | cut -d" " -f1)
|
|
|
|
out=""
|
|
|
|
|
|
|
|
if [ -n "$remote" ]; then
|
|
|
|
out="...$remote"
|
|
|
|
ahead=$(echo "$base" | grep -E -o 'ahead[ [:digit:]]+' | cut -d" " -f2)
|
|
|
|
behind=$(echo "$base" | grep -E -o 'behind[ [:digit:]]+' | cut -d" " -f2)
|
|
|
|
|
|
|
|
[ -n "$ahead" ] && out+=" +$ahead"
|
|
|
|
[ -n "$behind" ] && out+=" -$behind"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$out"
|
|
|
|
}
|
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
# return the final message for the status bar
|
|
|
|
getMessage()
|
|
|
|
{
|
|
|
|
if [ $(checkForGitDir) == "true" ]; then
|
|
|
|
branch="$(getBranch)"
|
2022-09-20 14:29:45 +02:00
|
|
|
output=""
|
|
|
|
|
2021-07-27 04:57:16 +02:00
|
|
|
if [ $(checkForChanges) == "true" ]; then
|
|
|
|
|
|
|
|
changes="$(getChanges)"
|
|
|
|
|
|
|
|
if [ "${hide_status}" == "false" ]; then
|
|
|
|
if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "${changes} $branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
else
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "$diff_symbol ${changes} $branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "$branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
else
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "$diff_symbol $branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
if [ $(checkEmptySymbol $current_symbol) == "true" ]; then
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "$branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
else
|
2022-09-20 14:29:45 +02:00
|
|
|
output=$(echo "$current_symbol $branch")
|
2021-07-27 04:57:16 +02:00
|
|
|
fi
|
|
|
|
fi
|
2022-09-20 14:29:45 +02:00
|
|
|
|
|
|
|
[ "$show_remote_status" == "true" ] && output+=$(getRemoteInfo)
|
|
|
|
echo "$output"
|
2021-06-05 18:21:05 +02:00
|
|
|
else
|
2021-07-27 04:57:16 +02:00
|
|
|
echo $no_repo_message
|
2021-06-05 18:21:05 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
2021-07-27 04:57:16 +02:00
|
|
|
path=$(getPaneDir)
|
|
|
|
getMessage
|
2021-06-05 18:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#run main driver program
|
|
|
|
main
|