Created
April 13, 2024 08:48
-
-
Save ajgappmark/35a63fdda565a7090522e0ffe6360eac to your computer and use it in GitHub Desktop.
git ps1 to display branch name info for bash, works on debian, arch
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
#__git_sequencer_status is a function | |
function __git_sequencer_status () | |
{ | |
local todo; | |
if test -f "$g/CHERRY_PICK_HEAD"; then | |
r="|CHERRY-PICKING"; | |
return 0; | |
else | |
if test -f "$g/REVERT_HEAD"; then | |
r="|REVERTING"; | |
return 0; | |
else | |
if __git_eread "$g/sequencer/todo" todo; then | |
case "$todo" in | |
p[\ \ ] | pick[\ \ ]*) | |
r="|CHERRY-PICKING"; | |
return 0 | |
;; | |
revert[\ \ ]*) | |
r="|REVERTING"; | |
return 0 | |
;; | |
esac; | |
fi; | |
fi; | |
fi; | |
return 1 | |
} | |
# __git_eread is a function | |
function __git_eread () | |
{ | |
test -r "$1" && IFS=' | |
' read "$2" < "$1" | |
} | |
# __git_ps1 is a function | |
function __git_ps1 () | |
{ | |
local exit=$?; | |
local pcmode=no; | |
local detached=no; | |
local ps1pc_start='\u@\h:\w '; | |
local ps1pc_end='\$ '; | |
local printf_format=' (%s)'; | |
case "$#" in | |
2 | 3) | |
pcmode=yes; | |
ps1pc_start="$1"; | |
ps1pc_end="$2"; | |
printf_format="${3:-$printf_format}"; | |
PS1="$ps1pc_start$ps1pc_end" | |
;; | |
0 | 1) | |
printf_format="${1:-$printf_format}" | |
;; | |
*) | |
return $exit | |
;; | |
esac; | |
local ps1_expanded=yes; | |
[ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no; | |
[ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no; | |
local repo_info rev_parse_exit_code; | |
repo_info="$(git rev-parse --git-dir --is-inside-git-dir --is-bare-repository --is-inside-work-tree --short HEAD 2>/dev/null)"; | |
rev_parse_exit_code="$?"; | |
if [ -z "$repo_info" ]; then | |
return $exit; | |
fi; | |
local short_sha=""; | |
if [ "$rev_parse_exit_code" = "0" ]; then | |
short_sha="${repo_info##* | |
}"; | |
repo_info="${repo_info% | |
*}"; | |
fi; | |
local inside_worktree="${repo_info##* | |
}"; | |
repo_info="${repo_info% | |
*}"; | |
local bare_repo="${repo_info##* | |
}"; | |
repo_info="${repo_info% | |
*}"; | |
local inside_gitdir="${repo_info##* | |
}"; | |
local g="${repo_info% | |
*}"; | |
if [ "true" = "$inside_worktree" ] && [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] && [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] && git check-ignore -q .; then | |
return $exit; | |
fi; | |
local sparse=""; | |
if [ -z "${GIT_PS1_COMPRESSSPARSESTATE-}" ] && [ -z "${GIT_PS1_OMITSPARSESTATE-}" ] && [ "$(git config --bool core.sparseCheckout)" = "true" ]; then | |
sparse="|SPARSE"; | |
fi; | |
local r=""; | |
local b=""; | |
local step=""; | |
local total=""; | |
if [ -d "$g/rebase-merge" ]; then | |
__git_eread "$g/rebase-merge/head-name" b; | |
__git_eread "$g/rebase-merge/msgnum" step; | |
__git_eread "$g/rebase-merge/end" total; | |
r="|REBASE"; | |
else | |
if [ -d "$g/rebase-apply" ]; then | |
__git_eread "$g/rebase-apply/next" step; | |
__git_eread "$g/rebase-apply/last" total; | |
if [ -f "$g/rebase-apply/rebasing" ]; then | |
__git_eread "$g/rebase-apply/head-name" b; | |
r="|REBASE"; | |
else | |
if [ -f "$g/rebase-apply/applying" ]; then | |
r="|AM"; | |
else | |
r="|AM/REBASE"; | |
fi; | |
fi; | |
else | |
if [ -f "$g/MERGE_HEAD" ]; then | |
r="|MERGING"; | |
else | |
if __git_sequencer_status; then | |
:; | |
else | |
if [ -f "$g/BISECT_LOG" ]; then | |
r="|BISECTING"; | |
fi; | |
fi; | |
fi; | |
fi; | |
if [ -n "$b" ]; then | |
:; | |
else | |
if [ -h "$g/HEAD" ]; then | |
b="$(git symbolic-ref HEAD 2>/dev/null)"; | |
else | |
local head=""; | |
if ! __git_eread "$g/HEAD" head; then | |
return $exit; | |
fi; | |
b="${head#ref: }"; | |
if [ "$head" = "$b" ]; then | |
detached=yes; | |
b="$( | |
case "${GIT_PS1_DESCRIBE_STYLE-}" in | |
(contains) | |
git describe --contains HEAD ;; | |
(branch) | |
git describe --contains --all HEAD ;; | |
(tag) | |
git describe --tags HEAD ;; | |
(describe) | |
git describe HEAD ;; | |
(* | default) | |
git describe --tags --exact-match HEAD ;; | |
esac 2>/dev/null)" || b="$short_sha..."; | |
b="($b)"; | |
fi; | |
fi; | |
fi; | |
fi; | |
if [ -n "$step" ] && [ -n "$total" ]; then | |
r="$r $step/$total"; | |
fi; | |
local w=""; | |
local i=""; | |
local s=""; | |
local u=""; | |
local h=""; | |
local c=""; | |
local p=""; | |
if [ "true" = "$inside_gitdir" ]; then | |
if [ "true" = "$bare_repo" ]; then | |
c="BARE:"; | |
else | |
b="GIT_DIR!"; | |
fi; | |
else | |
if [ "true" = "$inside_worktree" ]; then | |
if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] && [ "$(git config --bool bash.showDirtyState)" != "false" ]; then | |
git diff --no-ext-diff --quiet || w="*"; | |
git diff --no-ext-diff --cached --quiet || i="+"; | |
if [ -z "$short_sha" ] && [ -z "$i" ]; then | |
i="#"; | |
fi; | |
fi; | |
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] && git rev-parse --verify --quiet refs/stash > /dev/null; then | |
s="$"; | |
fi; | |
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] && [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] && git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' > /dev/null 2> /dev/null; then | |
u="%${ZSH_VERSION+%}"; | |
fi; | |
if [ -n "${GIT_PS1_COMPRESSSPARSESTATE-}" ] && [ "$(git config --bool core.sparseCheckout)" = "true" ]; then | |
h="?"; | |
fi; | |
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then | |
__git_ps1_show_upstream; | |
fi; | |
fi; | |
fi; | |
local z="${GIT_PS1_STATESEPARATOR-" "}"; | |
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then | |
if [ $pcmode = yes ] || [ -n "${ZSH_VERSION-}" ]; then | |
__git_ps1_colorize_gitstring; | |
fi; | |
fi; | |
b=${b##refs/heads/}; | |
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then | |
__git_ps1_branch_name=$b; | |
b="\${__git_ps1_branch_name}"; | |
fi; | |
local f="$h$w$i$s$u"; | |
local gitstring="$c$b${f:+$z$f}${sparse}$r$p"; | |
if [ $pcmode = yes ]; then | |
if [ "${__git_printf_supports_v-}" != yes ]; then | |
gitstring=$(printf -- "$printf_format" "$gitstring"); | |
else | |
printf -v gitstring -- "$printf_format" "$gitstring"; | |
fi; | |
PS1="$ps1pc_start$gitstring$ps1pc_end"; | |
else | |
printf -- "$printf_format" "$gitstring"; | |
fi; | |
return $exit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment