如何使用 Composer 进行浅克隆?

How to perform shallow clone using Composer?

我得到了以下 composer.json:

{
    "require": {
        "php": ">=5.2.0",
        "queueit/KnownUser.V3.PHP": "dev-master"
    },
    "config": {
        "autoloader-suffix": "ComposerManager",
        "vendor-dir": "../../../all/libraries/composer"
    },
    "prefer-stable": true,
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "queueit/KnownUser.V3.PHP",
                "version": "dev-master",
                "source": {
                    "type": "git",
                    "url": "https://github.com/queueit/KnownUser.V3.PHP.git",
                    "reference": "master"
                }
            }
        }
    ]
}

然而当我运行:

$ composer -vvv update
...
Cloning master
Executing command (CWD): git clone --no-checkout 'https://github.com/queueit/KnownUser.V3.PHP.git' '.../sites/all/libraries/composer/queueit/KnownUser.V3.PHP' && cd '.../sites/all/libraries/composer/queueit/KnownUser.V3.PHP' && git remote add composer 'https://github.com/queueit/KnownUser.V3.PHP.git' && git fetch composer

克隆过程需要很长时间,存储库的大小增长超过 25MB:

$ du -hs ~/.composer/cache/vcs/https---github.com-queueit-KnownUser.V3.PHP.git/
25M ~/.composer/cache/vcs/https---github.com-queueit-KnownUser.V3.PHP.git/

然后 Composer 因超时而停止:

[Symfony\Component\Process\Exception\ProcessTimedOutException]
The process "git clone --no-checkout 'https://github.com/queueit/KnownUser.V3.PHP.git' '.../sites/all/libraries/composer/queueit/KnownUser.V3.PHP' && cd '.../sites/all/libraries/composer/queueit/KnownUser.V3.PHP' && git remote add composer 'https://github.com/queueit/KnownUser.V3.PHP.git' && git fetch composer" exceeded the timeout of 300 seconds.

我认为存储库太大,无法克隆所有 git 对象。


如何使用 a shallow clone 更快地克隆存储库?

例如,通过将额外的 --depth 1--single-branch git 参数传递给 Git 命令,以便它可以被 Composer 自动拾取?

我希望更改在 composer.json 文件中是独立的,因此当 运行ning composer install.

官方不支持使用 Composer 的浅克隆(没有任何补丁),因为 git 参数是硬编码的。

已经有一个添加此功能的请求:Add support for git shallow clones. However implementing this feature can cause some issues (such as not reaching the locked commits if the depth is not so high@stof 和其他)。

此外还有一个pull request which attempts to implement shallow clones by adding an extra --git-clone-depth parameter (tests shows some good results). However the change has been abandon due to faster git clones using cache.


为了快速破解,可以在 src/Composer/Downloader/GitDownloader.php 中编辑 doDownload() 中的 git 参数,例如通过更改此行中的 --depth 1 --single-branch

$command = 'git clone --no-checkout ...'

或者找到将深度 1 设置应用到 git 配置的方法。


对于更大的存储库,最简单的解决方法(没有任何技巧)就是通过指定如下变量来增加超时:

COMPOSER_PROCESS_TIMEOUT=0 composer install