From 734b8620e8184cf20b370184ad0decd913f665e0 Mon Sep 17 00:00:00 2001 From: Florin Iucha Date: Thu, 12 Sep 2019 12:09:31 -0400 Subject: [PATCH] Implement a better heuristic for Git worktrees The existing heuristic for handling worktrees assumes that one other worktree contains a ".git" subdirectory with the Git database. This heuristic fails when worktrees are attached to a bare repository. --- git-pre-commit-format | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/git-pre-commit-format b/git-pre-commit-format index 75a93e1..a493d6c 100755 --- a/git-pre-commit-format +++ b/git-pre-commit-format @@ -100,14 +100,23 @@ while true; do # something like PROJ/.git/modules/SUBMODULE and there would not be # a .git directory in PROJ/.git/modules/. top_dir="$maybe_top_dir" + elif [ -e "$git_common_dir/hooks" ]; then + top_dir=$(realpath "$git_common_dir") fi fi + if [ -d "$top_dir/hooks" ]; then + # We are done! top_dir is the root git directory. + hook_path="$top_dir/hooks/pre-commit" + break + fi + [ -e "$top_dir/.git" ] || \ error_exit "No .git directory in $top_dir." if [ -d "$top_dir/.git" ]; then # We are done! top_dir is the root git directory. + hook_path="$top_dir/.git/hooks/pre-commit" break elif [ -f "$top_dir/.git" ]; then # We are in a submodule. @@ -117,7 +126,6 @@ done readonly top_dir -hook_path="$top_dir/.git/hooks/pre-commit" readonly hook_path me=$(realpath "$bash_source") || exit 1