diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/CheckNonFastForwardHook.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/CheckNonFastForwardHook.java index cde3e83cdb..b9e2ad0de0 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/CheckNonFastForwardHook.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/CheckNonFastForwardHook.java @@ -1,10 +1,17 @@ package uk.ac.ic.wlgitbridge.git.handler.hook; import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.PreReceiveHook; import org.eclipse.jgit.transport.ReceiveCommand; import org.eclipse.jgit.transport.ReceivePack; +import org.eclipse.jgit.treewalk.TreeWalk; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.Collection; /** @@ -15,11 +22,39 @@ public class CheckNonFastForwardHook implements PreReceiveHook { @Override public void onPreReceive(ReceivePack receivePack, Collection receiveCommands) { for (ReceiveCommand receiveCommand : receiveCommands) { + receiveCommand.setResult(RefUpdate.Result.REJECTED); + System.out.println(receiveCommand.getRef()); + try { + + // a RevWalk allows to walk over commits based on some filtering that is + // defined + RevWalk walk = new RevWalk(receivePack.getRepository()); + + RevCommit commit = walk.parseCommit(receiveCommand.getNewId()); + RevTree tree = commit.getTree(); + System.out.println("Having tree: " + tree); + + // now use a TreeWalk to iterate over all files in the Tree recursively + // you can set Filters to narrow down the results if needed + TreeWalk treeWalk = new TreeWalk(receivePack.getRepository()); + treeWalk.addTree(tree); + treeWalk.setRecursive(true); + while (treeWalk.next()) { + File file = new File("/Users/Roxy/git/testing/" + treeWalk.getPathString()); + file.getParentFile().mkdirs(); + FileOutputStream out = new FileOutputStream(file); + receivePack.getRepository().open(treeWalk.getObjectId(0)).copyTo(out); + + } + } catch (IOException e) { + e.printStackTrace(); + } if (receiveCommand.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD) { receivePack.sendError("You can't do a force push"); receiveCommand.setResult(RefUpdate.Result.REJECTED); } } + System.out.println("Pre receive"); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/GitDirectoryContents.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/GitDirectoryContents.java index 14255ad9d3..4fd381c68d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/GitDirectoryContents.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/GitDirectoryContents.java @@ -5,6 +5,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode; import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; +import java.io.File; import java.io.IOException; import java.util.List; @@ -14,14 +15,14 @@ import java.util.List; public class GitDirectoryContents implements WritableRepositoryContents { private final List fileNodes; - private final String rootGitDirectoryPath; + private final File gitDirectory; private final String userName; private final String userEmail; private final String commitMessage; - public GitDirectoryContents(List fileNodes, String rootGitDirectoryPath, Snapshot snapshot) { + public GitDirectoryContents(List fileNodes, File rootGitDirectory, String projectName, Snapshot snapshot) { this.fileNodes = fileNodes; - this.rootGitDirectoryPath = rootGitDirectoryPath; + gitDirectory = new File(rootGitDirectory, projectName); userName = snapshot.getUserName(); userEmail = snapshot.getUserEmail(); commitMessage = snapshot.getComment(); @@ -30,7 +31,7 @@ public class GitDirectoryContents implements WritableRepositoryContents { @Override public void write() throws IOException, FailedConnectionException { for (FileNode fileNode : fileNodes) { - fileNode.writeToDisk(rootGitDirectoryPath); + fileNode.writeToDisk(gitDirectory); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java index 20b99b17c9..2e9890796d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java @@ -32,8 +32,8 @@ public abstract class FileNode { return getBlob().getContents(); } - public void writeToDisk(String repoDir) throws FailedConnectionException, IOException { - File file = new File(repoDir, filePath); + public void writeToDisk(File directory) throws FailedConnectionException, IOException { + File file = new File(directory, filePath); file.getParentFile().mkdirs(); file.createNewFile(); OutputStream out = new FileOutputStream(file); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java index 581e689c26..fb2e27c9c4 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java @@ -8,6 +8,7 @@ import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode; import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; +import java.io.File; import java.util.*; /** @@ -16,11 +17,11 @@ import java.util.*; public class WLFileStore { private final Map fileStore; - private final String rootGitDirectoryPath; + private final File rootGitDirectory; public WLFileStore(String rootGitDirectoryPath) { fileStore = new HashMap(); - this.rootGitDirectoryPath = rootGitDirectoryPath; + rootGitDirectory = new File(rootGitDirectoryPath); } public List updateForProject(WLProject project) throws FailedConnectionException, @@ -31,7 +32,8 @@ public class WLFileStore { List writableRepositories = new LinkedList(); for (Snapshot snapshot : snapshots) { writableRepositories.add(new GitDirectoryContents(directoryNode.updateFromSnapshot(snapshot), - rootGitDirectoryPath, + rootGitDirectory, + projectName, snapshot)); } return writableRepositories;