Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
Oh My Zsh
Commits
45a954cb
Commit
45a954cb
authored
Oct 13, 2016
by
Billy Conn
Committed by
Marc Cornellà
Oct 09, 2020
Browse files
git_prompt_status now uses hash lookups instead of multiple greps
parent
297238b7
Changes
1
Show whitespace changes
Inline
Side-by-side
lib/git.zsh
View file @
45a954cb
...
@@ -147,46 +147,104 @@ function git_prompt_long_sha() {
...
@@ -147,46 +147,104 @@ function git_prompt_long_sha() {
SHA
=
$(
__git_prompt_git rev-parse HEAD 2> /dev/null
)
&&
echo
"
$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER
"
SHA
=
$(
__git_prompt_git rev-parse HEAD 2> /dev/null
)
&&
echo
"
$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER
"
}
}
# Get the status of the working tree
function
git_prompt_status
()
{
function
git_prompt_status
()
{
emulate
-L
zsh
[[
"
$(
__git_prompt_git config
--get
oh-my-zsh.hide-status 2>/dev/null
)
"
=
1
]]
&&
return
[[
"
$(
__git_prompt_git config
--get
oh-my-zsh.hide-status 2>/dev/null
)
"
=
1
]]
&&
return
local
INDEX STATUS
local
status_prompt
=
""
INDEX
=
$(
__git_prompt_git status
--porcelain
-b
2> /dev/null
)
||
return
0
STATUS
=
""
# A lookup table of each git status encountered
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)
\\
?
\\
? '
]]
;
then
local
-A
statuses_seen
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS
"
fi
# Maps a git status prefix to an internal constant
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)(A |M |MM) '
]]
;
then
# This cannot use the prompt constants, as they may be empty
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_ADDED$STATUS
"
local
-A
prefix_constant_map
=(
fi
'?? '
'UNTRACKED'
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)([ AM]M| T) '
]]
;
then
'A '
'ADDED'
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS
"
'M '
'ADDED'
fi
'MM '
'ADDED'
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)R '
]]
;
then
' M '
'MODIFIED'
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS
"
'AM '
'MODIFIED'
fi
' T '
'MODIFIED'
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)([A ]D|D ) '
]]
;
then
'R '
'RENAMED'
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_DELETED$STATUS
"
' D '
'DELETED'
'D '
'DELETED'
'UU '
'UNMERGED'
'ahead'
'AHEAD'
'behind'
'BEHIND'
'diverged'
'DIVERGED'
'stashed'
'STASHED'
)
# Maps the internal constant to the prompt theme
local
-A
constant_prompt_map
=(
'UNTRACKED'
"
$ZSH_THEME_GIT_PROMPT_UNTRACKED
"
'ADDED'
"
$ZSH_THEME_GIT_PROMPT_ADDED
"
'MODIFIED'
"
$ZSH_THEME_GIT_PROMPT_MODIFIED
"
'RENAMED'
"
$ZSH_THEME_GIT_PROMPT_RENAMED
"
'DELETED'
"
$ZSH_THEME_GIT_PROMPT_DELETED
"
'UNMERGED'
"
$ZSH_THEME_GIT_PROMPT_UNMERGED
"
'AHEAD'
"
$ZSH_THEME_GIT_PROMPT_AHEAD
"
'BEHIND'
"
$ZSH_THEME_GIT_PROMPT_BEHIND
"
'DIVERGED'
"
$ZSH_THEME_GIT_PROMPT_DIVERGED
"
'STASHED'
"
$ZSH_THEME_GIT_PROMPT_STASHED
"
)
# The order that the prompt displays should be added to the prompt
local
status_constants
=(
UNTRACKED ADDED MODIFIED RENAMED DELETED STASHED
UNMERGED AHEAD BEHIND DIVERGED
)
local
status_text
=
$(
__git_prompt_git status
--porcelain
-b
2> /dev/null
)
# Don't continue on a catastrophic failure
if
[[
$?
-eq
128
]]
;
then
return
1
fi
fi
if
$(
__git_prompt_git rev-parse
--verify
refs/stash
>
/dev/null 2>&1
)
;
then
if
$(
__git_prompt_git rev-parse
--verify
refs/stash
>
/dev/null 2>&1
)
;
then
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_STASHED$STATUS
"
statuses_seen[
'STASHED'
]=
1
fi
fi
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)UU '
]]
;
then
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS
"
local
status_lines
=(
"
${
(@f)
${
status_text
}}
"
)
;
# If the tracking line exists, get and parse it
if
[[
$status_lines
[
1]
=
~
"^## [^ ]+
\[
(.*)
\]
"
]]
;
then
local
branch_statuses
=(
"
${
(@s/,/)match
}
"
)
for
branch_status
in
$branch_statuses
;
do
if
[[
!
$branch_status
=
~
"(behind|diverged|ahead) ([0-9]+)?"
]]
;
then
continue
fi
fi
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)## [^ ]
\+
.*ahead'
]]
;
then
local
last_parsed_status
=
$prefix_constant_map
[
$match
[
1]]
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS
"
statuses_seen[
$last_parsed_status
]=
$match
[
2]
done
shift
status_lines
fi
fi
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)## [^ ]
\+
.*behind'
]]
;
then
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS
"
# This not only gives us a status lookup, but the count of each type
for
status_line
in
${
status_lines
}
;
do
local
status_prefix
=
${
status_line
[1, 3]
}
local
status_constant
=
${
(v)prefix_constant_map[
$status_prefix
]
}
if
[[
-z
$status_constant
]]
;
then
continue
fi
fi
if
[[
"
${
INDEX
}
"
=
~
$'(^|
\n
)## [^ ]
\+
.*diverged'
]]
;
then
STATUS
=
"
$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS
"
((
statuses_seen[
$status_constant
]
++
))
done
# At this point, the statuses_seen hash contains:
# - Tracking => The difference between tracked and current
# - Modifications => The count of that type of modification
# - Stash => Whether or not a stash exists
# Might be useful for someone?
for
status_constant
in
$status_constants
;
do
if
[[
${
+statuses_seen[
$status_constant
]
}
-eq
1
]]
;
then
local
next_display
=
$constant_prompt_map
[
$status_constant
]
status_prompt
=
"
$next_display$status_prompt
"
fi
fi
echo
$STATUS
done
echo
$status_prompt
}
}
# Outputs the name of the current user
# Outputs the name of the current user
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment