Source:
public void testPushAfterGC() throws Exception { Repository db2 = createWorkRepository(); final StoredConfig config = db.getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); Git git1 = new Git(db); Git git2 = new Git(db2); git1.commit().setMessage("initial commit").call(); git1.branchCreate().setName("refs/heads/test").call(); RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*"); git1.push().setRemote("test").setRefSpecs(spec).call(); git2.branchCreate().setName("refs/heads/other").call(); git2.checkout().setName("refs/heads/other").call(); writeTrashFile("a", "content of a"); git2.add().addFilepattern("a").call(); RevCommit commit2 = git2.commit().setMessage("adding a").call(); Properties res = git1.gc().setExpire(null).call(); assertEquals(7, res.size()); writeTrashFile("b", "content of b"); git1.add().addFilepattern("b").call(); RevCommit commit3 = git1.commit().setMessage("adding b").call(); try { git1.push().setRemote("test").setRefSpecs(spec).call(); } catch (TransportException e) { assertTrue("should be caused by a MissingObjectException", e .getCause().getCause() instanceof MissingObjectException); fail("caught MissingObjectException for a change we don't have"); } try { db.resolve(commit2.getId().getName() + "^{commit}"); fail("id shouldn't exist locally"); } catch (MissingObjectException e) { } assertEquals(commit2.getId(), db2.resolve(commit2.getId().getName() + "^{commit}")); assertEquals(commit3.getId(), db2.resolve(commit3.getId().getName() + "^{commit}")); }

Target:
public void testPushAfterGC() throws Exception { Repository db2 = createWorkRepository(); final StoredConfig config = db.getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "test"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); Git git1 = new Git(db); Git git2 = new Git(db2); git1.commit().setMessage("initial commit").call(); RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*"); git1.push().setRemote("test").setRefSpecs(spec).call(); git2.branchCreate().setName("refs/heads/other").call(); git2.checkout().setName("refs/heads/other").call(); writeTrashFile("a", "content of a"); git2.add().addFilepattern("a").call(); RevCommit commit2 = git2.commit().setMessage("adding a").call(); Properties res = git1.gc().setExpire(null).call(); assertEquals(7, res.size()); writeTrashFile("b", "content of b"); git1.add().addFilepattern("b").call(); RevCommit commit3 = git1.commit().setMessage("adding b").call(); try { git1.push().setRemote("test").setRefSpecs(spec).call(); } catch (TransportException e) { assertTrue("should be caused by a MissingObjectException", e .getCause().getCause() instanceof MissingObjectException); fail("caught MissingObjectException for a change we don't have"); } try { db.resolve(commit2.getId().getName() + "^{commit}"); fail("id shouldn't exist locally"); } catch (MissingObjectException e) { } assertEquals(commit2.getId(), db2.resolve(commit2.getId().getName() + "^{commit}")); assertEquals(commit3.getId(), db2.resolve(commit3.getId().getName() + "^{commit}")); }