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

This commit is contained in:
Winston Li 2015-02-22 02:10:54 +00:00
parent be806d293c
commit 70d9c0e31d
5 changed files with 32 additions and 30 deletions

View file

@ -46,21 +46,8 @@ public class WLBridgedProject {
} }
private void updateRepositoryFromSnapshots(Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException { private void updateRepositoryFromSnapshots(Repository repository) throws RepositoryNotFoundException, ServiceMayNotContinueException {
List<WritableRepositoryContents> writableRepositories;
try { try {
writableRepositories = writeLatexDataSource.getWritableRepositories(name, repository); 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");
}
} catch (InvalidProjectException e) { } catch (InvalidProjectException e) {
throw new RepositoryNotFoundException(name); throw new RepositoryNotFoundException(name);
} catch (SnapshotPostException e) { } catch (SnapshotPostException e) {

View file

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

View file

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

View file

@ -1,10 +1,12 @@
package uk.ac.ic.wlgitbridge.writelatex.model; 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 org.eclipse.jgit.lib.Repository;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback; import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.bridge.RawDirectory; import uk.ac.ic.wlgitbridge.bridge.RawDirectory;
import uk.ac.ic.wlgitbridge.bridge.RawFile; 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.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.CandidateSnapshot; import uk.ac.ic.wlgitbridge.writelatex.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher; import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher;
@ -18,6 +20,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.TimeZone;
/** /**
* Created by Winston on 06/11/14. * Created by Winston on 06/11/14.
@ -40,26 +43,36 @@ public class DataStore implements CandidateSnapshotCallback {
resourceFetcher = new ResourceFetcher(persistentStore); 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)); LinkedList<Snapshot> snapshots = snapshotFetcher.getSnapshotsForProjectAfterVersion(name, persistentStore.getLatestVersionForProject(name));
if (!snapshots.isEmpty()) { if (!snapshots.isEmpty()) {
persistentStore.setLatestVersionForProject(name, snapshots.getLast().getVersionID()); persistentStore.setLatestVersionForProject(name, snapshots.getLast().getVersionID());
} }
List<WritableRepositoryContents> commits = makeCommitsFromSnapshots(name, repository, snapshots); makeCommitsFromSnapshots(name, repository, snapshots);
return commits;
} }
private List<WritableRepositoryContents> makeCommitsFromSnapshots(String name, Repository repository, List<Snapshot> snapshots) throws IOException { private void makeCommitsFromSnapshots(String name, Repository repository, List<Snapshot> snapshots) throws IOException, GitAPIException {
List<WritableRepositoryContents> commits = new LinkedList<WritableRepositoryContents>();
for (Snapshot snapshot : snapshots) { for (Snapshot snapshot : snapshots) {
List<RawFile> files = new LinkedList<RawFile>(); List<RawFile> files = new LinkedList<RawFile>();
files.addAll(snapshot.getSrcs()); files.addAll(snapshot.getSrcs());
for (SnapshotAttachment snapshotAttachment : snapshot.getAtts()) { for (SnapshotAttachment snapshotAttachment : snapshot.getAtts()) {
files.add(resourceFetcher.get(name, snapshotAttachment.getUrl(), snapshotAttachment.getPath(), repository)); 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 { public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents) throws SnapshotPostException, IOException, FailedConnectionException {

View file

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