-
Notifications
You must be signed in to change notification settings - Fork 94
/
geometry_git
107 lines (80 loc) · 3.44 KB
/
geometry_git
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
# geometry_git - please see the readme for documentation on all features
_geometry_git_guard() {
git rev-parse 2>/dev/null
}
geometry_git_stashes() {
_geometry_git_guard || return
git rev-parse --quiet --verify refs/stash >/dev/null \
&& ansi ${GEOMETRY_GIT_COLOR_STASHES:="144"} ${GEOMETRY_GIT_SYMBOL_STASHES:="●"}
}
geometry_git_time() {
local last_commit; last_commit=$(git log -1 --pretty=format:'%at' 2> /dev/null)
[[ -z "$last_commit" ]] && ansi ${GEOMETRY_COLOR_NO_TIME:-default} ${GEOMETRY_GIT_NO_COMMITS_MESSAGE:-welcome} && return
local now; now=$(date +%s)
local seconds_since_last_commit; seconds_since_last_commit=$((now - last_commit))
geometry::time $seconds_since_last_commit ${GEOMETRY_GIT_TIME_DETAILED:-false}
}
geometry_git_branch() {
_geometry_git_guard || return
ansi ${GEOMETRY_GIT_COLOR_BRANCH:-242} $(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD)
}
geometry_git_status() {
command git rev-parse --git-dir > /dev/null 2>&1 || return
git diff-index --quiet HEAD 2> /dev/null \
&& [[ -z "$(git status --porcelain --ignore-submodules)" ]] \
&& ansi ${GEOMETRY_GIT_COLOR_CLEAN:-green} ${GEOMETRY_GIT_SYMBOL_CLEAN:-"⬢"} \
|| ansi ${GEOMETRY_GIT_COLOR_DIRTY:-red} ${GEOMETRY_GIT_SYMBOL_DIRTY:-"⬡"}
}
geometry_git_rebase() {
_geometry_git_guard || return
local git_dir
git_dir=$(git rev-parse --git-dir)
[[ -d "$git_dir/rebase-merge" ]] || [[ -d "$git_dir/rebase-apply" ]] || return
echo ${GEOMETRY_GIT_SYMBOL_REBASE:-"®"}
}
geometry_git_remote() {
local unpushed=${GEOMETRY_GIT_SYMBOL_UNPUSHED:-"⇡"}
local unpulled=${GEOMETRY_GIT_SYMBOL_UNPULLED:-"⇣"}
local local_commit && local_commit="$(git rev-parse "@" 2>/dev/null)"
local remote_commit && remote_commit="$(git rev-parse "@{u}" 2>/dev/null)"
[[ $local_commit == "@" || $local_commit == $remote_commit ]] && return
local common_base && common_base="$(git merge-base "@" "@{u}" 2>/dev/null)" # last common commit
[[ $common_base == $remote_commit ]] && echo $unpushed && return
[[ $common_base == $local_commit ]] && echo $unpulled && return
echo "$unpushed $unpulled"
}
geometry_git_symbol() { echo ${(j: :):-$(geometry_git_rebase) $(geometry_git_remote)}; }
geometry_git_conflicts() {
_geometry_git_guard || return
local _grep
local conflicts conflict_list
local file_count
local total raw_total
conflicts=$(git diff --name-only --diff-filter=U -z 2>/dev/null)
[[ -z "$conflicts" ]] && return
_grep=${GEOMETRY_GIT_GREP:=${commands[rg]:=${commands[ag]:=${commands[grep]}}}}
conflict_list=$($_grep -cH '^=======$' ${(0)conflicts})
file_count="${(w)#conflict_list}"
raw_total=$(echo $conflict_list | cut -d ':' -f2 | paste -sd+ - | bc)
total=${raw_total##*( )}
[[ -z "$total" ]] && ansi ${GEOMETRY_GIT_COLOR_CONFLICTS_SOLVED:-green} ${GEOMETRY_GIT_SYMBOL_CONFLICTS_SOLVED:-"◆"} && return
count="(${file_count}f|${total}c)"
ansi ${GEOMETRY_GIT_COLOR_CONFLICTS_UNSOLVED:-red} "${GEOMETRY_GIT_SYMBOL_CONFLICTS_UNSOLVED:-'◈'} $count"
}
geometry_git() {
(( $+commands[git] )) || return
_geometry_git_guard || return
$(git rev-parse --is-bare-repository) \
&& ansi ${GEOMETRY_GIT_COLOR_BARE:=blue} ${GEOMETRY_GIT_SYMBOL_BARE:="⬢"} \
&& return
local git_info && git_info=(
geometry_git_rebase
geometry_git_remote
geometry_git_branch
geometry_git_conflicts
geometry_git_time
geometry_git_stashes
geometry_git_status
)
geometry::wrap $PWD $git_info
}