diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java index e4e5d28f7..1203e83dc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java @@ -58,10 +58,12 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -959,6 +961,92 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase { "HEAD").getReverseEntries().get(0).getComment()); } + @Test + public void testCreateMissingObject() throws IOException { + String name = "refs/heads/abc"; + ObjectId bad = + ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + RefUpdate ru = db.updateRef(name); + ru.setNewObjectId(bad); + Result update = ru.update(); + assertEquals(Result.NEW, update); + + Ref ref = db.exactRef(name); + assertNotNull(ref); + assertFalse(ref.isPeeled()); + assertEquals(bad, ref.getObjectId()); + + try (RevWalk rw = new RevWalk(db)) { + rw.parseAny(ref.getObjectId()); + fail("Expected MissingObjectException"); + } catch (MissingObjectException expected) { + assertEquals(bad, expected.getObjectId()); + } + + RefDirectory refdir = (RefDirectory) db.getRefDatabase(); + try { + // Packing requires peeling, which fails. + refdir.pack(Arrays.asList(name)); + } catch (MissingObjectException expected) { + assertEquals(bad, expected.getObjectId()); + } + } + + @Test + public void testUpdateMissingObject() throws IOException { + String name = "refs/heads/abc"; + RefUpdate ru = updateRef(name); + Result update = ru.update(); + assertEquals(Result.NEW, update); + ObjectId oldId = ru.getNewObjectId(); + + ObjectId bad = + ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + ru = db.updateRef(name); + ru.setNewObjectId(bad); + update = ru.update(); + assertEquals(Result.REJECTED, update); + + Ref ref = db.exactRef(name); + assertNotNull(ref); + assertEquals(oldId, ref.getObjectId()); + } + + @Test + public void testForceUpdateMissingObject() throws IOException { + String name = "refs/heads/abc"; + RefUpdate ru = updateRef(name); + Result update = ru.update(); + assertEquals(Result.NEW, update); + + ObjectId bad = + ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + ru = db.updateRef(name); + ru.setNewObjectId(bad); + update = ru.forceUpdate(); + assertEquals(Result.FORCED, update); + + Ref ref = db.exactRef(name); + assertNotNull(ref); + assertFalse(ref.isPeeled()); + assertEquals(bad, ref.getObjectId()); + + try (RevWalk rw = new RevWalk(db)) { + rw.parseAny(ref.getObjectId()); + fail("Expected MissingObjectException"); + } catch (MissingObjectException expected) { + assertEquals(bad, expected.getObjectId()); + } + + RefDirectory refdir = (RefDirectory) db.getRefDatabase(); + try { + // Packing requires peeling, which fails. + refdir.pack(Arrays.asList(name)); + } catch (MissingObjectException expected) { + assertEquals(bad, expected.getObjectId()); + } + } + private static void writeReflog(Repository db, ObjectId newId, String msg, String refName) throws IOException { RefDirectory refs = (RefDirectory) db.getRefDatabase();