如何测试Git仓库是否浅? [英] 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.
到目前为止,我尝试的内容很少,基本上是在克隆上创建错误消息.目前,我只是提取到unshallow的文件,如果失败,那么我将进行普通的提取,因为如果存储库很浅,那么之后它就应该是浅表的:
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
但是,并不能保证以后将其变浅(从远程获取也可以变浅),因此对git存储库的(非)浅度进行测试会更好.
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为2.15或更高版本,请运行:
If your Git is 2.15 or later, run:
git rev-parse --is-shallow-repository
将会打印false
(不浅)或true
(浅):
which will print false
(not shallow) or true
(shallow):
if $(git rev-parse --is-shallow-repository); then
... repository is shallow ...
fi
下面的答案可以追溯到2.15之前的Git版本.
The answer below dates back to Git versions before 2.15.
如果您的Git早于2.15,则 1 只需在Git存储库目录中测试文件shallow
:
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
或(简称):
[ -f $(git rev-parse --git-dir)/shallow ] && echo true || echo false
您可以将其转换为shell函数:
You can turn this into a shell function:
test_shallow() {
[ -f $(git rev-parse --git-dir)/shallow ] && echo true || echo false
}
甚至自动执行Git版本检查:
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
}
1 git --version
将打印当前版本号:
1git --version
will print the current version number:
$ git --version
2.14.1
$ git --version
git version 2.7.4
等(目前,我在不同的VM/机器上有多个版本.)您还可以运行:
etc. (I have multiple versions on different VMs/machines at this point.) You can also run:
git rev-parse --is-shallow-repository
如果仅打印--is-shallow-repository
,则您的Git为2.15之前的版本,并且缺少该选项.
If it just prints --is-shallow-repository
, your Git is pre-2.15 and lacks the option.
这篇关于如何测试Git仓库是否浅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!