Class BaseReceivePack
- java.lang.Object
-
- org.eclipse.jgit.transport.BaseReceivePack
-
- Direct Known Subclasses:
ReceivePack
public abstract class BaseReceivePack extends java.lang.Object
Base implementation of the side of a push connection that receives objects.Contains high-level operations for initializing and closing streams, advertising refs, reading commands, and receiving and applying a pack. Subclasses compose these operations into full service implementations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BaseReceivePack.FirstLine
Data in the first line of a request, the line itself plus capabilities.protected static class
BaseReceivePack.ReceiveConfig
Configuration for receive operations.
-
Field Summary
Fields Modifier and Type Field Description protected java.io.OutputStream
msgOut
Optional message output stream.protected PacketLineIn
pckIn
Packet line input stream aroundrawIn
.protected PacketLineOut
pckOut
Packet line output stream aroundrawOut
.protected java.io.InputStream
rawIn
Raw input stream.protected java.io.OutputStream
rawOut
Raw output stream.
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseReceivePack(Repository into)
Create a new pack receive for an open repository.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected boolean
anyRejects()
protected void
close()
Close and flush (if necessary) the underlying streams.protected void
enableCapabilities()
Enable capabilities based on a previously read capabilities line.protected void
executeCommands()
Execute commands to update references.protected void
failPendingCommands()
Set the result to fail for any command that was not processed yet.protected java.util.List<ReceiveCommand>
filterCommands(ReceiveCommand.Result want)
Filter the list of commands according to result.java.util.Set<ObjectId>
getAdvertisedObjects()
Get objects advertised to the client.protected java.util.Map<java.lang.String,Ref>
getAdvertisedOrDefaultRefs()
java.util.Map<java.lang.String,Ref>
getAdvertisedRefs()
Get refs which were advertised to the client.AdvertiseRefsHook
getAdvertiseRefsHook()
java.util.List<ReceiveCommand>
getAllCommands()
protected java.util.Set<ObjectId>
getClientShallowCommits()
Get the commits from the client's shallow file.protected abstract java.lang.String
getLockMessageProcessName()
java.io.OutputStream
getMessageOutputStream()
long
getPackSize()
Get the size of the received pack file including the index size.RefFilter
getRefFilter()
PersonIdent
getRefLogIdent()
Repository
getRepository()
RevWalk
getRevWalk()
int
getTimeout()
protected boolean
hasCommands()
protected boolean
hasError()
protected void
init(java.io.InputStream input, java.io.OutputStream output, java.io.OutputStream messages)
Initialize the instance with the given streams.boolean
isAllowBranchDeletes()
boolean
isAllowCreates()
boolean
isAllowDeletes()
boolean
isAllowNonFastForwards()
boolean
isBiDirectionalPipe()
protected boolean
isCapabilityEnabled(java.lang.String name)
Check if the peer requested a capability.boolean
isCheckReceivedObjects()
boolean
isCheckReferencedObjectsAreReachable()
boolean
isExpectDataAfterPackFooter()
boolean
isSideBand()
Check whether the client expects a side-band stream.protected boolean
needPack()
protected void
receivePackAndCheckConnectivity()
Receive a pack from the stream and check connectivity if necessary.protected void
recvCommands()
Receive a list of commands from the input.protected void
release()
Release any resources used by this object.void
sendAdvertisedRefs(RefAdvertiser adv)
Generate an advertisement of available refs and capabilities.void
sendError(java.lang.String what)
Send an error message to the client.void
sendMessage(java.lang.String what)
Send a message to the client, if it supports receiving them.protected void
sendStatusReport(boolean forClient, java.lang.Throwable unpackError, org.eclipse.jgit.transport.BaseReceivePack.Reporter out)
Send a status report.void
setAdvertisedRefs(java.util.Map<java.lang.String,Ref> allRefs, java.util.Set<ObjectId> additionalHaves)
Set the refs advertised by this ReceivePack.void
setAdvertiseRefsHook(AdvertiseRefsHook advertiseRefsHook)
Set the hook used while advertising the refs to the client.void
setAllowBranchDeletes(boolean canDelete)
void
setAllowCreates(boolean canCreate)
void
setAllowDeletes(boolean canDelete)
void
setAllowNonFastForwards(boolean canRewind)
void
setBiDirectionalPipe(boolean twoWay)
void
setCheckReceivedObjects(boolean check)
void
setCheckReferencedObjectsAreReachable(boolean b)
Validate all referenced but not supplied objects are reachable.void
setExpectDataAfterPackFooter(boolean e)
void
setMaxObjectSizeLimit(long limit)
Set the maximum allowed Git object size.void
setMaxPackSizeLimit(long limit)
Set the maximum allowed pack size.void
setObjectChecker(ObjectChecker impl)
void
setRefFilter(RefFilter refFilter)
Set the filter used while advertising the refs to the client.void
setRefLogIdent(PersonIdent pi)
Set the identity of the user appearing in the affected reflogs.void
setTimeout(int seconds)
Set the timeout before willing to abort an IO call.protected void
unlockPack()
Unlock the pack written by this object.protected void
validateCommands()
Validate the command list.
-
-
-
Field Detail
-
rawIn
protected java.io.InputStream rawIn
Raw input stream.
-
rawOut
protected java.io.OutputStream rawOut
Raw output stream.
-
msgOut
protected java.io.OutputStream msgOut
Optional message output stream.
-
pckIn
protected PacketLineIn pckIn
Packet line input stream aroundrawIn
.
-
pckOut
protected PacketLineOut pckOut
Packet line output stream aroundrawOut
.
-
-
Constructor Detail
-
BaseReceivePack
protected BaseReceivePack(Repository into)
Create a new pack receive for an open repository.- Parameters:
into
- the destination repository.
-
-
Method Detail
-
getLockMessageProcessName
protected abstract java.lang.String getLockMessageProcessName()
- Returns:
- the process name used for pack lock messages.
-
getRepository
public final Repository getRepository()
- Returns:
- the repository this receive completes into.
-
getRevWalk
public final RevWalk getRevWalk()
- Returns:
- the RevWalk instance used by this connection.
-
getAdvertisedRefs
public final java.util.Map<java.lang.String,Ref> getAdvertisedRefs()
Get refs which were advertised to the client.- Returns:
- all refs which were advertised to the client, or null if
setAdvertisedRefs(Map, Set)
has not been called yet.
-
setAdvertisedRefs
public void setAdvertisedRefs(java.util.Map<java.lang.String,Ref> allRefs, java.util.Set<ObjectId> additionalHaves)
Set the refs advertised by this ReceivePack.Intended to be called from a
PreReceiveHook
.- Parameters:
allRefs
- explicit set of references to claim as advertised by this ReceivePack 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.additionalHaves
- explicit set of additional haves to claim as advertised. If null, assumes the default set of additional haves from the repository.
-
getAdvertisedObjects
public final java.util.Set<ObjectId> getAdvertisedObjects()
Get objects advertised to the client.- Returns:
- the set of objects advertised to the as present in this repository,
or null if
setAdvertisedRefs(Map, Set)
has not been called yet.
-
isCheckReferencedObjectsAreReachable
public boolean isCheckReferencedObjectsAreReachable()
- Returns:
- true if this instance will validate all referenced, but not supplied by the client, objects are reachable from another reference.
-
setCheckReferencedObjectsAreReachable
public void setCheckReferencedObjectsAreReachable(boolean b)
Validate all referenced but not supplied objects are reachable.If enabled, this instance will verify that references to objects not contained within the received pack are already reachable through at least one other reference displayed as part of
getAdvertisedRefs()
.This feature is useful when the application doesn't trust the client to not provide a forged SHA-1 reference to an object, in an attempt to access parts of the DAG that they aren't allowed to see and which have been hidden from them via the configured
AdvertiseRefsHook
orRefFilter
.Enabling this feature may imply at least some, if not all, of the same functionality performed by
setCheckReceivedObjects(boolean)
. Applications are encouraged to enable both features, if desired.- Parameters:
b
-true
to enable the additional check.
-
isBiDirectionalPipe
public boolean isBiDirectionalPipe()
- 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)
- 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.
-
isExpectDataAfterPackFooter
public boolean isExpectDataAfterPackFooter()
- Returns:
- true if there is data expected after the pack footer.
-
setExpectDataAfterPackFooter
public void setExpectDataAfterPackFooter(boolean e)
- Parameters:
e
- true if there is additional data in InputStream after pack.
-
isCheckReceivedObjects
public boolean isCheckReceivedObjects()
- Returns:
- true if this instance will verify received objects are formatted correctly. Validating objects requires more CPU time on this side of the connection.
-
setCheckReceivedObjects
public void setCheckReceivedObjects(boolean check)
- Parameters:
check
- true to enable checking received objects; false to assume all received objects are valid.- See Also:
setObjectChecker(ObjectChecker)
-
setObjectChecker
public void setObjectChecker(ObjectChecker impl)
- Parameters:
impl
- if non-null the object checking instance to verify each received object with; null to disable object checking.- Since:
- 3.4
-
isAllowCreates
public boolean isAllowCreates()
- Returns:
- true if the client can request refs to be created.
-
setAllowCreates
public void setAllowCreates(boolean canCreate)
- Parameters:
canCreate
- true to permit create ref commands to be processed.
-
isAllowDeletes
public boolean isAllowDeletes()
- Returns:
- true if the client can request refs to be deleted.
-
setAllowDeletes
public void setAllowDeletes(boolean canDelete)
- Parameters:
canDelete
- true to permit delete ref commands to be processed.
-
isAllowBranchDeletes
public boolean isAllowBranchDeletes()
- Returns:
- true if the client can delete from
refs/heads/
. - Since:
- 3.6
-
setAllowBranchDeletes
public void setAllowBranchDeletes(boolean canDelete)
- Parameters:
canDelete
- true to permit deletion of branches from therefs/heads/
namespace.- Since:
- 3.6
-
isAllowNonFastForwards
public boolean isAllowNonFastForwards()
- Returns:
- true if the client can request non-fast-forward updates of a ref, possibly making objects unreachable.
-
setAllowNonFastForwards
public void setAllowNonFastForwards(boolean canRewind)
- Parameters:
canRewind
- true to permit the client to ask for non-fast-forward updates of an existing ref.
-
getRefLogIdent
public PersonIdent getRefLogIdent()
- Returns:
- identity of the user making the changes in the reflog.
-
setRefLogIdent
public void setRefLogIdent(PersonIdent pi)
Set the identity of the user appearing in the affected reflogs.The timestamp portion of the identity is ignored. A new identity with the current timestamp will be created automatically when the updates occur and the log records are written.
- Parameters:
pi
- identity of the user. If null the identity will be automatically determined based on the repository configuration.
-
getAdvertiseRefsHook
public AdvertiseRefsHook getAdvertiseRefsHook()
- Returns:
- the hook used while advertising the refs to the client
-
getRefFilter
public RefFilter getRefFilter()
- Returns:
- the filter used while advertising the refs to the client
-
setAdvertiseRefsHook
public void setAdvertiseRefsHook(AdvertiseRefsHook advertiseRefsHook)
Set the hook used while advertising the refs to the client.If the
AdvertiseRefsHook
chooses to callsetAdvertisedRefs(Map,Set)
, only refs set by this hook and selected by theRefFilter
will be shown to the client. Clients may still attempt to create or update a reference not advertised by the configuredAdvertiseRefsHook
. These attempts should be rejected by a matchingPreReceiveHook
.- Parameters:
advertiseRefsHook
- the hook; may be null to show all refs.
-
setRefFilter
public void setRefFilter(RefFilter refFilter)
Set the filter used while advertising the refs to the client.Only refs allowed by this filter will be shown to the client. The filter is run against the refs specified by the
AdvertiseRefsHook
(if applicable).- Parameters:
refFilter
- the filter; may be null to show all refs.
-
getTimeout
public int getTimeout()
- 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.
-
setMaxObjectSizeLimit
public void setMaxObjectSizeLimit(long limit)
Set the maximum allowed Git object size.If an object is larger than the given size the pack-parsing will throw an exception aborting the receive-pack operation.
- Parameters:
limit
- the Git object size limit. If zero then there is not limit.
-
setMaxPackSizeLimit
public void setMaxPackSizeLimit(long limit)
Set the maximum allowed pack size.A pack exceeding this size will be rejected.
- Parameters:
limit
- the pack size limit, in bytes- Since:
- 3.3
-
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.
-
getAllCommands
public java.util.List<ReceiveCommand> getAllCommands()
- Returns:
- all of the command received by the current request.
-
sendError
public void sendError(java.lang.String what)
Send an error message to the client.If any error messages are sent before the references are advertised to the client, the errors will be sent instead of the advertisement and the receive operation will be aborted. All clients should receive and display such early stage errors.
If the reference advertisements have already been sent, messages are sent in a side channel. If the client doesn't support receiving messages, the message will be discarded, with no other indication to the caller or to the client.
PreReceiveHook
s should always try to useReceiveCommand.setResult(Result, String)
with a result status ofReceiveCommand.Result.REJECTED_OTHER_REASON
to indicate any reasons for rejecting an update. Messages attached to a command are much more likely to be returned 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.
-
sendMessage
public void sendMessage(java.lang.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.
-
getMessageOutputStream
public java.io.OutputStream getMessageOutputStream()
- Returns:
- an underlying stream for sending messages to the client.
-
getPackSize
public long getPackSize()
Get the size of the received pack file including the index size. This can only be called if the pack is already received.- Returns:
- the size of the received pack including index size
- Throws:
java.lang.IllegalStateException
- if called before the pack has been received- Since:
- 3.3
-
getClientShallowCommits
protected java.util.Set<ObjectId> getClientShallowCommits()
Get the commits from the client's shallow file.- Returns:
- if the client is a shallow repository, the list of edge commits that define the client's shallow boundary. Empty set if the client is earlier than Git 1.9, or is a full clone.
- Since:
- 3.5
-
hasCommands
protected boolean hasCommands()
- Returns:
- true if any commands to be executed have been read.
-
hasError
protected boolean hasError()
- Returns:
- true if an error occurred that should be advertised.
-
init
protected void init(java.io.InputStream input, java.io.OutputStream output, java.io.OutputStream messages)
Initialize the instance with the given streams.- Parameters:
input
- raw input to read client commands and pack data from. Caller must ensure the input is buffered, otherwise read performance may suffer.output
- response back to the Git network client. 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.
-
getAdvertisedOrDefaultRefs
protected java.util.Map<java.lang.String,Ref> getAdvertisedOrDefaultRefs()
- Returns:
- advertised refs, or the default if not explicitly advertised.
-
receivePackAndCheckConnectivity
protected void receivePackAndCheckConnectivity() throws java.io.IOException
Receive a pack from the stream and check connectivity if necessary.- Throws:
java.io.IOException
- an error occurred during unpacking or connectivity checking.
-
unlockPack
protected void unlockPack() throws java.io.IOException
Unlock the pack written by this object.- Throws:
java.io.IOException
- the pack could not be unlocked.
-
sendAdvertisedRefs
public void sendAdvertisedRefs(RefAdvertiser adv) throws java.io.IOException, ServiceMayNotContinueException
Generate an advertisement of available refs and capabilities.- Parameters:
adv
- the advertisement formatter.- Throws:
java.io.IOException
- the formatter failed to write an advertisement.ServiceMayNotContinueException
- the hook denied advertisement.
-
recvCommands
protected void recvCommands() throws java.io.IOException
Receive a list of commands from the input.- Throws:
java.io.IOException
-
enableCapabilities
protected void enableCapabilities()
Enable capabilities based on a previously read capabilities line.
-
isCapabilityEnabled
protected boolean isCapabilityEnabled(java.lang.String name)
Check if the peer requested a capability.- Parameters:
name
- protocol name identifying the capability.- Returns:
- true if the peer requested the capability to be enabled.
-
needPack
protected boolean needPack()
- Returns:
- true if a pack is expected based on the list of commands.
-
validateCommands
protected void validateCommands()
Validate the command list.
-
anyRejects
protected boolean anyRejects()
- Returns:
- if any commands have been rejected so far.
- Since:
- 3.6
-
failPendingCommands
protected void failPendingCommands()
Set the result to fail for any command that was not processed yet.- Since:
- 3.6
-
filterCommands
protected java.util.List<ReceiveCommand> filterCommands(ReceiveCommand.Result want)
Filter the list of commands according to result.- Parameters:
want
- desired status to filter by.- Returns:
- a copy of the command list containing only those commands with the desired status.
-
executeCommands
protected void executeCommands()
Execute commands to update references.
-
sendStatusReport
protected void sendStatusReport(boolean forClient, java.lang.Throwable unpackError, org.eclipse.jgit.transport.BaseReceivePack.Reporter out) throws java.io.IOException
Send a status report.- Parameters:
forClient
- true if this report is for a Git client, false if it is for an end-user.unpackError
- an error that occurred during unpacking, ornull
out
- the reporter for sending the status strings.- Throws:
java.io.IOException
- an error occurred writing the status report.
-
close
protected void close() throws java.io.IOException
Close and flush (if necessary) the underlying streams.- Throws:
java.io.IOException
-
release
protected void release() throws java.io.IOException
Release any resources used by this object.- Throws:
java.io.IOException
- the pack could not be unlocked.
-
-