How to Test if Git Repository is Shallow?

后端 未结 1 1667
悲哀的现实
悲哀的现实 2021-02-12 14:12

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 a

1条回答
  •  滥情空心
    2021-02-12 14:42

    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.

    0 讨论(0)
提交回复
热议问题