Browse Source

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
stable-4.9
Dave Borowitz 7 years ago
parent
commit
28202a6758
  1. 88
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java

88
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();

Loading…
Cancel
Save