You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteHelperTransport.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package org.eclipse.jgit.integrate.remotehelper.internal;
  2. import org.eclipse.jgit.errors.NotSupportedException;
  3. import org.eclipse.jgit.errors.TransportException;
  4. import org.eclipse.jgit.integrate.remotehelper.RemoteHelperCapability;
  5. import org.eclipse.jgit.integrate.remotehelper.RemoteHelperCommand;
  6. import org.eclipse.jgit.integrate.remotehelper.RemoteHelperContext;
  7. import org.eclipse.jgit.integrate.remotehelper.RemoteHelperProvider;
  8. import org.eclipse.jgit.transport.FetchConnection;
  9. import org.eclipse.jgit.transport.PushConnection;
  10. import org.eclipse.jgit.transport.Transport;
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. public class RemoteHelperTransport extends RemoteHelperProvider {
  14. private Transport transport;
  15. public RemoteHelperTransport(RemoteHelperContext context) {
  16. super(context);
  17. Transport transport;
  18. try {
  19. transport = Transport.open(context.getRepository(), context.getUrl());
  20. } catch (Exception e) {
  21. throw new RuntimeException(e);
  22. }
  23. setTransport(transport);
  24. }
  25. @Override
  26. public RemoteHelperCommand getCommand(String name) {
  27. if ("fetch".equals(name)) {
  28. return new TransportFetchCommand(this);
  29. } else if ("list".equals(name)) {
  30. return new TransportListCommand(this);
  31. } else if ("push".equals(name)) {
  32. return new TransportPushCommand(this);
  33. }
  34. return null;
  35. }
  36. @Override
  37. public Collection<RemoteHelperCapability> getCapabilities() {
  38. ArrayList<RemoteHelperCapability> capabilities = new ArrayList<>();
  39. try {
  40. FetchConnection fetchConnection = getFetchConnection();
  41. PushConnection pushConnection = getPushConnection();
  42. if (fetchConnection != null) {
  43. fetchConnection.close();
  44. capabilities.add(RemoteHelperCapability.FETCH);
  45. }
  46. if (pushConnection != null) {
  47. pushConnection.close();
  48. capabilities.add(RemoteHelperCapability.PUSH);
  49. }
  50. } catch (TransportException e) {
  51. throw new RuntimeException(e);
  52. }
  53. return capabilities;
  54. }
  55. public FetchConnection getFetchConnection() throws TransportException {
  56. try {
  57. return getTransport().openFetch();
  58. } catch (NotSupportedException e) {
  59. return null;
  60. }
  61. }
  62. public PushConnection getPushConnection() throws TransportException {
  63. try {
  64. return getTransport().openPush();
  65. } catch (NotSupportedException e) {
  66. return null;
  67. }
  68. }
  69. public void setTransport(Transport transport) {
  70. this.transport = transport;
  71. }
  72. public Transport getTransport() {
  73. if (transport == null) {
  74. throw new IllegalStateException("Transport was not initialized.");
  75. }
  76. return transport;
  77. }
  78. @Override
  79. public void close() {
  80. transport.close();
  81. }
  82. }