From 28202a67586931f1adf5b4a612e85f559b9dbe43 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Fri, 7 Jul 2017 10:31:01 -0400 Subject: [PATCH] Add tests for updating single refs to missing objects The reader may find it surprising that this succeeds without incident unless there is peeling or a fast-forward check involved. This behavior may be changed in the future, but for now, just document the current behavior. Change-Id: I348b37e93e0264dc0905c4d58ce881852d1dfe5e --- .../internal/storage/file/RefUpdateTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) 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();