Explorar el Código

Fixed directory problem in file store and wrote working code for reading new revs.

Winston Li hace 12 años
padre
commit
6d4309267e

+ 35 - 0
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<ReceiveCommand> 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");
     }
 
 }

+ 5 - 4
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<FileNode> fileNodes;
-    private final String rootGitDirectoryPath;
+    private final File gitDirectory;
     private final String userName;
     private final String userEmail;
     private final String commitMessage;
 
-    public GitDirectoryContents(List<FileNode> fileNodes, String rootGitDirectoryPath, Snapshot snapshot) {
+    public GitDirectoryContents(List<FileNode> 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);
         }
     }
 

+ 2 - 2
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);

+ 5 - 3
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<String, WLDirectoryNode> fileStore;
-    private final String rootGitDirectoryPath;
+    private final File rootGitDirectory;
 
     public WLFileStore(String rootGitDirectoryPath) {
         fileStore = new HashMap<String, WLDirectoryNode>();
-        this.rootGitDirectoryPath = rootGitDirectoryPath;
+        rootGitDirectory = new File(rootGitDirectoryPath);
     }
 
     public List<WritableRepositoryContents> updateForProject(WLProject project) throws FailedConnectionException,
@@ -31,7 +32,8 @@ public class WLFileStore {
         List<WritableRepositoryContents> writableRepositories = new LinkedList<WritableRepositoryContents>();
         for (Snapshot snapshot : snapshots) {
             writableRepositories.add(new GitDirectoryContents(directoryNode.updateFromSnapshot(snapshot),
-                                                              rootGitDirectoryPath,
+                                                              rootGitDirectory,
+                                                              projectName,
                                                               snapshot));
         }
         return writableRepositories;