return;
}
}
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
Status status = ref.getStatus();
if (permission.atLeast(AccessPermission.PUSH)) {
assertTrue("User failed to push commit?! " + status.name(), Status.OK.equals(status));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));
GitBlitSuite.close(git);
// skip delete test
return;
}
}
// create a local branch and push the new branch back to the origin
git.branchCreate().setName("protectme").call();
RefSpec refSpec = new RefSpec("refs/heads/protectme:refs/heads/protectme");
results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
Status status = ref.getStatus();
if (Status.OK.equals(expectedCreate)) {
assertTrue("User failed to push creation?! " + status.name(), status.equals(expectedCreate));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push ref creation! " + status.name(), status.equals(expectedCreate));
GitBlitSuite.close(git);
// skip delete test
return;
}
}
// delete the branch locally
git.branchDelete().setBranchNames("protectme").call();
// push a delete ref command
refSpec = new RefSpec(":refs/heads/protectme");
results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
Status status = ref.getStatus();
if (Status.OK.equals(expectedDelete)) {
assertTrue("User failed to push ref deletion?! " + status.name(), status.equals(Status.OK));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push ref deletion?! " + status.name(), status.equals(expectedDelete));
GitBlitSuite.close(git);
// skip rewind test
return;
}
}
// rewind master by two commits
git.reset().setRef("HEAD~2").setMode(ResetType.HARD).call();
// commit a change on this detached HEAD
file = new File(local, "REWINDCHK");
os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
git.add().addFilepattern(file.getName()).call();
RevCommit commit = git.commit().setMessage("rewind master and new commit").call();
// Reset master to our new commit now we our local branch tip is no longer
// upstream of the remote branch tip. It is an alternate tip of the branch.
JGitUtils.setBranchRef(git.getRepository(), "refs/heads/master", commit.getName());
// Try pushing our new tip to the origin.
// This requires the server to "rewind" it's master branch and update it
// to point to our alternate tip. This leaves the original master tip
// unreferenced.
results = git.push().setCredentialsProvider(cp).setRemote("origin").setForce(true).call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
Status status = ref.getStatus();
if (Status.OK.equals(expectedRewind)) {
assertTrue("User failed to rewind master?! " + status.name(), status.equals(expectedRewind));
} else {
assertTrue("User was able to rewind master?! " + status.name(), status.equals(expectedRewind));
}