Skip to content

Instantly share code, notes, and snippets.

@Ragnoroct
Created February 26, 2020 19:45
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f to your computer and use it in GitHub Desktop.
Save Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f to your computer and use it in GitHub Desktop.
Blazing fast simple git branch name for ps1
# Copyright (c) 2019 Will Bender. All rights reserved.
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
# Very fast __git_ps1 implementation
# Inspired by https://gist.github.com/wolever/6525437
# Mainly this is useful for Windows users stuck on msys, cygwin, or slower wsl 1.0 because git/fs operations are just slower
# Caching can be added by using export but PROMPT_COMMAND is necessary since $() is a subshell and cannot modify parent state.
# Linux: time __ps1_ps1 (~7ms)
# Windows msys2: time __git_ps1 (~100ms)
# Windows msys2: time git rev-parse --abbrev-ref HEAD 2> /dev/null (~86ms)
# Windows msys2: time __fastgit_ps1 (~1-3ms)
# Simple PS1 without colors using format arg. Feel free to use PROMPT_COMMAND
export PS1="\u@\h \w \$(__fastgit_ps1 '[%s] ')$ "
# 100% pure Bash (no forking) function to determine the name of the current git branch
function __fastgit_ps1 () {
local headfile head branch
local dir="$PWD"
while [ -n "$dir" ]; do
if [ -e "$dir/.git/HEAD" ]; then
headfile="$dir/.git/HEAD"
break
fi
dir="${dir%/*}"
done
if [ -e "$headfile" ]; then
read -r head < "$headfile" || return
case "$head" in
ref:*) branch="${head##*/}" ;;
"") branch="" ;;
*) branch="${head:0:7}" ;; #Detached head. You can change the format for this too.
esac
fi
if [ -z "$branch" ]; then
return 0
fi
if [ -z "$1" ]; then
# Default format
printf "(%s) " "$branch"
else
# Use passed format string
printf "$1" "$branch"
fi
}
@patricknelson
Copy link

And thank you for this! I took your advice from the header and optimized it slightly more so it uses PROMPT_COMMAND.

In your case, you omit the branch entirely in case it's not set, which is good. In my case, it doesn't rely on executing the function separately (which appears to slow it down very slightly), so what I do instead is just wrap the $branch in parenthesis if it's not empty (and also put it at the end of the line and break to the next line for my $ prompt).

p.s. How did you time this?

# Very very fast __git_ps1 implementation 
# 100% pure Bash (no forking) function to determine the name of the current git branch
# Modified from: https://gist.github.com/Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f
# Which was inspired by https://gist.github.com/wolever/6525437
function __fastgit_ps1 () {
    branch=""

    local headfile head branch
    local dir="$PWD"

    while [ -n "$dir" ]; do
        if [ -e "$dir/.git/HEAD" ]; then
            headfile="$dir/.git/HEAD"
            break
        fi
        dir="${dir%/*}"
    done

    if [ -e "$headfile" ]; then
        read -r head < "$headfile" || return
        case "$head" in
            ref:*) branch="${head##*/}" ;;
            "") branch="" ;;
            *) branch="${head:0:7}" ;;  #Detached head. You can change the format for this too.
        esac
    fi

    if [ ! -z "$branch" ]; then
        branch="($branch)"
    fi

    # Edit to suit your needs. Note the branch will be wrapped in parenthesis if it's set. Completely empty otherwise.
    export PS1="\[\e]0;\W\a\]\n\[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \[\033[36m\]$branch\[\033[0m\]\n\$ "
}
export PROMPT_COMMAND=__fastgit_ps1

@Ragnoroct
Copy link
Author

@patricknelson
Nice!!

Haha, I just ran time __fastgit_ps1 :P Nothing crazy so it might not be totally accurate.

@patricknelson
Copy link

Ah, duh! Makes sense. In that case for me it's like 1ms which is pretty good, considering how miserably slow this computer can be at times. Interestingly in another window, it consistently runs 2ms - 3ms. Compare that to the ~500ms for __git_ps1 in git-prompt.sh!

It's insane I put up with that for so long. That is, the lag of sitting there and literally just waiting for my prompt to reappear after simply pressing enter.

@bingzhangdai
Copy link

bingzhangdai commented Mar 20, 2021

Greate idea, but this does not work if you are under git submodules.

david@Ubuntu2:/m/c/U/b/R/b/contrib[de69ed6]$ cat .git
gitdir: ../.git/modules/contrib

Modified a little bit, the following function will print current branch

function _get_git_branch() {
    local _head_file _head
    local _dir="$PWD"

    while [[ -n "$_dir" ]]; do
        _head_file="$_dir/.git/HEAD"
        if [[ -f "$_dir/.git" ]]; then
            read -r _head_file < "$_dir/.git" && _head_file="$_dir/${_head_file#gitdir: }/HEAD"
        fi
        [[ -e "$_head_file" ]] && break
        _dir="${_dir%/*}"
    done

    if [[ -e "$_head_file" ]]; then
        read -r _head < "$_head_file" || return
        case "$_head" in
            ref:*) printf "${_head#ref: refs/heads/}" ;;
            "") ;;
            # HEAD detached
            *) printf "${_head:0:9}" ;;
        esac
        return 0
    fi

    return 1
}

my gist: https://gist.github.com/bingzhangdai/dd4e283a14290c079a76c4ba17f19d69

@GitCodeDestroyer
Copy link

Hello everyone! Can someone explain where I should add this code or what I should do with it?

@GitCodeDestroyer
Copy link

That's would be very nice!

@patricknelson
Copy link

patricknelson commented Dec 29, 2021

@GitCodeDestroyer place it in your bash startup script. That varies widely depending on your setup (and if you even use bash as your command line interpreter). For example, for me on Windows using Cygwin, it's a file named .bash_profile. On many MacOS systems, people use .profile and .bash_login and still in some others it's called .bashrc. 🤷‍♂️

The file will be stored in your home directory; you can find it via typing:

# switch to home directory
cd

# show the current directory path
pwd

# show all files, including the hidden dot files
ls -lah

@megholm
Copy link

megholm commented Oct 14, 2022

Yeah! +1
Thanks, Patrick and Ragnoroct!

@avinmaster
Copy link

@patricknelson Thanks!!)

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