我正在使用Rust git2 crate 克隆Git存储库,就像这样
use git2::Repository;
fn main() {
let repo = Repository::clone(
"https://github.com/rossmacarthur/dotfiles",
"dotfiles"
).expect("failed to clone repository");
repo.checkout("mybranch"); // need something like this.
}
我希望能够签出分支、提交或标记.
我看过以下文档,但仍然不确定使用哪种方法
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_head
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_tree
- https://docs.rs/git2/0.8.0/git2/struct.Repository.html#method.checkout_index
我可以做以下操作,但它只会更改文件
let object = repo
.revparse_single("mybranch")
.expect("failed to find identifier");
repo.checkout_tree(&object, None)
.expect(&format!("failed to checkout '{:?}'", object));
如果我重置,它会改变头部,但不会改变当前分支
repo.reset(&object, git2::ResetType::Soft, None)
.expect(&format!("failed to checkout '{:?}'", object));