rust-lang / git2-rs

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

Is there any case that diff_index_to_workdir return empty diff but in fact the diff exists? #837

Closed swordow closed 2 years ago

swordow commented 2 years ago

I have a problem that using "git diff" in repo outputs the patch, but using git2rs's diff_index_to_workdir api does not ouput anything. Any one had this problem before? The following is the test code:

    let mut repo= git2::Repository::open(repoPath).unwrap(); // the repoPath assumed be right and unwrap always works fine
    let mut index = repo.index().unwrap();
    index.read(true); // force read, not necessray
    let mut diffOpts = DiffOptions::default();
    let diff = repo.diff_index_to_workdir(Some(&index), Some(&mut diffOpts)).unwarp();  // git diff  and assumed it has no error
    // check if contains any delfa
    if (diff.deltas().len() > 0)
    {
       //the problem is here, code  nerver runs here 
    }
    else
    {
       // always goes here and  mean that diff contains no delta
    }
swordow commented 2 years ago

Sorry for the logic bug... :-(

ghost commented 2 years ago

What was your logic bug? I'm trying to implement something similar; your code is helpful.

swordow commented 2 years ago

What was your logic bug? I'm trying to implement something similar; your code is helpful.

The code works fine. The previous version which was wrong is that i counted the delta num incorrectly. PS: If the modification was staged, the deltas().len() returns 0 for the api compareding the index and workdir.