@@ -6,71 +6,70 @@ _omb_module_require lib:omb-prompt-base
66
77# # Note: The same name of a functionis defined in omb-prompt-base. We comment
88# # out this function for now.
9- # function git_prompt_info() {
9+ # function git_prompt_info {
1010# local ref
11- # if [[ " $(_omb_prompt_git config --get oh-my-bash.hide-status 2>/dev/null)" != "1" ]]; then
11+ # if [[ $(_omb_prompt_git config --get oh-my-bash.hide-status 2>/dev/null) != 1 ]]; then
1212# ref=$(_omb_prompt_git symbolic-ref HEAD 2> /dev/null) || \
1313# ref=$(_omb_prompt_git rev-parse --short HEAD 2> /dev/null) || return 0
1414# echo "$OSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$OSH_THEME_GIT_PROMPT_SUFFIX"
1515# fi
1616# }
1717
1818# Checks if working tree is dirty
19- function parse_git_dirty() {
20- local STATUS=' '
21- local FLAGS
22- FLAGS=(' --porcelain' )
23- if [[ " $( _omb_prompt_git config --get oh-my-bash.hide-dirty) " != " 1" ]]; then
24- if [[ $POST_1_7_2_GIT -gt 0 ]]; then
25- FLAGS+=( ' --ignore-submodules=dirty' )
19+ function parse_git_dirty {
20+ local STATUS=
21+ local -a FLAGS=(' --porcelain' )
22+ if [[ $( _omb_prompt_git config --get oh-my-bash.hide-dirty) != 1 ]]; then
23+ if (( POST_1 _7 _2 _GIT > 0 )) ; then
24+ FLAGS+=(' --ignore-submodules=dirty' )
2625 fi
27- if [[ " $DISABLE_UNTRACKED_FILES_DIRTY " == " true" ]]; then
28- FLAGS+=( ' --untracked-files=no' )
26+ if [[ $DISABLE_UNTRACKED_FILES_DIRTY == " true" ]]; then
27+ FLAGS+=(' --untracked-files=no' )
2928 fi
3029 STATUS=$( _omb_prompt_git status " ${FLAGS[@]} " 2> /dev/null | tail -n1)
3130 fi
32- if [[ -n $STATUS ]]; then
31+ if [[ $STATUS ]]; then
3332 echo " $OSH_THEME_GIT_PROMPT_DIRTY "
3433 else
3534 echo " $OSH_THEME_GIT_PROMPT_CLEAN "
3635 fi
3736}
3837
3938# Gets the difference between the local and remote branches
40- function git_remote_status() {
41- local remote ahead behind git_remote_status git_remote_status_detailed git_remote_origin
42- git_remote_origin= $( _omb_prompt_git rev-parse --verify ${hook_com[branch]} @{upstream} --symbolic-full-name 2> /dev/null )
43- remote= ${git_remote_origin / refs \/ remotes \/ / }
44- if [[ -n ${remote} ]] ; then
45- ahead =$( _omb_prompt_git rev-list ${hook_com[branch]} @{upstream}..HEAD 2> /dev/null | wc -l)
46- behind= $( _omb_prompt_git rev-list HEAD.. ${hook_com[branch]} @{upstream} 2> /dev/null | wc -l )
47-
48- if [[ $ ahead -eq 0 ]] && [[ $ behind -eq 0 ]] ; then
49- git_remote_status=" $OSH_THEME_GIT_PROMPT_EQUAL_REMOTE "
50- elif [[ $ ahead -gt 0 ]] && [[ $ behind -eq 0 ]] ; then
51- git_remote_status=" $OSH_THEME_GIT_PROMPT_AHEAD_REMOTE "
52- git_remote_status_detailed=" $OSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE $(( ahead)) $_omb_prompt_reset_color "
53- elif [[ $ behind -gt 0 ]] && [[ $ ahead -eq 0 ]] ; then
54- git_remote_status=" $OSH_THEME_GIT_PROMPT_BEHIND_REMOTE "
55- git_remote_status_detailed=" $OSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE $(( behind)) $_omb_prompt_reset_color "
56- elif [[ $ ahead -gt 0 ]] && [[ $ behind -gt 0 ]] ; then
57- git_remote_status=" $OSH_THEME_GIT_PROMPT_DIVERGED_REMOTE "
58- git_remote_status_detailed=" $OSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE $(( ahead)) $_omb_prompt_reset_color$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE $(( behind)) $_omb_prompt_reset_color "
39+ function git_remote_status {
40+ local git_remote_origin= $( _omb_prompt_git rev-parse --verify ${hook_com[branch]} @{upstream} --symbolic-full-name 2> /dev/null )
41+ local remote= ${git_remote_origin / refs \/ remotes \/ / }
42+ if [[ $remote ]] ; then
43+ local ahead= $( _omb_prompt_git rev-list ${hook_com[branch]} @{upstream}..HEAD 2> /dev/null | wc -l )
44+ local behind =$( _omb_prompt_git rev-list HEAD.. ${hook_com[branch]} @{upstream} 2> /dev/null | wc -l)
45+
46+ local git_remote_status git_remote_status_detailed
47+ if (( ahead == 0 && behind == 0 )) ; then
48+ git_remote_status=$OSH_THEME_GIT_PROMPT_EQUAL_REMOTE
49+ elif (( ahead > 0 && behind == 0 )) ; then
50+ git_remote_status=$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE
51+ git_remote_status_detailed=$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE $(( ahead)) $_omb_prompt_reset_color
52+ elif (( behind > 0 && ahead == 0 )) ; then
53+ git_remote_status=$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE
54+ git_remote_status_detailed=$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE $(( behind)) $_omb_prompt_reset_color
55+ elif (( ahead > 0 && behind > 0 )) ; then
56+ git_remote_status=$OSH_THEME_GIT_PROMPT_DIVERGED_REMOTE
57+ git_remote_status_detailed=$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_AHEAD_REMOTE $(( ahead)) $_omb_prompt_reset_color$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$OSH_THEME_GIT_PROMPT_BEHIND_REMOTE $(( behind)) $_omb_prompt_reset_color
5958 fi
6059
61- if [[ -n $OSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
62- git_remote_status=" $OSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$OSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX "
60+ if [[ $OSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
61+ git_remote_status=$OSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$OSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX
6362 fi
6463
65- echo $git_remote_status
64+ echo " $git_remote_status "
6665 fi
6766}
6867
6968# Outputs the name of the current branch
7069# Usage example: git pull origin $(git_current_branch)
7170# Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
7271# it's not a symbolic ref, but in a Git repo.
73- function git_current_branch() {
72+ function git_current_branch {
7473 local ref
7574 ref=$( _omb_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
7675 local ret=$?
@@ -83,112 +82,103 @@ function git_current_branch() {
8382
8483
8584# Gets the number of commits ahead from remote
86- function git_commits_ahead() {
85+ function git_commits_ahead {
8786 if _omb_prompt_git rev-parse --git-dir & > /dev/null; then
88- local commits=" $( _omb_prompt_git rev-list --count @{upstream}..HEAD) "
89- if [[ " $ commits" != 0 ]] ; then
87+ local commits=$( _omb_prompt_git rev-list --count @{upstream}..HEAD)
88+ if (( commits != 0 )) ; then
9089 echo " $OSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$OSH_THEME_GIT_COMMITS_AHEAD_SUFFIX "
9190 fi
9291 fi
9392}
9493
9594# Gets the number of commits behind remote
96- function git_commits_behind() {
95+ function git_commits_behind {
9796 if _omb_prompt_git rev-parse --git-dir & > /dev/null; then
98- local commits=" $( _omb_prompt_git rev-list --count HEAD..@{upstream}) "
99- if [[ " $ commits" != 0 ]] ; then
97+ local commits=$( _omb_prompt_git rev-list --count HEAD..@{upstream})
98+ if (( commits != 0 )) ; then
10099 echo " $OSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$OSH_THEME_GIT_COMMITS_BEHIND_SUFFIX "
101100 fi
102101 fi
103102}
104103
105104# Outputs if current branch is ahead of remote
106- function git_prompt_ahead() {
107- if [[ -n " $( _omb_prompt_git rev-list origin/$( git_current_branch) ..HEAD 2> /dev/null) " ]]; then
105+ function git_prompt_ahead {
106+ if [[ $( _omb_prompt_git rev-list origin/$( git_current_branch) ..HEAD 2> /dev/null) ]]; then
108107 echo " $OSH_THEME_GIT_PROMPT_AHEAD "
109108 fi
110109}
111110
112111# Outputs if current branch is behind remote
113- function git_prompt_behind() {
114- if [[ -n " $( _omb_prompt_git rev-list HEAD..origin/$( git_current_branch) 2> /dev/null) " ]]; then
112+ function git_prompt_behind {
113+ if [[ $( _omb_prompt_git rev-list HEAD..origin/$( git_current_branch) 2> /dev/null) ]]; then
115114 echo " $OSH_THEME_GIT_PROMPT_BEHIND "
116115 fi
117116}
118117
119118# Outputs if current branch exists on remote or not
120- function git_prompt_remote() {
121- if [[ -n " $( _omb_prompt_git show-ref origin/$( git_current_branch) 2> /dev/null) " ]]; then
119+ function git_prompt_remote {
120+ if [[ $( _omb_prompt_git show-ref origin/$( git_current_branch) 2> /dev/null) ]]; then
122121 echo " $OSH_THEME_GIT_PROMPT_REMOTE_EXISTS "
123122 else
124123 echo " $OSH_THEME_GIT_PROMPT_REMOTE_MISSING "
125124 fi
126125}
127126
128127# Formats prompt string for current git commit short SHA
129- function git_prompt_short_sha() {
128+ function git_prompt_short_sha {
130129 local SHA
131- SHA=$( _omb_prompt_git rev-parse --short HEAD 2> /dev/null) && echo " $OSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$OSH_THEME_GIT_PROMPT_SHA_AFTER "
130+ SHA=$( _omb_prompt_git rev-parse --short HEAD 2> /dev/null) &&
131+ echo " $OSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$OSH_THEME_GIT_PROMPT_SHA_AFTER "
132132}
133133
134134# Formats prompt string for current git commit long SHA
135- function git_prompt_long_sha() {
135+ function git_prompt_long_sha {
136136 local SHA
137- SHA=$( _omb_prompt_git rev-parse HEAD 2> /dev/null) && echo " $OSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$OSH_THEME_GIT_PROMPT_SHA_AFTER "
137+ SHA=$( _omb_prompt_git rev-parse HEAD 2> /dev/null) &&
138+ echo " $OSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$OSH_THEME_GIT_PROMPT_SHA_AFTER "
138139}
139140
140141# Get the status of the working tree
141- function git_prompt_status() {
142- local INDEX STATUS
143- INDEX=$( _omb_prompt_git status --porcelain -b 2> /dev/null)
144- STATUS=" "
145- if $( echo " $INDEX " | command grep -E ' ^\?\? ' & > /dev/null) ; then
146- STATUS=" $OSH_THEME_GIT_PROMPT_UNTRACKED$STATUS "
147- fi
148- if $( echo " $INDEX " | grep ' ^A ' & > /dev/null) ; then
149- STATUS=" $OSH_THEME_GIT_PROMPT_ADDED$STATUS "
150- elif $( echo " $INDEX " | grep ' ^M ' & > /dev/null) ; then
151- STATUS=" $OSH_THEME_GIT_PROMPT_ADDED$STATUS "
152- fi
153- if $( echo " $INDEX " | grep ' ^ M ' & > /dev/null) ; then
154- STATUS=" $OSH_THEME_GIT_PROMPT_MODIFIED$STATUS "
155- elif $( echo " $INDEX " | grep ' ^AM ' & > /dev/null) ; then
156- STATUS=" $OSH_THEME_GIT_PROMPT_MODIFIED$STATUS "
157- elif $( echo " $INDEX " | grep ' ^ T ' & > /dev/null) ; then
158- STATUS=" $OSH_THEME_GIT_PROMPT_MODIFIED$STATUS "
159- fi
160- if $( echo " $INDEX " | grep ' ^R ' & > /dev/null) ; then
161- STATUS=" $OSH_THEME_GIT_PROMPT_RENAMED$STATUS "
162- fi
163- if $( echo " $INDEX " | grep ' ^ D ' & > /dev/null) ; then
164- STATUS=" $OSH_THEME_GIT_PROMPT_DELETED$STATUS "
165- elif $( echo " $INDEX " | grep ' ^D ' & > /dev/null) ; then
166- STATUS=" $OSH_THEME_GIT_PROMPT_DELETED$STATUS "
167- elif $( echo " $INDEX " | grep ' ^AD ' & > /dev/null) ; then
168- STATUS=" $OSH_THEME_GIT_PROMPT_DELETED$STATUS "
169- fi
170- if $( _omb_prompt_git rev-parse --verify refs/stash > /dev/null 2>&1 ) ; then
171- STATUS=" $OSH_THEME_GIT_PROMPT_STASHED$STATUS "
172- fi
173- if $( echo " $INDEX " | grep ' ^UU ' & > /dev/null) ; then
174- STATUS=" $OSH_THEME_GIT_PROMPT_UNMERGED$STATUS "
175- fi
176- if $( echo " $INDEX " | grep ' ^## [^ ]\+ .*ahead' & > /dev/null) ; then
177- STATUS=" $OSH_THEME_GIT_PROMPT_AHEAD$STATUS "
178- fi
179- if $( echo " $INDEX " | grep ' ^## [^ ]\+ .*behind' & > /dev/null) ; then
180- STATUS=" $OSH_THEME_GIT_PROMPT_BEHIND$STATUS "
181- fi
182- if $( echo " $INDEX " | grep ' ^## [^ ]\+ .*diverged' & > /dev/null) ; then
183- STATUS=" $OSH_THEME_GIT_PROMPT_DIVERGED$STATUS "
184- fi
185- echo $STATUS
142+ function git_prompt_status {
143+ local INDEX=$( _omb_prompt_git status --porcelain -b 2> /dev/null)
144+ local STATUS=
145+ if command grep -qE ' ^\?\? ' <<< " $INDEX" ; then
146+ STATUS=$OSH_THEME_GIT_PROMPT_UNTRACKED$STATUS
147+ fi
148+ if command grep -q ' ^[AM] ' <<< " $INDEX" ; then
149+ STATUS=$OSH_THEME_GIT_PROMPT_ADDED$STATUS
150+ fi
151+ if command grep -qE ' ^[ A]M |^ T ' <<< " $INDEX" ; then
152+ STATUS=$OSH_THEME_GIT_PROMPT_MODIFIED$STATUS
153+ fi
154+ if command grep -q ' ^R ' <<< " $INDEX" ; then
155+ STATUS=$OSH_THEME_GIT_PROMPT_RENAMED$STATUS
156+ fi
157+ if command grep -qE ' ^[ A]D |D ' <<< " $INDEX" ; then
158+ STATUS=$OSH_THEME_GIT_PROMPT_DELETED$STATUS
159+ fi
160+ if _omb_prompt_git rev-parse --verify refs/stash & > /dev/null; then
161+ STATUS=$OSH_THEME_GIT_PROMPT_STASHED$STATUS
162+ fi
163+ if command grep -q ' ^UU ' <<< " $INDEX" ; then
164+ STATUS=$OSH_THEME_GIT_PROMPT_UNMERGED$STATUS
165+ fi
166+ if command grep -q ' ^## [^ ]\+ .*ahead' <<< " $INDEX" ; then
167+ STATUS=$OSH_THEME_GIT_PROMPT_AHEAD$STATUS
168+ fi
169+ if command grep -q ' ^## [^ ]\+ .*behind' <<< " $INDEX" ; then
170+ STATUS=$OSH_THEME_GIT_PROMPT_BEHIND$STATUS
171+ fi
172+ if command grep -q ' ^## [^ ]\+ .*diverged' <<< " $INDEX" ; then
173+ STATUS=$OSH_THEME_GIT_PROMPT_DIVERGED$STATUS
174+ fi
175+ echo " $STATUS "
186176}
187177
188178# Compares the provided version of git to the version installed and on path
189179# Outputs -1, 0, or 1 if the installed version is less than, equal to, or
190180# greater than the input version, respectively.
191- function git_compare_version() {
181+ function git_compare_version {
192182 local INPUT_GIT_VERSION INSTALLED_GIT_VERSION
193183 _omb_util_split INPUT_GIT_VERSION " $1 " ' .'
194184 _omb_util_split INSTALLED_GIT_VERSION " $( _omb_prompt_git --version 2> /dev/null) "
@@ -210,13 +200,13 @@ function git_compare_version() {
210200
211201# Outputs the name of the current user
212202# Usage example: $(git_current_user_name)
213- function git_current_user_name() {
203+ function git_current_user_name {
214204 _omb_prompt_git config user.name 2> /dev/null
215205}
216206
217207# Outputs the email of the current user
218208# Usage example: $(git_current_user_email)
219- function git_current_user_email() {
209+ function git_current_user_email {
220210 _omb_prompt_git config user.email 2> /dev/null
221211}
222212
0 commit comments