Skip to content

Commit

Permalink
[Fix sorin-ionescu#221] Add a simple git-info
Browse files Browse the repository at this point in the history
  • Loading branch information
sorin-ionescu authored and linuslundahl committed Oct 17, 2013
1 parent d26df90 commit 100f4ef
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 50 deletions.
27 changes: 23 additions & 4 deletions modules/git/README.md
Expand Up @@ -253,19 +253,38 @@ setting a style is as follows.
| Name | Format Code | Description
| --------- | :---------: | ---------------------------------------------------
| action | %s | Special action name
| added | %a | Added files count
| ahead | %A | Commits ahead of remote count
| behind | %B | Commits behind of remote count
| branch | %b | Branch name
| commit | %c | Commit hash
| position | %p | Commits from the nearest tag count
| remote | %R | Remote name
| stashed | %S | Stashed states count

### Concise Contexts

| Name | Format Code | Description
| --------- | :---------: | ---------------------------------------------------
| clean | %C | Clean state
| dirty | %D | Dirty files count
| indexed | %i | Indexed files count
| unindexed | %I | Unindexed files count
| untracked | %u | Untracked files count

The following contexts must be enabled with the following zstyle:

zstyle ':prezto:module:git:info' verbose 'yes'

### Verbose Contexts

| Name | Format Code | Description
| --------- | :---------: | ---------------------------------------------------
| added | %a | Added files count
| clean | %C | Clean state
| deleted | %d | Deleted files count
| dirty | %D | Dirty files count
| modified | %m | Modified files count
| position | %p | Commits from the nearest tag count
| remote | %R | Remote name
| renamed | %r | Renamed files count
| stashed | %S | Stashed states count
| unmerged | %U | Unmerged files count
| untracked | %u | Untracked files count

Expand Down
161 changes: 115 additions & 46 deletions modules/git/functions/git-info
Expand Up @@ -151,6 +151,9 @@ function git-info {
local dirty_format
local dirty_formatted
local ignore_submodules
local indexed=0
local indexed_format
local indexed_formatted
local -A info_formats
local info_format
local line_number=0
Expand All @@ -171,6 +174,10 @@ function git-info {
local stashed_format
local stashed_formatted
local status_cmd
local status_mode
local unindexed=0
local unindexed_format
local unindexed_formatted
local unmerged=0
local unmerged_format
local unmerged_formatted
Expand Down Expand Up @@ -291,57 +298,117 @@ function git-info {
fi
fi

# Use porcelain status for easy parsing.
status_cmd="git status --porcelain --ignore-submodules=${ignore_submodules:-none}"

# Get current status.
while IFS=$'\n' read line; do
# Count added, deleted, modified, renamed, unmerged, untracked, dirty.
# T (type change) is undocumented, see http://git.io/FnpMGw.
# For a table of scenarii, see http://i.imgur.com/2YLu1.png.
[[ "$line" == ([ACDMT][\ MT]|[ACMT]D)\ * ]] && (( added++ ))
[[ "$line" == [\ ACMRT]D\ * ]] && (( deleted++ ))
[[ "$line" == ?[MT]\ * ]] && (( modified++ ))
[[ "$line" == R?\ * ]] && (( renamed++ ))
[[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
[[ "$line" == \?\?\ * ]] && (( untracked++ ))
(( dirty++ ))
done < <(${(z)status_cmd} 2> /dev/null)

# Format added.
if (( added > 0 )); then
zstyle -s ':prezto:module:git:info:added' format 'added_format'
zformat -f added_formatted "$added_format" "a:$added_format"
fi
# Get status type.
if ! zstyle -t ':prezto:module:git:info' verbose; then
# Format indexed.
zstyle -s ':prezto:module:git:info:indexed' format 'indexed_format'
if [[ -n "$indexed_format" ]]; then
((
indexed+=$(
git diff-index \
--no-ext-diff \
--name-only \
--cached \
--ignore-submodules=${ignore_submodules:-none} \
HEAD \
2> /dev/null \
| wc -l
)
))
if (( indexed > 0 )); then
zformat -f indexed_formatted "$indexed_format" "i:$indexed"
fi
fi

# Format deleted.
if (( deleted > 0 )); then
zstyle -s ':prezto:module:git:info:deleted' format 'deleted_format'
zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
fi
# Format unindexed.
zstyle -s ':prezto:module:git:info:unindexed' format 'unindexed_format'
if [[ -n "$unindexed_format" ]]; then
((
unindexed+=$(
git diff-files \
--no-ext-diff \
--name-only \
--ignore-submodules=${ignore_submodules:-none} \
2> /dev/null \
| wc -l
)
))
if (( unindexed > 0 )); then
zformat -f unindexed_formatted "$unindexed_format" "I:$unindexed"
fi
fi

# Format modified.
if (( modified > 0 )); then
zstyle -s ':prezto:module:git:info:modified' format 'modified_format'
zformat -f modified_formatted "$modified_format" "m:$modified"
fi
# Format untracked.
zstyle -s ':prezto:module:git:info:untracked' format 'untracked_format'
if [[ -n "$untracked_format" ]]; then
((
untracked+=$(
git ls-files \
--other \
--exclude-standard \
2> /dev/null \
| wc -l
)
))
if (( untracked > 0 )); then
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
fi
fi

# Format renamed.
if (( renamed > 0 )); then
zstyle -s ':prezto:module:git:info:renamed' format 'renamed_format'
zformat -f renamed_formatted "$renamed_format" "r:$renamed"
fi
(( dirty = indexed + unindexed + untracked ))
else
# Use porcelain status for easy parsing.
status_cmd="git status --porcelain --ignore-submodules=${ignore_submodules:-none}"

# Get current status.
while IFS=$'\n' read line; do
# Count added, deleted, modified, renamed, unmerged, untracked, dirty.
# T (type change) is undocumented, see http://git.io/FnpMGw.
# For a table of scenarii, see http://i.imgur.com/2YLu1.png.
[[ "$line" == ([ACDMT][\ MT]|[ACMT]D)\ * ]] && (( added++ ))
[[ "$line" == [\ ACMRT]D\ * ]] && (( deleted++ ))
[[ "$line" == ?[MT]\ * ]] && (( modified++ ))
[[ "$line" == R?\ * ]] && (( renamed++ ))
[[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
[[ "$line" == \?\?\ * ]] && (( untracked++ ))
(( dirty++ ))
done < <(${(z)status_cmd} 2> /dev/null)

# Format added.
if (( added > 0 )); then
zstyle -s ':prezto:module:git:info:added' format 'added_format'
zformat -f added_formatted "$added_format" "a:$added_format"
fi

# Format unmerged.
if (( unmerged > 0 )); then
zstyle -s ':prezto:module:git:info:unmerged' format 'unmerged_format'
zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
fi
# Format deleted.
if (( deleted > 0 )); then
zstyle -s ':prezto:module:git:info:deleted' format 'deleted_format'
zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
fi

# Format untracked.
if (( untracked > 0 )); then
zstyle -s ':prezto:module:git:info:untracked' format 'untracked_format'
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
# Format modified.
if (( modified > 0 )); then
zstyle -s ':prezto:module:git:info:modified' format 'modified_format'
zformat -f modified_formatted "$modified_format" "m:$modified"
fi

# Format renamed.
if (( renamed > 0 )); then
zstyle -s ':prezto:module:git:info:renamed' format 'renamed_format'
zformat -f renamed_formatted "$renamed_format" "r:$renamed"
fi

# Format unmerged.
if (( unmerged > 0 )); then
zstyle -s ':prezto:module:git:info:unmerged' format 'unmerged_format'
zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
fi

# Format untracked.
if (( untracked > 0 )); then
zstyle -s ':prezto:module:git:info:untracked' format 'untracked_format'
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
fi
fi

# Format dirty and clean.
Expand All @@ -364,6 +431,8 @@ function git-info {
"c:$commit_formatted" \
"d:$deleted_formatted" \
"D:$dirty_formatted" \
"i:$indexed_formatted" \
"I:$unindexed_formatted" \
"m:$modified_formatted" \
"p:$position_formatted" \
"R:$remote_formatted" \
Expand Down
4 changes: 4 additions & 0 deletions modules/prompt/functions/prompt_sorin_setup
Expand Up @@ -46,10 +46,14 @@ function prompt_sorin_setup {
# Add hook for calling git-info before each command.
add-zsh-hook precmd prompt_sorin_precmd

# Set editor-info parameters.
zstyle ':prezto:module:editor:info:completing' format '%B%F{red}...%f%b'
zstyle ':prezto:module:editor:info:keymap:primary' format ' %B%F{red}❯%F{yellow}❯%F{green}❯%f%b'
zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format ' %F{red}♺%f'
zstyle ':prezto:module:editor:info:keymap:alternate' format ' %B%F{green}❮%F{yellow}❮%F{red}❮%f%b'

# Set git-info parameters.
zstyle ':prezto:module:git:info' verbose 'yes'
zstyle ':prezto:module:git:info:action' format ':%%B%F{yellow}%s%f%%b'
zstyle ':prezto:module:git:info:added' format ' %%B%F{green}✚%f%%b'
zstyle ':prezto:module:git:info:ahead' format ' %%B%F{yellow}⬆%f%%b'
Expand Down

0 comments on commit 100f4ef

Please sign in to comment.