-
Notifications
You must be signed in to change notification settings - Fork 9
/
zsh_aliases
85 lines (73 loc) · 2.62 KB
/
zsh_aliases
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
git_user() {
user=$(git -C "$1" config user.name)
author=$(git -C "$1" config duet.env.git-author-initials)
committer=$(git -C "$1" config duet.env.git-committer-initials)
if [ -n "${committer}" ]; then
echo "${author} & ${committer}%{$fg[black]%}@%{$reset_color%}"
elif [ -n "${author}" ]; then
echo "${author}%{$fg[black]%}@%{$reset_color%}"
elif [ -z $user ]; then
echo "%{$fg_bold[red]%}no user%{$fg[black]%}@%{$reset_color%}"
else
echo "$user%{$fg[black]%}@%{$reset_color%}"
fi
}
git_root() {
local folder='.'
for i in $(seq 0 $(pwd|tr -cd '/'|wc -c)); do
[ -d "$folder/.git" ] && echo "$folder" && return
folder="../$folder"
done
}
git_branch() {
local git_root="$1"
local line="$2"
local branch="???"
local ahead=''
local behind=''
case "$line" in
\#\#\ HEAD*)
branch="$(git -C "$git_root" tag --points-at HEAD)"
[ -z "$branch" ] && branch="$(git -C "$git_root" rev-parse --short HEAD)"
branch="%{$fg[yellow]%}${branch}%{$reset_color%}"
;;
*)
branch="${line#\#\# }"
branch="%{$fg[green]%}${branch%%...*}%{$reset_color%}"
ahead="$(echo $line | sed -En -e 's|^.*(\[ahead ([[:digit:]]+)).*\]$|\2|p')"
behind="$(echo $line | sed -En -e 's|^.*(\[.*behind ([[:digit:]]+)).*\]$|\2|p')"
[ -n "$ahead" ] && ahead="%{$fg_bold[white]%}↑%{$reset_color%}$ahead"
[ -n "$behind" ] && behind="%{$fg_bold[white]%}↓%{$reset_color%}$behind"
;;
esac
print "${branch}${ahead}${behind}"
}
git_status() {
local untracked=0
local modified=0
local deleted=0
local staged=0
local branch=''
local output=''
for line in "${(@f)$(git -C "$1" status --porcelain -b 2>/dev/null)}"
do
case "$line" in
\#\#*) branch="$(git_branch "$1" "$line")" ;;
\?\?*) ((untracked++)) ;;
U?*|?U*|DD*|AA*|\ M*|\ D*) ((modified++)) ;;
?M*|?D*) ((modified++)); ((staged++)) ;;
??*) ((staged++)) ;;
esac
done
output="$branch"
[ $staged -gt 0 ] && output="${output} %{$fg_bold[green]%}S%{$fg_no_bold[black]%}:%{$reset_color$fg[green]%}$staged%{$reset_color%}"
[ $modified -gt 0 ] && output="${output} %{$fg_bold[red]%}M%{$fg_no_bold[black]%}:%{$reset_color$fg[red]%}$modified%{$reset_color%}"
[ $deleted -gt 0 ] && output="${output} %{$fg_bold[red]%}D%{$fg_no_bold[black]%}:%{$reset_color$fg[red]%}$deleted%{$reset_color%}"
[ $untracked -gt 0 ] && output="${output} %{$fg_bold[yellow]%}?%{$fg_no_bold[black]%}:%{$reset_color$fg[yellow]%}$untracked%{$reset_color%}"
echo "$output"
}
git_prompt_info() {
local GIT_ROOT="$(git_root)"
[ -z "$GIT_ROOT" ] && return
print " $(git_status "$GIT_ROOT") "
}