forked from brujoand/sbp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.bash
executable file
·80 lines (68 loc) · 2.18 KB
/
git.bash
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
#! /usr/bin/env bash
segment_direction=$3
settings_git_max_length=$4
incoming_icon="$settings_git_incoming_icon"
outgoing_icon="$settings_git_outgoing_icon"
path=${PWD}
while [[ $path ]]; do
if [[ -d "${path}/.git" || -f "${path}/.git" ]]; then
git_folder="${path}/.git"
break
fi
path=${path%/*}
done
type git &>/dev/null || exit 0
git_status="$(git status --porcelain --branch 2>/dev/null)"
additions=0
modifications=0
deletions=0
untracked=0
while read -r line; do
compacted=${line// }
action=${compacted:0:1}
case $action in
A)
additions_icon=' +'
additions=$(( additions + 1 ))
;;
M|R)
modifications_icon=' ~'
modifications=$(( modifications + 1 ))
;;
D)
deletions_icon=' -'
deletions=$(( deletions + 1 ))
;;
\?)
untracked_icon=' ?'
untracked=$(( untracked + 1 ))
;;
\#)
branch_line=${line/\#\# /}
branch_data=${branch_line/% *}
branch="${branch_data/...*/}"
upstream_data="${branch_line#* }"
upstream_stripped="${upstream_data//[\[|\]]}"
if [[ "$upstream_data" != "$upstream_stripped" ]]; then
outgoing_filled="${upstream_stripped/ahead /${outgoing_icon}}"
upstream_status="${outgoing_filled/behind /${incoming_icon}}"
fi
esac
done <<< "$git_status"
git_state="${additions_icon}${additions/#0/}${modifications_icon}${modifications/#0/}${deletions_icon}${deletions/#0/}${untracked_icon}${untracked/#0/}"
# git status does not support detached head
if [[ "$branch" != 'HEAD' ]]; then
git_head="$branch"
else
git_head=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
fi
if [[ $(( ${#git_head} + ${#git_state} )) -gt "$settings_git_max_length" ]]; then
git_head="${git_head:0:10}.."
fi
segment_value="${git_state} ${settings_git_icon} ${git_head} ${upstream_status}"
segment_value="${segment_value// / }"
shopt -s extglob
segment_value="${segment_value##*( )}" # Trim leading whitespaces
segment_value="${segment_value%%*( )}" # Trim trailing whitespaces
shopt -u extglob
pretty_print_segment "$settings_git_color_primary" "$settings_git_color_secondary" "${segment_value// / }" "$segment_direction"