]> source.dussan.org Git - jgit.git/commitdiff
Report diff entries for files that only change mode 26/4826/4
authorKevin Sawicki <kevin@github.com>
Thu, 15 Dec 2011 01:51:42 +0000 (17:51 -0800)
committerKevin Sawicki <kevin@github.com>
Thu, 15 Dec 2011 01:51:42 +0000 (17:51 -0800)
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.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffEntry.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java

index 21069efa56839f25284526022e2d448dfe5532e6..cb7cad8340c6a1bb406769f0014eeddb71d12d63 100644 (file)
@@ -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<DiffEntry> 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());
+       }
 }
index 5bccccfc1b9f5c9c83af9126b1b22e1392ee5511..424f9ffc4027e49bd8a2bb4415b33ba5fa034b51 100644 (file)
@@ -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())
index 0dbd0ba1d7df82f263880b8e346536a4d0272164..e2757e68a8af52cf1fb7d40a50a348cb495bb8c2 100644 (file)
@@ -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;