diff --git a/git_status.py b/git_status.py deleted file mode 100755 index c78d7cd..0000000 --- a/git_status.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/python3 -import os -import pygit2 - -def test_status(num, stat_key): - if num & stat_key: - return 1 - else: - return 0 - -if os.path.isdir(".git"): - repo = pygit2.Repository(".git") - files = repo.status() - staged = 0 - unstaged = 0 - confl = 0 - for filename in files: - staged += test_status(files[filename], pygit2.GIT_STATUS_INDEX_NEW) - staged += test_status(files[filename], pygit2.GIT_STATUS_INDEX_DELETED) - staged += test_status(files[filename], pygit2.GIT_STATUS_INDEX_MODIFIED) - unstaged += test_status(files[filename], pygit2.GIT_STATUS_WT_NEW) - unstaged += test_status(files[filename], pygit2.GIT_STATUS_WT_DELETED) - unstaged += test_status(files[filename], pygit2.GIT_STATUS_WT_MODIFIED) - confl += test_status(files[filename], pygit2.GIT_STATUS_CONFLICTED) - result = str() - if staged > 0: - result += 's' + str(staged) - if unstaged > 0: - result += '+' + str(unstaged) - if confl > 0: - result += 'x' + str(confl) - if not result: - result += '.' - print(result) diff --git a/gitstatus.zsh b/gitstatus.zsh new file mode 100755 index 0000000..db8a136 --- /dev/null +++ b/gitstatus.zsh @@ -0,0 +1,29 @@ +#!/usr/bin/env zsh +git_status='' +staged_count=0 +unstaged_count=0 +untracked_count=0 +ifs_temp=$IFS +IFS= +if ! raw_status=$(git status --porcelain -bu 2>/dev/null); then + exit 1 +fi + +while read line; do + if [[ $line[1,2] == '##' ]]; then + IFS='.' + read branch _ _ origin <<< $line[4,-1] + fi + [[ $line[1,2] =~ '.[MD]' ]] && (( unstaged_count++ )) + [[ $line[1,2] =~ '[MDARC].' ]] && (( staged_count++ )) + [[ $line[1,2] == '??' ]] && (( untracked_count++ )) +done <<< $raw_status + +(( $unstaged_count > 0 )) && git_status+="u$unstaged_count" +(( $staged_count > 0 )) && git_status+="s$staged_count" +(( $untracked_count > 0 )) && git_status+="+$untracked_count" +[[ -z $git_status ]] && git_status='.' + +full_status="[ $branch {$origin} $git_status ]" + +printf '%s' $full_status