Skip to content

Commit 43ef5c9

Browse files
committed
lib/githelpers: shfmt && shellcheck
My apologies to future `git blame` hunters ♥
1 parent f06c821 commit 43ef5c9

File tree

2 files changed

+117
-111
lines changed

2 files changed

+117
-111
lines changed

clean_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ themes/candy
152152
themes/command_duration.theme.bash
153153
themes/easy
154154
themes/essential
155+
themes/githelpers.theme.bash
155156
themes/modern
156157
themes/p4helpers.theme.bash
157158
themes/pete

themes/githelpers.theme.bash

Lines changed: 116 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,106 @@
1-
#!/usr/bin/env bash
1+
# shellcheck shell=bash
22

3-
function _git-symbolic-ref {
4-
git symbolic-ref -q HEAD 2> /dev/null
3+
function _git-symbolic-ref() {
4+
git symbolic-ref -q HEAD 2> /dev/null
55
}
66

77
# When on a branch, this is often the same as _git-commit-description,
88
# but this can be different when two branches are pointing to the
99
# same commit. _git-branch is used to explicitly choose the checked-out
1010
# branch.
11-
function _git-branch {
12-
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
13-
test -n "${VCS_STATUS_LOCAL_BRANCH}" && echo "${VCS_STATUS_LOCAL_BRANCH}" || return 1
14-
else
15-
git symbolic-ref -q --short HEAD 2> /dev/null || return 1
16-
fi
11+
function _git-branch() {
12+
if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
13+
if [[ -n "${VCS_STATUS_LOCAL_BRANCH:-}" ]]; then
14+
echo "${VCS_STATUS_LOCAL_BRANCH}"
15+
else
16+
return 1
17+
fi
18+
else
19+
git symbolic-ref -q --short HEAD 2> /dev/null || return 1
20+
fi
1721
}
1822

19-
function _git-tag {
20-
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
21-
test -n "${VCS_STATUS_TAG}" && echo "${VCS_STATUS_TAG}"
22-
else
23-
git describe --tags --exact-match 2> /dev/null
24-
fi
23+
function _git-tag() {
24+
if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
25+
if [[ -n "${VCS_STATUS_TAG:-}" ]]; then
26+
echo "${VCS_STATUS_TAG}"
27+
fi
28+
else
29+
git describe --tags --exact-match 2> /dev/null
30+
fi
2531
}
2632

27-
function _git-commit-description {
28-
git describe --contains --all 2> /dev/null
33+
function _git-commit-description() {
34+
git describe --contains --all 2> /dev/null
2935
}
3036

31-
function _git-short-sha {
32-
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
33-
echo ${VCS_STATUS_COMMIT:0:7}
34-
else
35-
git rev-parse --short HEAD
36-
fi
37+
function _git-short-sha() {
38+
if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
39+
echo "${VCS_STATUS_COMMIT:0:7}"
40+
else
41+
git rev-parse --short HEAD
42+
fi
3743
}
3844

3945
# Try the checked-out branch first to avoid collision with branches pointing to the same ref.
40-
function _git-friendly-ref {
41-
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
42-
_git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus
43-
else
44-
_git-branch || _git-tag || _git-commit-description || _git-short-sha
45-
fi
46+
function _git-friendly-ref() {
47+
if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
48+
_git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus
49+
else
50+
_git-branch || _git-tag || _git-commit-description || _git-short-sha
51+
fi
4652
}
4753

48-
function _git-num-remotes {
49-
git remote | wc -l
54+
function _git-num-remotes() {
55+
git remote | wc -l
5056
}
5157

52-
function _git-upstream {
53-
local ref
54-
ref="$(_git-symbolic-ref)" || return 1
55-
git for-each-ref --format="%(upstream:short)" "${ref}"
58+
function _git-upstream() {
59+
local ref
60+
ref="$(_git-symbolic-ref)" || return 1
61+
git for-each-ref --format="%(upstream:short)" "${ref}"
5662
}
5763

58-
function _git-upstream-remote {
59-
local upstream
60-
upstream="$(_git-upstream)" || return 1
64+
function _git-upstream-remote() {
65+
local upstream branch
66+
upstream="$(_git-upstream)" || return 1
6167

62-
local branch
63-
branch="$(_git-upstream-branch)" || return 1
64-
echo "${upstream%"/${branch}"}"
68+
branch="$(_git-upstream-branch)" || return 1
69+
echo "${upstream%"/${branch}"}"
6570
}
6671

67-
function _git-upstream-branch {
68-
local ref
69-
ref="$(_git-symbolic-ref)" || return 1
72+
function _git-upstream-branch() {
73+
local ref
74+
ref="$(_git-symbolic-ref)" || return 1
7075

71-
# git versions < 2.13.0 do not support "strip" for upstream format
72-
# regex replacement gives the wrong result for any remotes with slashes in the name,
73-
# so only use when the strip format fails.
74-
git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///"
76+
# git versions < 2.13.0 do not support "strip" for upstream format
77+
# regex replacement gives the wrong result for any remotes with slashes in the name,
78+
# so only use when the strip format fails.
79+
git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///"
7580
}
7681

77-
function _git-upstream-behind-ahead {
78-
git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null
82+
function _git-upstream-behind-ahead() {
83+
git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null
7984
}
8085

81-
function _git-upstream-branch-gone {
82-
[[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]]
86+
function _git-upstream-branch-gone() {
87+
[[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]]
8388
}
8489

85-
function _git-hide-status {
86-
[[ "$(git config --get bash-it.hide-status)" == "1" ]]
90+
function _git-hide-status() {
91+
[[ "$(git config --get bash-it.hide-status)" == "1" ]]
8792
}
8893

89-
function _git-status {
90-
local git_status_flags=
91-
[[ "${SCM_GIT_IGNORE_UNTRACKED}" = "true" ]] && git_status_flags='-uno' || true
92-
git status --porcelain ${git_status_flags} 2> /dev/null
94+
function _git-status() {
95+
local git_status_flags=
96+
if [[ "${SCM_GIT_IGNORE_UNTRACKED:-}" == "true" ]]; then
97+
git_status_flags='-uno'
98+
fi
99+
git status --porcelain "${git_status_flags:---}" 2> /dev/null
93100
}
94101

95-
function _git-status-counts {
96-
_git-status | awk '
102+
function _git-status-counts() {
103+
_git-status | awk '
97104
BEGIN {
98105
untracked=0;
99106
unstaged=0;
@@ -116,60 +123,58 @@ function _git-status-counts {
116123
}'
117124
}
118125

119-
function _git-remote-info {
120-
121-
# prompt handling only, reimplement because patching the routine below gets ugly
122-
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
123-
[[ "${VCS_STATUS_REMOTE_NAME}" == "" ]] && return
124-
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && local same_branch_name=true
125-
local same_branch_name=
126-
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && same_branch_name=true
127-
# no multiple remote support in gitstatusd
128-
if [[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" || "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]]; then
129-
if [[ "${same_branch_name}" != "true" ]]; then
130-
remote_info="${VCS_STATUS_REMOTE_NAME}/${VCS_STATUS_REMOTE_BRANCH}"
131-
else
132-
remote_info="${VCS_STATUS_REMOTE_NAME}"
133-
fi
134-
elif [[ ${same_branch_name} != "true" ]]; then
135-
remote_info="${VCS_STATUS_REMOTE_BRANCH}"
136-
fi
137-
if [[ -n "${remote_info}" ]];then
138-
# no support for gone remote branches in gitstatusd
139-
local branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}"
140-
echo "${branch_prefix}${remote_info}"
141-
fi
142-
else
143-
[[ "$(_git-upstream)" == "" ]] && return
144-
145-
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && local same_branch_name=true
146-
local same_branch_name=
147-
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true
148-
if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" && "$(_git-num-remotes)" -ge 2) ||
149-
"${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then
150-
if [[ "${same_branch_name}" != "true" ]]; then
151-
remote_info="\$(_git-upstream)"
152-
else
153-
remote_info="$(_git-upstream-remote)"
154-
fi
155-
elif [[ ${same_branch_name} != "true" ]]; then
156-
remote_info="\$(_git-upstream-branch)"
157-
fi
158-
if [[ -n "${remote_info}" ]];then
159-
local branch_prefix
160-
if _git-upstream-branch-gone; then
161-
branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX}"
162-
else
163-
branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}"
164-
fi
165-
echo "${branch_prefix}${remote_info}"
166-
fi
167-
fi
126+
function _git-remote-info() {
127+
local same_branch_name="" branch_prefix
128+
# prompt handling only, reimplement because patching the routine below gets ugly
129+
if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
130+
[[ "${VCS_STATUS_REMOTE_NAME?}" == "" ]] && return
131+
[[ "${VCS_STATUS_LOCAL_BRANCH?}" == "${VCS_STATUS_REMOTE_BRANCH?}" ]] && same_branch_name=true
132+
# no multiple remote support in gitstatusd
133+
if [[ "${SCM_GIT_SHOW_REMOTE_INFO:-}" == "true" || "${SCM_GIT_SHOW_REMOTE_INFO:-}" == "auto" ]]; then
134+
if [[ ${same_branch_name} != "true" ]]; then
135+
remote_info="${VCS_STATUS_REMOTE_NAME?}/${VCS_STATUS_REMOTE_BRANCH?}"
136+
else
137+
remote_info="${VCS_STATUS_REMOTE_NAME?}"
138+
fi
139+
elif [[ ${same_branch_name} != "true" ]]; then
140+
remote_info="${VCS_STATUS_REMOTE_BRANCH?}"
141+
fi
142+
if [[ -n "${remote_info}" ]]; then
143+
# no support for gone remote branches in gitstatusd
144+
branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}"
145+
echo "${branch_prefix}${remote_info}"
146+
fi
147+
else
148+
[[ "$(_git-upstream)" == "" ]] && return
149+
150+
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true
151+
if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" == "auto" && "$(_git-num-remotes)" -ge 2) ||
152+
"${SCM_GIT_SHOW_REMOTE_INFO}" == "true" ]]; then
153+
if [[ ${same_branch_name} != "true" ]]; then
154+
# shellcheck disable=SC2016
155+
remote_info='$(_git-upstream)'
156+
else
157+
remote_info="$(_git-upstream-remote)"
158+
fi
159+
elif [[ ${same_branch_name} != "true" ]]; then
160+
# shellcheck disable=SC2016
161+
remote_info='$(_git-upstream-branch)'
162+
fi
163+
if [[ -n "${remote_info}" ]]; then
164+
local branch_prefix
165+
if _git-upstream-branch-gone; then
166+
branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX:-}"
167+
else
168+
branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}"
169+
fi
170+
echo "${branch_prefix}${remote_info}"
171+
fi
172+
fi
168173
}
169174

170175
# Unused by bash-it, present for API compatibility
171-
function git_status_summary {
172-
awk '
176+
function git_status_summary() {
177+
awk '
173178
BEGIN {
174179
untracked=0;
175180
unstaged=0;

0 commit comments

Comments
 (0)