如何测试 composer.lock 是否是最新的?

How to test if composer.lock is up to date?

在开发过程中(团队中多人)有时 composer install returns:

警告:锁定文件不是最新的 composer.json 中的最新更改。您可能会得到过时的依赖项。 运行更新更新他们。

有没有一种方法可以非常快速地检查这个(以毫秒为单位,不做任何更改)?

我了解作曲家的工作原理。然而,当代码合并时,它并不一定会导致 composer.jsoncomposer.lock 文件上的合并冲突,并且几乎从来没有任何时候 运行 composer install 并不有趣更改,该命令需要几分钟时间。

如果我能够快速测试锁定失败是否不同步,我可以将其构建到 bash 环境中以通知每个命令。类似于人们喜欢将 git status 内置到 bash 提示中的方式。

此外,这在 CI 中更有意义,以确保它确实潜入稳定分支。

你可以运行

composer install --dry-run

--dry-run Outputs the operations but will not execute anything (implicitly enables --verbose).

这不会改变任何内容,但如果不是最新的则会显示警告。但是它仍然需要检查服务器是否有新版本的安装包,所以如果你安装了很多,这可能仍然需要几毫秒。反正它更快。

For composer < 1.3.0

是的,有一种方法可以非常快速地检查这一点。

"out-of-date" 检查基于存储在 composer.lock 中的 composer.json 内容的散列。没有加盐,它是内容的直接散列,所以非常非常容易做到。

<?php

$lock = json_decode(file_get_contents('composer.lock'))->hash;
$json = md5(file_get_contents('composer.json'));

if ($lock !== $json) {
    echo "Lock file out of date\n";
    exit(1);
}

echo "Lock file up to date\n";
exit(0);

For composer < 1.3.0

从@Domster 扩展,纯 bash 中的解决方案:

COMPOSER_IN_SYNC=$(expr "`cat composer.lock | grep '"hash":' | cut -d'"' -f4`" = "`md5sum composer.json | cut -d ' ' -f 1`")

$COMPOSER_IN_SYNC 将分别为 01

在较新的版本(我想是 1.3+)上,您可以 运行 以下内容:

$ composer validate --no-check-all --no-check-publish

可能会输出如下内容(带有可捕获的错误退出代码):

./composer.json is valid for simple usage with composer but has
strict errors that make it unable to be published as a package:
See https://getcomposer.org/doc/04-schema.md for details on the 
schema
The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update`.