From 0cd467b4b3e8cf3aa3209f7182583c9c6b9e8d4a Mon Sep 17 00:00:00 2001 From: pztrn Date: Mon, 4 Jan 2016 20:50:31 +0500 Subject: [PATCH] Gitinfo and pztrn prompt: untracked files, and speed improvements. --- zsh/apps/gitinfo.app.zsh | 118 ++++++++++++++++++++++++--------------- zsh/prompts/pztrn.zsh | 3 +- 2 files changed, 76 insertions(+), 45 deletions(-) diff --git a/zsh/apps/gitinfo.app.zsh b/zsh/apps/gitinfo.app.zsh index 53ba799..6c98ffe 100644 --- a/zsh/apps/gitinfo.app.zsh +++ b/zsh/apps/gitinfo.app.zsh @@ -2,28 +2,29 @@ GITINFO_BRANCH="" GITINFO_COMMIT_ID="" GITINFO_COMMIT_SHORTID="" GITINFO_COMMIT_COUNT=0 -GITINFO_NEW_FILES="" -GITINFO_MODIFIED_FILES="" GITINFO_DELETED_FILES="" +GITINFO_MODIFIED_FILES="" +GITINFO_NEW_FILES="" +GITINFO_RECHECK=0 GITINFO_REMOTES="" GITINFO_STASHES_COUNT=0 +GITINFO_TOPLEVEL_DIR="" +GITINFO_UNTRACKED_COUNT=0 ###################################################################### # Main function. ###################################################################### function gitinfo() { + GITINFO_RECHECK=1 + gitinfo_internal_checks if [ $? -eq 1 ]; then echo "This is not a git repository." return 1 fi - gitinfo_get_branch - gitinfo_get_changes - gitinfo_get_commit_data - gitinfo_get_remotes - gitinfo_get_stashes + gitinfo_get_info gitinfo_check if [ $? -eq 0 ]; then @@ -32,6 +33,7 @@ function gitinfo() echo "| Branch | $fg[yellow]${GITINFO_BRANCH}$reset_color" echo "| Commit | $fg[green]${GITINFO_COMMIT_ID}$reset_color" echo "| Commits count | ${GITINFO_COMMIT_COUNT}" + echo "| Changes | $fg[green]${GITINFO_NEW_FILES}$reset_color new, $fg[yellow]${GITINFO_MODIFIED_FILES}$reset_color modified, $fg[red]${GITINFO_DELETED_FILES}$reset_color deleted, $fg[gray]${GITINFO_UNTRACKED_COUNT}$reset_color untracked" echo "| Stashes | ${GITINFO_STASHES_COUNT}" echo "======================================================================" else @@ -45,7 +47,7 @@ function gitinfo() function gitinfo_chpwd() { emulate -L zsh - precmd_functions=(${precmd_functions[@]} "gitinfo_get_branch" "gitinfo_get_commit_data" "gitinfo_get_changes" "gitinfo_get_remotes" "gitinfo_get_stashes") + precmd_functions=(${precmd_functions[@]} "gitinfo_get_info") } ###################################################################### @@ -56,26 +58,32 @@ function gitinfo_check() { curdir=`pwd` - if [ "${#GITINFO_BRANCH}" -eq 0 ]; then + GITINFO_TOPLEVEL_DIR=`git rev-parse --show-toplevel 2>/dev/null` + if [ $? -ne 0 ]; then + GITINFO_TOPLEVEL_DIR="" return 1 fi - objects=`ls .git/objects | grep -v "info\|pack" | wc -l` - if [ ${objects} -lt 1 ]; then + if [ "${curdir/${GITINFO_TOPLEVEL_DIR}}" != "${curdir}" ]; then + GITINFO_RECHECK=1 + gitinfo_internal_checks + + if [ $? -eq 0 ]; then + return 0 + else + return 1 + fi + else + GITINFO_RECHECK=0 return 1 fi } ###################################################################### -# Gets current directory's branch. +# Gets current repo branch. ###################################################################### function gitinfo_get_branch() { - gitinfo_internal_checks - if [ $? -eq 1 ]; then - GITINFO_BRANCH="" - return 1 - fi GITINFO_BRANCH=`git branch | grep "*" | cut -d " " -f 2` } @@ -84,56 +92,74 @@ function gitinfo_get_branch() ###################################################################### function gitinfo_get_changes() { - gitinfo_internal_checks - if [ $? -eq 1 ]; then - GITINFO_NEW_FILES="" - GITINFO_MODIFIED_FILES="" - GITINFO_DELETED_FILES="" - return 1 - fi GITINFO_NEW_FILES=`LC_ALL=C git status | grep "new" | wc -l` GITINFO_MODIFIED_FILES=`LC_ALL=C git status | grep "modified" | wc -l` GITINFO_DELETED_FILES=`LC_ALL=C git status | grep "deleted" | wc -l` } ###################################################################### -# Gets current directory's commit info, like long hash, short hash, +# Gets current repo commit info, like long hash, short hash, # commits count. ###################################################################### function gitinfo_get_commit_data() { - gitinfo_internal_checks - if [ $? -eq 1 ]; then - GITINFO_COMMIT_SHORTID="" - GITINFO_COMMIT_ID="" - GITINFO_COMMIT_COUNT="" - return 1 - fi GITINFO_COMMIT_SHORTID=`git rev-parse --short HEAD` GITINFO_COMMIT_ID=`git log | head -n 1 | cut -d " " -f 2` GITINFO_COMMIT_COUNT=`git log | grep "commit " | wc -l` } +###################################################################### +# This function should be called to obtain neccessary data. +###################################################################### +function gitinfo_get_info() +{ + gitinfo_check + if [ $? -eq 0 ]; then + gitinfo_get_branch + gitinfo_get_changes + gitinfo_get_commit_data + gitinfo_get_remotes + gitinfo_get_stashes + gitinfo_get_untracked + else + GITINFO_BRANCH="" + GITINFO_COMMIT_ID="" + GITINFO_COMMIT_SHORTID="" + GITINFO_COMMIT_COUNT=0 + GITINFO_DELETED_FILES="" + GITINFO_MODIFIED_FILES="" + GITINFO_NEW_FILES="" + GITINFO_RECHECK=0 + GITINFO_REMOTES="" + GITINFO_STASHES_COUNT=0 + GITINFO_TOPLEVEL_DIR="" + GITINFO_UNTRACKED_COUNT=0 + fi +} + ###################################################################### # Gets remotes ###################################################################### function gitinfo_get_remotes() { - gitinfo_internal_checks - if [ $? -eq 1 ]; then - GITINFO_REMOTES="" - return 1 - fi GITINFO_REMOTES=$(git remote -v | awk {' print $2 '} | uniq | wc -l) } ###################################################################### -# Gets current directory's stashes count. +# Gets current repo stashes count. ###################################################################### function gitinfo_get_stashes() { + GITINFO_STASHES_COUNT=`git stash list 2>/dev/null | wc -l` +} + +###################################################################### +# Gets current repo untracked files and directories count. +###################################################################### +function gitinfo_get_untracked() +{ + GITINFO_UNTRACKED_COUNT=`LC_ALL=C git status | grep -v "be\ committed\|On\ branch\|nothing\ added\|Untracked\ files\|git\ add\|Your\ branch\|new\ file\|git\ reset\|nothing\ to\ commit\|not\ staged\|git\ checkout\|modified\:" | sed '/^$/d' | wc -l` - GITINFO_STASHES_COUNT=$(git stash list 2>/dev/null | wc -l) } ###################################################################### @@ -141,14 +167,18 @@ function gitinfo_get_stashes() ###################################################################### function gitinfo_internal_checks() { - curdir=`pwd` - if [ ! -d "${curdir}/.git" ]; then - GITINFO_STASHES_COUNT="" + if [ ${GITINFO_RECHECK} -eq 0 ]; then return 1 fi - objects=`ls .git/objects | grep -v "info\|pack" | wc -l` - if [ ${objects} -lt 1 ]; then + if [ ${#GITINFO_TOPLEVEL_DIR} -ne 0 ]; then + objects=`ls ${GITINFO_TOPLEVEL_DIR}/.git/objects | grep -v "info\|pack" | wc -l` + if [ ${objects} -lt 1 ]; then + return 1 + fi + else return 1 fi + + GITINFO_RECHECK=0 } diff --git a/zsh/prompts/pztrn.zsh b/zsh/prompts/pztrn.zsh index 5db2697..4be6eba 100644 --- a/zsh/prompts/pztrn.zsh +++ b/zsh/prompts/pztrn.zsh @@ -77,10 +77,11 @@ prompt_pztrn_gitinfo() gitinfo_check #echo $? if [[ $? -eq 0 ]]; then + gitinfo_get_info p_gitinfo_branch="$vars['brackets_start']$vars['usercolor']${GITINFO_BRANCH}$reset_color$vars['brackets_end']" p_gitinfo_commit="$vars['brackets_start']%{$fg[magenta]%}${GITINFO_COMMIT_SHORTID}$reset_color (%{$fg[cyan]%}${GITINFO_COMMIT_COUNT}$reset_color)$vars['brackets_end']" p_gitinfo_remotes="$vars['brackets_start']%{$fg[green]%}${GITINFO_REMOTES} remote(s)$reset_color$vars['brackets_end']" - p_gitinfo_files="$vars['brackets_start']%{$fg[green]%}${GITINFO_NEW_FILES}$reset_color new, %{$fg[yellow]%}${GITINFO_MODIFIED_FILES}$reset_color modified, %{$fg[red]%}${GITINFO_DELETED_FILES} deleted$vars['brackets_end']" + p_gitinfo_files="$vars['brackets_start']%{$fg[green]%}${GITINFO_NEW_FILES}$reset_color new, %{$fg[yellow]%}${GITINFO_MODIFIED_FILES}$reset_color modified, %{$fg[red]%}${GITINFO_DELETED_FILES}$reset_color deleted, %{$fg[gray]%}${GITINFO_UNTRACKED_COUNT}$reset_color untracked$vars['brackets_end']" p_gitinfo=$'\n'"$p_gitinfo_branch$p_gitinfo_commit$p_gitinfo_files$p_gitinfo_remotes" else p_gitinfo=""