Class BasePackConnection

java.lang.Object
org.eclipse.jgit.transport.BaseConnection
org.eclipse.jgit.transport.BasePackConnection
All Implemented Interfaces:
AutoCloseable, Connection
Direct Known Subclasses:
BasePackFetchConnection, BasePackPushConnection

abstract class BasePackConnection extends BaseConnection
Base helper class for pack-based operations implementations. Provides partial implementation of pack-protocol - refs advertising and capabilities support, and some other helper methods.
See Also:
  • Field Details

    • CAPABILITY_SYMREF_PREFIX

      protected static final String CAPABILITY_SYMREF_PREFIX
      See Also:
    • local

      protected final Repository local
      The repository this transport fetches into, or pushes out of.
    • uri

      protected final URIish uri
      Remote repository location.
    • transport

      protected final Transport transport
      A transport connected to uri.
    • timeoutIn

      protected TimeoutInputStream timeoutIn
      Low-level input stream, if a timeout was configured.
    • timeoutOut

      protected TimeoutOutputStream timeoutOut
      Low-level output stream, if a timeout was configured.
    • myTimer

      private InterruptTimer myTimer
      Timer to manage timeoutIn and timeoutOut.
    • in

      protected InputStream in
      Input stream reading from the remote.
    • out

      protected OutputStream out
      Output stream sending to the remote.
    • pckIn

      protected PacketLineIn pckIn
      Packet line decoder around in.
    • pckOut

      protected PacketLineOut pckOut
      Packet line encoder around out.
    • outNeedsEnd

      protected boolean outNeedsEnd
      Send PacketLineOut.end() before closing out?
    • statelessRPC

      protected boolean statelessRPC
      True if this is a stateless RPC connection.
    • remoteCapabilities

      private final Map<String,String> remoteCapabilities
      Capability tokens advertised by the remote side.
    • additionalHaves

      protected final Set<ObjectId> additionalHaves
      Extra objects the remote has, but which aren't offered as refs.
    • protocol

  • Constructor Details

    • BasePackConnection

      BasePackConnection(PackTransport packTransport)
  • Method Details

    • getProtocolVersion

      TransferConfig.ProtocolVersion getProtocolVersion()
    • setProtocolVersion

      void setProtocolVersion(@NonNull TransferConfig.ProtocolVersion protocol)
    • init

      protected final void init(InputStream myIn, OutputStream myOut)
      Configure this connection with the directional pipes.
      Parameters:
      myIn - input stream to receive data from the peer. Caller must ensure the input is buffered, otherwise read performance may suffer.
      myOut - output stream to transmit data to the peer. Caller must ensure the output is buffered, otherwise write performance may suffer.
    • readAdvertisedRefs

      protected boolean readAdvertisedRefs() throws TransportException
      Reads the advertised references through the initialized stream.

      Subclass implementations may call this method only after setting up the input and output streams with init(InputStream, OutputStream).

      If any errors occur, this connection is automatically closed by invoking close() and the exception is wrapped (if necessary) and thrown as a TransportException.

      Returns:
      true if the refs were read; false otherwise indicating that lsRefs(java.util.Collection<org.eclipse.jgit.transport.RefSpec>, java.lang.String...) must be called
      Throws:
      TransportException - the reference list could not be scanned.
    • readLine

      private String readLine() throws IOException
      Throws:
      IOException
    • readAdvertisedRefsImpl

      private boolean readAdvertisedRefsImpl() throws IOException
      Throws:
      IOException
    • lsRefs

      protected void lsRefs(Collection<RefSpec> refSpecs, String... additionalPatterns) throws TransportException
      Issue a protocol V2 ls-refs command and read its response.
      Parameters:
      refSpecs - to produce ref prefixes from if the server supports git protocol V2
      additionalPatterns - to use for ref prefixes if the server supports git protocol V2
      Throws:
      TransportException - if the command could not be run or its output not be read
    • lsRefsImpl

      private void lsRefsImpl(Collection<RefSpec> refSpecs, String... additionalPatterns) throws IOException
      Throws:
      IOException
    • getRefPrefixes

      private Collection<String> getRefPrefixes(Collection<RefSpec> refSpecs, String... additionalPatterns)
    • readCapabilitiesV2

      private void readCapabilitiesV2() throws IOException
      Throws:
      IOException
    • addCapability

      private void addCapability(String capability)
    • toId

      private ObjectId toId(String line, String value) throws PackProtocolException
      Throws:
      PackProtocolException
    • processLineV1

      private void processLineV1(String name, ObjectId id, Map<String,Ref> avail) throws IOException
      Throws:
      IOException
    • processLineV2

      private void processLineV2(String line, ObjectId id, String rest, Map<String,Ref> avail, Map<String,String> symRefs) throws IOException
      Throws:
      IOException
    • updateWithSymRefs

      static void updateWithSymRefs(Map<String,Ref> refMap, Map<String,String> symRefs)
      Updates the given refMap with SymbolicRefs defined by the given symRefs.

      For each entry, symRef, in symRefs, whose value is a key in refMap, adds a new entry to refMap with that same key and value of a new SymbolicRef with source=symRef.key and target=refMap.get(symRef.value), then removes that entry from symRefs.

      If refMap already contains an entry for symRef.key, it is replaced.

      For example, given:

       refMap.put("refs/heads/main", ref);
       symRefs.put("HEAD", "refs/heads/main");
       
      then:
       updateWithSymRefs(refMap, symRefs);
       
      has the effect of:
       refMap.put("HEAD",
                      new SymbolicRef("HEAD", refMap.get(symRefs.remove("HEAD"))))
       

      Any entry in symRefs whose value is not a key in refMap is ignored. Any circular symRefs are ignored.

      Upon completion, symRefs will contain only any unresolvable entries.

      Parameters:
      refMap - a non-null, modifiable, Map to update, and the provider of symref targets.
      symRefs - a non-null, modifiable, Map of symrefs.
      Throws:
      NullPointerException - if refMap or symRefs is null
    • noRepository

      protected TransportException noRepository(Throwable cause)
      Create an exception to indicate problems finding a remote repository. The caller is expected to throw the returned exception. Subclasses may override this method to provide better diagnostics.
      Parameters:
      cause - root cause exception
      Returns:
      a TransportException saying a repository cannot be found and possibly why.
    • isCapableOf

      protected boolean isCapableOf(String option)
      Whether this option is supported
      Parameters:
      option - option string
      Returns:
      whether this option is supported
    • wantCapability

      protected boolean wantCapability(StringBuilder b, String option)
      Request capability
      Parameters:
      b - buffer
      option - option we want
      Returns:
      true if the requested option is supported
    • getCapability

      protected String getCapability(String option)
      Return a capability value.
      Parameters:
      option - to get
      Returns:
      the value stored, if any.
    • addUserAgentCapability

      protected void addUserAgentCapability(StringBuilder b)
      Add user agent capability
      Parameters:
      b - a StringBuilder object.
    • getPeerUserAgent

      public String getPeerUserAgent()
      User agent advertised by the remote server. User agent advertised by the remote server.
      Specified by:
      getPeerUserAgent in interface Connection
      Overrides:
      getPeerUserAgent in class BaseConnection
      Returns:
      agent (version of Git) running on the remote server. Null if the server does not advertise this version.
    • duplicateAdvertisement

      private PackProtocolException duplicateAdvertisement(String name)
    • invalidRefAdvertisementLine

      private PackProtocolException invalidRefAdvertisementLine(String line)
    • close

      public void close()

      Close any resources used by this connection.

      If the remote repository is contacted by a network socket this method must close that network socket, disconnecting the two peers. If the remote repository is actually local (same system) this method must close any open file handles used to read the "remote" repository.

      If additional messages were produced by the remote peer, these should still be retained in the connection instance for Connection.getMessages().

      AutoClosable.close() declares that it throws Exception. Implementers shouldn't throw checked exceptions. This override narrows the signature to prevent them from doing so.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Connection
      Specified by:
      close in class BaseConnection
    • endOut

      protected void endOut()
      Tell the peer we are disconnecting, if it cares to know.