git.sh
1 #!/usr/bin/env bash 2 3 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 source $current_dir/utils.sh 5 6 IFS=' ' read -r -a hide_status <<< $(get_tmux_option "@dracula-git-disable-status" "false") 7 IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@dracula-git-show-current-symbol" "✓") 8 IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@dracula-git-show-diff-symbol" "!") 9 IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@dracula-git-no-repo-message" "") 10 IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@dracula-git-no-untracked-files" "false") 11 IFS=' ' read -r -a show_remote_status <<< $(get_tmux_option "@dracula-git-show-remote-status" "false") 12 13 # Get added, modified, updated and deleted files from git status 14 getChanges() 15 { 16 declare -i added=0; 17 declare -i modified=0; 18 declare -i updated=0; 19 declare -i deleted=0; 20 21 for i in $(git -C $path --no-optional-locks status -s) 22 23 do 24 case $i in 25 'A') 26 added+=1 27 ;; 28 'M') 29 modified+=1 30 ;; 31 'U') 32 updated+=1 33 ;; 34 'D') 35 deleted+=1 36 ;; 37 38 esac 39 done 40 41 output="" 42 [ $added -gt 0 ] && output+="${added}A" 43 [ $modified -gt 0 ] && output+=" ${modified}M" 44 [ $updated -gt 0 ] && output+=" ${updated}U" 45 [ $deleted -gt 0 ] && output+=" ${deleted}D" 46 47 echo $output 48 } 49 50 51 # getting the #{pane_current_path} from dracula.sh is no longer possible 52 getPaneDir() 53 { 54 nextone="false" 55 for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); 56 do 57 if [ "$nextone" == "true" ]; then 58 echo $i 59 return 60 fi 61 if [ "$i" == "1" ]; then 62 nextone="true" 63 fi 64 done 65 } 66 67 68 # check if the current or diff symbol is empty to remove ugly padding 69 checkEmptySymbol() 70 { 71 symbol=$1 72 if [ "$symbol" == "" ]; then 73 echo "true" 74 else 75 echo "false" 76 fi 77 } 78 79 # check to see if the current repo is not up to date with HEAD 80 checkForChanges() 81 { 82 [ $no_untracked_files == "false" ] && no_untracked="" || no_untracked="-uno" 83 if [ "$(checkForGitDir)" == "true" ]; then 84 if [ "$(git -C $path --no-optional-locks status -s $no_untracked)" != "" ]; then 85 echo "true" 86 else 87 echo "false" 88 fi 89 else 90 echo "false" 91 fi 92 } 93 94 # check if a git repo exists in the directory 95 checkForGitDir() 96 { 97 if [ "$(git -C $path rev-parse --abbrev-ref HEAD)" != "" ]; then 98 echo "true" 99 else 100 echo "false" 101 fi 102 } 103 104 # return branch name if there is one 105 getBranch() 106 { 107 if [ $(checkForGitDir) == "true" ]; then 108 echo $(git -C $path rev-parse --abbrev-ref HEAD) 109 else 110 echo $no_repo_message 111 fi 112 } 113 114 getRemoteInfo() 115 { 116 base=$(git -C $path for-each-ref --format='%(upstream:short) %(upstream:track)' "$(git -C $path symbolic-ref -q HEAD)") 117 remote=$(echo "$base" | cut -d" " -f1) 118 out="" 119 120 if [ -n "$remote" ]; then 121 out="...$remote" 122 ahead=$(echo "$base" | grep -E -o 'ahead[ [:digit:]]+' | cut -d" " -f2) 123 behind=$(echo "$base" | grep -E -o 'behind[ [:digit:]]+' | cut -d" " -f2) 124 125 [ -n "$ahead" ] && out+=" +$ahead" 126 [ -n "$behind" ] && out+=" -$behind" 127 fi 128 129 echo "$out" 130 } 131 132 # return the final message for the status bar 133 getMessage() 134 { 135 if [ $(checkForGitDir) == "true" ]; then 136 branch="$(getBranch)" 137 output="" 138 139 if [ $(checkForChanges) == "true" ]; then 140 141 changes="$(getChanges)" 142 143 if [ "${hide_status}" == "false" ]; then 144 if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then 145 output=$(echo "${changes} $branch") 146 else 147 output=$(echo "$diff_symbol ${changes} $branch") 148 fi 149 else 150 if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then 151 output=$(echo "$branch") 152 else 153 output=$(echo "$diff_symbol $branch") 154 fi 155 fi 156 157 else 158 if [ $(checkEmptySymbol $current_symbol) == "true" ]; then 159 output=$(echo "$branch") 160 else 161 output=$(echo "$current_symbol $branch") 162 fi 163 fi 164 165 [ "$show_remote_status" == "true" ] && output+=$(getRemoteInfo) 166 echo "$output" 167 else 168 echo $no_repo_message 169 fi 170 } 171 172 main() 173 { 174 path=$(getPaneDir) 175 getMessage 176 } 177 178 #run main driver program 179 main