Class UploadPack
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Validator corresponding toUploadPack.RequestPolicy.ADVERTISED
.static final class
Validator corresponding toUploadPack.RequestPolicy.ANY
.private static interface
static class
Deprecated.private static interface
private class
static final class
Validator corresponding toUploadPack.RequestPolicy.REACHABLE_COMMIT
.static final class
Validator corresponding toUploadPack.RequestPolicy.REACHABLE_COMMIT_TIP
.static enum
Policy the server uses to validate client requestsstatic interface
Validator for client requests.private static class
private class
static final class
Validator corresponding toUploadPack.RequestPolicy.TIP
. -
Field Summary
FieldsModifier and TypeFieldDescriptionObjects we sent in our advertisement list.private AdvertiseRefsHook
Hook used while advertising the refs to the client.private boolean
Whether theadvertiseRefsHook
has been invoked.private boolean
Is the client connection a bi-directional socket or pipe?private CachedPackUriProvider
private boolean
Whether the client requested to use protocol V2 through a side channel (such as the Git-Protocol HTTP header).private final RevFlag
Marked on objects incommonBase
.Objects on both sides, these don't have to be sent.private FetchRequest
Request this instance is handling.private final Repository
Database we read the objects from.private UploadPack.ErrorWriter
private OutputStream
private GitProtocolConstants.MultiAck
private boolean
private Boolean
null ifcommonBase
should be examined again.private int
Commit time of the oldest common commit, in seconds.private PackConfig
Configuration to pass into the PackWriter.private PacketLineIn
private final RevFlag
Marked on objects both we and the client have.private PostUploadHook
Hook for taking post upload actions.private PreUploadHook
Hook handling the various upload phases.private ProtocolV2Hook
Hook used while processing Git protocol v2 requests.private InputStream
private RefFilter
Filter used while advertising the refs to the client.Refs eligible for advertising to the client, set usingsetAdvertisedRefs(java.util.Map<java.lang.String, org.eclipse.jgit.lib.Ref>)
.private UploadPack.RequestValidator
private final RevFlag
Objects where we found a path from the want list to a common base.private final RevFlagSet
private boolean
private PackStatistics
private int
Timeout in seconds to wait for client interaction.private InterruptTimer
Timer to managetimeout
.private TransferConfig
Configuration for various transfer options.(package private) String
Caller user agentprivate final RevWalk
Revision traversal support overdb
.private final RevFlag
Marked on objects the client has asked us to give them.Objects the client wants to obtain.Raw ObjectIds the client has asked for, before validating them. -
Constructor Summary
ConstructorsConstructorDescriptionUploadPack
(Repository copyFrom) Create a new pack upload for an open repository. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
private void
addTagChain
(RevObject o, PackWriter pw) private static void
checkNotAdvertisedWants
(UploadPack up, List<ObjectId> notAdvertisedWants, Collection<Ref> visibleRefs) private void
computeShallowsAndUnshallows
(FetchRequest req, UploadPack.IOConsumer<ObjectId> shallowFunc, UploadPack.IOConsumer<ObjectId> unshallowFunc, List<ObjectId> deepenNots) Read refs on behalf of the client.private void
fetchV2
(PacketLineOut pckOut) private Ref
Find a ref in the usual search path on behalf of the client.private static void
findSymrefs
(RefAdvertiser adv, Map<String, Ref> refs) Get refs which were advertised to the client.Get the hook used while advertising the refs to the client.int
getDepth()
Returns the clone/fetch depth.final long
Deprecated.UsegetFilterSpec()
insteadgetFilteredRefs
(Collection<String> refPrefixes) final FilterSpec
Returns the filter spec for the current request.Get an underlying stream for sending messages to the clientGet the user agent of the client.Get the configured post upload hook.Get the configured pre upload hook.Get the currently installed protocol v2 hook.Get the filter used while advertising the refs to the client.final Repository
Get the repository this upload is reading from.Get policy used by the service to validate client requestsfinal RevWalk
Get the RevWalk instance used by this connection.Get the PackWriter's statistics if a pack was sent to the client.int
Get timeout (in seconds) before aborting an IO operation.importantRefsFirst
(Collection<Ref> visibleRefs) boolean
Whether this class expects a bi-directional pipe opened between the client and itself.boolean
Check whether the client expects a side-band stream.private void
lsRefsV2
(PacketLineOut pckOut) Returns the specified references.private boolean
negotiate
(FetchRequest req, PackStatistics.Accumulator accumulator, PacketLineOut pckOut) objectIdsToRevObjects
(RevWalk walk, Iterable<ObjectId> objectIds) private static RevCommit
objectIdToRevCommit
(RevWalk walk, ObjectId objectId) Translate an object id to a RevCommit.private static RevObject
objectIdToRevObject
(RevWalk walk, ObjectId objectId) Translate an object id to a RevObject.private boolean
private boolean
private void
parseWants
(PackStatistics.Accumulator accumulator) private ObjectId
processHaveLines
(List<ObjectId> peerHas, ObjectId last, PacketLineOut out, PackStatistics.Accumulator accumulator) refIdSet
(Collection<Ref> refs) private static ObjectId
refToObjectId
(Ref ref) void
Generate an advertisement of available refs and capabilities.void
sendAdvertisedRefs
(RefAdvertiser adv, String serviceName) Generate an advertisement of available refs and capabilities.void
sendMessage
(String what) Send a message to the client, if it supports receiving them.private void
sendPack
(ProgressMonitor pm, PacketLineOut pckOut, OutputStream packOut, FetchRequest req, PackStatistics.Accumulator accumulator, Collection<Ref> allTags, List<ObjectId> unshallowCommits, List<ObjectId> deepenNots) Send the requested objects to the client.private void
sendPack
(PackStatistics.Accumulator accumulator, FetchRequest req, Collection<Ref> allTags, List<ObjectId> unshallowCommits, List<ObjectId> deepenNots, PacketLineOut pckOut) Send the requested objects to the client.private boolean
serveOneCommandV2
(PacketLineOut pckOut) private void
service
(PacketLineOut pckOut) private void
serviceV2
(PacketLineOut pckOut) void
setAdvertisedRefs
(Map<String, Ref> allRefs) Set the refs advertised by this UploadPack.void
setAdvertiseRefsHook
(AdvertiseRefsHook advertiseRefsHook) Set the hook used while advertising the refs to the client.void
setBiDirectionalPipe
(boolean twoWay) Set whether this class will assume the socket is a fully bidirectional pipe between the two peersvoid
void
setExtraParameters
(Collection<String> params) Set the Extra Parameters provided by the client.void
Set the configuration used by the pack generator.void
Set the hook for post upload actions (logging, repacking).void
Set the hook that controls how this instance will behave.void
Set the protocol V2 hook.void
setRefFilter
(RefFilter refFilter) Set the filter used while advertising the refs to the client.void
Set the policy used to enforce validation of a client's want list.void
setRequestValidator
(UploadPack.RequestValidator validator) Set custom validator for client want list.void
setTimeout
(int seconds) Set the timeout before willing to abort an IO call.void
Set configuration controlling transfer options.void
upload
(InputStream input, OutputStream output, OutputStream messages) Execute the upload task on the socket.void
uploadWithExceptionPropagation
(InputStream input, OutputStream output, OutputStream messages) Execute the upload task on the socket.private boolean
private void
verifyClientShallow
(Set<ObjectId> shallowCommits) private void
wantedRefs
(FetchV2Request req) private boolean
wantSatisfied
(RevObject want)
-
Field Details
-
db
Database we read the objects from. -
walk
Revision traversal support overdb
. -
packConfig
Configuration to pass into the PackWriter. -
transferConfig
Configuration for various transfer options. -
timeout
private int timeoutTimeout in seconds to wait for client interaction. -
biDirectionalPipe
private boolean biDirectionalPipeIs the client connection a bi-directional socket or pipe?If true, this class assumes it can perform multiple read and write cycles with the client over the input and output streams. This matches the functionality available with a standard TCP/IP connection, or a local operating system or in-memory pipe.
If false, this class runs in a read everything then output results mode, making it suitable for single round-trip systems RPCs such as HTTP.
-
timer
Timer to managetimeout
. -
clientRequestedV2
private boolean clientRequestedV2Whether the client requested to use protocol V2 through a side channel (such as the Git-Protocol HTTP header). -
rawIn
-
rawOut
-
pckIn
-
msgOut
-
errOut
-
refs
Refs eligible for advertising to the client, set usingsetAdvertisedRefs(java.util.Map<java.lang.String, org.eclipse.jgit.lib.Ref>)
. -
protocolV2Hook
Hook used while processing Git protocol v2 requests. -
advertiseRefsHook
Hook used while advertising the refs to the client. -
advertiseRefsHookCalled
private boolean advertiseRefsHookCalledWhether theadvertiseRefsHook
has been invoked. -
refFilter
Filter used while advertising the refs to the client. -
preUploadHook
Hook handling the various upload phases. -
postUploadHook
Hook for taking post upload actions. -
userAgent
String userAgentCaller user agent -
wantIds
Raw ObjectIds the client has asked for, before validating them. -
wantAll
Objects the client wants to obtain. -
commonBase
Objects on both sides, these don't have to be sent. -
oldestTime
private int oldestTimeCommit time of the oldest common commit, in seconds. -
okToGiveUp
null ifcommonBase
should be examined again. -
sentReady
private boolean sentReady -
advertised
Objects we sent in our advertisement list. -
WANT
Marked on objects the client has asked us to give them. -
PEER_HAS
Marked on objects both we and the client have. -
COMMON
Marked on objects incommonBase
. -
SATISFIED
Objects where we found a path from the want list to a common base. -
SAVE
-
requestValidator
-
multiAck
-
noDone
private boolean noDone -
statistics
-
currentRequest
Request this instance is handling. We need to keep a reference to it forpre upload hooks
. They receive a reference this instance and invoke methods like getDepth() to get information about the request. -
cachedPackUriProvider
-
-
Constructor Details
-
UploadPack
Create a new pack upload for an open repository.- Parameters:
copyFrom
- the source repository.
-
-
Method Details
-
getRepository
Get the repository this upload is reading from.- Returns:
- the repository this upload is reading from.
-
getRevWalk
Get the RevWalk instance used by this connection.- Returns:
- the RevWalk instance used by this connection.
-
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
ifsetAdvertisedRefs(Map)
has not been called yet or if#sendPack()
has been called.
-
setAdvertisedRefs
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 configuredgetRefFilter()
. 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
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
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 isUploadPack.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 toUploadPack.RequestPolicy.REACHABLE_COMMIT
orUploadPack.RequestPolicy.REACHABLE_COMMIT_TIP
when callers havesetBiDirectionalPipe(boolean)
set to false. Overrides any policy specified in aTransferConfig
.
-
setRequestValidator
Set custom validator for client want list.- Parameters:
validator
- custom validator for client want list.- Since:
- 3.1
-
getAdvertiseRefsHook
Get the hook used while advertising the refs to the client.- Returns:
- the hook used while advertising the refs to the client.
-
getRefFilter
Get the filter used while advertising the refs to the client.- Returns:
- the filter used while advertising the refs to the client.
-
setAdvertiseRefsHook
Set the hook used while advertising the refs to the client.If the
AdvertiseRefsHook
chooses to callsetAdvertisedRefs(Map)
, only refs set by this hook and selected by theRefFilter
will be shown to the client.- Parameters:
advertiseRefsHook
- the hook; may be null to show all refs.
-
setProtocolV2Hook
Set the protocol V2 hook.- Parameters:
hook
- the hook; if null no special actions are taken.- Since:
- 5.1
-
getProtocolV2Hook
Get the currently installed protocol v2 hook.- Returns:
- the hook or a default implementation if none installed.
- Since:
- 5.5
-
setRefFilter
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 theTransferConfig
.- Parameters:
refFilter
- the filter; may be null to show all refs.
-
getPreUploadHook
Get the configured pre upload hook.- Returns:
- the configured pre upload hook.
-
setPreUploadHook
Set the hook that controls how this instance will behave.- Parameters:
hook
- the hook; if null no special actions are taken.
-
getPostUploadHook
Get the configured post upload hook.- Returns:
- the configured post upload hook.
- Since:
- 4.1
-
setPostUploadHook
Set the hook for post upload actions (logging, repacking).- Parameters:
hook
- the hook; if null no special actions are taken.- Since:
- 4.1
-
setPackConfig
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
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
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
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
- Parameters:
p
- provider of URIs corresponding to cached packs (to support the packfile URIs feature)- Since:
- 5.5
-
useProtocolV2
private boolean useProtocolV2() -
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
-output
-messages
-- Throws:
IOException
-
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.
- 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
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
-
getAdvertisedOrDefaultRefs
- Throws:
IOException
-
getFilteredRefs
- Throws:
IOException
-
mapRefs
Returns the specified references.This produces an immutable map containing whatever subset of the refs named by the caller are present in the supplied
refs
map.- Parameters:
refs
- Map to search for refs to return.names
- which refs to search for inrefs
.- Returns:
- the requested Refs, omitting any that are null or missing.
-
exactRefs
Read refs on behalf of the client.This checks whether the refs are present in the ref advertisement since otherwise the client might not be supposed to be able to read them.
- Parameters:
names
- unabbreviated names of references.- Returns:
- the requested Refs, omitting any that are not visible or do not exist.
- Throws:
IOException
- on failure to read a ref or check it for visibility.
-
findRef
Find a ref in the usual search path on behalf of the client.This checks that the ref is present in the ref advertisement since otherwise the client might not be supposed to be able to read it.
- Parameters:
name
- short name of the ref to find, e.g. "master" to find "refs/heads/master".- Returns:
- the requested Ref, or
null
if it is not visible or does not exist. - Throws:
IOException
- on failure to read the ref or check it for visibility.
-
service
- Throws:
IOException
-
lsRefsV2
- Throws:
IOException
-
wantedRefs
- Throws:
IOException
-
fetchV2
- Throws:
IOException
-
serveOneCommandV2
- Throws:
IOException
-
getV2CapabilityAdvertisement
-
serviceV2
- Throws:
IOException
-
refIdSet
-
computeShallowsAndUnshallows
private void computeShallowsAndUnshallows(FetchRequest req, UploadPack.IOConsumer<ObjectId> shallowFunc, UploadPack.IOConsumer<ObjectId> unshallowFunc, List<ObjectId> deepenNots) throws IOException - Throws:
IOException
-
verifyClientShallow
private void verifyClientShallow(Set<ObjectId> shallowCommits) throws IOException, PackProtocolException - Throws:
IOException
PackProtocolException
-
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
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
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.UsegetFilterSpec()
insteadDeprecated synonym forgetFilterSpec().getBlobLimit()
.- Returns:
- filter blob limit requested by the client, or -1 if no limit
- Since:
- 5.3
-
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
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 HTTPUser-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
-
negotiate
private boolean negotiate(FetchRequest req, PackStatistics.Accumulator accumulator, PacketLineOut pckOut) throws IOException - Throws:
IOException
-
processHaveLines
private ObjectId processHaveLines(List<ObjectId> peerHas, ObjectId last, PacketLineOut out, PackStatistics.Accumulator accumulator) throws IOException - Throws:
IOException
-
parseWants
- Throws:
IOException
-
want
-
checkNotAdvertisedWants
private static void checkNotAdvertisedWants(UploadPack up, List<ObjectId> notAdvertisedWants, Collection<Ref> visibleRefs) throws IOException - Throws:
IOException
-
importantRefsFirst
-
refToObjectId
-
objectIdToRevCommit
Translate an object id to a RevCommit.- Parameters:
walk
- walk on the relevant object storaeobjectId
- Object Id- Returns:
- RevCommit instance or null if the object is missing
-
objectIdToRevObject
Translate an object id to a RevObject.- Parameters:
walk
- walk on the relevant object storageobjectId
- Object Id- Returns:
- RevObject instance or null if the object is missing
-
objectIdsToRevObjects
private static List<RevObject> objectIdsToRevObjects(RevWalk walk, Iterable<ObjectId> objectIds) throws MissingObjectException, IOException - Throws:
MissingObjectException
IOException
-
addCommonBase
-
okToGiveUp
- Throws:
PackProtocolException
-
okToGiveUpImp
- Throws:
PackProtocolException
-
wantSatisfied
- Throws:
IOException
-
sendPack
private void sendPack(PackStatistics.Accumulator accumulator, FetchRequest req, @Nullable Collection<Ref> allTags, List<ObjectId> unshallowCommits, List<ObjectId> deepenNots, PacketLineOut pckOut) throws IOException Send the requested objects to the client.- Parameters:
accumulator
- where to write statistics about the content of the pack.req
- request in processallTags
- refs to search for annotated tags to include in the pack if theinvalid reference
#OPTION_INCLUDE_TAG
unshallowCommits
- shallow commits on the client that are now becoming unshallowdeepenNots
- objects that the client specified using --shallow-excludepckOut
- output writer- Throws:
IOException
- if an error occurred while generating or writing the pack.
-
sendPack
private void sendPack(ProgressMonitor pm, PacketLineOut pckOut, OutputStream packOut, FetchRequest req, PackStatistics.Accumulator accumulator, @Nullable Collection<Ref> allTags, List<ObjectId> unshallowCommits, List<ObjectId> deepenNots) throws IOException Send the requested objects to the client.- Parameters:
pm
- progress monitorpckOut
- PacketLineOut that shares the output with packOutpackOut
- packfile outputreq
- request being processedaccumulator
- where to write statistics about the content of the pack.allTags
- refs to search for annotated tags to include in the pack if theinvalid reference
#OPTION_INCLUDE_TAG
unshallowCommits
- shallow commits on the client that are now becoming unshallowdeepenNots
- objects that the client specified using --shallow-exclude- Throws:
IOException
- if an error occurred while generating or writing the pack.
-
findSymrefs
-
addTagChain
- Throws:
IOException
-
FirstWant
instead