From 0b5d2cce554edb04e3724b449889ce75971d86e4 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Wed, 12 Jul 2023 23:29:39 -0700 Subject: [PATCH] Improve `git-ignore` command (#1047) * Improve `git-ignore` command * fix: Improve warnings/errors and modify `not_need_git_repo` --- bin/git-ignore | 41 +++++++++++++++++++++++++++-------- etc/git-extras-completion.zsh | 3 ++- not_need_git_repo | 1 + 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/bin/git-ignore b/bin/git-ignore index 6eda7b1..e380a2e 100755 --- a/bin/git-ignore +++ b/bin/git-ignore @@ -1,6 +1,6 @@ #!/usr/bin/env bash -: ${GIT_DIR:=$(git rev-parse --git-dir)} +GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) function show_contents { local file="${2/#~/$HOME}" @@ -11,10 +11,32 @@ function show_contents { fi } +function cd_to_git_root { + local error_level="$1" + + if ! git rev-parse --git-dir &>/dev/null; then + if [ "$error_level" = '--warn' ]; then + echo "Warning: Not currently in a Git repository" >&2 + elif [ "$error_level" = '--error' ]; then + echo "Error: Not currently in a Git repository" >&2 + exit 1 + fi + fi + + local result= + if result=$(git rev-parse --show-toplevel 2>/dev/null); then + cd "$result" || exit + fi +} + function global_ignore() { - git config --global core.excludesFile || \ - ([ -n "$XDG_CONFIG_HOME" ] && echo "$XDG_CONFIG_HOME/git/ignore") || \ - echo "$HOME/.config/git/ignore" + if ! git config --global core.excludesFile 2>/dev/null; then + if [ -f "$HOME/.gitignore" ]; then + echo "$HOME/.gitignore" + else + echo "${XDG_CONFIG_HOME:-$HOME/.config}/git/ignore" + fi + fi } function show_global { @@ -22,7 +44,8 @@ function show_global { } function add_global { - local global_gitignore="$(global_ignore)" + local global_gitignore + global_gitignore="$(global_ignore)" if [ -z "$global_gitignore" ]; then echo "Can't find global .gitignore." echo "" @@ -34,22 +57,22 @@ function add_global { } function show_local { - cd "$(git root)" + cd_to_git_root --warn show_contents Local .gitignore } function add_local { - cd "$(git root)" + cd_to_git_root --warn add_patterns .gitignore "$@" } function show_private { - cd "$(git root)" + cd_to_git_root --error show_contents Private "${GIT_DIR}/info/exclude" } function add_private { - cd "$(git root)" + cd_to_git_root --error test -d "${GIT_DIR}/info" || mkdir -p "${GIT_DIR}/info" add_patterns "${GIT_DIR}/info/exclude" "$@" } diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index f5fd387..834565d 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -265,7 +265,8 @@ _git-ignore() { _arguments -C \ '(--local -l)'{--local,-l}'[show local gitignore]' \ '(--global -g)'{--global,-g}'[show global gitignore]' \ - '(--private -p)'{--private,-p}'[show repo gitignore]' + '(--private -p)'{--private,-p}'[show repo gitignore]' \ + '*:filename:_files' } diff --git a/not_need_git_repo b/not_need_git_repo index 7bec362..b66a029 100644 --- a/not_need_git_repo +++ b/not_need_git_repo @@ -5,5 +5,6 @@ git-bulk git-extras git-force-clone git-fork +git-ignore git-setup git-standup