Last active
January 15, 2025 21:14
-
-
Save juliyvchirkov/883a050c34e23f4b4fba6ecaeb050c3b to your computer and use it in GitHub Desktop.
sh: simplified custom function __git_ps1 to display current branch or tag name in git repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############ *posix* ############# | |
# shellcheck shell=sh enable=all # | |
################################## | |
# | |
# If one is missing the original *__git_ps1* function from @git/git package, this wrapper can be utilized as | |
# temporary solution | |
# | |
# @see https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh | |
# | |
# Just like the original, it delivers PS1 prompt in both ways thru export or output to stdout, supplied either with | |
# a current branch name like *main*, *dev* etc. or tags like *v1.4.0* or with nothing, if current working directory | |
# is outside of any git tree | |
# | |
# Itʼs a pity this implementation can offer no colorized PS1 output like *__git_ps1* function from @git/git package | |
# does, but thatʼs ok for less than 30 lines of code vs over 500 lines of original | |
# | |
# Designed and developed by Juliy V. Chirkov https://juliyvchirkov.dev at 2021-2023 under the MIT lisense | |
# https://t.me/juliyvchirkov | |
# | |
################################################################################################################### | |
# | |
# At 2023 Iʼve totally turned away in daily shell scripting from *bash* in favor of fully *posix* compliant way of | |
# implementation focused on sh, the standard command language interpreter | |
# | |
# @see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html | |
# | |
# The thing is after the pros and cons has been completely reviewed, Iʼve found it quite reasonable to reject minor | |
# amenities *bash* delivers to simplify a coding routine for the sake of portability to develop things that are | |
# a priori blessed to success on any device regardless of a shell. *bash*, *dash*, *ksh*, *posh*, *yash*, *zsh*, | |
# whatever - a code will run everywhere the same way as long as a shell honours *posix* standards | |
# | |
# Long story short, during summer and fall of 2023 Iʼve revised and reimplemented from scratch the lionʼs share of | |
# own shell scripts, and this wrapper has got its code upgrade among others | |
[ -z "${SHDEBUG}" ] || set -vx | |
__git_ps1() { | |
__git_ps1Template="${3:-${1:- %s}}" | |
__git_ps1Branch="$(/usr/bin/git symbolic-ref -q --short HEAD 2>/dev/null)" | |
if [ -z "${__git_ps1Branch}" ]; then | |
__git_ps1Branch="$(/usr/bin/git describe --tags --exact-match 2>/dev/null)" | |
[ -z "${__git_ps1Branch}" ] || __git_ps1Branch="(${__git_ps1Branch})" | |
fi | |
if [ -n "${__git_ps1Branch}" ]; then | |
if [ "${__git_ps1Template%\%s*}" = "${__git_ps1Template}" ]; then | |
__git_ps1Template="${__git_ps1Template} %s" | |
fi | |
if [ $# -lt 2 ]; then | |
############################# | |
# shellcheck disable=SC2059 # | |
############################# | |
command printf -- "${__git_ps1Template}" "${__git_ps1Branch}" | |
else | |
############################# | |
# shellcheck disable=SC2059 # | |
############################# | |
PS1="${1}$(command printf -- "${__git_ps1Template}" "${__git_ps1Branch}")${2}" | |
export PS1 >/dev/null 2>&1 | |
fi | |
fi | |
unset __git_ps1Template __git_ps1Branch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment