From 219993258ba4b38bd6e24ef87bc443ad5536eede Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Mon, 19 Nov 2018 13:55:53 -0500 Subject: [PATCH 01/11] - Move global variables to the global.sh file - Copy gitpromt to svn_prompt.sh and adapt to work with SVN * should merge with git prompt so it can work with either svn or git --- globals.sh | 33 ++++++ svn_prompt.sh | 285 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 318 insertions(+) create mode 100755 globals.sh create mode 100644 svn_prompt.sh diff --git a/globals.sh b/globals.sh new file mode 100755 index 0000000..c4a5bf4 --- /dev/null +++ b/globals.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +VERSION="1.2.1" +RC_FILE=~/.gitpromptrc + +RED="\033[0;31m" +GREEN="\033[0;32m" +YELLOW="\033[0;33m" +BLUE="\033[0;34m" +MAGENTA="\033[0;35m" +CYAN="\033[0;36m" +RESET="\033[0m" +WHITE=$RESET +BLACK=$RESET + +CLI_EXIT_STATUS_COLOR=$RED +CLI_TIME_COLOR=$RESET +CLI_BRACKET_COLOR=$BLUE +CLI_AT_COLOR=$RESET +CLI_USERNAME_COLOR=$GREEN +CLI_HOSTNAME_COLOR=$GREEN +CLI_HOSTALIAS_COLOR=$RESET +CLI_COLON_COLOR=$RESET +CLI_PWD_COLOR=$YELLOW +CLI_BRANCH_COLOR=$RESET + +CLI_ADDED_COLOR=$YELLOW +CLI_UNTRACKED_COLOR=$CYAN +CLI_MODIFIED_COLOR=$BLUE +CLI_DELETED_COLOR=$RED +CLI_RENAMED_COLOR=$MAGENTA +CLI_COPIED_COLOR=$MAGENTA +CLI_UNMERGED_COLOR=$MAGENTA \ No newline at end of file diff --git a/svn_prompt.sh b/svn_prompt.sh new file mode 100644 index 0000000..c600227 --- /dev/null +++ b/svn_prompt.sh @@ -0,0 +1,285 @@ +#!/bin/bash +# SvnPrompt.sh by Craig Moore -- http://svnhub.com/craigtmoore/SvnPrompt + +source globals.sh + +# +# INIT FUNCTIONS +# + +function Init +{ + EchoGreeting + + if [ ! -e $RC_FILE ]; then + MkConfigFile + echo "It seems like this is your first time using SvnPrompt." + echo "SvnPrompt makes the prompt more informative, especially " + echo "(but not only) if you use svn." + else + ReadConfigFile + fi + + SetEditor +} + +function EchoGreeting +{ + echo "[SvnPrompt version $VERSION by Craig Moore enabled. Type 'SPHelp' for help.]" +} + +function SetEditor +{ + if [[ -z "$EDITOR" ]]; then + if [[ -n "$VISUAL" ]]; then + EDITOR="$VISUAL" + else + if [[ $(which nano) ]]; then + EDITOR="nano" + else + EDITOR="vi" + fi + fi + fi +} + +function SPHelp +# Displays the help information +{ + cat <&2 + return 1 + fi + + ReadConfigFile + SetPrompt +} + +function SPReset +{ + MkConfigFile + ReadConfigFile + SetPrompt +} + +# +# CONFIG FILE FUNCTIONS +# + +function MkConfigFile +{ + if [[ -e "$RC_FILE" ]]; then + echo -n "Do you want to reset your SvnPrompt config? [y/N]: " + read answer + if [[ "$answer" != "y" ]]; then + echo "Very well - it will be left as it is." + return 0 + fi + fi + + cat <$RC_FILE +# This is the config file for SvnPrompt. +# +# Color key: +# +# ,-----+------ CLI_BRACKET_COLOR -----------------+-------. +# | | | | +# | | CLI_AT_COLOR | | +# | | | | | +# | | | CLI_COLON_COLOR | | +# | | | | | | +# V V v V V V +# [02:44] craigtmoore @ shodan: ~/devel/shell/SvnPrompt [develop]: Modified +# ^ ^ ^ ^ ^ +# | | | | | +# | | CLI_HOSTNAME_COLOR CLI_PWD_COLOR CLI_BRANCH_COLOR +# | | +# | CLI_USERNAME_COLOR +# | +# CLI_TIME_COLOR + +CLI_EXIT_STATUS_COLOR=\$RED +CLI_TIME_COLOR=\$RESET +CLI_BRACKET_COLOR=\$BLUE +CLI_AT_COLOR=\$RESET +CLI_USERNAME_COLOR=\$GREEN +CLI_HOSTNAME_COLOR=\$GREEN +CLI_HOSTALIAS_COLOR=\$RESET +CLI_COLON_COLOR=\$RESET +CLI_PWD_COLOR=\$YELLOW +CLI_BRANCH_COLOR=\$RESET + +CLI_ADDED_COLOR=\$YELLOW +CLI_UNTRACKED_COLOR=\$CYAN +CLI_MODIFIED_COLOR=\$BLUE +CLI_DELETED_COLOR=\$RED +CLI_RENAMED_COLOR=\$MAGENTA +CLI_COPIED_COLOR=\$MAGENTA +CLI_UNMERGED_COLOR=\$MAGENTA +EOF +} + +function ReadConfigFile +{ + . $RC_FILE +} + +# Display the exit status of the previous command, if non-zero. +function ExitStatus +{ + gs_exitstatus=$? + + if [ $gs_exitstatus -ne 0 ]; then + echo -en "${CLI_EXIT_STATUS_COLOR}Exit status: $gs_exitstatus $RESET" + fi +} + +function SetHostAlias +{ + if [ -n "$HOSTALIAS" ]; then + hostalias="$CLI_BRACKET_COLOR[$CLI_HOSTALIAS_COLOR$HOSTALIAS$CLI_BRACKET_COLOR]$RESET" + else + hostalias="" + fi +} + +function SetPrompt +{ + SetHostAlias + export PS1="\n\$(ExitStatus)$CLI_TIME_COLOR\$(date +%H:%M)$RESET $CLI_USERNAME_COLOR\u$CLI_AT_COLOR@$CLI_HOSTNAME_COLOR\h$RESET$hostalias $CLI_PWD_COLOR\w$RESET \$(svnStatus)\n\$ " +} + +# This is called before printing the each word in a list. The words should be +# comma separated, so it prints a comma unless the word it's supposed to print +# next is the FIRST word. +function MaybeEchoComma +{ + if [[ ! -z "${svn_first}" ]]; then + svn_first="" + else + echo -n ", " + fi +} + +# Show the svn commit status. +function CommitStatus +{ + local added + local untracked + local modified + local deleted + local renamed + local copied + local unmerged + local missing + svn status | while read -r line; do + if [[ ${line} =~ ^A ]]; then + if [[ -z "${added}" ]]; then + added=1 + MaybeEchoComma + echo -en "${CLI_ADDED_COLOR}Added${RESET}" + fi + elif [[ ${line} =~ ^\? ]]; then + if [[ -z "${untracked}" ]]; then + untracked=1 + MaybeEchoComma + echo -en "${CLI_UNTRACKED_COLOR}Untracked${RESET}" + fi + elif [[ ${line} =~ ^M ]]; then + if [[ -z "${modified}" ]]; then + modified=1 + MaybeEchoComma + echo -en "${CLI_MODIFIED_COLOR}Modified${RESET}" + fi + elif [[ ${line} =~ ^D ]]; then + if [[ -z "${deleted}" ]]; then + deleted=1 + MaybeEchoComma + echo -en "${CLI_DELETED_COLOR}Deleted${RESET}" + fi + elif [[ ${line} =~ ^R ]]; then + if [[ -z "${renamed}" ]]; then + renamed=1 + MaybeEchoComma + echo -en "${CLI_RENAMED_COLOR}Renamed${RESET}" + fi + elif [[ ${line} =~ ^C ]]; then + if [[ -z "${copied}" ]]; then + copied=1 + echo -en ", ${CLI_COPIED_COLOR}Copied${RESET}" + fi + elif [[ ${line} =~ ^! ]]; then + if [[ -z "${missing}" ]]; then + copied=1 + MaybeEchoComma + echo -en "${CLI_UNMERGED_COLOR}Missing${RESET}" + fi + fi + done + + return 0 +} + +function svnStatus +{ + + svn_first=1 + + # If we're inside a .svn directory, we can't find the branch / commit status. + if pwd | grep -q /.svn; then + return 0 + fi + + if svn info >/dev/null 2>&1; then + local svn_url=$(svn info | grep ^URL | cut -c6-) + svn_branch="" + if [[ ${svn_url} =~ "branches" ]]; then + svn_branch=${svn_url##*branches/} + svn_branch=$( echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;' ) + elif [[ ${svn_url} =~ "trunk" ]]; then + svn_branch="trunk" + elif [[ ${svn_url} =~ "tags" ]]; then + svn_branch=${svn_url##*tags/} + svn_branch="tags/$( echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;' )" + else + svn_branch="Unknown" + fi + local svn_status=$(CommitStatus) + if [[ $? -eq 0 ]]; then + if [[ -z "${svn_status}" ]]; then + echo -e "$CLI_BRACKET_COLOR[$CLI_BRANCH_COLOR$svn_branch$CLI_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" + else + echo -e "$CLI_BRACKET_COLOR[$CLI_BRANCH_COLOR$svn_branch$CLI_BRACKET_COLOR]$RESET: $svn_status" + fi + fi + fi +} + +function Main +{ + Init + SetPrompt +} + +Main + + From e30d038c64f5f229dd0388d69a84828605ee38e5 Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Mon, 19 Nov 2018 14:02:20 -0500 Subject: [PATCH 02/11] - Delete globals.sh (doesn't work with .bashrc if its in a separate file) - Copy contents of globals.sh into svn_prompt.sh --- globals.sh | 33 --------------------------------- svn_prompt.sh | 32 +++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 34 deletions(-) delete mode 100755 globals.sh diff --git a/globals.sh b/globals.sh deleted file mode 100755 index c4a5bf4..0000000 --- a/globals.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -VERSION="1.2.1" -RC_FILE=~/.gitpromptrc - -RED="\033[0;31m" -GREEN="\033[0;32m" -YELLOW="\033[0;33m" -BLUE="\033[0;34m" -MAGENTA="\033[0;35m" -CYAN="\033[0;36m" -RESET="\033[0m" -WHITE=$RESET -BLACK=$RESET - -CLI_EXIT_STATUS_COLOR=$RED -CLI_TIME_COLOR=$RESET -CLI_BRACKET_COLOR=$BLUE -CLI_AT_COLOR=$RESET -CLI_USERNAME_COLOR=$GREEN -CLI_HOSTNAME_COLOR=$GREEN -CLI_HOSTALIAS_COLOR=$RESET -CLI_COLON_COLOR=$RESET -CLI_PWD_COLOR=$YELLOW -CLI_BRANCH_COLOR=$RESET - -CLI_ADDED_COLOR=$YELLOW -CLI_UNTRACKED_COLOR=$CYAN -CLI_MODIFIED_COLOR=$BLUE -CLI_DELETED_COLOR=$RED -CLI_RENAMED_COLOR=$MAGENTA -CLI_COPIED_COLOR=$MAGENTA -CLI_UNMERGED_COLOR=$MAGENTA \ No newline at end of file diff --git a/svn_prompt.sh b/svn_prompt.sh index c600227..62978c0 100644 --- a/svn_prompt.sh +++ b/svn_prompt.sh @@ -1,7 +1,37 @@ #!/bin/bash # SvnPrompt.sh by Craig Moore -- http://svnhub.com/craigtmoore/SvnPrompt -source globals.sh +VERSION="1.2.1" +RC_FILE=~/.gitpromptrc + +RED="\033[0;31m" +GREEN="\033[0;32m" +YELLOW="\033[0;33m" +BLUE="\033[0;34m" +MAGENTA="\033[0;35m" +CYAN="\033[0;36m" +RESET="\033[0m" +WHITE=$RESET +BLACK=$RESET + +CLI_EXIT_STATUS_COLOR=$RED +CLI_TIME_COLOR=$RESET +CLI_BRACKET_COLOR=$BLUE +CLI_AT_COLOR=$RESET +CLI_USERNAME_COLOR=$GREEN +CLI_HOSTNAME_COLOR=$GREEN +CLI_HOSTALIAS_COLOR=$RESET +CLI_COLON_COLOR=$RESET +CLI_PWD_COLOR=$YELLOW +CLI_BRANCH_COLOR=$RESET + +CLI_ADDED_COLOR=$YELLOW +CLI_UNTRACKED_COLOR=$CYAN +CLI_MODIFIED_COLOR=$BLUE +CLI_DELETED_COLOR=$RED +CLI_RENAMED_COLOR=$MAGENTA +CLI_COPIED_COLOR=$MAGENTA +CLI_UNMERGED_COLOR=$MAGENTA # # INIT FUNCTIONS From 3f226d607a3896ef9556ce72f5733f1b3076582c Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Mon, 19 Nov 2018 14:51:53 -0500 Subject: [PATCH 03/11] - Can now determine if the local copy is out of date or not --- svn_prompt.sh | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/svn_prompt.sh b/svn_prompt.sh index 62978c0..70dc3d8 100644 --- a/svn_prompt.sh +++ b/svn_prompt.sh @@ -33,6 +33,9 @@ CLI_RENAMED_COLOR=$MAGENTA CLI_COPIED_COLOR=$MAGENTA CLI_UNMERGED_COLOR=$MAGENTA + +svn_first="" + # # INIT FUNCTIONS # @@ -80,7 +83,7 @@ function SPHelp SvnPrompt help ============== SvnPrompt is a script which configures your prompt to be a little more -helpful; see https://www.svnhub.com/craigtmoore/SvnPrompt for details about +helpful; see https://www.github.com/craigtmoore/GitPrompt for details about what it does. SvnPrompt commands @@ -198,12 +201,12 @@ function SetPrompt export PS1="\n\$(ExitStatus)$CLI_TIME_COLOR\$(date +%H:%M)$RESET $CLI_USERNAME_COLOR\u$CLI_AT_COLOR@$CLI_HOSTNAME_COLOR\h$RESET$hostalias $CLI_PWD_COLOR\w$RESET \$(svnStatus)\n\$ " } -# This is called before printing the each word in a list. The words should be +# This is called before printing each word in a list. The words should be # comma separated, so it prints a comma unless the word it's supposed to print # next is the FIRST word. function MaybeEchoComma { - if [[ ! -z "${svn_first}" ]]; then + if [[ -n "${svn_first}" ]]; then svn_first="" else echo -n ", " @@ -221,6 +224,8 @@ function CommitStatus local copied local unmerged local missing + local out_of_date + local check_if_up_to_date svn status | while read -r line; do if [[ ${line} =~ ^A ]]; then if [[ -z "${added}" ]]; then @@ -255,24 +260,49 @@ function CommitStatus elif [[ ${line} =~ ^C ]]; then if [[ -z "${copied}" ]]; then copied=1 - echo -en ", ${CLI_COPIED_COLOR}Copied${RESET}" + MaybeEchoComma + echo -en "${CLI_COPIED_COLOR}Copied${RESET}" fi elif [[ ${line} =~ ^! ]]; then if [[ -z "${missing}" ]]; then - copied=1 + missing=1 MaybeEchoComma echo -en "${CLI_UNMERGED_COLOR}Missing${RESET}" fi fi + if [[ -z "${check_if_up_to_date}" ]]; then + echo "check_if_up_to_date = ${check_if_up_to_date}" + check_if_up_to_date=1 + svn status -u | sed '$d' | while read -r status_line; do + if [[ ${status_line} =~ \* ]]; then + if [[ -z "${out_of_date}" ]]; then + out_of_date=1 + MaybeEchoComma + echo -en "${CLI_UNMERGED_COLOR}OutOfDate${RESET}" + fi + fi; + done; + fi; done + if [[ -z "${check_if_up_to_date}" ]]; then + check_if_up_to_date=1 + svn status -u | sed '$d' | while read -r status_line; do + if [[ ${status_line} =~ \* ]]; then + if [[ -z "${out_of_date}" ]]; then + out_of_date=1 + MaybeEchoComma + echo -en "${CLI_UNMERGED_COLOR}OutOfDate${RESET}" + fi + fi; + done; + fi; return 0 } function svnStatus { - - svn_first=1 + svn_first="yes" # If we're inside a .svn directory, we can't find the branch / commit status. if pwd | grep -q /.svn; then From 89f0b85022d22176d9cace3f4789e1589569c33b Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Mon, 19 Nov 2018 23:07:40 -0500 Subject: [PATCH 04/11] - Update gitprompt.sh: * update formatting (replace all tabs with spaces) - Update svn_prompt.sh: * make check_if_up_to_date, global (so it is available in the 2nd while loop * remove echo debug statment --- gitprompt.sh | 255 +++++++++++++++++++++++++------------------------- svn_prompt.sh | 3 +- 2 files changed, 129 insertions(+), 129 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 24f7894..da9f2da 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -1,4 +1,5 @@ #!/bin/bash + # gitprompt.sh by Christer Enfors -- http://github.com/enfors/gitprompt GITPROMPT_VERSION="1.2.1" @@ -39,39 +40,39 @@ GIT_UNMERGED_COLOR=$MAGENTA function Init { - EchoGreeting - - if [ ! -e $RC_FILE ]; then - MkConfigFile - echo "It seems like this is your first time using GitPrompt." - echo "GitPrompt makes the prompt more informative, especially " - echo "(but not only) if you use git." - else - ReadConfigFile - fi + EchoGreeting + + if [ ! -e $RC_FILE ]; then + MkConfigFile + echo "It seems like this is your first time using GitPrompt." + echo "GitPrompt makes the prompt more informative, especially " + echo "(but not only) if you use git." + else + ReadConfigFile + fi - SetEditor + SetEditor } function EchoGreeting { - echo "[GitPrompt version $GITPROMPT_VERSION by Christer Enfors enabled." \ - "Type 'GPHelp' for help.]" + echo "[GitPrompt version $GITPROMPT_VERSION by Christer Enfors enabled." \ + "Type 'GPHelp' for help.]" } function SetEditor { - if [ -z "$EDITOR" ]; then - if [ -n "$VISUAL" ]; then - EDITOR="$VISUAL" - else - if [ $(which nano) ]; then - EDITOR="nano" - else - EDITOR="vi" - fi - fi + if [ -z "$EDITOR" ]; then + if [ -n "$VISUAL" ]; then + EDITOR="$VISUAL" + else + if [ $(which nano) ]; then + EDITOR="nano" + else + EDITOR="vi" + fi fi + fi } # @@ -80,7 +81,7 @@ function SetEditor function GPHelp { - cat <&2 - return 1 - fi - - ReadConfigFile - SetPrompt + if [ $? -ne 0 ]; then + echo "Editing config file failed; aborting." >&2 + return 1 + fi + + ReadConfigFile + SetPrompt } function GPReset { - MkConfigFile - ReadConfigFile - SetPrompt + MkConfigFile + ReadConfigFile + SetPrompt } # @@ -120,16 +121,16 @@ function GPReset function MkConfigFile { - if [ -e "$RC_FILE" ]; then - echo -n "Do you want to reset your GitPrompt config? [y/N]: " - read answer - if [ "$answer" != "y" ]; then - echo "Very well - it will be left as it is." - return 0 - fi + if [ -e "$RC_FILE" ]; then + echo -n "Do you want to reset your GitPrompt config? [y/N]: " + read answer + if [ "$answer" != "y" ]; then + echo "Very well - it will be left as it is." + return 0 fi - - cat <$RC_FILE + fi + + cat << EOF > $RC_FILE # This is the config file for GitPrompt. # # Color key: @@ -173,32 +174,32 @@ EOF function ReadConfigFile { - . $RC_FILE + . $RC_FILE } # Display the exit status of the previous command, if non-zero. function ExitStatus { - gs_exitstatus=$? + gs_exitstatus=$? - if [ $gs_exitstatus -ne 0 ]; then - echo -en "${GIT_EXIT_STATUS_COLOR}Exit status: $gs_exitstatus $RESET" - fi + if [ $gs_exitstatus -ne 0 ]; then + echo -en "${GIT_EXIT_STATUS_COLOR}Exit status: $gs_exitstatus $RESET" + fi } function SetHostAlias { - if [ -n "$HOSTALIAS" ]; then - hostalias="$GIT_BRACKET_COLOR[$GIT_HOSTALIAS_COLOR$HOSTALIAS$GIT_BRACKET_COLOR]$RESET" - else - hostalias="" - fi + if [ -n "$HOSTALIAS" ]; then + hostalias="$GIT_BRACKET_COLOR[$GIT_HOSTALIAS_COLOR$HOSTALIAS$GIT_BRACKET_COLOR]$RESET" + else + hostalias="" + fi } function SetPrompt { - SetHostAlias - 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\$ " + SetHostAlias + 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\$ " } # This is called before printing the each word in a list. The words should be @@ -206,97 +207,97 @@ function SetPrompt # next is the FIRST word. function MaybeEchoComma { - if [ ! -z "$gs_first" ]; then - gs_first= - else - echo -n ", " - fi + if [ ! -z "$gs_first" ]; then + gs_first= + else + echo -n ", " + fi } # Show the git commit status. function CommitStatus { - unset added - git status -s --porcelain | while read -r line; do - if [[ $line == A* ]]; then - if [ -z "$added" ]; then - added=1 - MaybeEchoComma - echo -en "${GIT_ADDED_COLOR}Added${RESET}" - fi - elif [[ $line == \?\?* ]]; then - if [ -z "$untracked" ]; then - untracked=1 - MaybeEchoComma - echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" - fi - elif [[ $line == M* ]]; then - if [ -z "$modified" ]; then - modified=1 - MaybeEchoComma - echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" - fi - elif [[ $line == D* ]]; then - if [ -z "$deleted" ]; then - deleted=1 - MaybeEchoComma - echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" - fi - elif [[ $line == R* ]]; then - if [ -z "$renamed" ]; then - renamed=1 - MaybeEchoComma - echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" - fi - elif [[ $line == C* ]]; then - if [ -z "$copied" ]; then - copied=1 - echo -en ", ${GIT_COPIED_COLOR}Copied${RESET}" - fi - elif [[ $line == U* ]]; then - if [ -z "$unmerged" ]; then - copied=1 - MaybeEchoComma - echo -en "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}" - fi - else - echo "UNKNOWN STATUS" - return 1 - fi - done + unset added + git status -s --porcelain | while read -r line; do + if [[ $line == A* ]]; then + if [ -z "$added" ]; then + added=1 + MaybeEchoComma + echo -en "${GIT_ADDED_COLOR}Added${RESET}" + fi + elif [[ $line == \?\?* ]]; then + if [ -z "$untracked" ]; then + untracked=1 + MaybeEchoComma + echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" + fi + elif [[ $line == M* ]]; then + if [ -z "$modified" ]; then + modified=1 + MaybeEchoComma + echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" + fi + elif [[ $line == D* ]]; then + if [ -z "$deleted" ]; then + deleted=1 + MaybeEchoComma + echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" + fi + elif [[ $line == R* ]]; then + if [ -z "$renamed" ]; then + renamed=1 + MaybeEchoComma + echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" + fi + elif [[ $line == C* ]]; then + if [ -z "$copied" ]; then + copied=1 + echo -en ", ${GIT_COPIED_COLOR}Copied${RESET}" + fi + elif [[ $line == U* ]]; then + if [ -z "$unmerged" ]; then + copied=1 + MaybeEchoComma + echo -en "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}" + fi + else + echo "UNKNOWN STATUS" + return 1 + fi + done - return 0 + return 0 } function GitStatus { - gs_first=1 + gs_first=1 - # If we're inside a .git directory, we can't find the branch / commit status. - if pwd | grep -q /.git; then - return 0 - fi + # If we're inside a .git directory, we can't find the branch / commit status. + if pwd | grep -q /.git; then + return 0 + fi - if git rev-parse --git-dir >/dev/null 2>&1; then - gs_branch=$(git branch | grep "^* " | cut -c 3-) + if git rev-parse --git-dir > /dev/null 2>&1; then + gs_branch=$(git branch | grep "^* " | cut -c 3-) - gs_gitstatus=$(CommitStatus) + gs_gitstatus=$(CommitStatus) - if [ $? -eq 0 ]; then - if [ -z "$gs_gitstatus" ]; then - echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" - else - echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: $gs_gitstatus" - fi - fi + if [ $? -eq 0 ]; then + if [ -z "$gs_gitstatus" ]; then + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" + else + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: $gs_gitstatus" + fi fi + fi } function Main { - Init - SetPrompt + Init + SetPrompt } Main diff --git a/svn_prompt.sh b/svn_prompt.sh index 70dc3d8..28c18de 100644 --- a/svn_prompt.sh +++ b/svn_prompt.sh @@ -225,7 +225,7 @@ function CommitStatus local unmerged local missing local out_of_date - local check_if_up_to_date + check_if_up_to_date="" svn status | while read -r line; do if [[ ${line} =~ ^A ]]; then if [[ -z "${added}" ]]; then @@ -271,7 +271,6 @@ function CommitStatus fi fi if [[ -z "${check_if_up_to_date}" ]]; then - echo "check_if_up_to_date = ${check_if_up_to_date}" check_if_up_to_date=1 svn status -u | sed '$d' | while read -r status_line; do if [[ ${status_line} =~ \* ]]; then From c85a1d6bef581d4715263dbfb6a238e96dc96a0f Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Mon, 19 Nov 2018 23:23:58 -0500 Subject: [PATCH 05/11] - Deleted svn_prompt.sh - Update gitprompt.sh: * merged changes from svn_prompt.sh into gitprompt.sh * update version number 2.0.0 - Update README.md: * Add note about svn status to README --- README.md | 2 +- gitprompt.sh | 144 +++++++++++++++++++-- svn_prompt.sh | 344 -------------------------------------------------- 3 files changed, 132 insertions(+), 358 deletions(-) delete mode 100644 svn_prompt.sh diff --git a/README.md b/README.md index bd3d71e..c9179f9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# gitprompt -- A Bash prompt which integrates git status. +# gitprompt -- A Bash prompt which integrates git and svn status ## Project status diff --git a/gitprompt.sh b/gitprompt.sh index da9f2da..3f1e9a8 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -1,8 +1,9 @@ #!/bin/bash # gitprompt.sh by Christer Enfors -- http://github.com/enfors/gitprompt +# svn support added by Craig Moore -- http://github.com/craigtmoore/gitprompt -GITPROMPT_VERSION="1.2.1" +GITPROMPT_VERSION="2.0.0" RC_FILE=~/.gitpromptrc RED="\033[0;31m" @@ -199,7 +200,7 @@ function SetHostAlias function SetPrompt { SetHostAlias - 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\$ " + export PS1="\n\$(ExitStatus)$GIT_TIME_COLOR\$(date +%H:%M)$RESET $GIT_USERNAME_COLOR\u$GIT_AT_COLOR@$GIT_HOSTNAME_COLOR\h$RESET$hostalias $GIT_PWD_COLOR\w$RESET \$(GetStatus)\n\$ " } # This is called before printing the each word in a list. The words should be @@ -215,47 +216,54 @@ function MaybeEchoComma } # Show the git commit status. -function CommitStatus +function GitCommitStatus { - unset added + local added + local untracked + local modified + local deleted + local renamed + local copied + local unmerged + local missing git status -s --porcelain | while read -r line; do if [[ $line == A* ]]; then - if [ -z "$added" ]; then + if [[ -z "$added" ]]; then added=1 MaybeEchoComma echo -en "${GIT_ADDED_COLOR}Added${RESET}" fi elif [[ $line == \?\?* ]]; then - if [ -z "$untracked" ]; then + if [[ -z "$untracked" ]]; then untracked=1 MaybeEchoComma echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" fi elif [[ $line == M* ]]; then - if [ -z "$modified" ]; then + if [[ -z "$modified" ]]; then modified=1 MaybeEchoComma echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" fi elif [[ $line == D* ]]; then - if [ -z "$deleted" ]; then + if [[ -z "$deleted" ]]; then deleted=1 MaybeEchoComma echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" fi elif [[ $line == R* ]]; then - if [ -z "$renamed" ]; then + if [[ -z "$renamed" ]]; then renamed=1 MaybeEchoComma echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" fi elif [[ $line == C* ]]; then - if [ -z "$copied" ]; then + if [[ -z "$copied" ]]; then copied=1 echo -en ", ${GIT_COPIED_COLOR}Copied${RESET}" fi elif [[ $line == U* ]]; then - if [ -z "$unmerged" ]; then + if [[ -z "$unmerged" ]]; then copied=1 MaybeEchoComma echo -en "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}" @@ -269,7 +277,93 @@ function CommitStatus return 0 } -function GitStatus +# Show the svn commit status. +function SvnCommitStatus +{ + local added + local untracked + local modified + local deleted + local renamed + local copied + local unmerged + local missing + local out_of_date + check_if_up_to_date="" + svn status | while read -r line; do + if [[ ${line} =~ ^A ]]; then + if [[ -z "${added}" ]]; then + added=1 + MaybeEchoComma + echo -en "${GIT_ADDED_COLOR}Added${RESET}" + fi + elif [[ ${line} =~ ^\? ]]; then + if [[ -z "${untracked}" ]]; then + untracked=1 + MaybeEchoComma + echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" + fi + elif [[ ${line} =~ ^M ]]; then + if [[ -z "${modified}" ]]; then + modified=1 + MaybeEchoComma + echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" + fi + elif [[ ${line} =~ ^D ]]; then + if [[ -z "${deleted}" ]]; then + deleted=1 + MaybeEchoComma + echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" + fi + elif [[ ${line} =~ ^R ]]; then + if [[ -z "${renamed}" ]]; then + renamed=1 + MaybeEchoComma + echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" + fi + elif [[ ${line} =~ ^C ]]; then + if [[ -z "${copied}" ]]; then + copied=1 + MaybeEchoComma + echo -en "${GIT_COPIED_COLOR}Copied${RESET}" + fi + elif [[ ${line} =~ ^! ]]; then + if [[ -z "${missing}" ]]; then + missing=1 + MaybeEchoComma + echo -en "${GIT_UNMERGED_COLOR}Missing${RESET}" + fi + fi + if [[ -z "${check_if_up_to_date}" ]]; then + check_if_up_to_date=1 + svn status -u | sed '$d' | while read -r status_line; do + if [[ ${status_line} =~ \* ]]; then + if [[ -z "${out_of_date}" ]]; then + out_of_date=1 + MaybeEchoComma + echo -en "${GIT_UNMERGED_COLOR}OutOfDate${RESET}" + fi + fi; + done; + fi; + done + if [[ -z "${check_if_up_to_date}" ]]; then + check_if_up_to_date=1 + svn status -u | sed '$d' | while read -r status_line; do + if [[ ${status_line} =~ \* ]]; then + if [[ -z "${out_of_date}" ]]; then + out_of_date=1 + MaybeEchoComma + echo -en "${GIT_UNMERGED_COLOR}OutOfDate${RESET}" + fi + fi; + done; + fi + + return 0 +} + +function GetStatus { gs_first=1 @@ -282,7 +376,7 @@ function GitStatus if git rev-parse --git-dir > /dev/null 2>&1; then gs_branch=$(git branch | grep "^* " | cut -c 3-) - gs_gitstatus=$(CommitStatus) + gs_gitstatus=$(GitCommitStatus) if [ $? -eq 0 ]; then if [ -z "$gs_gitstatus" ]; then @@ -292,6 +386,30 @@ function GitStatus fi fi fi + + if svn info > /dev/null 2>&1; then + local svn_url=$(svn info | grep ^URL | cut -c6-) + svn_branch="" + if [[ ${svn_url} =~ "branches" ]]; then + svn_branch=${svn_url##*branches/} + svn_branch=$(echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;') + elif [[ ${svn_url} =~ "trunk" ]]; then + svn_branch="trunk" + elif [[ ${svn_url} =~ "tags" ]]; then + svn_branch=${svn_url##*tags/} + svn_branch="tags/$(echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;')" + else + svn_branch="Unknown" + fi + local svn_status=$(CommitStatus) + if [[ $? -eq 0 ]]; then + if [[ -z "${svn_status}" ]]; then + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$svn_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" + else + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$svn_branch$GIT_BRACKET_COLOR]$RESET: $svn_status" + fi + fi + fi } function Main diff --git a/svn_prompt.sh b/svn_prompt.sh deleted file mode 100644 index 28c18de..0000000 --- a/svn_prompt.sh +++ /dev/null @@ -1,344 +0,0 @@ -#!/bin/bash -# SvnPrompt.sh by Craig Moore -- http://svnhub.com/craigtmoore/SvnPrompt - -VERSION="1.2.1" -RC_FILE=~/.gitpromptrc - -RED="\033[0;31m" -GREEN="\033[0;32m" -YELLOW="\033[0;33m" -BLUE="\033[0;34m" -MAGENTA="\033[0;35m" -CYAN="\033[0;36m" -RESET="\033[0m" -WHITE=$RESET -BLACK=$RESET - -CLI_EXIT_STATUS_COLOR=$RED -CLI_TIME_COLOR=$RESET -CLI_BRACKET_COLOR=$BLUE -CLI_AT_COLOR=$RESET -CLI_USERNAME_COLOR=$GREEN -CLI_HOSTNAME_COLOR=$GREEN -CLI_HOSTALIAS_COLOR=$RESET -CLI_COLON_COLOR=$RESET -CLI_PWD_COLOR=$YELLOW -CLI_BRANCH_COLOR=$RESET - -CLI_ADDED_COLOR=$YELLOW -CLI_UNTRACKED_COLOR=$CYAN -CLI_MODIFIED_COLOR=$BLUE -CLI_DELETED_COLOR=$RED -CLI_RENAMED_COLOR=$MAGENTA -CLI_COPIED_COLOR=$MAGENTA -CLI_UNMERGED_COLOR=$MAGENTA - - -svn_first="" - -# -# INIT FUNCTIONS -# - -function Init -{ - EchoGreeting - - if [ ! -e $RC_FILE ]; then - MkConfigFile - echo "It seems like this is your first time using SvnPrompt." - echo "SvnPrompt makes the prompt more informative, especially " - echo "(but not only) if you use svn." - else - ReadConfigFile - fi - - SetEditor -} - -function EchoGreeting -{ - echo "[SvnPrompt version $VERSION by Craig Moore enabled. Type 'SPHelp' for help.]" -} - -function SetEditor -{ - if [[ -z "$EDITOR" ]]; then - if [[ -n "$VISUAL" ]]; then - EDITOR="$VISUAL" - else - if [[ $(which nano) ]]; then - EDITOR="nano" - else - EDITOR="vi" - fi - fi - fi -} - -function SPHelp -# Displays the help information -{ - cat <&2 - return 1 - fi - - ReadConfigFile - SetPrompt -} - -function SPReset -{ - MkConfigFile - ReadConfigFile - SetPrompt -} - -# -# CONFIG FILE FUNCTIONS -# - -function MkConfigFile -{ - if [[ -e "$RC_FILE" ]]; then - echo -n "Do you want to reset your SvnPrompt config? [y/N]: " - read answer - if [[ "$answer" != "y" ]]; then - echo "Very well - it will be left as it is." - return 0 - fi - fi - - cat <$RC_FILE -# This is the config file for SvnPrompt. -# -# Color key: -# -# ,-----+------ CLI_BRACKET_COLOR -----------------+-------. -# | | | | -# | | CLI_AT_COLOR | | -# | | | | | -# | | | CLI_COLON_COLOR | | -# | | | | | | -# V V v V V V -# [02:44] craigtmoore @ shodan: ~/devel/shell/SvnPrompt [develop]: Modified -# ^ ^ ^ ^ ^ -# | | | | | -# | | CLI_HOSTNAME_COLOR CLI_PWD_COLOR CLI_BRANCH_COLOR -# | | -# | CLI_USERNAME_COLOR -# | -# CLI_TIME_COLOR - -CLI_EXIT_STATUS_COLOR=\$RED -CLI_TIME_COLOR=\$RESET -CLI_BRACKET_COLOR=\$BLUE -CLI_AT_COLOR=\$RESET -CLI_USERNAME_COLOR=\$GREEN -CLI_HOSTNAME_COLOR=\$GREEN -CLI_HOSTALIAS_COLOR=\$RESET -CLI_COLON_COLOR=\$RESET -CLI_PWD_COLOR=\$YELLOW -CLI_BRANCH_COLOR=\$RESET - -CLI_ADDED_COLOR=\$YELLOW -CLI_UNTRACKED_COLOR=\$CYAN -CLI_MODIFIED_COLOR=\$BLUE -CLI_DELETED_COLOR=\$RED -CLI_RENAMED_COLOR=\$MAGENTA -CLI_COPIED_COLOR=\$MAGENTA -CLI_UNMERGED_COLOR=\$MAGENTA -EOF -} - -function ReadConfigFile -{ - . $RC_FILE -} - -# Display the exit status of the previous command, if non-zero. -function ExitStatus -{ - gs_exitstatus=$? - - if [ $gs_exitstatus -ne 0 ]; then - echo -en "${CLI_EXIT_STATUS_COLOR}Exit status: $gs_exitstatus $RESET" - fi -} - -function SetHostAlias -{ - if [ -n "$HOSTALIAS" ]; then - hostalias="$CLI_BRACKET_COLOR[$CLI_HOSTALIAS_COLOR$HOSTALIAS$CLI_BRACKET_COLOR]$RESET" - else - hostalias="" - fi -} - -function SetPrompt -{ - SetHostAlias - export PS1="\n\$(ExitStatus)$CLI_TIME_COLOR\$(date +%H:%M)$RESET $CLI_USERNAME_COLOR\u$CLI_AT_COLOR@$CLI_HOSTNAME_COLOR\h$RESET$hostalias $CLI_PWD_COLOR\w$RESET \$(svnStatus)\n\$ " -} - -# This is called before printing each word in a list. The words should be -# comma separated, so it prints a comma unless the word it's supposed to print -# next is the FIRST word. -function MaybeEchoComma -{ - if [[ -n "${svn_first}" ]]; then - svn_first="" - else - echo -n ", " - fi -} - -# Show the svn commit status. -function CommitStatus -{ - local added - local untracked - local modified - local deleted - local renamed - local copied - local unmerged - local missing - local out_of_date - check_if_up_to_date="" - svn status | while read -r line; do - if [[ ${line} =~ ^A ]]; then - if [[ -z "${added}" ]]; then - added=1 - MaybeEchoComma - echo -en "${CLI_ADDED_COLOR}Added${RESET}" - fi - elif [[ ${line} =~ ^\? ]]; then - if [[ -z "${untracked}" ]]; then - untracked=1 - MaybeEchoComma - echo -en "${CLI_UNTRACKED_COLOR}Untracked${RESET}" - fi - elif [[ ${line} =~ ^M ]]; then - if [[ -z "${modified}" ]]; then - modified=1 - MaybeEchoComma - echo -en "${CLI_MODIFIED_COLOR}Modified${RESET}" - fi - elif [[ ${line} =~ ^D ]]; then - if [[ -z "${deleted}" ]]; then - deleted=1 - MaybeEchoComma - echo -en "${CLI_DELETED_COLOR}Deleted${RESET}" - fi - elif [[ ${line} =~ ^R ]]; then - if [[ -z "${renamed}" ]]; then - renamed=1 - MaybeEchoComma - echo -en "${CLI_RENAMED_COLOR}Renamed${RESET}" - fi - elif [[ ${line} =~ ^C ]]; then - if [[ -z "${copied}" ]]; then - copied=1 - MaybeEchoComma - echo -en "${CLI_COPIED_COLOR}Copied${RESET}" - fi - elif [[ ${line} =~ ^! ]]; then - if [[ -z "${missing}" ]]; then - missing=1 - MaybeEchoComma - echo -en "${CLI_UNMERGED_COLOR}Missing${RESET}" - fi - fi - if [[ -z "${check_if_up_to_date}" ]]; then - check_if_up_to_date=1 - svn status -u | sed '$d' | while read -r status_line; do - if [[ ${status_line} =~ \* ]]; then - if [[ -z "${out_of_date}" ]]; then - out_of_date=1 - MaybeEchoComma - echo -en "${CLI_UNMERGED_COLOR}OutOfDate${RESET}" - fi - fi; - done; - fi; - done - if [[ -z "${check_if_up_to_date}" ]]; then - check_if_up_to_date=1 - svn status -u | sed '$d' | while read -r status_line; do - if [[ ${status_line} =~ \* ]]; then - if [[ -z "${out_of_date}" ]]; then - out_of_date=1 - MaybeEchoComma - echo -en "${CLI_UNMERGED_COLOR}OutOfDate${RESET}" - fi - fi; - done; - fi; - - return 0 -} - -function svnStatus -{ - svn_first="yes" - - # If we're inside a .svn directory, we can't find the branch / commit status. - if pwd | grep -q /.svn; then - return 0 - fi - - if svn info >/dev/null 2>&1; then - local svn_url=$(svn info | grep ^URL | cut -c6-) - svn_branch="" - if [[ ${svn_url} =~ "branches" ]]; then - svn_branch=${svn_url##*branches/} - svn_branch=$( echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;' ) - elif [[ ${svn_url} =~ "trunk" ]]; then - svn_branch="trunk" - elif [[ ${svn_url} =~ "tags" ]]; then - svn_branch=${svn_url##*tags/} - svn_branch="tags/$( echo ${svn_branch} | sed -r 's;(.*)/.*;\1;;' )" - else - svn_branch="Unknown" - fi - local svn_status=$(CommitStatus) - if [[ $? -eq 0 ]]; then - if [[ -z "${svn_status}" ]]; then - echo -e "$CLI_BRACKET_COLOR[$CLI_BRANCH_COLOR$svn_branch$CLI_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" - else - echo -e "$CLI_BRACKET_COLOR[$CLI_BRANCH_COLOR$svn_branch$CLI_BRACKET_COLOR]$RESET: $svn_status" - fi - fi - fi -} - -function Main -{ - Init - SetPrompt -} - -Main - - From 1c9f32d93d212912190b8b38ab3ce3b4a2223b2d Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Tue, 20 Nov 2018 07:56:33 -0500 Subject: [PATCH 06/11] - Update gitprompt: * GetStatus() change CommitStatus to SvnCommitStatus --- gitprompt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitprompt.sh b/gitprompt.sh index 3f1e9a8..0177bba 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -401,7 +401,7 @@ function GetStatus else svn_branch="Unknown" fi - local svn_status=$(CommitStatus) + local svn_status=$(SvnCommitStatus) if [[ $? -eq 0 ]]; then if [[ -z "${svn_status}" ]]; then echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$svn_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" From d58b494e447562aa235fde8ee1612075433cc691 Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Tue, 20 Nov 2018 09:19:10 -0500 Subject: [PATCH 07/11] - Update gitprompt: * Delete MaybeEchoComma * Delete the 'gs_first' global variable (it was used by the MaybeEchoComma function) * Add MakeStatus: similar to MaybeEchoComma, but makes the new status value (with or without a comma) * Update GitCommitStatus and SvnCommitStatus: - now uses the new MakeStatus method - Also they now pass in the values using a '<<<' redirect so that the 'status' variable doesn't become detached from the current shell - Update SvnCommitStatus to have a fall back "UNKNOWN STATUS" --- gitprompt.sh | 111 ++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 68 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 0177bba..534e8cf 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -203,21 +203,20 @@ function SetPrompt export PS1="\n\$(ExitStatus)$GIT_TIME_COLOR\$(date +%H:%M)$RESET $GIT_USERNAME_COLOR\u$GIT_AT_COLOR@$GIT_HOSTNAME_COLOR\h$RESET$hostalias $GIT_PWD_COLOR\w$RESET \$(GetStatus)\n\$ " } -# This is called before printing the each word in a list. The words should be -# comma separated, so it prints a comma unless the word it's supposed to print -# next is the FIRST word. -function MaybeEchoComma -{ - if [ ! -z "$gs_first" ]; then - gs_first= +function MakeStatus { + oldStatus="$1" + newStatus="$2" + if [[ -z "${oldStatus}" ]]; then + echo "${newStatus}" else - echo -n ", " + echo "${oldStatus}, ${newStatus}" fi } # Show the git commit status. function GitCommitStatus { + status="" local added local untracked local modified @@ -226,60 +225,57 @@ function GitCommitStatus local copied local unmerged local missing - git status -s --porcelain | while read -r line; do + while read -r line; do if [[ $line == A* ]]; then if [[ -z "$added" ]]; then added=1 - MaybeEchoComma - echo -en "${GIT_ADDED_COLOR}Added${RESET}" + status="$(MakeStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" fi elif [[ $line == \?\?* ]]; then if [[ -z "$untracked" ]]; then untracked=1 - MaybeEchoComma - echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" + status="$(MakeStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" fi elif [[ $line == M* ]]; then if [[ -z "$modified" ]]; then modified=1 - MaybeEchoComma - echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" + status="$(MakeStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" fi elif [[ $line == D* ]]; then if [[ -z "$deleted" ]]; then deleted=1 - MaybeEchoComma - echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" + status="$(MakeStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" fi elif [[ $line == R* ]]; then if [[ -z "$renamed" ]]; then renamed=1 - MaybeEchoComma - echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" + status="$(MakeStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" fi elif [[ $line == C* ]]; then if [[ -z "$copied" ]]; then copied=1 - echo -en ", ${GIT_COPIED_COLOR}Copied${RESET}" + status="$(MakeStatus "${status}" ", ${GIT_COPIED_COLOR}Copied${RESET}")" fi elif [[ $line == U* ]]; then if [[ -z "$unmerged" ]]; then copied=1 - MaybeEchoComma - echo -en "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}" + status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}")" fi else - echo "UNKNOWN STATUS" + status="UNKNOWN STATUS" return 1 fi - done - + done <<< $( git status -s --porcelain ) + if [[ -n "${status}" ]]; then + echo -en "${status}" + fi return 0 } # Show the svn commit status. function SvnCommitStatus { + status="" local added local untracked local modified @@ -289,85 +285,64 @@ function SvnCommitStatus local unmerged local missing local out_of_date - check_if_up_to_date="" - svn status | while read -r line; do + while read -r line; do if [[ ${line} =~ ^A ]]; then if [[ -z "${added}" ]]; then added=1 - MaybeEchoComma - echo -en "${GIT_ADDED_COLOR}Added${RESET}" + status="$(MakeStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" fi elif [[ ${line} =~ ^\? ]]; then if [[ -z "${untracked}" ]]; then untracked=1 - MaybeEchoComma - echo -en "${GIT_UNTRACKED_COLOR}Untracked${RESET}" + status="$(MakeStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" fi elif [[ ${line} =~ ^M ]]; then if [[ -z "${modified}" ]]; then modified=1 - MaybeEchoComma - echo -en "${GIT_MODIFIED_COLOR}Modified${RESET}" + status="$(MakeStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" fi elif [[ ${line} =~ ^D ]]; then if [[ -z "${deleted}" ]]; then deleted=1 - MaybeEchoComma - echo -en "${GIT_DELETED_COLOR}Deleted${RESET}" + status="$(MakeStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" fi elif [[ ${line} =~ ^R ]]; then if [[ -z "${renamed}" ]]; then renamed=1 - MaybeEchoComma - echo -en "${GIT_RENAMED_COLOR}Renamed${RESET}" + status="$(MakeStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" fi elif [[ ${line} =~ ^C ]]; then if [[ -z "${copied}" ]]; then copied=1 - MaybeEchoComma - echo -en "${GIT_COPIED_COLOR}Copied${RESET}" + status="$(MakeStatus "${status}" "${GIT_COPIED_COLOR}Copied${RESET}")" fi elif [[ ${line} =~ ^! ]]; then if [[ -z "${missing}" ]]; then missing=1 - MaybeEchoComma - echo -en "${GIT_UNMERGED_COLOR}Missing${RESET}" + status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}Missing${RESET}")" + fi + else + status="UNKNOWN STATUS" + return 1 + fi + done <<< $( svn status ) + svn status -u | sed '$d' | while read -r status_line; do + if [[ ${status_line} =~ \* ]]; then + if [[ -z "${out_of_date}" ]]; then + out_of_date=1 + status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}OutOfDate${RESET}")" + echo "${status}" fi fi - if [[ -z "${check_if_up_to_date}" ]]; then - check_if_up_to_date=1 - svn status -u | sed '$d' | while read -r status_line; do - if [[ ${status_line} =~ \* ]]; then - if [[ -z "${out_of_date}" ]]; then - out_of_date=1 - MaybeEchoComma - echo -en "${GIT_UNMERGED_COLOR}OutOfDate${RESET}" - fi - fi; - done; - fi; done - if [[ -z "${check_if_up_to_date}" ]]; then - check_if_up_to_date=1 - svn status -u | sed '$d' | while read -r status_line; do - if [[ ${status_line} =~ \* ]]; then - if [[ -z "${out_of_date}" ]]; then - out_of_date=1 - MaybeEchoComma - echo -en "${GIT_UNMERGED_COLOR}OutOfDate${RESET}" - fi - fi; - done; + if [[ -n "${status}" ]]; then + echo -en "${status}" fi - return 0 } function GetStatus { - - gs_first=1 - # If we're inside a .git directory, we can't find the branch / commit status. if pwd | grep -q /.git; then return 0 From bd3b1c80e336e3e3a23ef684841c242d06e9b8ca Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Tue, 20 Nov 2018 09:20:55 -0500 Subject: [PATCH 08/11] - Update README.md: update version number to match gitprompt.sh value --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c9179f9..e1ee650 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Project status -The current version is 1.2.1. +The current version is 2.0.0. A brief article about it has been published in Hacker Noon: https://hackernoon.com/why-linux-developers-should-use-gitprompt-8d654e5b87e1 From 76f7c0f6773788da758412fa4edc980b216ca5f7 Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Tue, 20 Nov 2018 09:29:03 -0500 Subject: [PATCH 09/11] - Update gitprompt.sh: * remove extra 'echo' and pass value to while loop using "<<<" --- gitprompt.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 534e8cf..53fd10d 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -326,15 +326,14 @@ function SvnCommitStatus return 1 fi done <<< $( svn status ) - svn status -u | sed '$d' | while read -r status_line; do + while read -r status_line; do if [[ ${status_line} =~ \* ]]; then if [[ -z "${out_of_date}" ]]; then out_of_date=1 status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}OutOfDate${RESET}")" - echo "${status}" fi fi - done + done <<< $( svn status -u | sed '$d' ) if [[ -n "${status}" ]]; then echo -en "${status}" fi From e6dcb7834c65e675395944dedaba3840fe21bf1e Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Wed, 21 Nov 2018 09:49:15 -0500 Subject: [PATCH 10/11] - Update gitprompt.sh: * Rename MakeStatus to UpdateStatus * Update GitCommitStatus and SvnCommitStatus - Add default values for local variables - Merge nested if statements into a single and status * Update SvnCommitStatus: * Add '--non-interactive --ignore-externals' to the svn status command --- gitprompt.sh | 173 ++++++++++++++++++++------------------------------- 1 file changed, 69 insertions(+), 104 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 53fd10d..0be46cd 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -203,7 +203,7 @@ function SetPrompt export PS1="\n\$(ExitStatus)$GIT_TIME_COLOR\$(date +%H:%M)$RESET $GIT_USERNAME_COLOR\u$GIT_AT_COLOR@$GIT_HOSTNAME_COLOR\h$RESET$hostalias $GIT_PWD_COLOR\w$RESET \$(GetStatus)\n\$ " } -function MakeStatus { +function UpdateStatus { oldStatus="$1" newStatus="$2" if [[ -z "${oldStatus}" ]]; then @@ -217,55 +217,39 @@ function MakeStatus { function GitCommitStatus { status="" - local added - local untracked - local modified - local deleted - local renamed - local copied - local unmerged - local missing + local added="" + local untracked="" + local modified="" + local deleted="" + local renamed="" + local copied="" + local unmerged="" + local missing="" + local unknown="" while read -r line; do - if [[ $line == A* ]]; then - if [[ -z "$added" ]]; then - added=1 - status="$(MakeStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" - fi - elif [[ $line == \?\?* ]]; then - if [[ -z "$untracked" ]]; then - untracked=1 - status="$(MakeStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" - fi - elif [[ $line == M* ]]; then - if [[ -z "$modified" ]]; then - modified=1 - status="$(MakeStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" - fi - elif [[ $line == D* ]]; then - if [[ -z "$deleted" ]]; then - deleted=1 - status="$(MakeStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" - fi - elif [[ $line == R* ]]; then - if [[ -z "$renamed" ]]; then - renamed=1 - status="$(MakeStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" - fi - elif [[ $line == C* ]]; then - if [[ -z "$copied" ]]; then - copied=1 - status="$(MakeStatus "${status}" ", ${GIT_COPIED_COLOR}Copied${RESET}")" - fi - elif [[ $line == U* ]]; then - if [[ -z "$unmerged" ]]; then - copied=1 - status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}")" - fi - else - status="UNKNOWN STATUS" - return 1 + if [[ ${line} == A* && -z "$added" ]]; then + added=1 + status="$(UpdateStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" + elif [[ ${line} == \?\?* && -z "$untracked" ]]; then + untracked=1 + status="$(UpdateStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" + elif [[ ${line} == M* && -z "$modified" ]]; then + modified=1 + status="$(UpdateStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" + elif [[ ${line} == D* && -z "$deleted" ]]; then + deleted=1 + status="$(UpdateStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" + elif [[ ${line} == R* && -z "$renamed" ]]; then + renamed=1 + status="$(UpdateStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" + elif [[ ${line} == C* && -z "$copied" ]]; then + copied=1 + status="$(UpdateStatus "${status}" ", ${GIT_COPIED_COLOR}Copied${RESET}")" + elif [[ ${line} == U* && -z "$unmerged" ]]; then + copied=1 + status="$(UpdateStatus "${status}" "${GIT_UNMERGED_COLOR}Updated-but-unmerged${RESET}")" fi - done <<< $( git status -s --porcelain ) + done <<< $(git status -s --porcelain) if [[ -n "${status}" ]]; then echo -en "${status}" fi @@ -276,64 +260,47 @@ function GitCommitStatus function SvnCommitStatus { status="" - local added - local untracked - local modified - local deleted - local renamed - local copied - local unmerged - local missing - local out_of_date + local added="" + local untracked="" + local modified="" + local deleted="" + local renamed="" + local copied="" + local unmerged="" + local missing="" + local out_of_date="" while read -r line; do - if [[ ${line} =~ ^A ]]; then - if [[ -z "${added}" ]]; then - added=1 - status="$(MakeStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" - fi - elif [[ ${line} =~ ^\? ]]; then - if [[ -z "${untracked}" ]]; then - untracked=1 - status="$(MakeStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" - fi - elif [[ ${line} =~ ^M ]]; then - if [[ -z "${modified}" ]]; then - modified=1 - status="$(MakeStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" - fi - elif [[ ${line} =~ ^D ]]; then - if [[ -z "${deleted}" ]]; then - deleted=1 - status="$(MakeStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" - fi - elif [[ ${line} =~ ^R ]]; then - if [[ -z "${renamed}" ]]; then - renamed=1 - status="$(MakeStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" - fi - elif [[ ${line} =~ ^C ]]; then - if [[ -z "${copied}" ]]; then - copied=1 - status="$(MakeStatus "${status}" "${GIT_COPIED_COLOR}Copied${RESET}")" - fi - elif [[ ${line} =~ ^! ]]; then - if [[ -z "${missing}" ]]; then - missing=1 - status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}Missing${RESET}")" - fi - else - status="UNKNOWN STATUS" - return 1 + if [[ ${line} =~ ^A && -z "${added}" ]]; then + added=1 + status="$(UpdateStatus "${status}" "${GIT_ADDED_COLOR}Added${RESET}")" + elif [[ ${line} =~ ^\? && -z "${untracked}" ]]; then + untracked=1 + status="$(UpdateStatus "${status}" "${GIT_UNTRACKED_COLOR}Untracked${RESET}")" + elif [[ ( ${line} =~ ^M ) && ( -z "${modified}" ) ]]; then + modified=1 + status="$(UpdateStatus "${status}" "${GIT_MODIFIED_COLOR}Modified${RESET}")" + elif [[ ${line} =~ ^D && -z "${deleted}" ]]; then + deleted=1 + status="$(UpdateStatus "${status}" "${GIT_DELETED_COLOR}Deleted${RESET}")" + elif [[ ${line} =~ ^R && -z "${renamed}" ]]; then + renamed=1 + status="$(UpdateStatus "${status}" "${GIT_RENAMED_COLOR}Renamed${RESET}")" + elif [[ ${line} =~ ^C && -z "${copied}" ]]; then + copied=1 + status="$(UpdateStatus "${status}" "${GIT_COPIED_COLOR}Copied${RESET}")" + elif [[ ${line} =~ ^! && -z "${missing}" ]]; then + missing=1 + status="$(UpdateStatus "${status}" "${GIT_UNMERGED_COLOR}Missing${RESET}")" fi - done <<< $( svn status ) + done <<< $(svn status --non-interactive --ignore-externals) while read -r status_line; do if [[ ${status_line} =~ \* ]]; then if [[ -z "${out_of_date}" ]]; then out_of_date=1 - status="$(MakeStatus "${status}" "${GIT_UNMERGED_COLOR}OutOfDate${RESET}")" + status="$(UpdateStatus "${status}" "${GIT_UNMERGED_COLOR}OutOfDate${RESET}")" fi fi - done <<< $( svn status -u | sed '$d' ) + done <<<"$(svn status -u | sed '$d')" if [[ -n "${status}" ]]; then echo -en "${status}" fi @@ -352,12 +319,10 @@ function GetStatus gs_gitstatus=$(GitCommitStatus) - if [ $? -eq 0 ]; then - if [ -z "$gs_gitstatus" ]; then - echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" - else - echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: $gs_gitstatus" - fi + if [[ $? -eq 0 && -z "$gs_gitstatus" ]]; then + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: ${GREEN}Up-to-date${RESET}" + else + echo -e "$GIT_BRACKET_COLOR[$GIT_BRANCH_COLOR$gs_branch$GIT_BRACKET_COLOR]$RESET: $gs_gitstatus" fi fi From 250b2b973ceb9f246529a9b9928739c51e2d1ef6 Mon Sep 17 00:00:00 2001 From: Craig Moore Date: Wed, 21 Nov 2018 09:59:39 -0500 Subject: [PATCH 11/11] - Add .gitignore file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29b636a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +*.iml \ No newline at end of file