JGit detect rename in working copy


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:

  • Git-flow and client-specific functionality
  • How do I remove an invalid remote I added in Git?
  • Git diffing crashes on Windows when submodules exist
  • Git - fatal: bad default revision 'HEAD' & notice: HEAD points to unborn branch (master)
  • Using su/sudo when accessing remote Git repositories over SSH
  • How to list files in specific revision in `git`?
  • # 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.

  • Use Git's patience diff algorithm for interactive add
  • Upstream gone message on switching back to an empty master branch?
  • How to push from one bare git repo to another?
  • git p4 clone has no files
  • Git Shell in Windows: patch's default character encoding is UCS-2 Little Endian - how to change this to ANSI or UTF-8 without BOM?
  • How can I make git show a list of the files that are being tracked?
  • 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.addTree(new FileTreeIterator(repository));
    RenameDetector rd = new RenameDetector(repository);
    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.