rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.67k stars 384 forks source link

Add implementation and test for discover_path #883

Closed mikayla-maki closed 1 year ago

mikayla-maki commented 1 year ago

Hi y'all,

We're looking to use this library to build the git integration in our code editor (zed.dev). In order to do so, we wanted to cache the Repository::open_ext() call based on the repository path. But git2-rs's implementation of Repository::discover() automatically opens the repository for us, making it impossible to cache :(

I've added a Repository::discover_path() function that more closely matches the libgit2 git_repository_discover() call. I had to add an extra allocation to convert the git buffer into a PathBuf and would appreciate feedback on if there's a way to avoid the allocation.

I also added 2 tests, one for a successful discovery and one for an unsuccessful one that uses ceiling_dirs.