rust-lang / git2-rs

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

Pass byte slice as `root` to tree walk callback #1034

Open bsidhom opened 4 months ago

bsidhom commented 4 months ago

Fixes https://github.com/rust-lang/git2-rs/issues/1033.

Here's an example run of the example from that issue, but with the fix:

> cargo run --release
Creating repo at "/var/folders/0h/1lcyrkv11ynfjq5w7bt0sl_00000gn/T/git-repo-_YIz7w.git"
Populating repo
Repo populated

Walking via git2:
9376e30e1b6e8f7aef8aa51d19dfc0726f1835a0: Commit to hold reference
  a.txt:
  > Toplevel file. This is walkable because it's traversed _before_ the broken subtree.
  b�.txt:
  > Toplevel file. This is walkable as above, but it also has a filename which is not valid utf-8.
  c�: <tree>
  c�/nested.txt:
  > Not walkable due to the parent directory name being non-utf-8 bytes. This will abort the walk.
  d.txt:
  > Not walkable due to the broken tree above ending the walk.

Listing committed files using git command:
100644 blob 596c677536cbdd42ed208abb4135afcb95bd967c    a.txt
100644 blob 330e18bbaa623998f17ce703cdcf7158915c6bc5    "b\300.txt"
100644 blob 5fd5bb9cddb4076e8786e90670ba46e621174482    "c\300/nested.txt"
100644 blob 07bd6651bc96c9554dab80f1f15b867d4091116a    d.txt

As noted in that issue, I'm not sure if a breaking change is acceptable in this case (version is below 1 and it seems to be trying to match libgit2 as closely as possible, so I assume this is fine).

bsidhom commented 4 months ago

Since this was a breaking change, I had to add let filename = String::from_utf8_lossy(entry.name_bytes()); to the callback to get the example working.

ehuss commented 1 month ago

Thanks! Can the callback take a CStr instead? AFAIK, the value isn't an arbitrary slice of bytes since it cannot contain a null.