Added checking for the master branch.

This commit is contained in:
Winston Li 2014-12-19 14:40:25 +00:00
parent 77a9ef8cc8
commit 128e24d2fb
3 changed files with 48 additions and 4 deletions

View file

@ -8,6 +8,7 @@ import org.eclipse.jgit.transport.ReceivePack;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.WrongBranchException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
@ -56,6 +57,7 @@ public class WriteLatexPutHook implements PreReceiveHook {
}
private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, FailedConnectionException {
checkBranch(receiveCommand);
checkForcedPush(receiveCommand);
writeLatexDataSource.putDirectoryContentsToProjectWithName(repository.getWorkTree().getName(),
getPushedDirectoryContents(repository,
@ -63,6 +65,12 @@ public class WriteLatexPutHook implements PreReceiveHook {
hostname);
}
private void checkBranch(ReceiveCommand receiveCommand) throws WrongBranchException {
if (!receiveCommand.getRefName().equals("refs/heads/master")) {
throw new WrongBranchException();
}
}
private void checkForcedPush(ReceiveCommand receiveCommand) throws ForcedPushException {
if (receiveCommand.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD) {
throw new ForcedPushException();

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import java.util.Arrays;
@ -12,7 +13,7 @@ import java.util.List;
public class ForcedPushException extends SnapshotPostException {
private static final String[] DESCRIPTION_LINES = {
"You can't git push --force to a WriteLatex project.",
"You can't git push --force to a " + Util.getServiceName() + " project.",
"Try to put your changes on top of the current head.",
"If everything else fails, delete and reclone your repository, make your changes, then push again."
};
@ -31,4 +32,5 @@ public class ForcedPushException extends SnapshotPostException {
public void fromJSON(JsonElement json) {
}
}

View file

@ -0,0 +1,34 @@
package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import java.util.Arrays;
import java.util.List;
/**
* Created by Winston on 19/12/14.
*/
public class WrongBranchException extends SnapshotPostException {
private static final String[] DESCRIPTION_LINES = {
"You can't push any new branches.",
"Please use the master branch."
};
@Override
public String getMessage() {
return "wrong branch";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList(DESCRIPTION_LINES);
}
@Override
public void fromJSON(JsonElement json) {
}
}