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

Fix url index lookups for same commit and add log messages.

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

+ 1 - 14
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java

@@ -46,21 +46,8 @@ public class WLBridgedProject {
     }
 
     private void updateRepositoryFromSnapshots(Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException {
-        List<WritableRepositoryContents> writableRepositories;
         try {
-            writableRepositories = writeLatexDataSource.getWritableRepositories(name, repository);
-            for (WritableRepositoryContents contents : writableRepositories) {
-                contents.write();
-                Git git = new Git(repository);
-                for (String missing : git.status().call().getMissing()) {
-                    git.rm().setCached(true).addFilepattern(missing).call();
-                }
-                git.add().addFilepattern(".").call();
-                git.commit().setAuthor(new PersonIdent(contents.getUserName(), contents.getUserEmail(), contents.getWhen(), TimeZone.getDefault()))
-                        .setMessage(contents.getCommitMessage())
-                        .call();
-                Util.deleteInDirectoryApartFrom(contents.getDirectory(), ".git");
-            }
+            writeLatexDataSource.getWritableRepositories(name, repository);
         } catch (InvalidProjectException e) {
             throw new RepositoryNotFoundException(name);
         } catch (SnapshotPostException e) {

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

@@ -1,5 +1,6 @@
 package uk.ac.ic.wlgitbridge.bridge;
 
+import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.transport.ServiceMayNotContinueException;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
@@ -8,7 +9,6 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostEx
 import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException;
 
 import java.io.IOException;
-import java.util.List;
 
 /**
  * Created by Winston on 03/11/14.
@@ -21,7 +21,7 @@ public interface WriteLatexDataSource {
 
     /* Called by request thread. */
     public boolean repositoryExists(String projectName) throws ServiceMayNotContinueException;
-    public List<WritableRepositoryContents> getWritableRepositories(String projectName, Repository repository) throws IOException, SnapshotPostException;
+    public void getWritableRepositories(String projectName, Repository repository) throws IOException, SnapshotPostException, GitAPIException;
     public void putDirectoryContentsToProjectWithName(String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException;
 
     void checkPostbackKey(String projectName, String postbackKey) throws InvalidPostbackKeyException;

+ 3 - 5
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java

@@ -1,9 +1,9 @@
 package uk.ac.ic.wlgitbridge.writelatex;
 
+import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.transport.ServiceMayNotContinueException;
 import uk.ac.ic.wlgitbridge.bridge.RawDirectory;
-import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
 import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
 import uk.ac.ic.wlgitbridge.util.Util;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
@@ -16,7 +16,6 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.*;
 import uk.ac.ic.wlgitbridge.writelatex.model.DataStore;
 
 import java.io.IOException;
-import java.util.List;
 
 /**
  * Created by Winston on 16/11/14.
@@ -64,10 +63,9 @@ public class WriteLatexAPI implements WriteLatexDataSource {
     }
 
     @Override
-    public List<WritableRepositoryContents> getWritableRepositories(String projectName, Repository repository) throws IOException, SnapshotPostException {
+    public void getWritableRepositories(String projectName, Repository repository) throws IOException, SnapshotPostException, GitAPIException {
         Util.sout("Fetching project: " + projectName);
-        List<WritableRepositoryContents> writableRepositoryContents = dataModel.updateProjectWithName(projectName, repository);
-        return writableRepositoryContents;
+        dataModel.updateProjectWithName(projectName, repository);
     }
 
     @Override

+ 21 - 8
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/writelatex/model/DataStore.java

@@ -1,10 +1,12 @@
 package uk.ac.ic.wlgitbridge.writelatex.model;
 
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Repository;
 import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
 import uk.ac.ic.wlgitbridge.bridge.RawDirectory;
 import uk.ac.ic.wlgitbridge.bridge.RawFile;
-import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
 import uk.ac.ic.wlgitbridge.util.Util;
 import uk.ac.ic.wlgitbridge.writelatex.CandidateSnapshot;
 import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher;
@@ -18,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.TimeZone;
 
 /**
  * Created by Winston on 06/11/14.
@@ -40,26 +43,36 @@ public class DataStore implements CandidateSnapshotCallback {
         resourceFetcher = new ResourceFetcher(persistentStore);
     }
 
-    public List<WritableRepositoryContents> updateProjectWithName(String name, Repository repository) throws IOException, SnapshotPostException {
+    public void updateProjectWithName(String name, Repository repository) throws IOException, SnapshotPostException, GitAPIException {
         LinkedList<Snapshot> snapshots = snapshotFetcher.getSnapshotsForProjectAfterVersion(name, persistentStore.getLatestVersionForProject(name));
         if (!snapshots.isEmpty()) {
             persistentStore.setLatestVersionForProject(name, snapshots.getLast().getVersionID());
         }
-        List<WritableRepositoryContents> commits = makeCommitsFromSnapshots(name, repository, snapshots);
-        return commits;
+        makeCommitsFromSnapshots(name, repository, snapshots);
     }
 
-    private List<WritableRepositoryContents> makeCommitsFromSnapshots(String name, Repository repository, List<Snapshot> snapshots) throws IOException {
-        List<WritableRepositoryContents> commits = new LinkedList<WritableRepositoryContents>();
+    private void makeCommitsFromSnapshots(String name, Repository repository, List<Snapshot> snapshots) throws IOException, GitAPIException {
         for (Snapshot snapshot : snapshots) {
             List<RawFile> files = new LinkedList<RawFile>();
             files.addAll(snapshot.getSrcs());
             for (SnapshotAttachment snapshotAttachment : snapshot.getAtts()) {
                 files.add(resourceFetcher.get(name, snapshotAttachment.getUrl(), snapshotAttachment.getPath(), repository));
             }
-            commits.add(new GitDirectoryContents(files, rootGitDirectory, name, snapshot));
+            commit(new GitDirectoryContents(files, rootGitDirectory, name, snapshot), repository);
         }
-        return commits;
+    }
+
+    private void commit(GitDirectoryContents contents, Repository repository) throws IOException, GitAPIException {
+        contents.write();
+        Git git = new Git(repository);
+        for (String missing : git.status().call().getMissing()) {
+            git.rm().setCached(true).addFilepattern(missing).call();
+        }
+        git.add().addFilepattern(".").call();
+        git.commit().setAuthor(new PersonIdent(contents.getUserName(), contents.getUserEmail(), contents.getWhen(), TimeZone.getDefault()))
+                .setMessage(contents.getCommitMessage())
+                .call();
+        Util.deleteInDirectoryApartFrom(contents.getDirectory(), ".git");
     }
 
     public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents) throws SnapshotPostException, IOException, FailedConnectionException {

+ 5 - 1
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/writelatex/model/ResourceFetcher.java

@@ -33,13 +33,16 @@ public class ResourceFetcher {
             path = newPath;
             contents = fetch(projectName, url, path);
         } else {
+            Util.sout("Found (" + projectName + "): " + url);
+            Util.sout("At (" + projectName + "): " + path);
             contents = new RepositoryObjectTreeWalker(repository).getDirectoryContents().getFileTable().get(path).getContents();
         }
         return new RepositoryFile(path, contents);
     }
 
-    private byte[] fetch(String projectName, String url, String path) throws FailedConnectionException {
+    private byte[] fetch(String projectName, final String url, String path) throws FailedConnectionException {
         byte[] contents;
+        Util.sout("GET -> " + url);
         try {
             contents = new AsyncHttpClient().prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
 
@@ -53,6 +56,7 @@ public class ResourceFetcher {
 
                 @Override
                 public byte[] onCompleted(Response response) throws Exception {
+                    Util.sout(response.getStatusCode() + " " + response.getStatusText() + " (" + response.getResponseBody().length() + "B) -> " + url);
                     return bytes.toByteArray();
                 }