From 1dbb300ded46de220fe36e25c00cca2b5b613638 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 27 Feb 2018 18:21:32 -0800 Subject: [PATCH] hooks: truly find ourselves With hooks possibly running as a result of non-Girocco activity (e.g. a linked working tree), the possible combinations of GIT_DIR settings, ".git" being a directory, symlink or "gitdir:" file and a veritable plethora of possible current working directories create a truly perverse and potentially hostile hook script environment. The hook scripts attempt to navigate this maze of possibilities without requiring any particular Git version while still operating correctly when executed from within a chroot jail and yet always ending up with the current directory and GIT_DIR pointing at the "top" of the "bare" ".git" Girocco repository. Enhance the logic substantially (at almost no additional cost) to handle all (hopefully) of the bizarre situations a hook script could find itself being executed in. The previous version worked just fine for ordinary Girocco installations that do not have any linked working trees or non-bare repositories. This version tolerates much greater perversity while still always coming out on top. Signed-off-by: Kyle J. McKay --- hooks/post-commit | 8 +++++--- hooks/post-receive | 8 +++++--- hooks/pre-auto-gc | 8 +++++--- hooks/pre-receive | 8 +++++--- hooks/update | 8 +++++--- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/hooks/post-commit b/hooks/post-commit index d9cfac2..dcdb798 100755 --- a/hooks/post-commit +++ b/hooks/post-commit @@ -15,7 +15,9 @@ set -e # Make sure the current directory is where we expect to be -[ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1 +[ "${GIT_DIR+set}" != "set" ] || { [ -n "$GIT_DIR" ] && [ -d "$GIT_DIR" ]; } || unset GIT_DIR +[ -n "$GIT_DIR" ] || GIT_DIR="$(git rev-parse --git-dir)" +[ -n "$GIT_DIR" ] && cd -P "${GIT_DIR:-.}" || exit 1 case "${PWD%/*}" in */worktrees) # Gah! if @@ -25,10 +27,10 @@ case "${PWD%/*}" in */worktrees) [ -n "$_cmndir" ] && [ -d "$_cmndir" ] then # ...it is not, fix it! - cd "$_cmndir" || exit 1 + cd -P "$_cmndir" || exit 1 fi esac -[ "${GIT_DIR+set}" != "set" ] || GIT_DIR="." +GIT_DIR="." GIT_PREFIX= && export GIT_DIR # Get out of the mob case "$PWD" in *?/mob) diff --git a/hooks/post-receive b/hooks/post-receive index 59f397c..2f684ed 100755 --- a/hooks/post-receive +++ b/hooks/post-receive @@ -9,7 +9,9 @@ set -e # Make sure the current directory is where we expect to be -[ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1 +[ "${GIT_DIR+set}" != "set" ] || { [ -n "$GIT_DIR" ] && [ -d "$GIT_DIR" ]; } || unset GIT_DIR +[ -n "$GIT_DIR" ] || GIT_DIR="$(git rev-parse --git-dir)" +[ -n "$GIT_DIR" ] && cd -P "${GIT_DIR:-.}" || exit 1 case "${PWD%/*}" in */worktrees) # Gah! if @@ -19,10 +21,10 @@ case "${PWD%/*}" in */worktrees) [ -n "$_cmndir" ] && [ -d "$_cmndir" ] then # ...it is not, fix it! - cd "$_cmndir" || exit 1 + cd -P "$_cmndir" || exit 1 fi esac -[ "${GIT_DIR+set}" != "set" ] || GIT_DIR="." +GIT_DIR="." GIT_PREFIX= && export GIT_DIR # Get out of the mob case "$PWD" in *?/mob) diff --git a/hooks/pre-auto-gc b/hooks/pre-auto-gc index 5d3c66e..e224a6f 100755 --- a/hooks/pre-auto-gc +++ b/hooks/pre-auto-gc @@ -16,7 +16,9 @@ set -e # Make sure the current directory is where we expect to be -[ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1 +[ "${GIT_DIR+set}" != "set" ] || { [ -n "$GIT_DIR" ] && [ -d "$GIT_DIR" ]; } || unset GIT_DIR +[ -n "$GIT_DIR" ] || GIT_DIR="$(git rev-parse --git-dir)" +[ -n "$GIT_DIR" ] && cd -P "${GIT_DIR:-.}" || exit 1 case "${PWD%/*}" in */worktrees) # Gah! if @@ -26,10 +28,10 @@ case "${PWD%/*}" in */worktrees) [ -n "$_cmndir" ] && [ -d "$_cmndir" ] then # ...it is not, fix it! - cd "$_cmndir" || exit 1 + cd -P "$_cmndir" || exit 1 fi esac -[ "${GIT_DIR+set}" != "set" ] || GIT_DIR="." +GIT_DIR="." GIT_PREFIX= && export GIT_DIR # Get out of the mob case "$PWD" in *?/mob) diff --git a/hooks/pre-receive b/hooks/pre-receive index 2d744f2..7da9105 100755 --- a/hooks/pre-receive +++ b/hooks/pre-receive @@ -7,7 +7,9 @@ set -e # Make sure the current directory is where we expect to be -[ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1 +[ "${GIT_DIR+set}" != "set" ] || { [ -n "$GIT_DIR" ] && [ -d "$GIT_DIR" ]; } || unset GIT_DIR +[ -n "$GIT_DIR" ] || GIT_DIR="$(git rev-parse --git-dir)" +[ -n "$GIT_DIR" ] && cd -P "${GIT_DIR:-.}" || exit 1 case "${PWD%/*}" in */worktrees) # Gah! if @@ -17,10 +19,10 @@ case "${PWD%/*}" in */worktrees) [ -n "$_cmndir" ] && [ -d "$_cmndir" ] then # ...it is not, fix it! - cd "$_cmndir" || exit 1 + cd -P "$_cmndir" || exit 1 fi esac -[ "${GIT_DIR+set}" != "set" ] || GIT_DIR="." +GIT_DIR="." GIT_PREFIX= && export GIT_DIR # Get out of the mob case "$PWD" in *?/mob) diff --git a/hooks/update b/hooks/update index ad6cbb1..f966f42 100755 --- a/hooks/update +++ b/hooks/update @@ -7,7 +7,9 @@ set -e # Make sure the current directory is where we expect to be -[ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1 +[ "${GIT_DIR+set}" != "set" ] || { [ -n "$GIT_DIR" ] && [ -d "$GIT_DIR" ]; } || unset GIT_DIR +[ -n "$GIT_DIR" ] || GIT_DIR="$(git rev-parse --git-dir)" +[ -n "$GIT_DIR" ] && cd -P "${GIT_DIR:-.}" || exit 1 case "${PWD%/*}" in */worktrees) # Gah! if @@ -17,10 +19,10 @@ case "${PWD%/*}" in */worktrees) [ -n "$_cmndir" ] && [ -d "$_cmndir" ] then # ...it is not, fix it! - cd "$_cmndir" || exit 1 + cd -P "$_cmndir" || exit 1 fi esac -[ "${GIT_DIR+set}" != "set" ] || GIT_DIR="." +GIT_DIR="." GIT_PREFIX= && export GIT_DIR if ! [ -x @perlbin@ ]; then # We are INSIDE the chroot -- 2.11.4.GIT