WLBridgedProject.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package uk.ac.ic.wlgitbridge.bridge;
  2. import org.eclipse.jgit.api.Git;
  3. import org.eclipse.jgit.api.errors.GitAPIException;
  4. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  5. import org.eclipse.jgit.lib.PersonIdent;
  6. import org.eclipse.jgit.lib.Repository;
  7. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  8. import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
  9. import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
  10. import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
  11. import java.io.IOException;
  12. import java.util.List;
  13. import java.util.TimeZone;
  14. /**
  15. * Created by Winston on 05/11/14.
  16. */
  17. public class WLBridgedProject {
  18. private final Repository repository;
  19. private final String name;
  20. private final WriteLatexDataSource writeLatexDataSource;
  21. public WLBridgedProject(Repository repository, String name, WriteLatexDataSource writeLatexDataSource) {
  22. this.repository = repository;
  23. this.name = name;
  24. this.writeLatexDataSource = writeLatexDataSource;
  25. }
  26. public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException, FailedConnectionException {
  27. writeLatexDataSource.lockForProject(name);
  28. if (repository.getObjectDatabase().exists()) {
  29. updateRepositoryFromSnapshots(repository);
  30. } else {
  31. buildRepositoryFromScratch(repository);
  32. }
  33. writeLatexDataSource.unlockForProject(name);
  34. }
  35. private void updateRepositoryFromSnapshots(Repository repository) throws ServiceNotEnabledException, RepositoryNotFoundException, FailedConnectionException {
  36. List<WritableRepositoryContents> writableRepositories;
  37. try {
  38. writableRepositories = writeLatexDataSource.getWritableRepositories(name);
  39. } catch (InvalidProjectException e) {
  40. throw new RepositoryNotFoundException(name);
  41. }
  42. try {
  43. for (WritableRepositoryContents contents : writableRepositories) {
  44. contents.write();
  45. Git git = new Git(repository);
  46. git.add().addFilepattern(".").call();
  47. git.commit().setAuthor(new PersonIdent(contents.getUserName(), contents.getUserEmail(), contents.getWhen(), TimeZone.getDefault()))
  48. .setMessage(contents.getCommitMessage())
  49. .call();
  50. System.out.println(repository.getDirectory());
  51. WLFileStore.deleteInDirectoryApartFrom(contents.getDirectory(), ".git");
  52. }
  53. } catch (GitAPIException e) {
  54. throw new ServiceNotEnabledException();
  55. } catch (IOException e) {
  56. throw new ServiceNotEnabledException();
  57. }
  58. }
  59. private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException, ServiceNotEnabledException, FailedConnectionException {
  60. if (!writeLatexDataSource.repositoryExists(name)) {
  61. throw new RepositoryNotFoundException(name);
  62. }
  63. try {
  64. repository.create();
  65. } catch (IOException e) {
  66. throw new ServiceNotEnabledException();
  67. }
  68. updateRepositoryFromSnapshots(repository);
  69. }
  70. }