From 4c88079291e08dd733510d46b760e2c8d6f3da7a Mon Sep 17 00:00:00 2001 From: lowne Date: Sun, 1 Aug 2021 15:29:14 +0200 Subject: [PATCH] include helper git functions in `fish_prompt.fish` --- functions/fish_prompt.fish | 99 ++++++++++++++++++++++++++--- functions/git_ahead.fish | 8 --- functions/git_branch_name.fish | 15 ----- functions/git_has_untracked.fish | 3 - functions/git_is_detached_head.fish | 3 - functions/git_is_dirty.fish | 3 - functions/git_is_empty.fish | 3 - functions/git_is_repo.fish | 5 -- functions/git_is_staged.fish | 3 - functions/git_is_stashed.fish | 3 - functions/git_is_tag.fish | 3 - functions/git_is_touched.fish | 3 - functions/git_repository_root.fish | 3 - functions/git_untracked_files.fish | 15 ----- 14 files changed, 89 insertions(+), 80 deletions(-) delete mode 100644 functions/git_ahead.fish delete mode 100644 functions/git_branch_name.fish delete mode 100644 functions/git_has_untracked.fish delete mode 100644 functions/git_is_detached_head.fish delete mode 100644 functions/git_is_dirty.fish delete mode 100644 functions/git_is_empty.fish delete mode 100644 functions/git_is_repo.fish delete mode 100644 functions/git_is_staged.fish delete mode 100644 functions/git_is_stashed.fish delete mode 100644 functions/git_is_tag.fish delete mode 100644 functions/git_is_touched.fish delete mode 100644 functions/git_repository_root.fish delete mode 100644 functions/git_untracked_files.fish diff --git a/functions/fish_prompt.fish b/functions/fish_prompt.fish index 988e33b..0be0de0 100644 --- a/functions/fish_prompt.fish +++ b/functions/fish_prompt.fish @@ -1,3 +1,82 @@ +# git helper functions +# unused +function __git_is_repo -d "Test if the current directory is a Git repository" + if not command git rev-parse --git-dir > /dev/null 2>/dev/null + return 1 + end +end +# unused +function __git_repository_root -d "Get the top level directory of the current git repository" + __git_is_repo; and command git rev-parse --show-toplevel +end +# unused +function __git_is_empty -d "Test if a repository is empty" + git_is_repo; and test -z (command git rev-list -n 1 --all 2>/dev/null) +end +# unused +function __git_is_tag -d "Test if HEAD is on top of a tag (can be simple, annotated or signed)" + __git_is_detached_head; and command git describe --tags --exact-match HEAD 2>/dev/null > /dev/null +end + +function __git_ahead -a ahead behind diverged none + command git rev-list --count --left-right "@{upstream}...HEAD" 2>/dev/null | command awk " + /^0\t0/ { print \"$none\" ? \"$none\" : \"\"; exit 0 } + /^[0-9]+\t0/ { print \"$behind\" ? \"$behind\" : \"-\"; exit 0 } + /^0\t[0-9]+/ { print \"$ahead\" ? \"$ahead\" : \"+\"; exit 0 } + // { print \"$diverged\" ? \"$diverged\" : \"±\"; exit 0 } + " +end + +function __git_branch_name -d "Get the name of the current Git branch, tag or sha1" + set -l branch_name (command git symbolic-ref --short HEAD 2>/dev/null) + + if test -z "$branch_name" + set -l tag_name (command git describe --tags --exact-match HEAD 2>/dev/null) + + if test -z "$tag_name" + command git rev-parse --short HEAD 2>/dev/null + else + printf "%s\n" "$tag_name" + end + else + printf "%s\n" "$branch_name" + end +end + +function __git_is_staged -d "Test if there are changes staged for commit" + not command git diff --cached --no-ext-diff --quiet --exit-code 2>/dev/null +end + +function __git_is_dirty -d "Test if there are changes not staged for commit" + not command git diff --no-ext-diff --quiet --exit-code 2>/dev/null +end + +function __git_untracked_files -d "Get the number of untracked files in a repository" + git_is_repo; and command git ls-files --others --exclude-standard | command awk ' + BEGIN { n = 0 } + { n++ } + END { + print n + exit !n + }' +end + +function __git_has_untracked -d "Test if there are any untracked files in the working tree" + test "0" != (__git_untracked_files) +end +function __git_is_detached_head -d "Test if the repository is in a detached HEAD state" + not command git symbolic-ref HEAD 2>/dev/null > /dev/null +end +function __git_is_stashed -d "Test if there are changes in the Git stash" + command git rev-parse --verify --quiet refs/stash > /dev/null 2>/dev/null +end +function __git_is_touched -d "Test if there are any changes in the working tree" + __git_is_staged; or __git_is_dirty +end + +################################################################33 +# prompt function +################################################################33 function fish_prompt set -l status_copy $status set -l pwd_info (pwd_info "/") @@ -48,38 +127,38 @@ function fish_prompt segment $base_color " $pwd_info[3] " end - if set branch_name (git_branch_name) + if set branch_name (__git_branch_name) set -l git_color $text_color green set -l git_glyph "" - if git_is_staged + if __git_is_staged set git_color $text_color yellow - if git_is_dirty + if __git_is_dirty set git_color $git_color $text_color red end - else if git_is_dirty + else if __git_is_dirty set git_color $text_color red - else if git_is_touched + else if __git_is_touched set git_color $text_color red - else if git_has_untracked + else if __git_has_untracked set git_color $text_color blue end - if git_is_detached_head + if __git_is_detached_head set git_glyph "➤" - else if git_is_stashed + else if __git_is_stashed set git_glyph "╍╍" end set -l prompt - set -l git_ahead (git_ahead "+ " "- " "+- ") + set -l git_ahead (__git_ahead "+ " "- " "+- ") - if test "$branch_name" = master + if test "$branch_name" = master -o "$branch_name" = main set prompt " $git_glyph $git_ahead" else set prompt " $git_glyph $branch_name $git_ahead" diff --git a/functions/git_ahead.fish b/functions/git_ahead.fish deleted file mode 100644 index f7e7bea..0000000 --- a/functions/git_ahead.fish +++ /dev/null @@ -1,8 +0,0 @@ -function git_ahead -a ahead behind diverged none - command git rev-list --count --left-right "@{upstream}...HEAD" 2>/dev/null | command awk " - /^0\t0/ { print \"$none\" ? \"$none\" : \"\"; exit 0 } - /^[0-9]+\t0/ { print \"$behind\" ? \"$behind\" : \"-\"; exit 0 } - /^0\t[0-9]+/ { print \"$ahead\" ? \"$ahead\" : \"+\"; exit 0 } - // { print \"$diverged\" ? \"$diverged\" : \"±\"; exit 0 } - " -end diff --git a/functions/git_branch_name.fish b/functions/git_branch_name.fish deleted file mode 100644 index f08675e..0000000 --- a/functions/git_branch_name.fish +++ /dev/null @@ -1,15 +0,0 @@ -function git_branch_name -d "Get the name of the current Git branch, tag or sha1" - set -l branch_name (command git symbolic-ref --short HEAD 2>/dev/null) - - if test -z "$branch_name" - set -l tag_name (command git describe --tags --exact-match HEAD 2>/dev/null) - - if test -z "$tag_name" - command git rev-parse --short HEAD 2>/dev/null - else - printf "%s\n" "$tag_name" - end - else - printf "%s\n" "$branch_name" - end -end diff --git a/functions/git_has_untracked.fish b/functions/git_has_untracked.fish deleted file mode 100644 index 6c4a2bf..0000000 --- a/functions/git_has_untracked.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_has_untracked -d "Test if there are any untracked files in the working tree" - test "0" != (git_untracked_files) -end diff --git a/functions/git_is_detached_head.fish b/functions/git_is_detached_head.fish deleted file mode 100644 index 8703b7a..0000000 --- a/functions/git_is_detached_head.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_detached_head -d "Test if the repository is in a detached HEAD state" - git_is_repo; and not command git symbolic-ref HEAD 2>/dev/null > /dev/null -end diff --git a/functions/git_is_dirty.fish b/functions/git_is_dirty.fish deleted file mode 100644 index 608a055..0000000 --- a/functions/git_is_dirty.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_dirty -d "Test if there are changes not staged for commit" - git_is_repo; and not command git diff --no-ext-diff --quiet --exit-code 2>/dev/null -end diff --git a/functions/git_is_empty.fish b/functions/git_is_empty.fish deleted file mode 100644 index 6047591..0000000 --- a/functions/git_is_empty.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_empty -d "Test if a repository is empty" - git_is_repo; and test -z (command git rev-list -n 1 --all 2>/dev/null) -end diff --git a/functions/git_is_repo.fish b/functions/git_is_repo.fish deleted file mode 100644 index 1f69a06..0000000 --- a/functions/git_is_repo.fish +++ /dev/null @@ -1,5 +0,0 @@ -function git_is_repo -d "Test if the current directory is a Git repository" - if not command git rev-parse --git-dir > /dev/null 2>/dev/null - return 1 - end -end diff --git a/functions/git_is_staged.fish b/functions/git_is_staged.fish deleted file mode 100644 index 885ba93..0000000 --- a/functions/git_is_staged.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_staged -d "Test if there are changes staged for commit" - git_is_repo; and not command git diff --cached --no-ext-diff --quiet --exit-code 2>/dev/null -end diff --git a/functions/git_is_stashed.fish b/functions/git_is_stashed.fish deleted file mode 100644 index 25a5a3b..0000000 --- a/functions/git_is_stashed.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_stashed -d "Test if there are changes in the Git stash" - command git rev-parse --verify --quiet refs/stash > /dev/null 2>/dev/null -end diff --git a/functions/git_is_tag.fish b/functions/git_is_tag.fish deleted file mode 100644 index ab18b29..0000000 --- a/functions/git_is_tag.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_tag -d "Test if HEAD is on top of a tag (can be simple, annotated or signed)" - git_is_detached_head; and command git describe --tags --exact-match HEAD 2>/dev/null > /dev/null -end diff --git a/functions/git_is_touched.fish b/functions/git_is_touched.fish deleted file mode 100644 index 1b3b622..0000000 --- a/functions/git_is_touched.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_is_touched -d "Test if there are any changes in the working tree" - git_is_staged; or git_is_dirty -end diff --git a/functions/git_repository_root.fish b/functions/git_repository_root.fish deleted file mode 100644 index 4be29f2..0000000 --- a/functions/git_repository_root.fish +++ /dev/null @@ -1,3 +0,0 @@ -function git_repository_root -d "Get the top level directory of the current git repository" - git_is_repo; and command git rev-parse --show-toplevel -end diff --git a/functions/git_untracked_files.fish b/functions/git_untracked_files.fish deleted file mode 100644 index f70a919..0000000 --- a/functions/git_untracked_files.fish +++ /dev/null @@ -1,15 +0,0 @@ -function git_untracked_files -d "Get the number of untracked files in a repository" - git_is_repo; and command git ls-files --others --exclude-standard | command awk ' - - BEGIN { - n = 0 - } - - { n++ } - - END { - print n - exit !n - } - ' -end