-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_git_ps1
137 lines (116 loc) · 3.27 KB
/
fast_git_ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /bin/bash
# Shamelessly copied from https://gist.github.com/Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f#gistcomment-3560622
# 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
# ripped out of the official git-prompt.sh
# Helper function to read the first line of a file into a variable.
# __git_eread requires 2 arguments, the file path and the name of the
# variable, in that order.
__git_eread ()
{
test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
}
function __git_status() {
local b=$1
local r=$2
local w=""
local i=""
local s=""
local u=""
git diff --no-ext-diff --quiet || w="*"
git diff --no-ext-diff --cached --quiet || i="+"
if [ -z "$short_sha" ] && [ -z "$i" ]; then
i="#"
fi
if git rev-parse --verify --quiet refs/stash >/dev/null; then
s="$"
fi
if git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' &>/dev/null; then
u="%"
fi
local f="$w$i$s$u"
__format "$b" "$f" "$r"
}
function __format() {
local b=$1
local f=$2
local r=$3
b=${b##refs/heads/}
local z=" "
echo "$b${f:+$z$f}$r"
}
function __git_state() {
local g=$1
local b=$2
local r=""
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"
elif [ -f "$g/rebase-apply/applying" ]; then
r="|AM"
else
r="|AM/REBASE"
fi
elif [ -f "$g/MERGE_HEAD" ]; then
r="|MERGING"
elif __git_sequencer_status; then
:
elif [ -f "$g/BISECT_LOG" ]; then
r="|BISECTING"
fi
fi
if [ -n "$step" ] && [ -n "$total" ]; then
r="$r $step/$total"
fi
echo "$r"
}
function __fastgit_ps1 () {
local lastExitCode=$?
local headfile="" head="" branch="" state=""
local dir="$PWD"
local gitdir=""
while [ -n "$dir" ]; do
gitdir="$dir/.git"
# handle workspaces
if [ -f "$gitdir" ]; then
read -r line < "$gitdir"
if [[ "$line" =~ gitdir: ]]; then
gitdir="${line#gitdir: }"
fi
fi
if [ -e "$gitdir/HEAD" ]; then
headfile="$gitdir/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 [ -n "$branch" ]; then
state="$(__git_state "$gitdir" "$branch")"
state="($(__format "$branch" "" "$state"))"
fi
local exitCode
exitCode="\[\e[0;$((lastExitCode == 0 ? 0 : 91))m\][$lastExitCode]\e[0m"
# Edit to suit your needs
export PS1="\[\e]0;\W\a\]\n\[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \[\033[36m\]$state\[\033[0m\] $exitCode\n\$ "
}