From 6cf008cdb981a284e1ecf74a19a077258896c4d2 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 23 Apr 2015 10:51:17 -0700 Subject: [PATCH] shlib.sh: add is_git_dir utility function Signed-off-by: Kyle J. McKay --- shlib.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/shlib.sh b/shlib.sh index 9f22fa4..560e1e8 100644 --- a/shlib.sh +++ b/shlib.sh @@ -300,6 +300,27 @@ check_interval() { [ $ns -lt $(($os+$2)) ] } +# Check to see if the single argument is a Git directory +is_git_dir() { + # Just like Git's test except we ignore GIT_OBJECT_DIRECTORY + # And we are slightly more picky (must be refs/.+ not refs/.*) + [ -d "$1/objects" -a -x "$1/objects" ] || return 1 + [ -d "$1/refs" -a -x "$1/refs" ] || return 1 + if [ -L "$1/HEAD" ]; then + _hr="$(readlink "$1/HEAD")" + case "$_hr" in "refs/"?*) :;; *) return 1;; esac + fi + [ -f "$1/HEAD" -a -r "$1/HEAD" ] || return 1 + read -r _hr <"$1/HEAD" || return 1 + case "$_hr" in + $octet20) + return 0;; + ref:*) + _hr="${_hr##ref:*[[:space:]]}" + case "$_hr" in "refs/"?*) return 0;; esac + esac + return 1 +} # List all Git repositories, with given prefix if specified, one-per-line # All project names starting with _ are always excluded from the result -- 2.11.4.GIT