何ができるようになるか
% git init
Initialized empty Git repository
% mkdir -p dir_1/dir_2
% echo 'abc' > file_1
% tree
.
├── dir_1
│ └── dir_2
└── file_1
2 directories, 1 file
% cat file_1
abc
% cd dir_1
% cat $GIT_ROOT/file_1
abc
% cd dir_2
% cat $GIT_ROOT/file_1
abc
% cd $GIT_ROOT
このようにgitのサブディレクトリにいても環境変数GIT_ROOT
を通してgitのrootディレクトリからの相対パスでファイルを指定・アクセスできるようになる。
やり方
.zshrcに以下を書く
function precmd_set_git_root_variable() {
GIT_ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd precmd_set_git_root_variable