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

Successfully creating changed and new files.

Winston Li 12 лет назад
Родитель
Сommit
30675a11d4

+ 2 - 1
services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java

@@ -4,6 +4,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc
 import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
 import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
 
+import java.io.IOException;
 import java.util.List;
 
 /**
@@ -14,7 +15,7 @@ public interface WriteLatexDataSource {
     /* Called by request thread. */
     public boolean repositoryExists(String projectName) throws FailedConnectionException;
     public List<WritableRepositoryContents> getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException;
-    public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException;
+    public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException;
     public void expectPostback(String projectName);
 
     /* Called by postback thread. */

+ 9 - 4
services/git-bridge/src/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java

@@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.git.handler.hook;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.transport.PreReceiveHook;
 import org.eclipse.jgit.transport.ReceiveCommand;
+import org.eclipse.jgit.transport.ReceiveCommand.Result;
 import org.eclipse.jgit.transport.ReceivePack;
 import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
 import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
@@ -10,6 +11,7 @@ import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
 import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
 import uk.ac.ic.wlgitbridge.writelatex.OutOfDateException;
 import uk.ac.ic.wlgitbridge.writelatex.SnapshotPostException;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
 
 import java.io.IOException;
 import java.util.Collection;
@@ -32,21 +34,24 @@ public class WriteLatexPutHook implements PreReceiveHook {
                 handleReceiveCommand(receivePack.getRepository(), receiveCommand);
             } catch (IOException e) {
                 receivePack.sendError("IOException");
-                receiveCommand.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, "I/O Exception");
+                receiveCommand.setResult(Result.REJECTED_OTHER_REASON, "I/O Exception");
+            } catch (FailedConnectionException e) {
+                receivePack.sendError("failed connection");
+                receiveCommand.setResult(Result.REJECTED_OTHER_REASON, "failed connection");
             } catch (OutOfDateException e) {
-                receiveCommand.setResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD);
+                receiveCommand.setResult(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);
+                receiveCommand.setResult(Result.REJECTED_OTHER_REASON, message);
             }
         }
     }
 
-    private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws ForcedPushException, IOException, SnapshotPostException {
+    private void handleReceiveCommand(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException, FailedConnectionException {
         checkForcedPush(receiveCommand);
         writeLatexDataSource.putDirectoryContentsToProjectWithName(repository.getWorkTree().getName(),
                                                                    getPushedDirectoryContents(repository,

+ 26 - 1
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java

@@ -1,8 +1,11 @@
 package uk.ac.ic.wlgitbridge.writelatex;
 
+import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
 import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
 import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
+import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.node.WLDirectoryNode;
 import uk.ac.ic.wlgitbridge.writelatex.model.WLProject;
 
@@ -21,11 +24,33 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot {
         projectName = project.getName();
         this.directoryNode = directoryNode;
         this.callback = callback;
+        System.out.println(getJsonRepresentation());
     }
 
     @Override
     public JsonElement getJsonRepresentation() {
-        return null;
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("latestVerId", previousVersionID);
+        jsonObject.add("files", getFilesAsJson());
+        jsonObject.addProperty("postbackUrl", "wsomewhere");
+        return jsonObject;
+    }
+
+    private JsonArray getFilesAsJson() {
+        JsonArray filesArray = new JsonArray();
+        for (FileNode fileNode : directoryNode.getFileNodes()) {
+            filesArray.add(getFileAsJson(fileNode));
+        }
+        return filesArray;
+    }
+
+    private JsonObject getFileAsJson(FileNode fileNode) {
+        JsonObject file = new JsonObject();
+        file.addProperty("name", fileNode.getFilePath());
+        if (fileNode.isChanged()) {
+            file.addProperty("url", "url");
+        }
+        return file;
     }
 
     @Override

+ 4 - 1
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java

@@ -7,8 +7,10 @@ import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
 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.api.request.push.SnapshotPushRequest;
 import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 
@@ -41,8 +43,9 @@ public class WriteLatexAPI implements WriteLatexDataSource {
     }
 
     @Override
-    public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException {
+    public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException {
         CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents);
+        new SnapshotPushRequest(candidate);
         throw new SnapshotPostException() {
 
             @Override

+ 39 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequest.java

@@ -0,0 +1,39 @@
+package uk.ac.ic.wlgitbridge.writelatex.api.request.push;
+
+import com.google.gson.JsonElement;
+import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.HTTPMethod;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
+
+/**
+ * Created by Winston on 16/11/14.
+ */
+public class SnapshotPushRequest extends SnapshotAPIRequest<SnapshotPushRequestResult> {
+
+    private static final String API_CALL = "/snapshots";
+
+    private final CandidateSnapshot candidateSnapshot;
+
+    public SnapshotPushRequest(CandidateSnapshot candidateSnapshot) {
+        super(candidateSnapshot.getProjectName(), API_CALL);
+        this.candidateSnapshot = candidateSnapshot;
+    }
+
+    @Override
+    protected HTTPMethod httpMethod() {
+        return HTTPMethod.POST;
+    }
+
+    @Override
+    protected String getPostBody() {
+        return candidateSnapshot.getJsonRepresentation().toString();
+    }
+
+    @Override
+    protected SnapshotPushRequestResult parseResponse(JsonElement json) throws FailedConnectionException {
+        System.out.println(json);
+        return null;
+    }
+
+}

+ 22 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/SnapshotPushRequestResult.java

@@ -0,0 +1,22 @@
+package uk.ac.ic.wlgitbridge.writelatex.api.request.push;
+
+import com.google.gson.JsonElement;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Result;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
+
+/**
+ * Created by Winston on 16/11/14.
+ */
+public class SnapshotPushRequestResult extends Result {
+
+    public SnapshotPushRequestResult(Request request, JsonElement json) throws FailedConnectionException {
+        super(request, json);
+    }
+
+    @Override
+    public void fromJSON(JsonElement json) throws FailedConnectionException {
+
+    }
+
+}

+ 15 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/BlobNode.java

@@ -1,10 +1,14 @@
 package uk.ac.ic.wlgitbridge.writelatex.filestore.node;
 
 import uk.ac.ic.wlgitbridge.bridge.RawFile;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
+import uk.ac.ic.wlgitbridge.writelatex.filestore.RepositoryFile;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.Blob;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.ByteBlob;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.blob.RawFileBlob;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -19,6 +23,11 @@ public class BlobNode extends FileNode {
         blob = new RawFileBlob(rawFile);
     }
 
+    public BlobNode(RepositoryFile repositoryFile, Map<String, FileNode> fileNodeTable, File projectAttDirectory) throws IOException, FailedConnectionException {
+        this(repositoryFile, fileNodeTable);
+        writeChanged(projectAttDirectory);
+    }
+
     @Override
     public void handleIndexer(FileNodeIndexer fileNodeIndexer) {
         fileNodeIndexer.index(this);
@@ -29,4 +38,10 @@ public class BlobNode extends FileNode {
         return blob;
     }
 
+    private void writeChanged(File projectAttDirectory) throws FailedConnectionException, IOException {
+        if (isChanged()) {
+            writeToDisk(projectAttDirectory);
+        }
+    }
+
 }

+ 7 - 3
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/FileNode.java

@@ -16,7 +16,7 @@ import java.util.Map;
 public abstract class FileNode {
 
     private final String filePath;
-    private final boolean unchanged;
+    private final boolean changed;
 
     public FileNode(RawFile file, Map<String, FileNode> context) {
         this(file.getPath(), context);
@@ -25,13 +25,17 @@ public abstract class FileNode {
     public FileNode(String filePath, Map<String, FileNode> context) {
         this.filePath = filePath;
         FileNode currentFileNode = context.get(filePath);
-        unchanged = currentFileNode != null && equals(currentFileNode);
+        changed = currentFileNode == null || !equals(currentFileNode);
     }
 
     public String getFilePath() {
         return filePath;
     }
 
+    public boolean isChanged() {
+        return changed;
+    }
+
     public byte[] getContents() throws FailedConnectionException {
         return getBlob().getContents();
     }
@@ -56,7 +60,7 @@ public abstract class FileNode {
 
     @Override
     public String toString() {
-        return String.valueOf(unchanged);
+        return String.valueOf(changed);
     }
 
 }

+ 14 - 6
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/node/WLDirectoryNode.java

@@ -8,6 +8,8 @@ import uk.ac.ic.wlgitbridge.writelatex.filestore.RepositoryFile;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.store.FileIndexStore;
 import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -19,18 +21,24 @@ import java.util.Map.Entry;
  */
 public class WLDirectoryNode {
 
+    private final String projectName;
     private Map<String, FileNode> fileNodeTable;
     private FileIndexStore fileIndexStore;
 
-    public WLDirectoryNode() {
-        this(new HashMap<String, FileNode>(), new FileIndexStore());
+    public WLDirectoryNode(String projectName) {
+        this(projectName, new HashMap<String, FileNode>(), new FileIndexStore());
     }
 
-    public WLDirectoryNode(Map<String, FileNode> fileNodeTable, FileIndexStore fileIndexStore) {
+    public WLDirectoryNode(String projectName, Map<String, FileNode> fileNodeTable, FileIndexStore fileIndexStore) {
+        this.projectName = projectName;
         this.fileNodeTable = fileNodeTable;
         this.fileIndexStore = fileIndexStore;
     }
 
+    public List<FileNode> getFileNodes() {
+        return new LinkedList<FileNode>(fileNodeTable.values());
+    }
+
     public List<FileNode> updateFromSnapshot(Snapshot snapshot) throws FailedConnectionException {
         Map<String, FileNode> updatedFileNodeTable = new HashMap<String, FileNode>();
         List<SnapshotFile> srcs = snapshot.getSrcs();
@@ -49,13 +57,13 @@ public class WLDirectoryNode {
         return fileNodes;
     }
 
-    public WLDirectoryNode createFromRawDirectoryContents(RawDirectoryContents rawDirectoryContents) {
+    public WLDirectoryNode createFromRawDirectoryContents(RawDirectoryContents rawDirectoryContents, File attachmentDirectory) throws IOException, FailedConnectionException {
         Map<String, FileNode> candidateFileNodeTable = new HashMap<String, FileNode>();
         for (Entry<String, byte[]> fileContents : rawDirectoryContents.getFileContentsTable().entrySet()) {
-            BlobNode blobNode = new BlobNode(new RepositoryFile(fileContents), fileNodeTable);
+            BlobNode blobNode = new BlobNode(new RepositoryFile(fileContents), fileNodeTable, new File(attachmentDirectory, projectName));
             candidateFileNodeTable.put(blobNode.getFilePath(), blobNode);
         }
-        return new WLDirectoryNode(candidateFileNodeTable,
+        return new WLDirectoryNode(projectName, candidateFileNodeTable,
                                    new FileIndexStore(new LinkedList<FileNode>(candidateFileNodeTable.values())));
     }
 

+ 7 - 3
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/WLFileStore.java

@@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
 import uk.ac.ic.wlgitbridge.writelatex.model.WLProject;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -19,12 +20,15 @@ public class WLFileStore {
 
     private final Map<String, WLDirectoryNode> fileStore;
     private final File rootGitDirectory;
+    private final File attDirectory;
 
     public WLFileStore(String rootGitDirectoryPath) {
         fileStore = new HashMap<String, WLDirectoryNode>();
         rootGitDirectory = new File(rootGitDirectoryPath);
         rootGitDirectory.mkdirs();
         deleteInDirectory(rootGitDirectory);
+        attDirectory = new File(rootGitDirectory, ".wlgb/atts");
+        attDirectory.mkdirs();
     }
 
     public static void deleteInDirectory(File directory) {
@@ -51,14 +55,14 @@ public class WLFileStore {
         return writableRepositories;
     }
 
-    public WLDirectoryNode createNextDirectoryNodeInProjectFromContents(WLProject project, RawDirectoryContents directoryContents) {
-        return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents);
+    public WLDirectoryNode createNextDirectoryNodeInProjectFromContents(WLProject project, RawDirectoryContents directoryContents) throws IOException, FailedConnectionException {
+        return getDirectoryNodeForProjectName(project.getName()).createFromRawDirectoryContents(directoryContents, attDirectory);
     }
 
     private WLDirectoryNode getDirectoryNodeForProjectName(String projectName) {
         WLDirectoryNode directoryNode = fileStore.get(projectName);
         if (directoryNode == null) {
-            directoryNode = new WLDirectoryNode();
+            directoryNode = new WLDirectoryNode(projectName);
             fileStore.put(projectName, directoryNode);
         }
         return directoryNode;

+ 2 - 1
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java

@@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc
 import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
 import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -42,7 +43,7 @@ public class WLDataModel implements CandidateSnapshotCallback {
         return project;
     }
 
-    public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException {
+    public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents) throws SnapshotPostException, IOException, FailedConnectionException {
         return new WLDirectoryNodeSnapshot(getProjectWithName(projectName),
                                            fileStore.createNextDirectoryNodeInProjectFromContents(getProjectWithName(projectName),
                                                                                                   directoryContents),