I have a function here and wondered how well this can be refactored.
I currently have this:
status=`git status 2>&1 | tee` dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"` untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"` ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"` newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"` renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"` deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"` bits='' if [ "${renamed}" == "0" ]; then bits=">${bits}" fi if [ "${ahead}" == "0" ]; then bits="*${bits}" fi if [ "${newfile}" == "0" ]; then bits="+${bits}" fi if [ "${untracked}" == "0" ]; then bits="?${bits}" fi if [ "${deleted}" == "0" ]; then bits="x${bits}" fi if [ "${dirty}" == "0" ]; then bits="!${bits}" fi if [ ! "${bits}" == "" ]; then echo " ${bits}" else echo "" fi
I have played with it a little bit but I'm not an expert with bash so refactoring this is a little tricky, here is my attempt:
gitstatus=( "renamed;renamed:;>" "dirty;modified:;!" "untracked;Untracked files;?" "ahead;Your branch is ahead of;*" "newfile;new file:;+" "deleted;deleted:;x" ) bits='' for status in "${gitstatus[@]}"; do value=(${status//;/ }) check=`echo -n "${value[0]}" 2> /dev/null | grep "${value[1]}" &> /dev/null; echo "$?"` if [ "${check}" == "0" ]; then bits="${value[2]}${bits}" fi done if [ ! "${bits}" == "" ]; then echo " ${bits}" else echo "" fi
--porcelain
option, which is intended to give output that's easier to parse?\$\endgroup\$