From 0975f9dbf97124599fcb03dbb27de80c05a46e67 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Fri, 24 Jan 2014 10:09:15 -0800 Subject: [PATCH 1/6] aks:testing new file in local, but not in remote --- newfile.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newfile.txt diff --git a/newfile.txt b/newfile.txt new file mode 100644 index 0000000..e69de29 From e037ec0231b1791e8f1a626d96bbb9a63799aab8 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 00:19:32 -0700 Subject: [PATCH 2/6] aks: refactor prompt color names & generation into separate file --- prompt-colors.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 prompt-colors.sh diff --git a/prompt-colors.sh b/prompt-colors.sh new file mode 100644 index 0000000..9249a7e --- /dev/null +++ b/prompt-colors.sh @@ -0,0 +1,82 @@ +# prompt-colors.sh +# +# source this file to get color definitions +# if $debug or $verbose is set, the definitions +# are also printed to STDERR. + +define_color_names() { + + ColorNames=( Black Red Green Yellow Blue Magneta Cyan White ) + FgColors=( 30 31 32 33 34 35 36 37 ) + BgColors=( 40 41 42 43 44 45 46 47 ) + + local AttrNorm=0 + local AttrBright=1 + local AttrDim=2 + local AttrUnder=4 + local AttrBlink=5 + local AttrRev=7 + local AttrHide=8 + + # define "BoldCOLOR", "BrightCOLOR", and "DimCOLOR" names + + # _map_colors ATTRNAME ATTRVALUE + # + # Defines three names for every color, attribute combintaion: + # {ATTRNAME}{COLORNAME} + # {ATTRNAME}{COLORNAME}Fg + # {ATTRNAME}{COLORNAME}Bg + # + # Example: BoldRed, BoldRedFg, BoldRedBg + + _map_colors() { + local x=0 + local attrname="$1" + local attrcode=$2 + while (( x < 8 )) ; do + local colorname=${ColorNames[x]} + local fgcolorcode=${FgColors[x]} + local bgcolorcode=${BgColors[x]} + longcolorname="${attrname}${colorname}" + _def_color $longcolorname $attrcode $fgcolorcode + _def_color ${longcolorname}Fg $attrcode $fgcolorcode + _def_color ${longcolorname}Bg $attrcode $bgcolorcode + (( x++ )) + done + } + + # _term_color [ N | N M ] + _term_color() { + local cv + if (( $# > 1 )); then + cv="${1};${2}" + else + cv="${1}" + fi + echo "\[\033[${cv}m\]" + } + + # def_color NAME ATTRCODE COLORCODE + _def_color() { + local def="$1=\"\`_term_color $2 $3\`\"" + if [[ -n "$debug$verbose" ]]; then + echo 1>&2 "+ $def" + fi + eval "$def" + } + + #ResetColor="`_term_color 0`" # Text reset + + _map_colors Bold $AttrBright + _map_colors Bright $AttrBright + _map_colors Dim $AttrDim + _map_colors '' $AttrNorm + + _def_color IntenseBlack 0 90 + _def_color ResetColor 0 0 + +} +define_color_names + +# end of prompt-colors.sh +# vim: set ai sw=2 From d89b35ad7c2b26cb46dccf3ea98efd8a338df5fd Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 00:20:00 -0700 Subject: [PATCH 3/6] aks: make the prompt vars all global --- git-prompt-colors.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh index 686804c..56afa14 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -1,4 +1,17 @@ # These are the color definitions used by gitprompt.sh + + declare -g GIT_PROMPT_PREFIX + declare -g GIT_PROMPT_SUFFIX + declare -g GIT_PROMPT_SEPARATOR + declare -g GIT_PROMPT_BRANCH + declare -g GIT_PROMPT_STAGED + declare -g GIT_PROMPT_CONFLICTS + declare -g GIT_PROMPT_CHANGED + declare -g GIT_PROMPT_REMOTE + declare -g GIT_PROMPT_UNTRACKED + declare -g GIT_PROMPT_STASHED + declare -g GIT_PROMPT_CLEAN + GIT_PROMPT_PREFIX="[" # start of the git info string GIT_PROMPT_SUFFIX="]" # the end of the git info string GIT_PROMPT_SEPARATOR="|" # separates each item From 9eddd83375a9f323c85c32d2cdacb5f2fa1c1f87 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 00:21:28 -0700 Subject: [PATCH 4/6] aks: major rewrite with refactored colors, envar management --- gitprompt.sh | 221 ++++++++++++++++++++++++++------------------------- 1 file changed, 114 insertions(+), 107 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index f4ca70e..f4de844 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -22,92 +22,107 @@ function git_prompt_dir() fi } +# gp_set_file_var ENVAR SOMEFILE +# +# Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the +# directory of the current script. The SOMEFILE can be prefixed with '.', or +# not. +# +# Return 0 (success) if ENVAR not already defined, 1 (failure) otherwise. + +function gp_set_file_var() { + local envar="$1" + local file="$2" + if eval "test -z \"\$$envar\"" ; then + eval "$envar=" # set empty envar + gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" && return 0 + gp_maybe_set_envar_to_path "$envar" "$HOME/$file" && return 0 + gp_maybe_set_envar_to_path "$envar" "$HOME/lib/$file" && return 0 + git_prompt_dir + gp_maybe_set_envar_to_path "$envar" "$__GIT_PROMPT_DIR/$file" && return 0 + gp_maybe_set_envar_to_path "$envar" "${0##*/}/$file" && return 0 + fi + return 1 +} + +# gp_maybe_set_envar_to_path ENVAR FILEPATH +# +# return 0 (true) if FILEPATH is readable, set ENVAR to it +# return 1 (false) if not + +function gp_maybe_set_envar_to_path(){ + local envar="$1" + local file="$2" + if [[ -r "$file" ]]; then + eval "$envar=\"$file\"" + return 0 + fi + return 1 +} + function git_prompt_config() { - # Colors - ResetColor="\[\033[0m\]" # Text reset - - # Bold - local BoldGreen="\[\033[1;32m\]" # Green - local BoldBlue="\[\033[1;34m\]" # Blue - # High Intensty - local IntenseBlack="\[\033[0;90m\]" # Grey + # There are two files related to colors: + # + # prompt-colors.sh -- sets generic color names suitable for bash `PS1` prompt + # git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh - # Bold High Intensty - local Magenta="\[\033[1;95m\]" # Purple + if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then - # Regular Colors - local Yellow="\[\033[0;33m\]" - local White='\[\033[37m\]' - local Red="\[\033[0;31m\]" - local Blue="\[\033[0;34m\]" - local Cyan="\[\033[0;36m\]" + if [[ -n "${__PROMPT_COLORS_SH}" ]]; then + source "${__PROMPT_COLORS_SH}" # outsource the color defs + else + echo 1>&2 "Cannot find prompt-colors.sh!" + fi + fi # source the user's ~/.git-prompt-colors.sh file, or the one that should be # sitting in the same directory as this script - if [[ -z "$__GIT_PROMPT_COLORS_FILE" ]]; then - local pfx file dir - for dir in "$HOME" "$__GIT_PROMPT_DIR" ; do - for pfx in '.' '' ; do - file="$dir/${pfx}git-prompt-colors.sh" - if [[ -f "$file" ]]; then - __GIT_PROMPT_COLORS_FILE="$file" - break 2 - fi - done - done - fi - # if the envar is defined, source the file for custom colors - if [[ -n "$__GIT_PROMPT_COLORS_FILE" && -f "$__GIT_PROMPT_COLORS_FILE" ]]; then - source "$__GIT_PROMPT_COLORS_FILE" - else - # Default values for the appearance of the prompt. Do not change these - # below. Instead, copy these to `~/.git-prompt-colors.sh` and change them - # there. - GIT_PROMPT_PREFIX="[" - GIT_PROMPT_SUFFIX="]" - GIT_PROMPT_SEPARATOR="|" - GIT_PROMPT_BRANCH="${Magenta}" - GIT_PROMPT_STAGED="${Red}●" - GIT_PROMPT_CONFLICTS="${Red}✖" - GIT_PROMPT_CHANGED="${Blue}✚" - GIT_PROMPT_REMOTE=" " - GIT_PROMPT_UNTRACKED="${Cyan}…" - GIT_PROMPT_STASHED="${BoldBlue}⚑" - GIT_PROMPT_CLEAN="${BoldGreen}✔" + if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then + + # if the envar is defined, source the file for custom colors + if [[ -n "${__GIT_PROMPT_COLORS_FILE}" ]]; then + source "${__GIT_PROMPT_COLORS_FILE}" + else + echo 1>&2 "Cannot find git-prompt-colors.sh!" + fi fi - # Various variables you might want for your PS1 prompt instead - local Time12a="\$(date +%H:%M)" - # local Time12a="(\$(date +%H:%M:%S))" - # local Time12a="(\@))" - local PathShort="\w" + # Do this only once to define PROMPT_START and PROMPT_END - if [ "x${GIT_PROMPT_START}" == "x" ]; then - PROMPT_START="${Yellow}${PathShort}${ResetColor}" - else - PROMPT_START="${GIT_PROMPT_START}" - fi + if [[ -z "$PROMPT_START" || -z "$PROMPT_END" ]]; then - if [ "x${GIT_PROMPT_END}" == "x" ]; then - PROMPT_END=" \n${White}${Time12a}${ResetColor} $ " - else - PROMPT_END="${GIT_PROMPT_END}" + # Various variables you might want for your PS1 prompt instead + local Time12a="\$(date +%H:%M)" + # local Time12a="(\$(date +%H:%M:%S))" + # local Time12a="(\@))" + local PathShort="\w" + + if [[ -z "${GIT_PROMPT_START}" ]] ; then + PROMPT_START="${Yellow}${PathShort}${ResetColor}" + else + PROMPT_START="${GIT_PROMPT_START}" + fi + + if [[ -z "${GIT_PROMPT_END}" ]] ; then + PROMPT_END=" \n${White}${Time12a}${ResetColor} $ " + else + PROMPT_END="${GIT_PROMPT_END}" + fi fi EMPTY_PROMPT="${PROMPT_START}$($prompt_callback)${PROMPT_END}" # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes GIT_PROMPT_FETCH_TIMEOUT=${1-5} - if [ "x$__GIT_STATUS_CMD" == "x" ] - then + if [[ -z "$__GIT_STATUS_CMD" ]] ; then # if GIT_STATUS_CMD not defined.. git_prompt_dir local sfx file # look first for a '.sh' version, then use the python version for sfx in sh py ; do - file="${__GIT_PROMPT_DIR}/gitstatus.$sfx" + file="$__GIT_PROMPT_DIR/gitstatus.$sfx" if [[ -x "$file" ]]; then __GIT_STATUS_CMD="$file" break @@ -151,23 +166,9 @@ function checkUpstream() { } function updatePrompt() { - local GIT_PROMPT_PREFIX - local GIT_PROMPT_SUFFIX - local GIT_PROMPT_SEPARATOR - local GIT_PROMPT_BRANCH - local GIT_PROMPT_STAGED - local GIT_PROMPT_CONFLICTS - local GIT_PROMPT_CHANGED - local GIT_PROMPT_REMOTE - local GIT_PROMPT_UNTRACKED - local GIT_PROMPT_STASHED - local GIT_PROMPT_CLEAN local PROMPT_START local PROMPT_END local EMPTY_PROMPT - local ResetColor - local Blue - local GIT_PROMPT_FETCH_TIMEOUT local __GIT_STATUS_CMD git_prompt_config @@ -190,37 +191,43 @@ function updatePrompt() { if [[ -n "${GitStatus}" ]]; then local STATUS=" ${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}" - if [[ -n "${GIT_REMOTE}" ]]; then - STATUS="${STATUS}${GIT_PROMPT_REMOTE}${GIT_REMOTE}${ResetColor}" - fi - - STATUS="${STATUS}${GIT_PROMPT_SEPARATOR}" - if [ "${GIT_STAGED}" -ne "0" ]; then - STATUS="${STATUS}${GIT_PROMPT_STAGED}${GIT_STAGED}${ResetColor}" - fi - - if [ "${GIT_CONFLICTS}" -ne "0" ]; then - STATUS="${STATUS}${GIT_PROMPT_CONFLICTS}${GIT_CONFLICTS}${ResetColor}" - fi - - if [ "${GIT_CHANGED}" -ne "0" ]; then - STATUS="${STATUS}${GIT_PROMPT_CHANGED}${GIT_CHANGED}${ResetColor}" - fi - - if [ "${GIT_UNTRACKED}" -ne "0" ]; then - STATUS="${STATUS}${GIT_PROMPT_UNTRACKED}${GIT_UNTRACKED}${ResetColor}" - fi - - if [ "${GIT_STASHED}" -ne "0" ]; then - STATUS="${STATUS}${GIT_PROMPT_STASHED}${GIT_STASHED}${ResetColor}" - fi - - if [ "${GIT_CLEAN}" -eq "1" ]; then - STATUS="${STATUS}${GIT_PROMPT_CLEAN}" - fi - - STATUS="${STATUS}${ResetColor}${GIT_PROMPT_SUFFIX}" + # __add_status KIND VALEXPR INSERT + # eg: __add_status 'STAGED' '-ne 0' + __chk_gitvar_status() { + local v + if [[ "x$2" == "x-n" ]] ; then + v="$2 \"\${GIT_$1}\"" + else + v="\${GIT_$1} $2" + fi + if eval "test $v" ; then + if [[ $# -lt 2 || "$3" != '-' ]]; then + __add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" + else + __add_status "\${GIT_PROMPT_$1}\${ResetColor}" + fi + fi + } + + __add_gitvar_status() { + __add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" + } + + # __add_status SOMETEXT + __add_status() { + eval "STATUS=\"${STATUS}$1\"" + } + + __chk_gitvar_status 'REMOTE' '-n' + __add_status "$GIT_PROMPT_SEPARATOR" + __chk_gitvar_status 'STAGED' '-ne 0' + __chk_gitvar_status 'CONFLICTS' '-ne 0' + __chk_gitvar_status 'CHANGED' '-ne 0' + __chk_gitvar_status 'UNTRACKED' '-ne 0' + __chk_gitvar_status 'STASHED' '-ne 0' + __chk_gitvar_status 'CLEAN' '-eq 1' - + __add_status "${ResetColor}$GIT_PROMPT_SUFFIX" PS1="${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" if [[ -n "${VIRTUAL_ENV}" ]]; then From f4b7cb3befdd7724a831188fc8cb30588d3ae11a Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 00:29:20 -0700 Subject: [PATCH 5/6] aks: added space after the flags to improve readability --- git-prompt-help.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) mode change 100644 => 100755 git-prompt-help.sh diff --git a/git-prompt-help.sh b/git-prompt-help.sh old mode 100644 new mode 100755 index ca6c5de..791bc7f --- a/git-prompt-help.sh +++ b/git-prompt-help.sh @@ -3,6 +3,8 @@ # being displayed. git_prompt_help() { + source prompt-colors.sh + source git-prompt-colors.sh cat <|] @@ -18,12 +20,12 @@ remote branch. It can be empty, or one of: LOCALSTATUS is one of the following: - ${GIT_PROMPT_CLEAN}${ResetColor} - repository clean - ${GIT_PROMPT_STAGED}N${ResetColor} - N staged files - ${GIT_PROMPT_CONFLICTS}N${ResetColor} - N conflicted files - ${GIT_PROMPT_CHANGED}N${ResetColor} - N changed but *unstaged* files - ${GIT_PROMPT_UNTRACKED}N${ResetColor} - N untracked files - ${GIT_PROMPT_STASHED}N${ResetColor} - N stash entries + ${GIT_PROMPT_CLEAN}${ResetColor} - repository clean + ${GIT_PROMPT_STAGED} N${ResetColor} - N staged files + ${GIT_PROMPT_CONFLICTS} N${ResetColor} - N conflicted files + ${GIT_PROMPT_CHANGED} N${ResetColor} - N changed but *unstaged* files + ${GIT_PROMPT_UNTRACKED} N${ResetColor} - N untracked files + ${GIT_PROMPT_STASHED} N${ResetColor} - N stash entries See "git_prompt_examples" for examples. EOF From 76dcf8b530e047ec5be068af048cb731c7885d98 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 00:39:16 -0700 Subject: [PATCH 6/6] aks: updated README.md --- README.md | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6c899e8..59c2fe7 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ staged, changed, etc. (an original idea from this [blog post][]). -`gitstatus.sh` added by [AKS](http://github.com/aks). +`gitstatus.sh` and `git-prompt-help.sh` added by [AKS](http://github.com/aks). ## Examples -The prompt may look like the following: +The prompt may look like the following: ![Example prompt](gitprompt.png) @@ -49,27 +49,32 @@ The symbols are as follows: ## Install -1. Clone this repository to your homedir - e.g. ``git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt`` -2. Source the file ``gitprompt.sh`` from your ``~/.bashrc`` config file -3. Go in a git repository and test it! +1. Clone this repository to your home directory. + + git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt + +2. Source the file `gitprompt.sh` from `~/.bashrc` + +3. `cd` to a git repository and test it! ## Configuration -1. You can use ``GIT_PROMPT_START`` and ``GIT_PROMPT_END`` to tweak your prompt +1. You can define `GIT_PROMPT_START` and `GIT_PROMPT_END` to tweak your prompt. -2. The default colors are defined within ``gitprompt.sh``, but may be - overridden by copying ``git-prompt-colors.sh`` to your home directory at - ``~/.git-prompt-colors.sh``. This file may also be found in the same - directory as ``gitprompt.sh``, but without the leading ``.``. +2. The default colors are defined within `prompt-colors.sh`, which is sourced + by `gitprompt.sh`. The colors used for various git status are defined in + `git-prompt-colors.sh`. Both of these files may be overridden by copying + them to $HOME with a `.` prefix. They can also be placed in `$HOME/lib` + without the leading `.`. The defaults are the original files in the + `~/.bash-git-prompt` directory. 3. The current git repo information is obtained by the script `gitstatus.sh` or `gitstatus.py`. Both scripts do the same thing, but the bash script is a tad more quick, and is used by default. If you prefer the python script (possibly because you have enhanced it), simply delete or change the name of - ``gitstatus.sh``. + `gitstatus.sh`. -4. You can define ``prompt_callback`` function to tweak your prompt dynamically. +4. You can define `prompt_callback` function to tweak your prompt dynamically. ```sh function prompt_callback {