Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yonchu committed Feb 3, 2013
1 parent 2c66b63 commit 432c050
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
21 changes: 12 additions & 9 deletions lib/gitstatus-fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
from subprocess import Popen, PIPE, \
check_call, CalledProcessError

# The name of environmental variable
# for branch name on which count unmerged commits count.
ENV_MERGE_BRANCH = 'ZSH_VCS_PROMPT_MERGE_BRANCH'

# Git error messages.
ERR_MSG_NO_BRANCH = 'fatal: ref HEAD is not a symbolic ref'
ERR_MSG_UNKNOWN_OPTION_SHORT = "error: unknown option `short'"

Expand Down Expand Up @@ -60,11 +63,11 @@ def check_before_running():
def main():
#check_before_running()

# git top directory
# Git top directory.
top_dir = run_cmd('git rev-parse --show-toplevel').strip()
os.chdir(top_dir)

# branch
# Current branch name.
branch = ''
try:
# Old version git does not suppoert the option --short.
Expand All @@ -84,7 +87,7 @@ def main():
else:
raise

# staged
# Count staged files.
try:
staged_files = run_cmd('git diff --staged --name-status').splitlines()
staged_files = [namestat[0] for namestat in staged_files]
Expand All @@ -95,33 +98,33 @@ def main():
if namestat[0] in ['U', 'M', 'A', 'D', 'R', 'C']:
staged_files.append(namestat[0])

# conflicts
# Count conflicts.
conflicts = staged_files.count('U')
if conflicts > 0:
return 1
staged = len(staged_files) - conflicts

# unstaged
# Count unstaged files.
unstaged_files = run_cmd('git diff --name-status')
unstaged_files = [namestat[0] for namestat in unstaged_files.splitlines()]
unstaged = len(unstaged_files) - unstaged_files.count('U')

# untracked
# Count untracked files.
untracked_files = run_cmd('git ls-files --others --exclude-standard')
untracked_files = untracked_files.splitlines()
untracked = len(untracked_files)

# stash
# Count stashed files.
stash_list = run_cmd('git stash list')
stashed = len(stash_list.splitlines())

# checking clean
# Check if clean.
if not unstaged and not staged and not conflicts and not untracked:
clean = '1'
else:
clean = '0'

# remote
# Count difference commits between the current branch and the remote branch.
ahead = '0'
behind = '0'
head_branch = run_cmd('git symbolic-ref HEAD').strip()
Expand Down
2 changes: 1 addition & 1 deletion lib/run-vcsstatus.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env zsh

## The exe directory.
cwd=$(cd $(dirname $0) && pwd)
cwd=$(cd "$(dirname "$0")" && pwd)

source $cwd/vcsstatus.sh

Expand Down
21 changes: 11 additions & 10 deletions lib/vcsstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# http://d.hatena.ne.jp/yuroyoro/20110219/1298089409
# http://d.hatena.ne.jp/pasela/20110216/git_not_pushed
# http://liosk.blog103.fc2.com/blog-entry-209.html
# http://qiita.com/items/8d5a627d773758dd8078
autoload -Uz vcs_info || return 1
zstyle ':vcs_info:*' enable git svn hg bzr

Expand Down Expand Up @@ -67,6 +68,7 @@ function _zsh_vcs_prompt_vcs_detail_info() {
local vcs_action=0
local git_status

# Run vcs_info.
psvar=()
LANG=en_US.UTF-8 vcs_info
if [ -z "$vcs_info_msg_0_" ]; then
Expand All @@ -79,17 +81,15 @@ function _zsh_vcs_prompt_vcs_detail_info() {

# Get git status.
if is-at-least 4.3.11; then
git_status="$vcs_info_msg_2_"
git_status=$vcs_info_msg_2_
else
if [ "$vcs_name" = 'git' ]; then
git_status="$(_zsh_vcs_prompt_get_git_status "$vcs_branch_name")"
git_status=$(_zsh_vcs_prompt_get_git_status "$vcs_branch_name")
fi
fi

# Output result.
echo "$vcs_name\n$vcs_action\n$vcs_branch_name\n$git_status"

return 0
}

# The hook function.
Expand All @@ -99,8 +99,9 @@ function +vi-git-hook-detail-info() {
if [ "$1" != '2' ]; then
return 0
fi
local git_status="$(_zsh_vcs_prompt_get_git_status "$hook_com[branch]")"
hook_com[misc]+="$git_status"
local git_status
git_status=$(_zsh_vcs_prompt_get_git_status "$hook_com[branch]")
hook_com[misc]+=$git_status
return 0
}

Expand All @@ -127,18 +128,18 @@ function _zsh_vcs_prompt_get_git_status() {

if [ "$(command git rev-parse --is-inside-work-tree 2> /dev/null)" = 'true' ]; then
is_inside_work_tree='true'
staged_files="$(command git diff --staged --name-status)"
staged_files=$(command git diff --staged --name-status)
if [ $? -ne 0 ]; then
# Error occurs on old version git.
staged_files=$(command git status --short --porcelain | command grep '^[UMADRC]')
fi
unstaged_files="$(command git diff --name-status)"
untracked_files="$(command git ls-files --others --exclude-standard "$(command git rev-parse --show-toplevel)")"
unstaged_files=$(command git diff --name-status)
untracked_files=$(command git ls-files --others --exclude-standard "$(command git rev-parse --show-toplevel)")
if [ $? -ne 0 ]; then
# Error occurs on old version git.
untracked_files=$(cd "$1" > /dev/null && command git ls-files --others --exclude-standard)
fi
stash_list="$(command git stash list)"
stash_list=$(command git stash list)
else
clean='?'
fi
Expand Down
52 changes: 24 additions & 28 deletions zshrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 2.Add the following in your .zshrc:
# # e.g.
# RPROMPT='$(vcs_super_info)'
# ZSH_VCS_PROMPT_ENABLE_CACHING='true'
#
###############################################################################

Expand Down Expand Up @@ -39,7 +40,7 @@ ZSH_VCS_PROMPT_ENABLE_CACHING=${ZSH_VCS_PROMPT_ENABLE_CACHING:-'false'}
## Use the python script (lib/gitstatus-fast.py) by default.
ZSH_VCS_PROMPT_USING_PYTHON=${ZSH_VCS_PROMPT_USING_PYTHON:-'true'}

## The branch name to print no merge commit count.
## The branch name to print unmerged commits count.
# If not set, don't print count.
ZSH_VCS_PROMPT_MERGE_BRANCH=${ZSH_VCS_PROMPT_MERGE_BRANCH:-'master'}
export ZSH_VCS_PROMPT_MERGE_BRANCH
Expand Down Expand Up @@ -188,7 +189,7 @@ fi

# Setup logging.
ZSH_VCS_PROMPT_LOG_FILE="$ZSH_VCS_PROMPT_DIR/zsh-vcs-prompt.log"
ZSH_VCS_PROMPT_ERROR_COUNT='0'
ZSH_VCS_PROMPT_ERROR_COUNT=0
function _zsh_vcs_prompt_check_log_file() {
if [ "$ZSH_VCS_PROMPT_LOGGING_LEVEL" != '1' ]; then
return
Expand Down Expand Up @@ -264,10 +265,11 @@ function _zsh_vcs_prompt_precmd_hook_func() {

function _zsh_vcs_prompt_update_vcs_status() {
# Parse raw data.
local raw_data="$(vcs_super_info_raw_data)"
local raw_data
raw_data=$(vcs_super_info_raw_data)
if [ -z "$raw_data" ]; then
ZSH_VCS_PROMPT_VCS_STATUS=''
return 0
ZSH_VCS_PROMPT_VCS_STATUS=
return
fi

local -a vcs_status
Expand Down Expand Up @@ -295,22 +297,22 @@ function _zsh_vcs_prompt_update_vcs_status() {
# Select formats.
local used_formats
if [ "$vcs_name" = 'git' ]; then
used_formats="$ZSH_VCS_PROMPT_GIT_ACTION_FORMATS"
used_formats=$ZSH_VCS_PROMPT_GIT_ACTION_FORMATS
# Check action.
if [ -z "$action" -o "$action" = '0' ]; then
action=''
action=
if [ "$using_python" = '1' -a -n "$ZSH_VCS_PROMPT_GIT_FORMATS_USING_PYTHON" ]; then
used_formats="$ZSH_VCS_PROMPT_GIT_FORMATS_USING_PYTHON"
used_formats=$ZSH_VCS_PROMPT_GIT_FORMATS_USING_PYTHON
else
used_formats="$ZSH_VCS_PROMPT_GIT_FORMATS"
used_formats=$ZSH_VCS_PROMPT_GIT_FORMATS
fi
fi
else
used_formats="$ZSH_VCS_PROMPT_VCS_ACTION_FORMATS"
used_formats=$ZSH_VCS_PROMPT_VCS_ACTION_FORMATS
# Check action.
if [ -z "$action" -o "$action" = '0' ]; then
action=''
used_formats="$ZSH_VCS_PROMPT_VCS_FORMATS"
action=
used_formats=$ZSH_VCS_PROMPT_VCS_FORMATS
fi
fi

Expand All @@ -330,9 +332,9 @@ function _zsh_vcs_prompt_update_vcs_status() {
untracked=$(_zsh_vcs_prompt_set_sigil "$untracked" "$ZSH_VCS_PROMPT_UNTRACKED_SIGIL")
stashed=$(_zsh_vcs_prompt_set_sigil "$stashed" "$ZSH_VCS_PROMPT_STASHED_SIGIL")
if [ "$clean" = '1' ]; then
clean="$ZSH_VCS_PROMPT_CLEAN_SIGIL"
clean=$ZSH_VCS_PROMPT_CLEAN_SIGIL
elif [ "$clean" = '0' ]; then
clean=''
clean=
fi

# Compose prompt status.
Expand Down Expand Up @@ -392,17 +394,13 @@ function vcs_super_info_raw_data() {
if [ "$ZSH_VCS_PROMPT_USING_PYTHON" = 'true' ] \
&& type python > /dev/null 2>&1 \
&& type git > /dev/null 2>&1 \
&& [ "$(command git rev-parse --is-inside-work-tree 2> /dev/null)" = "true" ]; then
&& [ "$(command git rev-parse --is-inside-work-tree 2> /dev/null)" = 'true' ]; then

# Check python command.
# The python script to get git status.
local cmd_gitstatus="${ZSH_VCS_PROMPT_DIR}/lib/gitstatus-fast.py"
if [ ! -f "$cmd_gitstatus" ]; then
echo "[ zsh-vcs-prompt error: ${ZSH_VCS_PROMPT_DIR}/lib/gitstatus-fast.py is not found ]" 1>&2
return 1
fi

# Get vcs status.
local git_status="$(python "$cmd_gitstatus")"
local git_status
git_status=$(python "$cmd_gitstatus")
if [ -n "$git_status" ];then
using_python=1
local vcs_name='git'
Expand All @@ -416,20 +414,18 @@ function vcs_super_info_raw_data() {
fi
fi

# Don't use python.
# When don't use python or error occurs in the python scritp.
local vcs_status

if type _zsh_vcs_prompt_vcs_detail_info > /dev/null 2>&1; then
## zsh
# Run the sourced function.
vcs_status="$(_zsh_vcs_prompt_vcs_detail_info)"
else
## bash
# Run the external script
local cmd_run_vcsstatus="${ZSH_VCS_PROMPT_DIR}/lib/run-vcsstatus.sh"
if [ ! -f "$cmd_run_vcsstatus" ]; then
echo "[ zsh-vcs-prompt error: ${ZSH_VCS_PROMPT_DIR}/lib/run-vcsstatus.sh is not found ]" 1>&2
return 1
fi
vcs_status="$("$cmd_run_vcsstatus")"
vcs_status=$("$cmd_run_vcsstatus")
fi

if [ -z "$vcs_status" ]; then
Expand Down

0 comments on commit 432c050

Please sign in to comment.