Skip to content

Instantly share code, notes, and snippets.

@ncanceill
Last active August 29, 2015 14:18
Show Gist options
  • Save ncanceill/e941de4b6a59646df457 to your computer and use it in GitHub Desktop.
Save ncanceill/e941de4b6a59646df457 to your computer and use it in GitHub Desktop.
Test for git version detection
#!/usr/bin/env zsh
#compare the provided version of git to the version installed and on path
#prints 1 if input version <= installed version
#prints -1 otherwise
function git_compare_version() {
local INPUT_GIT_VERSION=$1;
local INSTALLED_GIT_VERSION
INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
INSTALLED_GIT_VERSION=($(command git --version 2>/dev/null));
INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
for i in {1..3}; do
if [[ $INSTALLED_GIT_VERSION[$i] -gt $INPUT_GIT_VERSION[$i] ]]; then
echo 1
return 0
fi
if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
echo -1
return 0
fi
done
echo 0
}
POST_1_7_2_GIT=$(git_compare_version "1.7.2")
for cmd in $(which -a git);do command $cmd --version 2>/dev/null;done
echo $POST_1_7_2_GIT
if [[ $POST_1_7_2_GIT -gt 0 ]]; then
echo cando
else
echo nocando
fi
#clean up the namespace slightly by removing the checker function
unset -f git_compare_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment