From 128e24d2fb65840c0a7433108c6dd1dbd049dfbd Mon Sep 17 00:00:00 2001 From: Winston Li Date: Fri, 19 Dec 2014 14:40:25 +0000 Subject: [PATCH] Added checking for the master branch. --- .../git/handler/hook/WriteLatexPutHook.java | 14 ++++++-- .../hook/exception/ForcedPushException.java | 4 ++- .../hook/exception/WrongBranchException.java | 34 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/WrongBranchException.java diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java index 18c9e72cd6..c1cc70f9d0 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java @@ -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,11 +57,18 @@ 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, - receiveCommand), - hostname); + getPushedDirectoryContents(repository, + receiveCommand), + 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 { diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/ForcedPushException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/ForcedPushException.java index d8bd0318b6..b29d071002 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/ForcedPushException.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/ForcedPushException.java @@ -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) { } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/WrongBranchException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/WrongBranchException.java new file mode 100644 index 0000000000..481f5e33d2 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/exception/WrongBranchException.java @@ -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 getDescriptionLines() { + return Arrays.asList(DESCRIPTION_LINES); + } + + @Override + public void fromJSON(JsonElement json) { + + } + +}