From 78bc526d9b7e5063d267a76b0bbefaa989139bf3 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 14 Dec 2011 17:51:42 -0800 Subject: [PATCH] Report diff entries for files that only change mode This also updates DiffFormatter to not write path lines for entries that have the same object id Bug: 361570 Change-Id: I830a78e2babf472503630a7aa020ebfd5c7e69c6 --- .../org/eclipse/jgit/diff/DiffEntryTest.java | 42 +++++++++++++++++++ .../src/org/eclipse/jgit/diff/DiffEntry.java | 3 ++ .../org/eclipse/jgit/diff/DiffFormatter.java | 3 ++ 3 files changed, 48 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java index 21069efa5..cb7cad834 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java @@ -46,13 +46,20 @@ import static org.eclipse.jgit.diff.DiffEntry.DEV_NULL; import static org.eclipse.jgit.util.FileUtils.delete; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.io.File; import java.util.List; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.diff.DiffEntry.ChangeType; +import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.dircache.DirCacheEditor; +import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; +import org.eclipse.jgit.dircache.DirCacheEntry; +import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.EmptyTreeIterator; @@ -295,4 +302,39 @@ public class DiffEntryTest extends RepositoryTestCase { DiffEntry.scan(walk, true); } + @Test + public void shouldReportFileModeChange() throws Exception { + writeTrashFile("a.txt", "content"); + Git git = new Git(db); + git.add().addFilepattern("a.txt").call(); + RevCommit c1 = git.commit().setMessage("initial commit").call(); + DirCache cache = db.lockDirCache(); + DirCacheEditor editor = cache.editor(); + final TreeWalk walk = new TreeWalk(db); + walk.addTree(c1.getTree()); + walk.setRecursive(true); + assertTrue(walk.next()); + + editor.add(new PathEdit("a.txt") { + + public void apply(DirCacheEntry ent) { + ent.setFileMode(FileMode.EXECUTABLE_FILE); + ent.setObjectId(walk.getObjectId(0)); + } + }); + assertTrue(editor.commit()); + RevCommit c2 = git.commit().setMessage("second commit").call(); + walk.reset(); + walk.addTree(c1.getTree()); + walk.addTree(c2.getTree()); + List diffs = DiffEntry.scan(walk, false); + assertEquals(1, diffs.size()); + DiffEntry diff = diffs.get(0); + assertEquals(ChangeType.MODIFY,diff.getChangeType()); + assertEquals(diff.getOldId(), diff.getNewId()); + assertEquals("a.txt", diff.getOldPath()); + assertEquals(diff.getOldPath(), diff.getNewPath()); + assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode()); + assertEquals(FileMode.REGULAR_FILE, diff.getOldMode()); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java index 5bccccfc1..424f9ffc4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java @@ -172,6 +172,9 @@ public class DiffEntry { r.add(entry); else r.addAll(breakModify(entry)); + } else if (entry.oldMode != entry.newMode) { + entry.changeType = ChangeType.MODIFY; + r.add(entry); } if (includeTrees && walk.isSubtree()) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java index 0dbd0ba1d..e2757e68a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java @@ -1088,6 +1088,9 @@ public class DiffFormatter { private void formatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent) throws IOException { + if (ent.oldId.equals(ent.newId)) + return; + final String oldp; final String newp;