这应该有效:
repo = Rugged::Repository.new("/path/to/repo")
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE)
walker.push(repo.head.target)
commit = walker.find do |commit|
commit.parents.size == 1 && commit.diff(paths: ["relative/path/to/file"]).size > 0
end
sha = commit.oid
取自并改编自https://github.com/libgit2/pygit2/issues/200#issuecomment-15899713
顺便说一句:仅仅因为 Rough 是用 C 编写的,并不意味着昂贵的操作突然变得便宜和快捷。显然,您节省了大量的字符串解析和类似的东西,但这并不总是瓶颈。
由于您对此处的实际文本差异不感兴趣,因此 libgit2GIT_DIFF_FORCE_BINARY
可能也有助于提高此查找的性能 - 不幸的是,这在 Rugged 中尚不可用(但很快就会提供)。
使用 Rugged 存储库本身对其进行测试,它可以正常工作:
repo = Rugged::Repository.new(".")
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE)
walker.push(repo.head.target)
commit = walker.find do |commit|
commit.parents.size == 1 && commit.diff(paths: ["Gemfile"]).size > 0
end
sha = commit.oid # => "8f5c763377f5bf0fb88d196b7c45a7d715264ad4"
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE)
walker.push(repo.head.target)
commit = walker.find do |commit|
commit.parents.size == 1 && commit.diff(paths: [".travis.yml"]).size > 0
end
sha = commit.oid # => "4e18e05944daa2ba8d63a2c6b149900e3b93a88f"