forked from brujoand/sbp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.bash
95 lines (82 loc) · 2.76 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env bash
segments::git() {
local max_length=$SEGMENTS_MAX_LENGTH
local incoming_icon="${SEGMENTS_GIT_INCOMING_ICON:-↓}"
local outgoing_icon="${SEGMENTS_GIT_OUTGOING_ICON:-↑}"
local branch_only="${SEGMENTS_GIT_BRANCH_ONLY:-false}"
local path=${PWD}
while [[ $path ]]; do
if [[ -d "${path}/.git" ]]; then
local git_folder="${path}/.git"
break
fi
path=${path%/*}
done
[[ -z $git_folder ]] && exit 0
if [[ $PWD == "$git_folder" ]]; then
print_themed_segment 'normal' '.git/'
return 0
fi
if [[ $branch_only == false ]]; then
local git_status
git_status="$(git status --porcelain --branch 2>/dev/null)"
local additions=0
local modifications=0
local deletions=0
local untracked=0
while read -r line; do
local compacted=${line// /}
local 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"
local 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
else
git_head=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
fi
git_size=$((${#git_state} + ${#SEGMENTS_GIT_ICON} + ${#git_head} + ${#upstream_status}))
if [[ $git_size -gt $max_length && $max_length -ne -1 ]]; then
available_space=$((max_length - ${#git_state} - ${#SEGMENTS_GIT_ICON} + ${#upstream_status}))
if [[ $available_space -gt 0 ]]; then
git_head="${git_head:0:available_space}.."
else
git_head=""
fi
fi
SPLITTER_LEFT=''
SPLITTER_RIGHT=''
print_themed_segment 'normal' "${git_state/ /}" "$SEGMENTS_GIT_ICON" "${git_head/ /}" "${upstream_status/ /}"
}