Class UploadPack

java.lang.Object
org.eclipse.jgit.transport.UploadPack
All Implemented Interfaces:
Closeable, AutoCloseable

public class UploadPack extends Object implements Closeable
Implements the server side of a fetch connection, transmitting objects.
  • Constructor Details

    • UploadPack

      public UploadPack(Repository copyFrom)
      Create a new pack upload for an open repository.
      Parameters:
      copyFrom - the source repository.
  • Method Details

    • getRepository

      public final Repository getRepository()
      Get the repository this upload is reading from.
      Returns:
      the repository this upload is reading from.
    • getRevWalk

      public final RevWalk getRevWalk()
      Get the RevWalk instance used by this connection.
      Returns:
      the RevWalk instance used by this connection.
    • getAdvertisedRefs

      public final Map<String,Ref> getAdvertisedRefs()
      Get refs which were advertised to the client.
      Returns:
      all refs which were advertised to the client. Only valid during the negotiation phase. Will return null if setAdvertisedRefs(Map) has not been called yet or if #sendPack() has been called.
    • setAdvertisedRefs

      public void setAdvertisedRefs(@Nullable Map<String,Ref> allRefs)
      Set the refs advertised by this UploadPack.

      Intended to be called from a PreUploadHook.

      Parameters:
      allRefs - explicit set of references to claim as advertised by this UploadPack instance. This overrides any references that may exist in the source repository. The map is passed to the configured getRefFilter(). If null, assumes all refs were advertised.
    • getTimeout

      public int getTimeout()
      Get timeout (in seconds) before aborting an IO operation.
      Returns:
      timeout (in seconds) before aborting an IO operation.
    • setTimeout

      public void setTimeout(int seconds)
      Set the timeout before willing to abort an IO call.
      Parameters:
      seconds - number of seconds to wait (with no data transfer occurring) before aborting an IO read or write operation with the connected client.
    • isBiDirectionalPipe

      public boolean isBiDirectionalPipe()
      Whether this class expects a bi-directional pipe opened between the client and itself.
      Returns:
      true if this class expects a bi-directional pipe opened between the client and itself. The default is true.
    • setBiDirectionalPipe

      public void setBiDirectionalPipe(boolean twoWay)
      Set whether this class will assume the socket is a fully bidirectional pipe between the two peers
      Parameters:
      twoWay - if true, this class will assume the socket is a fully bidirectional pipe between the two peers and takes advantage of that by first transmitting the known refs, then waiting to read commands. If false, this class assumes it must read the commands before writing output and does not perform the initial advertising.
    • getRequestPolicy

      public UploadPack.RequestPolicy getRequestPolicy()
      Get policy used by the service to validate client requests
      Returns:
      policy used by the service to validate client requests, or null for a custom request validator.
    • setRequestPolicy

      public void setRequestPolicy(UploadPack.RequestPolicy policy)
      Set the policy used to enforce validation of a client's want list.
      Parameters:
      policy - the policy used to enforce validation of a client's want list. By default the policy is UploadPack.RequestPolicy.ADVERTISED, which is the Git default requiring clients to only ask for an object that a reference directly points to. This may be relaxed to UploadPack.RequestPolicy.REACHABLE_COMMIT or UploadPack.RequestPolicy.REACHABLE_COMMIT_TIP when callers have setBiDirectionalPipe(boolean) set to false. Overrides any policy specified in a TransferConfig.
    • setRequestValidator

      public void setRequestValidator(@Nullable UploadPack.RequestValidator validator)
      Set custom validator for client want list.
      Parameters:
      validator - custom validator for client want list.
      Since:
      3.1
    • getAdvertiseRefsHook

      public AdvertiseRefsHook getAdvertiseRefsHook()
      Get the hook used while advertising the refs to the client.
      Returns:
      the hook used while advertising the refs to the client.
    • getRefFilter

      public RefFilter getRefFilter()
      Get the filter used while advertising the refs to the client.
      Returns:
      the filter used while advertising the refs to the client.
    • setAdvertiseRefsHook

      public void setAdvertiseRefsHook(@Nullable AdvertiseRefsHook advertiseRefsHook)
      Set the hook used while advertising the refs to the client.

      If the AdvertiseRefsHook chooses to call setAdvertisedRefs(Map), only refs set by this hook and selected by the RefFilter will be shown to the client.

      Parameters:
      advertiseRefsHook - the hook; may be null to show all refs.
    • setProtocolV2Hook

      public void setProtocolV2Hook(@Nullable ProtocolV2Hook hook)
      Set the protocol V2 hook.
      Parameters:
      hook - the hook; if null no special actions are taken.
      Since:
      5.1
    • getProtocolV2Hook

      public ProtocolV2Hook getProtocolV2Hook()
      Get the currently installed protocol v2 hook.
      Returns:
      the hook or a default implementation if none installed.
      Since:
      5.5
    • setRefFilter

      public void setRefFilter(@Nullable RefFilter refFilter)
      Set the filter used while advertising the refs to the client.

      Only refs allowed by this filter will be sent to the client. The filter is run against the refs specified by the AdvertiseRefsHook (if applicable). If null or not set, uses the filter implied by the TransferConfig.

      Parameters:
      refFilter - the filter; may be null to show all refs.
    • getPreUploadHook

      public PreUploadHook getPreUploadHook()
      Get the configured pre upload hook.
      Returns:
      the configured pre upload hook.
    • setPreUploadHook

      public void setPreUploadHook(@Nullable PreUploadHook hook)
      Set the hook that controls how this instance will behave.
      Parameters:
      hook - the hook; if null no special actions are taken.
    • getPostUploadHook

      public PostUploadHook getPostUploadHook()
      Get the configured post upload hook.
      Returns:
      the configured post upload hook.
      Since:
      4.1
    • setPostUploadHook

      public void setPostUploadHook(@Nullable PostUploadHook hook)
      Set the hook for post upload actions (logging, repacking).
      Parameters:
      hook - the hook; if null no special actions are taken.
      Since:
      4.1
    • setPackConfig

      public void setPackConfig(@Nullable PackConfig pc)
      Set the configuration used by the pack generator.
      Parameters:
      pc - configuration controlling packing parameters. If null the source repository's settings will be used.
    • setTransferConfig

      public void setTransferConfig(@Nullable TransferConfig tc)
      Set configuration controlling transfer options.
      Parameters:
      tc - configuration controlling transfer options. If null the source repository's settings will be used.
      Since:
      3.1
    • isSideBand

      public boolean isSideBand() throws RequestNotYetReadException
      Check whether the client expects a side-band stream.
      Returns:
      true if the client has advertised a side-band capability, false otherwise.
      Throws:
      RequestNotYetReadException - if the client's request has not yet been read from the wire, so we do not know if they expect side-band. Note that the client may have already written the request, it just has not been read.
    • setExtraParameters

      public void setExtraParameters(Collection<String> params)
      Set the Extra Parameters provided by the client.

      These are parameters passed by the client through a side channel such as the Git-Protocol HTTP header, to allow a client to request a newer response format while remaining compatible with older servers that do not understand different request formats.

      Parameters:
      params - parameters supplied by the client, split at colons or NUL bytes.
      Since:
      5.0
    • setCachedPackUriProvider

      public void setCachedPackUriProvider(@Nullable CachedPackUriProvider p)
      Set provider of cached pack URIs
      Parameters:
      p - provider of URIs corresponding to cached packs (to support the packfile URIs feature)
      Since:
      5.5
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • upload

      public void upload(InputStream input, OutputStream output, @Nullable OutputStream messages) throws IOException
      Execute the upload task on the socket.

      Same as uploadWithExceptionPropagation(java.io.InputStream, java.io.OutputStream, java.io.OutputStream) except that the thrown exceptions are handled in the method, and the error messages are sent to the clients.

      Call this method if the caller does not have an error handling mechanism. Call uploadWithExceptionPropagation(java.io.InputStream, java.io.OutputStream, java.io.OutputStream) if the caller wants to have its own error handling mechanism.

      Parameters:
      input - input stream
      output - output stream
      messages - stream for messages
      Throws:
      IOException - if an IO error occurred
    • uploadWithExceptionPropagation

      public void uploadWithExceptionPropagation(InputStream input, OutputStream output, @Nullable OutputStream messages) throws ServiceMayNotContinueException, IOException
      Execute the upload task on the socket.

      If the client passed extra parameters (e.g., "version=2") through a side channel, the caller must call setExtraParameters first to supply them. Callers of this method should call close() to terminate the internal interrupt timer thread. If the caller fails to terminate the thread, it will (eventually) terminate itself when the InterruptTimer instance is garbage collected.

      Parameters:
      input - raw input to read client commands from. Caller must ensure the input is buffered, otherwise read performance may suffer.
      output - response back to the Git network client, to write the pack data onto. Caller must ensure the output is buffered, otherwise write performance may suffer.
      messages - secondary "notice" channel to send additional messages out through. When run over SSH this should be tied back to the standard error channel of the command execution. For most other network connections this should be null.
      Throws:
      ServiceMayNotContinueException - thrown if one of the hooks throws this.
      IOException - thrown if the server or the client I/O fails, or there's an internal server error.
      Since:
      5.6
    • getStatistics

      public PackStatistics getStatistics()
      Get the PackWriter's statistics if a pack was sent to the client.
      Returns:
      statistics about pack output, if a pack was sent. Null if no pack was sent, such as during the negotiation phase of a smart HTTP connection, or if the client was already up-to-date.
      Since:
      4.1
    • sendAdvertisedRefs

      public void sendAdvertisedRefs(RefAdvertiser adv) throws IOException, ServiceMayNotContinueException
      Generate an advertisement of available refs and capabilities.
      Parameters:
      adv - the advertisement formatter.
      Throws:
      IOException - the formatter failed to write an advertisement.
      ServiceMayNotContinueException - the hook denied advertisement.
    • sendAdvertisedRefs

      public void sendAdvertisedRefs(RefAdvertiser adv, @Nullable String serviceName) throws IOException, ServiceMayNotContinueException
      Generate an advertisement of available refs and capabilities.
      Parameters:
      adv - the advertisement formatter.
      serviceName - if not null, also output "# service=serviceName" followed by a flush packet before the advertisement. This is required in v0 of the HTTP protocol, described in Git's Documentation/technical/http-protocol.txt.
      Throws:
      IOException - the formatter failed to write an advertisement.
      ServiceMayNotContinueException - the hook denied advertisement.
      Since:
      5.0
    • sendMessage

      public void sendMessage(String what)
      Send a message to the client, if it supports receiving them.

      If the client doesn't support receiving messages, the message will be discarded, with no other indication to the caller or to the client.

      Parameters:
      what - string describing the problem identified by the hook. The string must not end with an LF, and must not contain an LF.
      Since:
      3.1
    • getMessageOutputStream

      public OutputStream getMessageOutputStream()
      Get an underlying stream for sending messages to the client
      Returns:
      an underlying stream for sending messages to the client, or null.
      Since:
      3.1
    • getDepth

      public int getDepth()
      Returns the clone/fetch depth. Valid only after calling recvWants(). A depth of 1 means return only the wants.
      Returns:
      the depth requested by the client, or 0 if unbounded.
      Since:
      4.0
    • getFilterBlobLimit

      @Deprecated public final long getFilterBlobLimit()
      Deprecated.
      Use getFilterSpec() instead
      Deprecated synonym for getFilterSpec().getBlobLimit().
      Returns:
      filter blob limit requested by the client, or -1 if no limit
      Since:
      5.3
    • getFilterSpec

      public final FilterSpec getFilterSpec()
      Returns the filter spec for the current request. Valid only after calling recvWants(). This may be a no-op filter spec, but it won't be null.
      Returns:
      filter requested by the client
      Since:
      5.4
    • getPeerUserAgent

      public String getPeerUserAgent()
      Get the user agent of the client.

      If the client is new enough to use agent= capability that value will be returned. Older HTTP clients may also supply their version using the HTTP User-Agent header. The capability overrides the HTTP header if both are available.

      When an HTTP request has been received this method returns the HTTP User-Agent header value until capabilities have been parsed.

      Returns:
      user agent supplied by the client. Available only if the client is new enough to advertise its user agent.
      Since:
      4.0
    • getClientSID

      @Nullable public String getClientSID()
      Get the session ID if received from the client.
      Returns:
      The session ID if it has been received from the client.
      Since:
      6.4