#!/bin/bash # Thanks alrra, https://github.com/alrra/dotfiles/blob/main/src/shell/bash_prompt branchName="" tmp="" sync="" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check if the current directory is in a Git repository. ! git rev-parse &>/dev/null \ && exit # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check if in `.git/` directory (some of the following # checks don't make sense/won't work in the `.git` directory). [ "$(git rev-parse --is-inside-git-dir)" == "true" ] \ && exit # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check for uncommitted changes in the index. if ! git diff --quiet --ignore-submodules --cached; then tmp="$tmp+"; fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check for unstaged changes. if ! git diff-files --quiet --ignore-submodules --; then tmp="$tmp*"; fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check for untracked files. if [ -n "$(git ls-files --others --exclude-standard)" ]; then tmp="$tmp?"; fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Check for stashed files. if git rev-parse --verify refs/stash &>/dev/null; then tmp="$tmp""[s]"; fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [ -z "$tmp" ] && \ tmp="~"; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - branchName="$( printf "%s" "$( git rev-parse --abbrev-ref HEAD 2> /dev/null \ || git rev-parse --short HEAD 2> /dev/null \ || printf " (unknown)" )" | \ tr -d "\n" )" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Count how many commits behind and ahead we are. if git rev-list --left-right --count "$branchName"...origin/"$branchName" &>/dev/null; then read -ra counts <<< "$(git rev-list --left-right --count "$branchName"...origin/"$branchName")" to_push=${counts[0]} to_pull=${counts[1]} sync=$([[ ${counts[0]} -eq 0 && ${counts[1]} -eq 0 ]] || echo " $to_push↑ $to_pull↓") fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - printf "%s" "($branchName$tmp$sync)"