Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active October 23, 2023 20:03
Show Gist options
  • Save rbobillot/641a4476bf1c9f7338e9ca356717bb37 to your computer and use it in GitHub Desktop.
Save rbobillot/641a4476bf1c9f7338e9ca356717bb37 to your computer and use it in GitHub Desktop.
Simple Bash tool, displaying current branch, in the current Git repository
#!/bin/bash
export SHOW_BRANCH=yes
function git_working_status {
if [[ `git status 2> /dev/null` != "" && `echo $SHOW_BRANCH` == "yes" ]]
then
current_branch=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git status 2> /dev/null | head -1 | cut -d ' ' -f3`
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_branch.. 2> /dev/null`
current_diff=`LANG=en_US.UTF-8 LANGUAGE=en_US:en git diff origin/$current_branch 2>&1`
color="\001\033[92m\002"
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="[\u2387 ${local}${color}${current_branch}\001\033[0m\002]"
echo -en "$status" > /tmp/git_status.txt
echo -en "$status"
# TODO: detect if branch is up to date (compare with remote branch)
# 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
unset color_prompt force_color_prompt
@rbobillot
Copy link
Author

rbobillot commented Oct 16, 2023

You can paste this code in a ~/.prompt_show_branch file

Then, in your ~/.bashrc, remove or comment the default PS1 setting, and source the ~/.prompt_show_branch file

#if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#fi
#unset color_prompt force_color_prompt

PROMPT_SHOW_BRANCH_FILE="$HOME/.prompt_show_branch"
[[ -f $PROMPT_SHOW_BRANCH_FILE ]] && source $PROMPT_SHOW_BRANCH_FILE

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