'How to Test if Git Repository is Shallow?

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.

git


Solution 1:[1]

When you don't have access to the remote repository (not the case here, but still a possible scenario), a git clone is still a good way to test if that remote repository is shallow or not.

The problem is not transfer/create file needlessly when the clone would ultimately fail because of the shallow nature of said remote repository.

This is now fixed with Git 2.32 (Q2 2021): "git clone"(man) new --reject-shallow option fails the clone as soon as we notice that we are cloning from a shallow repository.

See commit 4fe788b (01 Apr 2021) by Li Linchao (Cactusinhand).
(Merged by Junio C Hamano -- gitster -- in commit 22eee7f, 08 Apr 2021)

builtin/clone.c: add --reject-shallow option

Signed-off-by: Li Linchao

In some scenarios, users may want more history than the repository offered for cloning, which happens to be a shallow repository, can give them.
But because users don't know it is a shallow repository until they download it to local, we may want to refuse to clone this kind of repository, without creating any unnecessary files.

The '--depth=x' option cannot be used as a solution; the source may be deep enough to give us 'x' commits when cloned, but the user may later need to deepen the history to arbitrary depth.

Teach '--reject-shallow' option to "git clone"(man) to abort as soon as we find out that we are cloning from a shallow repository.

No local file created!

The error message will be (with the test clone command):

git -c clone.rejectshallow=true clone --no-local shallow-repo
source repository is shallow, reject to clone.

git config now includes in its man page:

clone.rejectShallow

Reject to clone a repository if it is a shallow one, can be overridden by passing option --reject-shallow in command line.

git clone now includes in its man page:

--[no-]reject-shallow

Fail if the source repository is a shallow repository.
The 'clone.rejectShallow' configuration variable can be used to specify the default.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC