如何在 Cargo.toml 中的依赖项中指定某个提交?

How to specify a certain commit in dependencies in Cargo.toml?

我正在尝试使用 GitHub 中的外部依赖项配置我的 Rust 项目。不幸的是,最近的一些提交对界面进行了一些更改,因此我无法使用最新版本。开发人员也不关心不同版本的标签和单独的分支,所以我认为唯一正确的方法是以某种方式指定某个提交,其中接口适合我使用的内容。

我现在 Cargo.toml 中的内容是:

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

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

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

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

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

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

this section of the Cargo book中有简要提及。

正如 Cargo 指南的 Cargo.toml vs 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" }

Specifying dependencies中也提到了,虽然没有给出例子(强调我的):

Since we haven’t specified any other information, Cargo assumes that we intend to use the latest commit on the master branch to build our package. You can combine the git key with the rev, tag, or branch keys to specify something else. [...]