Просмотр исходного кода

Added checking for the master branch.

Winston Li 11 лет назад
Родитель
Сommit
128e24d2fb

+ 11 - 3
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.RawDirectoryContents;
 import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
 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.ForcedPushException;
+import uk.ac.ic.wlgitbridge.git.handler.hook.exception.WrongBranchException;
 import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
 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.OutOfDateException;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
 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 {
     private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, FailedConnectionException {
+        checkBranch(receiveCommand);
         checkForcedPush(receiveCommand);
         checkForcedPush(receiveCommand);
         writeLatexDataSource.putDirectoryContentsToProjectWithName(repository.getWorkTree().getName(),
         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 {
     private void checkForcedPush(ReceiveCommand receiveCommand) throws ForcedPushException {

+ 3 - 1
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;
 package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
 
 
 import com.google.gson.JsonElement;
 import com.google.gson.JsonElement;
+import uk.ac.ic.wlgitbridge.util.Util;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
 
 
 import java.util.Arrays;
 import java.util.Arrays;
@@ -12,7 +13,7 @@ import java.util.List;
 public class ForcedPushException extends SnapshotPostException {
 public class ForcedPushException extends SnapshotPostException {
 
 
     private static final String[] DESCRIPTION_LINES = {
     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.",
             "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."
             "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) {
     public void fromJSON(JsonElement json) {
 
 
     }
     }
+
 }
 }

+ 34 - 0
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<String> getDescriptionLines() {
+        return Arrays.asList(DESCRIPTION_LINES);
+    }
+
+    @Override
+    public void fromJSON(JsonElement json) {
+
+    }
+
+}