tide/functions/_tide_item_git.fish

72 lines
3.5 KiB
Fish
Raw Normal View History

2021-01-17 22:22:02 +03:00
function _tide_item_git
2021-08-22 01:05:08 +03:00
set -l location $_tide_location_color(git branch --show-current 2>/dev/null) || return
2021-05-16 20:02:38 +03:00
# --quiet = don't error if there are no commits
git rev-parse --quiet --git-dir --is-inside-git-dir --short HEAD |
2021-05-12 23:13:51 +03:00
read --local --line git_dir is_inside_git_dir sha
2021-05-16 20:02:38 +03:00
if test -z "$location" # Default to branch, then tag, then sha
2021-08-22 01:05:08 +03:00
set location '#'$_tide_location_color(git tag --points-at HEAD)[1] # get the first tag
test -z "$location" && set location '@'$_tide_location_color$sha
end
2021-01-17 02:15:33 +03:00
2021-01-17 04:17:18 +03:00
# Operation
2021-05-12 23:13:51 +03:00
if test -d $git_dir/rebase-merge
2021-06-14 02:44:11 +03:00
read tide_git_step <$git_dir/rebase-merge/msgnum
read tide_git_total_steps <$git_dir/rebase-merge/end
2021-05-12 23:13:51 +03:00
if test -f $git_dir/rebase-merge/interactive
2021-06-14 02:44:11 +03:00
set tide_git_operation rebase-i
2021-01-17 02:15:33 +03:00
else
2021-06-14 02:44:11 +03:00
set tide_git_operation rebase-m
2021-01-17 02:15:33 +03:00
end
2021-05-12 23:13:51 +03:00
else if test -d $git_dir/rebase-apply
2021-06-14 02:44:11 +03:00
read tide_git_step <$git_dir/rebase-apply/next
read tide_git_total_steps <$git_dir/rebase-apply/last
2021-05-12 23:13:51 +03:00
if test -f $git_dir/rebase-apply/rebasing
2021-06-14 02:44:11 +03:00
set tide_git_operation rebase
2021-05-12 23:13:51 +03:00
else if test -f $git_dir/rebase-apply/applying
2021-06-14 02:44:11 +03:00
set tide_git_operation am
2021-01-17 02:15:33 +03:00
else
2021-06-14 02:44:11 +03:00
set tide_git_operation am/rebase
2021-01-17 02:15:33 +03:00
end
2021-05-12 23:13:51 +03:00
else if test -f $git_dir/MERGE_HEAD
2021-06-14 02:44:11 +03:00
set tide_git_operation merge
2021-05-12 23:13:51 +03:00
else if test -f $git_dir/CHERRY_PICK_HEAD
2021-06-14 02:44:11 +03:00
set tide_git_operation cherry-pick
2021-05-12 23:13:51 +03:00
else if test -f $git_dir/REVERT_HEAD
2021-06-14 02:44:11 +03:00
set tide_git_operation revert
2021-05-12 23:13:51 +03:00
else if test -f $git_dir/BISECT_LOG
2021-06-14 02:44:11 +03:00
set tide_git_operation bisect
2021-01-17 02:15:33 +03:00
end
2021-01-16 20:49:48 +03:00
2021-05-16 20:02:38 +03:00
# Upstream behind/ahead. Suppress errors in case there is no upstream
2021-01-16 21:11:47 +03:00
git rev-list --count --left-right @{upstream}...HEAD 2>/dev/null |
2021-05-12 23:13:51 +03:00
read --local --delimiter=\t upstream_behind upstream_ahead
test "$upstream_behind" = 0 && set -e upstream_behind
test "$upstream_ahead" = 0 && set -e upstream_ahead
2021-01-16 21:11:47 +03:00
# Git status/stash
2021-06-14 02:44:11 +03:00
test "$is_inside_git_dir" = true && set -l tide_git_set_dir_option -C $git_dir/..
# Suppress errors in case we are in a bare repo
2021-06-14 02:44:11 +03:00
set -l git_info (git $tide_git_set_dir_option --no-optional-locks status --porcelain 2>/dev/null)
set -l stash (git $tide_git_set_dir_option stash list 2>/dev/null | count) || set -e stash
2021-05-12 23:13:51 +03:00
set -l conflicted (string match --regex '^UU' $git_info | count) || set -e conflicted
set -l staged (string match --regex '^[ADMR].' $git_info | count) || set -e staged
set -l dirty (string match --regex '^.[ADMR]' $git_info | count) || set -e dirty
set -l untracked (string match --regex '^\?\?' $git_info | count) || set -e untracked
2021-01-17 03:50:50 +03:00
if set -q tide_git_operation || set -q conflicted
set -g tide_git_bg_color $tide_git_bg_color_urgent
else if set -q staged || set -q dirty || set -q untracked
set -g tide_git_bg_color $tide_git_bg_color_unstable
end
2021-08-22 01:05:08 +03:00
_tide_print_item git $_tide_location_color $tide_git_icon' ' (set_color white; printf %s $location
2021-07-07 00:09:17 +03:00
set_color $tide_git_color_operation; printf %s ' '$tide_git_operation ' '$tide_git_step/$tide_git_total_steps
set_color $tide_git_color_upstream; printf %s ' ⇣'$upstream_behind ' ⇡'$upstream_ahead
set_color $tide_git_color_stash; printf %s ' *'$stash
set_color $tide_git_color_conflicted; printf %s ' ~'$conflicted
set_color $tide_git_color_staged; printf %s ' +'$staged
set_color $tide_git_color_dirty; printf %s ' !'$dirty
set_color $tide_git_color_untracked; printf %s ' ?'$untracked)
end