Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created September 10, 2011 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRayCode/1208792 to your computer and use it in GitHub Desktop.
Save AndrewRayCode/1208792 to your computer and use it in GitHub Desktop.
Git is_submodule function
function is_submodule() {
# Find out if the parent directory of our current git repo is also a git repo
parent_git=`cd "$_git_dir/.." && git rev-parse --show-toplevel 2> /dev/null`
if [[ -n $parent_git ]]; then
# If it is, list all submodules of the parent
submodules=`cd $parent_git && git submodule --quiet foreach 'echo $path'`
for line in $submodules; do
# cd to each directory, if it matches our current git repo then we are in a submodule
cd "$parent_git/$line"
if [[ `pwd` = $_git_dir ]]; then return 0; fi
done
fi
return 1
}
# Usage
if is_submodule; then
echo "In a submodule!"
else
echo "Not in a submodule"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment