When I make a local clone from a repository, the clone fails if the origin repository is shallow.
git clone -l -- . target-dir
As that is not always the case I'd like to find out prior clone but don't know how to do that.
What I tried so far is very little, basically creating error messages on clone. At the moment I just fetch to unshallow and if that fails, I do a plain fetch because if the repo would be shallow, it should be unshallow afterwards:
if ! git fetch --unshallow; then
git fetch
fi
However there is no guarantee for being unshallow afterwards (remote to fetch from can be shallow, too), so a test for the (un)shallowness of a git repository would be much better.
If your Git is 2.15 or later, run:
git rev-parse --is-shallow-repository
which will print false
(not shallow) or true
(shallow):
if $(git rev-parse --is-shallow-repository); then
... repository is shallow ...
fi
The answer below dates back to Git versions before 2.15.
If your Git is older than 2.15,1 just test for the file shallow
in the Git repository directory:
if [ -f $(git rev-parse --git-dir)/shallow ]; then
echo this is a shallow repository;
else
echo not a shallow repository;
fi
or (shorter):
[ -f $(git rev-parse --git-dir)/shallow ] && echo true || echo false
You can turn this into a shell function:
test_shallow() {
[ -f $(git rev-parse --git-dir)/shallow ] && echo true || echo false
}
and even automate the Git version checking:
test_shallow() {
set -- $(git rev-parse --is-shallow-repository)
if [ x$1 == x--is-shallow-repository ]; then
[ -f $(git rev-parse --git-dir)/shallow ] && set true || set false
fi
echo $1
}
1git --version
will print the current version number:
$ git --version
2.14.1
$ git --version
git version 2.7.4
etc. (I have multiple versions on different VMs/machines at this point.) You can also run:
git rev-parse --is-shallow-repository
If it just prints --is-shallow-repository
, your Git is pre-2.15 and lacks the option.