Added push exceptions and deleting root git directory on program start.

This commit is contained in:
Winston Li
2014-11-16 15:40:12 +00:00
parent 3cd94af7e9
commit 74ac6e2379
12 changed files with 169 additions and 23 deletions

View File

@@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.model.SnapshotPostException;
import java.util.List;
@@ -12,7 +13,6 @@ public interface WriteLatexDataSource {
public boolean repositoryExists(String name) throws FailedConnectionException;
public List<WritableRepositoryContents> getWritableRepositories(String name) throws FailedConnectionException, InvalidProjectException;
public CandidateSnapshot createCandidateSnapshot(RawDirectoryContents rawDirectoryContents);
public void approveCandidateSnapshot(CandidateSnapshot candidateSnapshot);
public void putDirectoryContentsToProjectWithName(String name, RawDirectoryContents directoryContents) throws SnapshotPostException;
}

View File

@@ -5,8 +5,8 @@ import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.git.handler.hook.WriteLatexPutHook;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.git.handler.hook.WriteLatexPutHook;
import javax.servlet.http.HttpServletRequest;

View File

@@ -14,6 +14,8 @@ import javax.servlet.http.HttpServletRequest;
public class WLUploadPackFactory implements UploadPackFactory<HttpServletRequest> {
@Override
public UploadPack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException {
return new UploadPack(repository);
UploadPack uploadPack = new UploadPack(repository);
uploadPack.sendMessage("Downloading files from WriteLatex");
return uploadPack;
}
}

View File

@@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.git.handler.hook;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PreReceiveHook;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.ReceivePack;
@@ -8,6 +8,8 @@ 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.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.writelatex.model.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.model.SnapshotPostException;
import java.io.IOException;
import java.util.Collection;
@@ -27,20 +29,28 @@ public class WriteLatexPutHook implements PreReceiveHook {
public void onPreReceive(ReceivePack receivePack, Collection<ReceiveCommand> receiveCommands) {
for (ReceiveCommand receiveCommand : receiveCommands) {
try {
handleReceiveCommand(receivePack, receiveCommand);
} catch (ForcedPushException e) {
receivePack.sendError("You can't do a force push");
receiveCommand.setResult(Result.REJECTED);
handleReceiveCommand(receivePack.getRepository(), receiveCommand);
} catch (IOException e) {
receivePack.sendError("IOException");
receiveCommand.setResult(Result.REJECTED);
receiveCommand.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, "I/O Exception");
} catch (OutOfDateException e) {
receiveCommand.setResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD);
} catch (SnapshotPostException e) {
String message = e.getMessage();
receivePack.sendError(message);
for (String line : e.getDescriptionLines()) {
receivePack.sendMessage("hint: " + line);
}
receiveCommand.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, message);
}
}
}
private void handleReceiveCommand(ReceivePack receivePack, ReceiveCommand receiveCommand) throws ForcedPushException, IOException {
private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws ForcedPushException, IOException, SnapshotPostException {
checkForcedPush(receiveCommand);
RawDirectoryContents directoryContents = getPushedDirectoryContents(receivePack, receiveCommand);
writeLatexDataSource.putDirectoryContentsToProjectWithName(repository.getWorkTree().getName(),
getPushedDirectoryContents(repository,
receiveCommand));
}
private void checkForcedPush(ReceiveCommand receiveCommand) throws ForcedPushException {
@@ -49,10 +59,10 @@ public class WriteLatexPutHook implements PreReceiveHook {
}
}
private RawDirectoryContents getPushedDirectoryContents(ReceivePack receivePack, ReceiveCommand receiveCommand) throws IOException {
return new RepositoryObjectTreeWalker(receivePack.getRepository(),
receiveCommand.getNewId())
.getDirectoryContents();
private RawDirectoryContents getPushedDirectoryContents(Repository repository, ReceiveCommand receiveCommand) throws IOException {
return new RepositoryObjectTreeWalker(repository,
receiveCommand.getNewId())
.getDirectoryContents();
}
}

View File

@@ -1,7 +1,29 @@
package uk.ac.ic.wlgitbridge.git.handler.hook.exception;
import uk.ac.ic.wlgitbridge.writelatex.model.SnapshotPostException;
import java.util.Arrays;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class ForcedPushException extends Exception {
public class ForcedPushException extends SnapshotPostException {
private static final String[] DESCRIPTION_LINES = {
"You can't git push --force to a WriteLatex 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."
};
@Override
public String getMessage() {
return "forced push prohibited";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList(DESCRIPTION_LINES);
}
}

View File

@@ -22,6 +22,17 @@ public class WLFileStore {
public WLFileStore(String rootGitDirectoryPath) {
fileStore = new HashMap<String, WLDirectoryNode>();
rootGitDirectory = new File(rootGitDirectoryPath);
rootGitDirectory.mkdirs();
deleteInDirectory(rootGitDirectory);
}
public static void deleteInDirectory(File directory) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
deleteInDirectory(file);
}
file.delete();
}
}
public List<WritableRepositoryContents> updateForProject(WLProject project) throws FailedConnectionException,

View File

@@ -0,0 +1,20 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class InvalidFilesException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
}
@Override
public List<String> getDescriptionLines() {
return null;
}
}

View File

@@ -0,0 +1,20 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class InvalidProjectException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
}
@Override
public List<String> getDescriptionLines() {
return null;
}
}

View File

@@ -0,0 +1,20 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class OutOfDateException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
}
@Override
public List<String> getDescriptionLines() {
return null;
}
}

View File

@@ -0,0 +1,13 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public abstract class SnapshotPostException extends Exception {
public abstract String getMessage();
public abstract List<String> getDescriptionLines();
}

View File

@@ -0,0 +1,20 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class UnexpectedErrorException extends SnapshotPostException {
@Override
public String getMessage() {
return null;
}
@Override
public List<String> getDescriptionLines() {
return null;
}
}

View File

@@ -1,6 +1,5 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
@@ -9,6 +8,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -44,13 +44,21 @@ public class WLDataModel implements WriteLatexDataSource {
}
@Override
public CandidateSnapshot createCandidateSnapshot(RawDirectoryContents rawDirectoryContents) {
return null;
}
public void putDirectoryContentsToProjectWithName(String name, RawDirectoryContents directoryContents) throws SnapshotPostException {
System.out.println("Pushing project with name: " + name);
System.out.println(directoryContents.getFileContentsTable());
throw new SnapshotPostException() {
@Override
public void approveCandidateSnapshot(CandidateSnapshot candidateSnapshot) {
@Override
public String getMessage() {
return "unimplemented";
}
@Override
public List<String> getDescriptionLines() {
return Arrays.asList("Not currently implemented");
}
};
}
private List<WritableRepositoryContents> updateProjectWithName(String name) throws FailedConnectionException, InvalidProjectException {