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:

  • Why doesn't Git recognize my .gitignore exclusions?
  • git report files changed on which I did not work
  • Ruby on rails and Heroku problem
  • How to undo “git commit --amend” done instead of “git commit”
  • Cygwin and TortoiseGit
  • Is git commit -am redundant, if I do git add before?
  • # 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.

  • Git diff, which developers contributed most
  • Unresolvable git merge conflict
  • How to upload images to GitHub without gimmicks (on a Mac)?
  • Tomcat application cannot be started
  • Installed Gitolite using root (dumb), locked myself out of ssh
  • Updating git bare repo from origin (without losing changes)
  • 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.