如何在 Cargo.toml 中指定依赖关系中的特定提交?

51

我试图在 Rust 项目中配置一个来自 GitHub 的外部依赖。不幸的是,一些最近的提交对接口进行了更改,因此我无法使用最新版本。开发人员也没有关注标签和为不同版本单独创建分支,所以我认为唯一正确的方法是指定某个提交,其中接口与我使用的版本相匹配。

现在在 Cargo.toml 中的内容如下:

[dependencies]
...
thelib = { git = 'https://github.com/someguys/thelib' }

我看到可以这样指定一个分支:

thelib = { git = 'https://github.com/someguys/thelib', branch = 'branch1' }

但是我还没有看到一个带有提交的可行示例。是否有人可以在这里提供一个?

2个回答

65
如 Cargo 指南中 Cargo.toml 与 Cargo.lock 的区别 部分所示,您可以使用 rev 属性来指定提交哈希值:

[...] If you build this package today, and then you send a copy to me, and I build this package tomorrow, something bad could happen. There could be more commits to rand in the meantime, and my build would include new commits while yours would not. Therefore, we would get different builds. This would be bad because we want reproducible builds.

We could fix this problem by putting a rev line in our Cargo.toml:

[dependencies]
rand = { git = "https://github.com/rust-lang-nursery/rand.git", rev = "9f35b8e" }

指定依赖项中也提到了这一点,虽然没有给出示例(我强调):

由于我们没有指定其他信息,Cargo假设我们打算使用master分支上的最新提交来构建我们的包。您可以将git键与revtagbranch键结合使用来指定其他内容。[...]


4
请注意,如果一个 crate 依赖于 Git,那么它将无法被发布。 - Squirrel

17

您可以使用rev键来指定提交哈希值。例如:

thelib = { git = "https://github.com/someguys/thelib", rev = "9f35b8e" }

这在Cargo书的这一部分中简要提到。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接