├── README.md └── gitprompt.sh /README.md: -------------------------------------------------------------------------------- 1 | # gitprompt -- A Bash prompt which integrates git status. 2 | 3 | ## Project status 4 | 5 | The current version is 1.2.1. 6 | 7 | A brief article about it has been published in Hacker Noon: 8 | https://hackernoon.com/why-linux-developers-should-use-gitprompt-8d654e5b87e1 9 | 10 | ## Installation 11 | 12 | To install gitprompt, first `cd` to a suitable directory where you want to 13 | install it. Let's say you want to install it in your home directory, so 14 | just type: 15 | 16 | $ cd 17 | $ git clone https://www.github.com/enfors/gitprompt 18 | 19 | To run it, run the following commands in your Bash prompt: 20 | 21 | $ cd gitprompt 22 | $ . gitprompt.sh 23 | 24 | If you want it all the time, add the following to your .profile or .bashrc: 25 | 26 | . ~/gitprompt/gitprompt.sh 27 | 28 | Happy Hacking! 29 | 30 | ## Features 31 | 32 | `GitPrompt` has a small but growing number of features 33 | 34 | ### Showing git status 35 | 36 | The main feature of `GitPrompt` is, as the name implies, to display 37 | git status (if files have been modified since the last commit, etc) in 38 | the prompt. 39 | 40 | ### Showing exit status 41 | 42 | If a command failes with a non-zero exit status, this status will be 43 | displayed in the prompt, reducing the risk that the user will miss it. 44 | 45 | ### Showing the current time in the prompt 46 | 47 | Every time you press enter, the current time on the machine will be 48 | displayed in the prompt. 49 | 50 | ### Showing username and hostname 51 | 52 | If you use many different accounts on many different machines, it is 53 | useful to always have the current username and hostname displayed in 54 | the prompt. 55 | 56 | ### Showing host aliases 57 | 58 | If you have a lot of different machines with less-than-helpful 59 | hostnames, which is sometimes the case, then you can set a shell 60 | variable called `HOSTALIAS` to a more descriptive name and it too 61 | will be displayed in the prompt. 62 | 63 | ### Customizing the colors 64 | 65 | You can customize the colors used with the `GPConfig` command. If things 66 | go wrong, you can reset the config with `GPReset`. 67 | -------------------------------------------------------------------------------- /gitprompt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # gitprompt.sh by Christer Enfors -- http://github.com/enfors/gitprompt 3 | 4 | GITPROMPT_VERSION="1.2.1" 5 | RC_FILE=~/.gitpromptrc 6 | 7 | RED="\033[0;31m" 8 | GREEN="\033[0;32m" 9 | YELLOW="\033[0;33m" 10 | BLUE="\033[0;34m" 11 | MAGENTA="\033[0;35m" 12 | CYAN="\033[0;36m" 13 | RESET="\033[0m" 14 | WHITE=$RESET 15 | BLACK=$RESET 16 | 17 | GIT_EXIT_STATUS_COLOR=$RED 18 | GIT_TIME_COLOR=$RESET 19 | GIT_BRACKET_COLOR=$BLUE 20 | GIT_AT_COLOR=$RESET 21 | GIT_USERNAME_COLOR=$GREEN 22 | GIT_HOSTNAME_COLOR=$GREEN 23 | GIT_HOSTALIAS_COLOR=$RESET 24 | GIT_COLON_COLOR=$RESET 25 | GIT_PWD_COLOR=$YELLOW 26 | GIT_BRANCH_COLOR=$RESET 27 | 28 | GIT_ADDED_COLOR=$YELLOW 29 | GIT_UNTRACKED_COLOR=$CYAN 30 | GIT_MODIFIED_COLOR=$BLUE 31 | GIT_DELETED_COLOR=$RED 32 | GIT_RENAMED_COLOR=$MAGENTA 33 | GIT_COPIED_COLOR=$MAGENTA 34 | GIT_UNMERGED_COLOR=$MAGENTA 35 | 36 | # 37 | # INIT FUNCTIONS 38 | # 39 | 40 | function Init 41 | { 42 | EchoGreeting 43 | 44 | if [ ! -e $RC_FILE ]; then 45 | MkConfigFile 46 | echo "It seems like this is your first time using GitPrompt." 47 | echo "GitPrompt makes the prompt more informative, especially " 48 | echo "(but not only) if you use git." 49 | else 50 | ReadConfigFile 51 | fi 52 | 53 | SetEditor 54 | } 55 | 56 | function EchoGreeting 57 | { 58 | echo "[GitPrompt version $GITPROMPT_VERSION by Christer Enfors enabled." \ 59 | "Type 'GPHelp' for help.]" 60 | } 61 | 62 | function SetEditor 63 | { 64 | if [ -z "$EDITOR" ]; then 65 | if [ -n "$VISUAL" ]; then 66 | EDITOR="$VISUAL" 67 | else 68 | if [ $(which nano) ]; then 69 | EDITOR="nano" 70 | else 71 | EDITOR="vi" 72 | fi 73 | fi 74 | fi 75 | } 76 | 77 | # 78 | # USER COMMAND FUNCTIONS 79 | # 80 | 81 | function GPHelp 82 | { 83 | cat <&2 103 | return 1 104 | fi 105 | 106 | ReadConfigFile 107 | SetPrompt 108 | } 109 | 110 | function GPReset 111 | { 112 | MkConfigFile 113 | ReadConfigFile 114 | SetPrompt 115 | } 116 | 117 | # 118 | # CONFIG FILE FUNCTIONS 119 | # 120 | 121 | function MkConfigFile 122 | { 123 | if [ -e "$RC_FILE" ]; then 124 | echo -n "Do you want to reset your GitPrompt config? [y/N]: " 125 | read answer 126 | if [ "$answer" != "y" ]; then 127 | echo "Very well - it will be left as it is." 128 | return 0 129 | fi 130 | fi 131 | 132 | cat <$RC_FILE 133 | # This is the config file for GitPrompt. 134 | # 135 | # Color key: 136 | # 137 | # ,-----+------ GIT_BRACKET_COLOR -----------------+-------. 138 | # | | | | 139 | # | | GIT_AT_COLOR | | 140 | # | | | | | 141 | # | | | GIT_COLON_COLOR | | 142 | # | | | | | | 143 | # V V v V V V 144 | # [02:44] enfors @ shodan: ~/devel/shell/gitprompt [develop]: Modified 145 | # ^ ^ ^ ^ ^ 146 | # | | | | | 147 | # | | GIT_HOSTNAME_COLOR GIT_PWD_COLOR GIT_BRANCH_COLOR 148 | # | | 149 | # | GIT_USERNAME_COLOR 150 | # | 151 | # GIT_TIME_COLOR 152 | 153 | GIT_EXIT_STATUS_COLOR=\$RED 154 | GIT_TIME_COLOR=\$RESET 155 | GIT_BRACKET_COLOR=\$BLUE 156 | GIT_AT_COLOR=\$RESET 157 | GIT_USERNAME_COLOR=\$GREEN 158 | GIT_HOSTNAME_COLOR=\$GREEN 159 | GIT_HOSTALIAS_COLOR=\$RESET 160 | GIT_COLON_COLOR=\$RESET 161 | GIT_PWD_COLOR=\$YELLOW 162 | GIT_BRANCH_COLOR=\$RESET 163 | 164 | GIT_ADDED_COLOR=\$YELLOW 165 | GIT_UNTRACKED_COLOR=\$CYAN 166 | GIT_MODIFIED_COLOR=\$BLUE 167 | GIT_DELETED_COLOR=\$RED 168 | GIT_RENAMED_COLOR=\$MAGENTA 169 | GIT_COPIED_COLOR=\$MAGENTA 170 | GIT_UNMERGED_COLOR=\$MAGENTA 171 | EOF 172 | } 173 | 174 | function ReadConfigFile 175 | { 176 | . $RC_FILE 177 | } 178 | 179 | # Display the exit status of the previous command, if non-zero. 180 | function ExitStatus 181 | { 182 | gs_exitstatus=$? 183 | 184 | if [ $gs_exitstatus -ne 0 ]; then 185 | echo -en "${GIT_EXIT_STATUS_COLOR}Exit status: $gs_exitstatus $RESET" 186 | fi 187 | } 188 | 189 | function SetHostAlias 190 | { 191 | if [ -n "$HOSTALIAS" ]; then 192 | hostalias="$GIT_BRACKET_COLOR[$GIT_HOSTALIAS_COLOR$HOSTALIAS$GIT_BRACKET_COLOR]$RESET" 193 | else 194 | hostalias="" 195 | fi 196 | } 197 | 198 | function SetPrompt 199 | { 200 | SetHostAlias 201 | export PS1="\$(ExitStatus)$GIT_BRACKET_COLOR[$GIT_TIME_COLOR\$(date +%H:%M)$GIT_BRACKET_COLOR]$RESET $GIT_USERNAME_COLOR\u$GIT_AT_COLOR @ $GIT_HOSTNAME_COLOR\h$RESET$hostalias: $GIT_PWD_COLOR\w$RESET \$(GitStatus)\n\$ " 202 | } 203 | 204 | # This is called before printing the each word in a list. The words should be 205 | # comma separated, so it prints a comma unless the word it's supposed to print 206 | # next is the FIRST word. 207 | function MaybeEchoComma 208 | { 209 | if [ ! -z "$gs_first" ]; then 210 | gs_first= 211 | else 212 | echo -n ", " 213 | fi 214 | } 215 | 216 | # Show the git commit status. 217 | function CommitStatus 218 | { 219 | unset added 220 | git status -s --porcelain | while read -r line; do 221 | if [[ $line == A* ]]; then 222 | if [ -z "$added" ]; then 223 | added=1 224 | MaybeEchoComma 225 | echo -en "${GIT_ADDED_COLOR}Added${RESET}" 226 | fi 227 | elif [[ $line == \?\?* ]]; then 228 | if [ -z "$untracked" ]; then 229 | untracked=1 230 | MaybeEchoComma 231 | echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" 232 | fi 233 | elif [[ $line == M* ]]; then 234 | if [ -z "$modified" ]; then 235 | modified=1 236 | MaybeEchoComma 237 | echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" 238 | fi 239 | elif [[ $line == D* ]]; then 240 | if [ -z "$deleted" ]; then 241 | deleted=1 242 | MaybeEchoComma 243 | echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" 244 | fi 245 | elif [[ $line == R* ]]; then 246 | if [ -z "$renamed" ]; then 247 | renamed=1 248 | MaybeEchoComma 249 | echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" 250 | fi 251 | elif [[ $line == C* ]]; then 252 | if [ -z "$copied" ]; then 253 | copied=1 254 | echo -en ", ${GIT_COPIED_COLOR}Copied${RESET}" 255 | fi 256 | elif [[ $line == U* ]]; then 257 | if [ -z "$unmerged" ]; then 258 | copied=1 259 | MaybeEchoComma 260 | echo -en "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}" 261 | fi 262 | else 263 | echo "UNKNOWN STATUS" 264 | return 1 265 | fi 266 | done 267 | 268 | return 0 269 | } 270 | 271 | function GitStatus 272 | { 273 | 274 | gs_first=1 275 | 276 | # If we're inside a .git directory, we can't find the branch / commit status. 277 | if pwd | grep -q /.git; then 278 | return 0 279 | fi 280 | 281 | if git rev-parse --git-dir >/dev/null 2>&1; then 282 | gs_branch=$(git branch | grep "^* " | cut -c 3-) 283 | 284 | gs_gitstatus=$(CommitStatus) 285 | 286 | if [ $? -eq 0 ]; then 287 | if [ -z "$gs_gitstatus" ]; then 288 | echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" 289 | else 290 | echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: $gs_gitstatus" 291 | fi 292 | fi 293 | fi 294 | } 295 | 296 | function Main 297 | { 298 | Init 299 | SetPrompt 300 | } 301 | 302 | Main 303 | 304 | 305 | --------------------------------------------------------------------------------