Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active July 12, 2021 13:38
Show Gist options
  • Save rbobillot/710df71012c7d43db95846e83898cba6 to your computer and use it in GitHub Desktop.
Save rbobillot/710df71012c7d43db95846e83898cba6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# UPDATE PROMPT WITH LAST VERSION
# TO INSTALL:
# Make sure in your ~/.bashrc that:
# - the line '[[ -s ~/.showbranch ]] && source ~/.showbranch' doesn't exist or is commented
#
# Then simply run:
# - curl "https://gist.githubusercontent.com`curl -s https://gist.github.com/rbobillo/710df71012c7d43db95846e83898cba6 | grep '>Raw<' | sed 's/.*href="\([^ ]*\)".*/\1/'`" > ~/.showbranch && echo '[[ -s ~/.showbranch ]] && source ~/.showbranch' >> ~/.bashrc && source ~/.bashrc
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_prompt=yes
else
color_prompt=
fi
fi
export SHOW_BRANCH=yes
export SHOW_DIFF=yes
function fetch_git {
current_git_branch=$1
diff_path=$2
files=(`ls -1 /tmp/${diff_path}* 2> /dev/null`)
timestamp=`date +%s`
prev_time=$([[ ${#files[@]} -gt 0 ]] && basename `echo ${files[-1]} | tr '_' '/'` || echo $(($timestamp - 11)))
echo ${#files[@]} $prev_time $(($timestamp - $prev_time)) > /tmp/qty
if [[ $(($timestamp - $prev_time)) -gt 10 ]]; then
if [[ ${#files[@]} == 0 ]]; then
touch "/tmp/${diff_path}_${timestamp}"
else
mv "/tmp/${diff_path}_${prev_time}" "/tmp/${diff_path}_${timestamp}"
fi
(git fetch --quiet origin ${current_git_branch} 2> /dev/null) &
fi
}
diff_arrays(){
awk 'BEGIN{RS=ORS=" "}
{NR==FNR?a[$0]++:a[$0]--}
END{for(k in a)if(a[k])print k}' <(echo -n "${!1}") <(echo -n "${!2}")
}
function diff_local_remote {
current_git_branch=$1
current_unt_status=$2
current_add_status=$3
current_repo_name=$(dirname `pwd | sed -e "s@$HOME/@@g"`)
[[ $current_repo_name = "." ]] && current_repo_name=$(basename `pwd`)
diff_path=".$(echo "$HOME/$current_repo_name/$current_git_branch" | tr '/' '_')"
fetch_git $current_git_branch $diff_path > /dev/null
local_changes=(`git status -s -uall | cut -d ' ' -f2-192 | tr ' ' '-'`)
remote_and_local_changes=(`git diff --numstat origin/master | cut -d$'\t' -f3-192 | tr ' ' '-'`)
remote_changes=(`diff_arrays $remote_and_local_changes $local_changes 2> /dev/null`)
local_changes_qty=${#local_changes[@]}
remote_changes_qty=${#remote_changes[@]}
# TODO: more precise diff log (untracked / committed or not ...)
[[ $local_changes_qty -gt 0 ]] && echo -ne " ↑${local_changes_qty}" || echo -ne ""
[[ $remote_changes_qty -gt 0 ]] && echo -ne " ↓${remote_changes_qty}" || echo -ne ""
}
function git_working_status {
if [[ `git status 2> /dev/null` != "" && `echo $SHOW_BRANCH` == "yes" ]]
then
current_git_branch=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git branch --show-current`
current_unt_status=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git status | egrep -i "untracked files|changes not staged"`
current_add_status=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git status | egrep -i "changes to be committed"`
current_log_status=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git log origin/$current_git_branch.. 2> /dev/null`
current_diff=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git diff origin/$current_git_branch 2>&1`
color="\001\033[92m\002"
remote_diff=`[[ $SHOW_DIFF == "yes" ]] && diff_local_remote $current_git_branch $current_unt_status $current_add_status || echo -ne ""`
if [[ $current_diff =~ "fatal" ]]; then local="\001\033[96m\002local\001\033[0m\002: "; fi
if [[ -n $current_unt_status ]]; then color="\001\033[31m\002" # repository modif: [branch name in RED]
elif [[ -n $current_add_status ]]; then color="\001\033[33m\002" # files added to git: [branch name in YELLOW]
elif [[ -n $current_log_status ]]; then color="\001\033[93;1m\002* \001\033[92m\002" # files commited: [YELLOW star + branch name in LIGHT GREEN]
fi
status="[${local}${color} \b${current_git_branch}\001\033[0m\002 \b${remote_diff}]" # the ' \b' is used to avoid wrong display if branch name is not an alpha string
echo -en "$status"
# TODO: optimise remote_diff if possible
# TODO: detect deleted remote branch
fi
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0m\]`git_working_status`\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\[`git_working_status`\]\$ '
fi
@rbobillot
Copy link
Author

prompt-show-branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment