rust-lang / git2-rs

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

Is there a way of getting the latest commit for a file on a specific branch. #943

Open busyLambda opened 1 year ago

busyLambda commented 1 year ago

588 is a simmilar issue to this one but it just requests a way to get the latest commit but i want to get the latest commit for a specific branch. I already know that revwalk is the way to go but i don't really have other ideas.

Some1and2-XC commented 6 days ago

This seems like more of an implementation issue than a library issue.

This being said here is how I would approach this.

  1. Get the newest commit.
  2. Look for the file through the trees and find the OID (the sha1 hash of the file)
  3. Go to the previous commit
  4. Find the file again
  5. See if the OID changed
  6. Repeat 3,4 and 5 until the file doesn't exist or the OID is different.
  7. Profit?

Because trees also have OIDs you can also do some caching to get some okay performance but this algorithm isn't very intensive. You can also do things like making sure branches work correctly as well as looking in other directories for your file but this depends on your needs.

Hope this helped.