aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Borowitz <dborowitz@google.com>2017-07-07 10:31:01 -0400
committerDave Borowitz <dborowitz@google.com>2017-07-17 11:56:35 -0400
commit28202a67586931f1adf5b4a612e85f559b9dbe43 (patch)
tree67ddd1c482d2a54d2d8e03241968b12e944e9915
parentf529fa6729a9b8b5c1329bf75639d26b0067f9d9 (diff)
downloadjgit-28202a67586931f1adf5b4a612e85f559b9dbe43.tar.gz
jgit-28202a67586931f1adf5b4a612e85f559b9dbe43.zip
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
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java88
1 files changed, 88 insertions, 0 deletions
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 e4e5d28f7d..1203e83dce 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();