it2git (3065B)
1 #!/bin/bash 2 # This script sets iTerm2 user-defined variables describing the state of the git 3 # repo in the current directory. 4 # 5 # For more information on the status bar, see: 6 # https://www.iterm2.com/3.3/documentation-status-bar.html 7 # 8 # Installation instructions for this script: 9 # 10 # bash: Place this in .bashrc. 11 # -------------------------------------------------------------------------------------- 12 # function iterm2_print_user_vars() { 13 # it2git 14 # } 15 16 # fish: Place this in ~/.config/fish/config.fish after the line 17 # "source ~/.iterm2_shell_integration.fish". 18 # -------------------------------------------------------------------------------------- 19 # function iterm2_print_user_vars 20 # it2git 21 # end 22 23 # tcsh: Place this in .tcshrc 24 # -------------------------------------------------------------------------------------- 25 # alias get_current_branch "bash -c '((git branch 2> /dev/null) | grep \* | cut -c3-)'" 26 # alias _iterm2_user_defined_vars 'it2git' 27 28 # zsh:Place this in .zshrc after "source /Users/georgen/.iterm2_shell_integration.zsh". 29 # -------------------------------------------------------------------------------------- 30 # iterm2_print_user_vars() { 31 # it2git 32 # } 33 34 GIT_BINARY=/usr/bin/git 35 36 dirty() { 37 # Outputs "dirty" or "clean" 38 OUTPUT=$("$GIT_BINARY" status --porcelain --ignore-submodules -unormal 2>/dev/null) 39 if (($?)); then 40 echo "clean" 41 return 42 fi 43 if [ -z "$OUTPUT" ]; then 44 echo "clean" 45 else 46 echo "dirty" 47 fi 48 } 49 50 counts() { 51 OUTPUT=$("$GIT_BINARY" rev-list --left-right --count HEAD...@'{u}' 2>/dev/null) 52 if (($?)); then 53 echo "error" 54 return 55 fi 56 echo "$OUTPUT" 57 } 58 59 branch() { 60 OUTPUT=$("$GIT_BINARY" symbolic-ref -q --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null) 61 if (($?)); then 62 return 63 fi 64 echo "$OUTPUT" 65 } 66 67 adds() { 68 "${GIT_BINARY}" ls-files --others --exclude-standard | wc -l 69 } 70 71 deletes() { 72 "${GIT_BINARY}" ls-files --deleted --exclude-standard | wc -l 73 } 74 function iterm2_begin_osc { 75 printf "\033]" 76 } 77 78 function iterm2_end_osc { 79 printf "\007" 80 } 81 82 function iterm2_set_user_var() { 83 iterm2_begin_osc 84 printf "1337;SetUserVar=%s=%s" "$1" $(printf "%s" "$2" | base64 | tr -d '\n') 85 iterm2_end_osc 86 } 87 88 git_poll () { 89 DIRECTORY="$1" 90 cd "$DIRECTORY" 91 DIRTY=$(dirty) 92 COUNTS=$(counts) 93 PUSH_COUNT=$(cut -f1 <<< "$COUNTS") 94 PULL_COUNT=$(cut -f2 <<< "$COUNTS") 95 BRANCH=$(branch) 96 97 iterm2_set_user_var gitBranch "$BRANCH" 98 iterm2_set_user_var gitDirty "$DIRTY" 99 iterm2_set_user_var gitPushCount "$PUSH_COUNT" 100 iterm2_set_user_var gitPullCount "$PULL_COUNT" 101 iterm2_set_user_var gitAdds "$ADDS" 102 iterm2_set_user_var gitDeletes "$DELETES" 103 } 104 105 "$GIT_BINARY" rev-parse --git-dir 2>/dev/null >/dev/null 106 if (($?)); then 107 iterm2_set_user_var gitBranch "" 108 iterm2_set_user_var gitDirty "" 109 iterm2_set_user_var gitPushCount "" 110 iterm2_set_user_var gitPullCount "" 111 iterm2_set_user_var gitAdds "" 112 iterm2_set_user_var gitDeletes "" 113 else 114 git_poll "$PWD" 115 fi 116 117