diff options
| -rw-r--r-- | .config/zsh/.zshrc | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index f83cac5..5aa5f14 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -374,35 +374,39 @@ extract () { alias config='git --git-dir=$HOME/.cfg --work-tree=$HOME' ## Set bare dotfiles repository git environment variables dynamically -#function set_git_env_vars() { -# local git_dir="$(git rev-parse --git-dir -C . 2>/dev/null)" -# if [[ -n "$git_dir" ]]; then -# local is_bare="$(git -C "$git_dir" rev-parse --is-bare-repository 2>/dev/null)" -# if [[ "$is_bare" == "true" ]]; then -# export GIT_DIR="$HOME/.cfg" -# export GIT_WORK_TREE=$(realpath $(eval echo ~)) -# else -# unset GIT_DIR -# unset GIT_WORK_TREE -# fi -# else -# local root_dir="$(git rev-parse --show-toplevel 2>/dev/null)" -# if [[ -n "$root_dir" ]]; then -# unset GIT_DIR -# export GIT_WORK_TREE="$root_dir" -# else -# export GIT_DIR="$HOME/.cfg" -# export GIT_WORK_TREE=$(realpath $(eval echo ~)) -# fi -# fi -#} -# -## Define an auto_cd hook to automatically update Git environment variables -#function chpwd() { -# set_git_env_vars -#} -## Call the function to set Git environment variables when the shell starts up -#set_git_env_vars +function set_git_env_vars() { + # Check if the current command is a package manager command + if [[ "${(%)${(z)history[1]}}" =~ ^(pacman|yay|apt|dnf|yum|brew) ]]; then + return + fi + local git_dir="$(git rev-parse --git-dir -C . 2>/dev/null)" + if [[ -n "$git_dir" ]]; then + local is_bare="$(git -C "$git_dir" rev-parse --is-bare-repository 2>/dev/null)" + if [[ "$is_bare" == "true" ]]; then + export GIT_DIR="$HOME/.cfg" + export GIT_WORK_TREE=$(realpath $(eval echo ~)) + else + unset GIT_DIR + unset GIT_WORK_TREE + fi + else + local root_dir="$(git rev-parse --show-toplevel 2>/dev/null)" + if [[ -n "$root_dir" ]]; then + unset GIT_DIR + export GIT_WORK_TREE="$root_dir" + else + export GIT_DIR="$HOME/.cfg" + export GIT_WORK_TREE=$(realpath $(eval echo ~)) + fi + fi +} + +# Define an auto_cd hook to automatically update Git environment variables +function chpwd() { + set_git_env_vars +} +# Call the function to set Git environment variables when the shell starts up +set_git_env_vars function gsp { |
