tmux-kanagawa/scripts/git.sh
Nuno Sá e9f46f76ca scripts: git: add '--no-optional-locks' option
As stated in 'git status --help':

"By default, git status will automatically refresh the index, updating
the cached stat information from the working tree and writing out the
result. Writing out the updated index is an optimization that isn’t
strictly necessary (status computes the values for itself, but writing
them out is just to save subsequent programs from repeating our
computation). When status is run in the background, the lock held during
the write may conflict with other simultaneous processes, causing them to
fail. Scripts running status in the background should consider using git
--no-optional-locks status (see git(1) for details)."

This was actually happen during rebasing whith the following error:

"error: Unable to create '/home/nsa/work/linux/.git/index.lock': File
exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue."

This change fixes the above...

Signed-off-by: Nuno Sá <noname.nuno@gmail.com>
2022-09-06 14:36:47 +02:00

156 lines
3.7 KiB
Bash
Executable file

#!/usr/bin/env bash
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
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" "")
IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@dracula-git-no-untracked-files" "false")
# 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;
for i in $(git -C $path --no-optional-locks status -s)
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
checkForChanges()
{
[ $no_untracked_files == "false" ] && no_untracked="" || no_untracked="-uno"
if [ "$(checkForGitDir)" == "true" ]; then
if [ "$(git -C $path --no-optional-locks status -s $no_untracked)" != "" ]; then
echo "true"
else
echo "false"
fi
else
echo "false"
fi
}
# check if a git repo exists in the directory
checkForGitDir()
{
if [ "$(git -C $path rev-parse --abbrev-ref HEAD)" != "" ]; then
echo "true"
else
echo "false"
fi
}
# return branch name if there is one
getBranch()
{
if [ $(checkForGitDir) == "true" ]; then
echo $(git -C $path rev-parse --abbrev-ref HEAD)
else
echo $no_repo_message
fi
}
# return the final message for the status bar
getMessage()
{
if [ $(checkForGitDir) == "true" ]; then
branch="$(getBranch)"
if [ $(checkForChanges) == "true" ]; then
changes="$(getChanges)"
if [ "${hide_status}" == "false" ]; then
if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then
echo "${changes} $branch"
else
echo "$diff_symbol ${changes} $branch"
fi
else
if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then
echo "$branch"
else
echo "$diff_symbol $branch"
fi
fi
else
if [ $(checkEmptySymbol $current_symbol) == "true" ]; then
echo "$branch"
else
echo "$current_symbol $branch"
fi
fi
else
echo $no_repo_message
fi
}
main()
{
path=$(getPaneDir)
getMessage
}
#run main driver program
main