将一个短暂的git依赖项打上特定的修订版本。

3

我依赖于crate ab,其中我将b补丁为一个git依赖项,其引用的版本为foo

# Cargo.toml of my crate
[dependencies]
a = "1.0.0"
b = "1.0.0"

[patch.crates-io]
b = { git = "https://github.com/user/example", rev = "foo" }

a 也作为 git 依赖项依赖于 b,但没有特定的 ref

# Cargo.toml of a
[dependencies]
b = { git = "https://github.com/user/example" }

我想要强制使用b和我的代码相同的ref,我尝试了以下方法:

# The ideal Cargo.toml of my crate
[dependencies]
a = "1.0.0"
b = "1.0.0"

# patch local dependency
[patch.crates-io]
b = { git = "https://github.com/user/example", rev = "foo" }

# patch transient dependency
[patch.'https://github.com/user/example']
b = { git = "https://github.com/user/example", rev = "foo" }

但是这种方法行不通,因为我正在打补丁的源代码仍然指向相同的源代码,只是在不同的 rev 中:

error: failed to resolve patches for `https://github.com/user/example`

Caused by:
  patch for `b` in `https://github.com/user/example` points to the same source, but patches must point to different sources
[Finished running. Exit status: 101]

到目前为止,我的解决方法是 fork b 然后进行以下修补:

# Cargo.toml of my crate using a work around
[dependencies]
a = "1.0.0"
b = "1.0.0"

[patch.crates-io]
b = { git = "https://github.com/me/example", rev = "foo" } # Using my fork

[patch.'https://github.com/user/example']
b = { git = "https://github.com/me/example", rev = "foo" } # Using my fork

这个方法可以工作,但是fork实际上没有用处。有没有更好的方法?


我尝试了这种方法,但是它也不起作用,因为它忽略了rev。整个GitHub问题让人觉得目前并不支持我的尝试,但很难确定,因为它并不完全是同一个功能。

1个回答

3
根据此 GitHub 问题,目前不支持此功能,因为 git 依赖项仅通过 URL 而不是版本区分。有些 URL 被视为相同,例如 .git 结尾总是被剥离,但 cargo 仍然只比较 URL,而没有其他内容。
不过我们可以利用这个“特性”:在 URL 的末尾添加 ?ref=foo 将使其看起来像一个新的源:
# Fixed Cargo.toml
[dependencies]
a = "1.0.0"
b = "1.0.0"

# patch local dependency
[patch.crates-io]
b = { git = "https://github.com/user/example?rev=foo" }

# patch transient dependency
[patch.'https://github.com/user/example']
b = { git = "https://github.com/user/example?rev=foo" }

请注意在这两个补丁中都使用URL hack,否则将会创建一个损坏的Cargo.lock文件,其中包含两个关于b的定义:

# Corrupt Cargo.toml
[dependencies]
a = "1.0.0"
b = "1.0.0"

# patch local dependency
[patch.crates-io]
b = { git = "https://github.com/user/example", rev = "foo" } # mistake!

# patch transient dependency
[patch.'https://github.com/user/example']
b = { git = "https://github.com/user/example?rev=foo" }

虽然我没有测试过,但这种方法原则上也适用于分支上的补丁,只需在URL后面添加/tree/<branch>即可。

这个命令似乎会破坏 cargo fetch --locked && cargo build --frozen - lnl

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