JGit detect rename in working copy

Contex

I’m trying to detect possible file rename that occurred after last commit, in a working copy.
On my example, I have a clean working copy and I do that:

git mv old.txt new.txt

Running $ git status shows the expected result:

  • How do I migrate a flat svn repo to git repo
  • Gitlab Configuration Issues:: NGINX Unicorn Port Conflict
  • git merge: Removing files I want to keep!
  • Git rebase conflict with nothing to merge?
  • LibGit2Sharp log remote
  • git log with additional details from body of the commit message
  • # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       renamed:    old.txt -> new.txt
    

    I tried

    Using a StatusCommand, I can see old.txt in the removed list, and new.txt in the added list.
    But I can’t find a way to link them together.

    I’m aware of the existence of RenameDetector, but it works using DiffEntry, and I don’t know how to get DiffEntries between HEAD and the Working Copy.

  • how to reset git repository once and for all into a clean state
  • git commit error saving vimfiles
  • Change commit message for specific commit
  • devtools::install_git Over SSH
  • Tracking a bzr repository as a Git submodule
  • deploying a website/webapp via git/gitolite permissions error
  • One Solution collect form web for “JGit detect rename in working copy”

    Never mind, found the answer.
    JGit’s API is very complicated..

    TreeWalk tw = new TreeWalk(repository);
    tw.setRecursive(true);
    tw.addTree(CommitUtils.getHead(repository).getTree());
    tw.addTree(new FileTreeIterator(repository));
    
    RenameDetector rd = new RenameDetector(repository);
    rd.addAll(DiffEntry.scan(tw));
    
    List<DiffEntry> lde = rd.compute(tw.getObjectReader(), null);
    for (DiffEntry de : lde) {
        if (de.getScore() >= rd.getRenameScore()) {
            System.out.println("file: " + de.getOldPath() + " copied/moved to: " + de.getNewPath());
        }
    }
    

    (This snippet also use Gitective library)

    Git Baby is a git and github fan, let's start git clone.