Basic version of clone working.

This commit is contained in:
Winston Li 2014-11-07 13:18:07 +00:00
parent 0f4e99361f
commit f43fd39fc7
20 changed files with 211 additions and 78 deletions

View file

@ -7,7 +7,7 @@ import org.eclipse.jetty.util.log.Log;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.git.WLGitServlet;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.writelatex.api.DummySnapshotDBAPI;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
import javax.servlet.ServletException;
import java.net.BindException;
@ -65,7 +65,7 @@ public class WLGitBridgeServer {
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(
new ServletHolder(
new WLGitServlet(servletContextHandler, new DummySnapshotDBAPI(), rootGitDirectoryPath)),
new WLGitServlet(servletContextHandler, new WLDataModel(), rootGitDirectoryPath)),
"/*"
);
jettyServer.setHandler(servletContextHandler);

View file

@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import java.io.File;
@ -10,6 +11,6 @@ import java.io.File;
*/
public interface RepositorySource {
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException;
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException, ServiceNotEnabledException;
}

View file

@ -1,7 +1,11 @@
package uk.ac.ic.wlgitbridge.bridge;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
@ -26,7 +30,7 @@ public class WLBridgedProject {
this.snapshotDBAPI = snapshotDBAPI;
}
public void buildRepository() throws RepositoryNotFoundException {
public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException {
if (repository.getObjectDatabase().exists()) {
updateRepositoryFromSnapshots(repository);
} else {
@ -34,11 +38,26 @@ public class WLBridgedProject {
}
}
private void updateRepositoryFromSnapshots(Repository repository) {
List<Snapshot> snapshotsToAdd = snapshotDBAPI.getSnapshotsToAddToRepository(repository);
private void updateRepositoryFromSnapshots(Repository repository) throws ServiceNotEnabledException {
try {
List<Snapshot> snapshotsToAdd = snapshotDBAPI.getSnapshotsToAddToProject(name);
for (Snapshot snapshot : snapshotsToAdd) {
snapshot.getData().writeAll(repositoryDirectory.getAbsolutePath());
Git git = new Git(repository);
AddCommand add = git.add();
add.addFilepattern(".");
add.call();
CommitCommand commit = git.commit();
commit.setMessage("Commit");
commit.call();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
throw new ServiceNotEnabledException();
}
}
private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException {
private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException, ServiceNotEnabledException {
if (!snapshotDBAPI.repositoryExists(name)) {
throw new RepositoryNotFoundException(name);
}

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.bridge.RepositorySource;
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
@ -22,7 +23,7 @@ public class SnapshotRepositoryBuilder implements RepositorySource {
}
@Override
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException {
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException, ServiceNotEnabledException {
File repositoryDirectory = new File(rootDirectory.getAbsolutePath(), name);
Repository repository = null;

View file

@ -1,37 +0,0 @@
package uk.ac.ic.wlgitbridge.writelatex.api;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by Winston on 03/11/14.
*/
public class DummySnapshotDBAPI implements SnapshotDBAPI {
private Map<String, Map<String, Integer>> projects;
public DummySnapshotDBAPI() {
projects = new HashMap<String, Map<String, Integer>>();
}
private void initTestData() {
}
@Override
public boolean repositoryExists(String name) {
return projects.containsKey(name);
}
@Override
public List<Snapshot> getSnapshotsToAddToRepository(Repository repository) {
return new LinkedList<Snapshot>();
}
}

View file

@ -11,6 +11,6 @@ import java.util.List;
public interface SnapshotDBAPI {
public boolean repositoryExists(String name);
public List<Snapshot> getSnapshotsToAddToRepository(Repository repository);
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable;
}

View file

@ -19,4 +19,9 @@ public class SnapshotGetDocResult extends Result {
public void fromJSON(JsonElement json) {
versionID = json.getAsJsonObject().get("latestVerId").getAsInt();
}
public int getVersionID() {
return versionID;
}
}

View file

@ -7,6 +7,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* Created by Winston on 06/11/14.
@ -42,6 +43,16 @@ public class SnapshotData implements JSONSource {
for (JsonElement json : jsonArray) {
atts.add(new WLAttachment(json));
}
System.out.println(atts);
}
public void writeAll(String repoDir) throws InterruptedException, ExecutionException, IOException {
for (WLFile src : srcs) {
src.writeToDisk(repoDir);
}
for (WLFile att : atts) {
att.writeToDisk(repoDir);
}
}
}

View file

@ -10,8 +10,11 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
public static final String API_CALL = "/snapshots";
private int versionID;
public SnapshotGetForVersionRequest(String projectName, int versionID) {
super(projectName, API_CALL + "/" + versionID);
this.versionID = versionID;
}
@Override
@ -19,4 +22,8 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest<SnapshotGet
return new SnapshotGetForVersionResult(this, json);
}
public int getVersionID() {
return versionID;
}
}

View file

@ -25,4 +25,8 @@ public class SnapshotGetForVersionResult extends Result {
snapshotData = new SnapshotData(json);
}
public SnapshotData getSnapshotData() {
return snapshotData;
}
}

View file

@ -5,7 +5,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
import java.io.IOException;
import java.io.*;
import java.util.concurrent.ExecutionException;
/**
@ -43,4 +43,36 @@ public class WLFile implements JSONSource {
path = jsonArray.get(1).getAsString();
}
public void writeToDisk(String repoDir) throws IOException, ExecutionException, InterruptedException {
System.out.println("write to " + repoDir);
File file = new File(repoDir, path);
file.getParentFile().mkdirs();
file.createNewFile();
OutputStream out = new FileOutputStream(file);
out.write(getContents());
out.close();
}
// @Override
// public String toString() {
// try {
// File file = new File("/Users/Roxy/git-test-files/" + path);
// file.getParentFile().mkdirs();
// file.createNewFile();
// OutputStream out = new FileOutputStream(file);
// out.write(getContents());
// out.close();
// return "{ path: " + path + ", contents: " + getContents().toString() + " }";
// } catch (ExecutionException e) {
// e.printStackTrace();
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return "Exception";
// }
}

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
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;
@ -21,7 +22,10 @@ public class SnapshotGetSavedVersResult extends Result {
@Override
public void fromJSON(JsonElement json) {
savedVers = new Gson().fromJson(json, LinkedList.class);
savedVers = new LinkedList<SnapshotInfo>();
for (JsonElement elem : json.getAsJsonArray()) {
savedVers.add(new Gson().fromJson(elem.getAsJsonObject(), SnapshotInfo.class));
}
}
public List<SnapshotInfo> getSavedVers() {

View file

@ -1,6 +1,8 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.WLUser;
/**
* Created by Winston on 03/11/14.
@ -8,9 +10,14 @@ import com.google.gson.JsonElement;
public class Snapshot implements JSONModel {
private int versionID;
private String comment;
private WLUser user;
private SnapshotData data;
public Snapshot() {
public Snapshot(int versionID, SnapshotData data) {
this.comment = comment;
this.data = data;
}
@Override
@ -18,4 +25,15 @@ public class Snapshot implements JSONModel {
}
public void writeToDisk() {
}
public SnapshotData getData() {
return data;
}
public String getComment() {
return comment;
}
}

View file

@ -1,13 +1,18 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
* Created by Winston on 06/11/14.
*/
public class WLDataModel {
public class WLDataModel implements SnapshotDBAPI {
private final Map<String, WLProject> projects;
@ -15,8 +20,32 @@ public class WLDataModel {
this.projects = projects;
}
public void updateProjectWithName(String name) throws InterruptedException, ExecutionException, IOException {
public WLDataModel() {
projects = new HashMap<String, WLProject>();
}
public void updateProjectWithName(String name) throws Throwable {
if (!projects.containsKey(name)) {
projects.put(name, new WLProject(name));
}
System.out.println(projects);
System.out.println(projects.get(name));
System.out.println(name);
projects.get(name).update();
}
@Override
public boolean repositoryExists(String name) {
if (!projects.containsKey(name)) {
projects.put(name, new WLProject(name));
}
return projects.containsKey(name);
}
@Override
public List<Snapshot> getSnapshotsToAddToProject(String name) throws Throwable {
updateProjectWithName(name);
return projects.get(name).getSnapshotsToAdd();
}
}

View file

@ -3,8 +3,12 @@ package uk.ac.ic.wlgitbridge.writelatex.model;
import com.google.gson.JsonElement;
import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocResult;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
import java.io.IOException;
import java.util.*;
@ -20,6 +24,9 @@ public class WLProject implements JSONModel {
private final Map<Integer, Snapshot> snapshots;
private final SortedSet<Integer> versions;
private int latestVersionID;
private List<Snapshot> snapshotsToAdd;
private SortedSet<Integer> idsToUpdate;
private HashMap<Integer, SnapshotInfo> msg;
public WLProject(String name) {
this.name = name;
@ -33,18 +40,25 @@ public class WLProject implements JSONModel {
}
public void update() throws InterruptedException, ExecutionException, IOException {
public void update() throws Throwable {
getNew();
}
private boolean getNew() throws InterruptedException, ExecutionException, IOException {
Request getDoc = new SnapshotGetDocRequest(name);
Request getSavedVers = new SnapshotGetSavedVersRequest(name);
private boolean getNew() throws Throwable {
SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(name);
SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(name);
getDoc.request();
getSavedVers.request();
List<Integer> ids = new LinkedList<Integer>();
List<Integer> fetchedIDs = new LinkedList<Integer>();
fetchedIDs.add(getDoc.getResult().getVersionID());
for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
msg = new HashMap<Integer, SnapshotInfo>();
msg.put(snapshotInfo.getVersionId(), snapshotInfo);
fetchedIDs.add(snapshotInfo.getVersionId());
}
boolean result = false;
@ -52,10 +66,10 @@ public class WLProject implements JSONModel {
// ids.addAll(getLatestVersionIDs(getSavedVers.getResult()));
List<Integer> idsToUpdate = new LinkedList<Integer>();
idsToUpdate = new TreeSet<Integer>();
boolean hasNew = false;
for (Integer id : ids) {
for (Integer id : fetchedIDs) {
boolean contains = versions.contains(id);
result = result || contains;
if (!contains) {
@ -68,14 +82,27 @@ public class WLProject implements JSONModel {
return result;
}
private void updateIDs(List<Integer> idsToUpdate) {
List<Request> requests = new LinkedList<Request>();
private void updateIDs(SortedSet<Integer> idsToUpdate) throws Throwable {
List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
for (int id : idsToUpdate) {
SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(name, id);
requests.add(request);
request.request();
}
for (SnapshotGetForVersionRequest request : requests) {
SnapshotGetForVersionResult result = request.getResult();
SnapshotData data = result.getSnapshotData();
Snapshot snapshot = new Snapshot(request.getVersionID(), data);
snapshots.put(request.getVersionID(), snapshot);
}
snapshotsToAdd = new LinkedList<Snapshot>();
for (int id : idsToUpdate) {
snapshotsToAdd.add(snapshots.get(id));
}
}
public List<Snapshot> getSnapshotsToAdd() {
return snapshotsToAdd;
}
}

View file

@ -1,15 +0,0 @@
package uk.ac.ic.wlgitbridge.test.writelatex.api;
import org.junit.Test;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetForVersionRequestTests {
@Test
public void nothingToTest() {
}
}

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.test.writelatex.api;
package uk.ac.ic.wlgitbridge.test.writelatex.api.request.base;
import org.junit.Test;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.test.writelatex.api;
package uk.ac.ic.wlgitbridge.test.writelatex.api.request.getdoc;
import org.junit.Test;

View file

@ -0,0 +1,25 @@
package uk.ac.ic.wlgitbridge.test.writelatex.api.request.getforversion;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult;
/**
* Created by Winston on 06/11/14.
*/
public class SnapshotGetForVersionRequestTests {
@Test
public void nothingToTest() {
SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest("1826rqgsdb", 76);
request.request();
try {
SnapshotGetForVersionResult result = request.getResult();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

View file

@ -22,6 +22,8 @@ public class WLProjectTests {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}