git prompt info completed

This commit is contained in:
Loic Nageleisen 2012-08-14 13:01:19 +02:00
parent fbdc826001
commit 8d701cf804

View file

@ -1,16 +1,23 @@
# finds .git folder # extracted from git's bash completion contrib
__git_ps1_repo_path() { # don't redefine it if it's there already
if [ -d .git ]; then if [[ -z "$(type -t __gitdir)" ]]; then
echo "$(pwd)/.git" __gitdir ()
{
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
echo "$__git_dir"
elif [ -d .git ]; then
echo .git
else else
git rev-parse --git-dir 2>/dev/null git rev-parse --git-dir 2>/dev/null
fi fi
elif [ -d "$1/.git" ]; then
echo "$1/.git"
else
echo "$1"
fi
} }
fi
# repo name
__git_ps1_repo_name() {
basename $(dirname $(__git_ps1_repo_path))
}
# head that's being merged # head that's being merged
__git_ps1_rebase_merge_head() { __git_ps1_rebase_merge_head() {
@ -34,19 +41,29 @@ __git_ps1_describe() {
# branch name # branch name
__git_ps1_branch() { __git_ps1_branch() {
local g="$1" local g="$1"
local branch
local branch="$(git symbolic-ref HEAD 2>/dev/null)" branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
[[ $? -ne 0 ]] && branch="$(__git_ps1_describe)" branch="$(__git_ps1_describe)" || \
[[ $? -ne 0 ]] && branch="$(git rev-parse --short HEAD)…" branch="$(git rev-parse --short HEAD)…" || \
[[ $? -ne 0 ]] && branch="unknown" branch="unknown"
echo ${branch##refs/heads/} echo ${branch##refs/heads/}
} }
# compute git status and set environment variables
__git_ps1_vars() { __git_ps1_vars() {
#local g="$(__gitdir)" local g="$(__gitdir)"
local g=".git"
[[ -z "$g" ]] && return # are we in a git repo?
if [[ -z "$g" ]]; then
unset GIT_PS1_STATUS
unset GIT_PS1_BRANCH
unset GIT_PS1_SUBJECT
unset GIT_PS1_TOPLEVEL
unset GIT_PS1_NAME
unset GIT_PS1_PREFIX
return
fi
local rebase=0 local rebase=0
local interactive=0 local interactive=0
@ -60,10 +77,47 @@ __git_ps1_vars() {
local work=0 local work=0
local staged=0 local staged=0
local unstaged=0 local unstaged=0
local initial=0 local new=0
local untracked=0 local untracked=0
local stash=0 local stashed=0
local toplevel=""
local prefix=""
# assess position in repository
[[ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]] && gitdir=1
[[ $gitdir -eq 1 && "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]] && bare=1
[[ $gitdir -eq 0 && "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]] && work=1
# gitdir corner case
[[ "$g" == '.' ]] && if [[ "$(basename "$PWD")" == ".git" ]]; then
# inside .git: not a bare repository!
# weird: --is-bare-repository returns true regardless
bare=0
g="$PWD"
else
# really a bare repository
bare=1
g="$PWD"
fi
# make relative path absolute
[[ "${g/#\//}" == "$g" ]] && g="$PWD/$g"
g="${g/%\//}" # strip trailing slash, if any
# find base dir (toplevel)
[[ $bare -eq 1 ]] && toplevel="$g"
[[ $bare -eq 0 ]] && toplevel="$(dirname $g)"
# find relative path within toplevel
prefix="${PWD/#$toplevel/}"
prefix="${prefix/#\//}" # strip starting slash
[[ -z "$prefix" ]] && prefix='.' # toplevel == prefix
# get the current branch, or whatever describes HEAD
branch=$(__git_ps1_branch)
# evaluate action
[[ -d "$g/rebase-merge" ]] && rebase=1 merge=1 subject=$(__git_ps1_rebase_merge_head) [[ -d "$g/rebase-merge" ]] && rebase=1 merge=1 subject=$(__git_ps1_rebase_merge_head)
[[ $rebase -eq 1 && -f "$g/rebase-merge/interactive" ]] && interactive=1 merge=0 [[ $rebase -eq 1 && -f "$g/rebase-merge/interactive" ]] && interactive=1 merge=0
[[ -d "$g/rebase-apply" ]] && rebase=1 apply=1 [[ -d "$g/rebase-apply" ]] && rebase=1 apply=1
@ -72,15 +126,10 @@ __git_ps1_vars() {
[[ $rebase -eq 0 && -f "$g/MERGE_HEAD" ]] && merge=1 [[ $rebase -eq 0 && -f "$g/MERGE_HEAD" ]] && merge=1
[[ $rebase -eq 0 && -f "$g/BISECT_LOG" ]] && bisect=1 [[ $rebase -eq 0 && -f "$g/BISECT_LOG" ]] && bisect=1
branch=$(__git_ps1_branch) # working directory status
[[ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]] && gitdir=1
[[ $gitdir -eq 1 && "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]] && bare=1
[[ $gitdir -eq 0 && "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]] && work=1
if [[ $work -eq 1 ]]; then if [[ $work -eq 1 ]]; then
## dirtiness, if config allows it ## dirtiness, if config allows it
if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then if [[ -n "${GIT_PS1_SHOWDIRTYSTATE-}" && "$(git config --bool bash.showDirtyState)" != "false" ]]; then
# unstaged files # unstaged files
git diff --no-ext-diff --ignore-submodules --quiet --exit-code || unstaged=1 git diff --no-ext-diff --ignore-submodules --quiet --exit-code || unstaged=1
@ -89,32 +138,41 @@ __git_ps1_vars() {
git diff-index --cached --quiet --ignore-submodules HEAD -- || staged=1 git diff-index --cached --quiet --ignore-submodules HEAD -- || staged=1
else else
# no current commit, we're fresh # no current commit, we're fresh
initial=1 new=1
fi fi
fi fi
## stash status ## stash status
git rev-parse --verify refs/stash >/dev/null 2>&1 && stash=1 if [[ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]]; then
git rev-parse --verify refs/stash >/dev/null 2>&1 && stashed=1
## untracked files
[ -n "$(git ls-files --others --exclude-standard)" ] && untracked=1
fi fi
echo rebase $rebase ## untracked files
echo interactive $interactive if [[ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]]; then
echo apply $apply [ -n "$(git ls-files --others --exclude-standard)" ] && untracked=1
echo merge $merge fi
echo bisect $bisect fi
echo subject $subject
echo branch $branch # built environment variables
echo gitdir $gitdir GIT_PS1_STATUS=""
echo bare $bare [[ $rebase -eq 1 ]] && GIT_PS1_STATUS+="R"
echo work $work [[ $interactive -eq 1 ]] && GIT_PS1_STATUS+="i"
echo staged $staged [[ $apply -eq 1 ]] && GIT_PS1_STATUS+="A"
echo unstaged $unstaged [[ $merge -eq 1 ]] && GIT_PS1_STATUS+="M"
echo whatever $whatever [[ $bisect -eq 1 ]] && GIT_PS1_STATUS+="B"
echo untracked $untracked [[ $gitdir -eq 1 ]] && GIT_PS1_STATUS+="g"
echo stash $stash [[ $bare -eq 1 ]] && GIT_PS1_STATUS+="b"
[[ $work -eq 1 ]] && GIT_PS1_STATUS+="w"
[[ $staged -eq 1 ]] && GIT_PS1_STATUS+="s"
[[ $unstaged -eq 1 ]] && GIT_PS1_STATUS+="u"
[[ $new -eq 1 ]] && GIT_PS1_STATUS+="n"
[[ $untracked -eq 1 ]] && GIT_PS1_STATUS+="t"
[[ $stashed -eq 1 ]] && GIT_PS1_STATUS+="h"
GIT_PS1_BRANCH="$branch"
GIT_PS1_SUBJECT="$subject"
GIT_PS1_TOPLEVEL="$toplevel"
GIT_PS1_NAME="$(basename "$toplevel")"
GIT_PS1_PREFIX="$prefix"
} }
# vim: ft=sh # vim: ft=sh