瀏覽代碼

Fix DiffFormatter NPEs for DiffEntry without content change

DiffEntry.getOldId() returns null for a diff without an index line (e.g.
only mode changed, rename without content change).

Bug: 407743
Change-Id: I42eac87421f2a53c985af260a253338f578492bc
tags/v3.0.0.201305281830-rc2
Robin Stocker 11 年之前
父節點
當前提交
92189b2df4

+ 30
- 1
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java 查看文件

/* /*
* Copyright (C) 2010, Google Inc.
* Copyright (C) 2010, 2013 Google Inc.
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
assertEquals(0, hh.toEditList().size()); assertEquals(0, hh.toEditList().size());
} }


@Test
public void testCreateFileHeaderWithoutIndexLine() throws Exception {
DiffEntry m = DiffEntry.modify(PATH_A);
m.oldMode = FileMode.REGULAR_FILE;
m.newMode = FileMode.EXECUTABLE_FILE;

FileHeader fh = df.toFileHeader(m);
String expected = DIFF + "a/src/a b/src/a\n" + //
"old mode 100644\n" + //
"new mode 100755\n";
assertEquals(expected, fh.getScriptText());
}

@Test
public void testCreateFileHeaderForRenameWithoutContentChange() throws Exception {
DiffEntry a = DiffEntry.delete(PATH_A, ObjectId.zeroId());
DiffEntry b = DiffEntry.add(PATH_B, ObjectId.zeroId());
DiffEntry m = DiffEntry.pair(ChangeType.RENAME, a, b, 100);
m.oldId = null;
m.newId = null;

FileHeader fh = df.toFileHeader(m);
String expected = DIFF + "a/src/a b/src/b\n" + //
"similarity index 100%\n" + //
"rename from src/a\n" + //
"rename to src/b\n";
assertEquals(expected, fh.getScriptText());
}

@Test @Test
public void testDiff() throws Exception { public void testDiff() throws Exception {
write(new File(db.getDirectory().getParent(), "test.txt"), "test"); write(new File(db.getDirectory().getParent(), "test.txt"), "test");

+ 6
- 1
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java 查看文件

editList = new EditList(); editList = new EditList();
type = PatchType.UNIFIED; type = PatchType.UNIFIED;


} else if (ent.getOldId() == null || ent.getNewId() == null) {
// Content not changed (e.g. only mode, pure rename)
editList = new EditList();
type = PatchType.UNIFIED;

} else { } else {
assertHaveRepository(); assertHaveRepository();


o.write('\n'); o.write('\n');
} }


if (!ent.getOldId().equals(ent.getNewId())) {
if (ent.getOldId() != null && !ent.getOldId().equals(ent.getNewId())) {
formatIndexLine(o, ent); formatIndexLine(o, ent);
} }
} }

Loading…
取消
儲存