/* * Copyright (C) 2008-2010, Google Inc. and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at * https://www.eclipse.org/org/documents/edl-v10.php. * * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.transport; import static java.nio.charset.StandardCharsets.UTF_8; import static org.eclipse.jgit.lib.Constants.HEAD; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_ATOMIC; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_DELETE_REFS; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_OFS_DELTA; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_OPTIONS; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_QUIET; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT; import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_ERR; import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SESSION_ID; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS; import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UncheckedIOException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.errors.InvalidObjectIdException; import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.errors.PackProtocolException; import org.eclipse.jgit.errors.TooLargePackException; import org.eclipse.jgit.errors.UnpackException; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.submodule.SubmoduleValidator; import org.eclipse.jgit.internal.submodule.SubmoduleValidator.SubmoduleValidationException; import org.eclipse.jgit.internal.transport.connectivity.FullConnectivityChecker; import org.eclipse.jgit.internal.transport.parser.FirstCommand; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.BatchRefUpdate; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.GitmoduleEntry; import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.ObjectChecker; import org.eclipse.jgit.lib.ObjectDatabase; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.ConnectivityChecker.ConnectivityCheckInfo; import org.eclipse.jgit.transport.PacketLineIn.InputOverLimitIOException; import org.eclipse.jgit.transport.ReceiveCommand.Result; import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser; import org.eclipse.jgit.util.io.InterruptTimer; import org.eclipse.jgit.util.io.LimitedInputStream; import org.eclipse.jgit.util.io.TimeoutInputStream; import org.eclipse.jgit.util.io.TimeoutOutputStream; /** * Implements the server side of a push connection, receiving objects. */ public class ReceivePack { /** Database we write the stored objects into. */ private final Repository db; /** Revision traversal support over {@link #db}. */ private final RevWalk walk; /** * Is 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.
*/
private boolean biDirectionalPipe = true;
/** Expecting data after the pack footer */
private boolean expectDataAfterPackFooter;
/** Should an incoming transfer validate objects? */
private ObjectChecker objectChecker;
/** Should an incoming transfer permit create requests? */
private boolean allowCreates;
/** Should an incoming transfer permit delete requests? */
private boolean allowAnyDeletes;
private boolean allowBranchDeletes;
/** Should an incoming transfer permit non-fast-forward requests? */
private boolean allowNonFastForwards;
/** Should an incoming transfer permit push options? **/
private boolean allowPushOptions;
/**
* Should the requested ref updates be performed as a single atomic
* transaction?
*/
private boolean atomic;
private boolean allowOfsDelta;
private boolean allowQuiet = true;
/** Should the server advertise and accept the session-id capability. */
private boolean allowReceiveClientSID;
/** Identity to record action as within the reflog. */
private PersonIdent refLogIdent;
/** Hook used while advertising the refs to the client. */
private AdvertiseRefsHook advertiseRefsHook;
/** Filter used while advertising the refs to the client. */
private RefFilter refFilter;
/** Timeout in seconds to wait for client interaction. */
private int timeout;
/** Timer to manage {@link #timeout}. */
private InterruptTimer timer;
private TimeoutInputStream timeoutIn;
// Original stream passed to init(), since rawOut may be wrapped in a
// sideband.
private OutputStream origOut;
/** Raw input stream. */
private InputStream rawIn;
/** Raw output stream. */
private OutputStream rawOut;
/** Optional message output stream. */
private OutputStream msgOut;
private SideBandOutputStream errOut;
/** Packet line input stream around {@link #rawIn}. */
private PacketLineIn pckIn;
/** Packet line output stream around {@link #rawOut}. */
private PacketLineOut pckOut;
private final MessageOutputWrapper msgOutWrapper = new MessageOutputWrapper();
private PackParser parser;
/** The refs we advertised as existing at the start of the connection. */
private Map
* We don't want to expose {@link #msgOut} directly because it can change
* several times over the course of a session.
*/
class MessageOutputWrapper extends OutputStream {
@Override
public void write(int ch) {
if (msgOut != null) {
try {
msgOut.write(ch);
} catch (IOException e) {
// Ignore write failures.
}
}
}
@Override
public void write(byte[] b, int off, int len) {
if (msgOut != null) {
try {
msgOut.write(b, off, len);
} catch (IOException e) {
// Ignore write failures.
}
}
}
@Override
public void write(byte[] b) {
write(b, 0, b.length);
}
@Override
public void flush() {
if (msgOut != null) {
try {
msgOut.flush();
} catch (IOException e) {
// Ignore write failures.
}
}
}
}
/**
* Get the repository this receive completes into.
*
* @return the repository this receive completes into.
*/
public Repository getRepository() {
return db;
}
/**
* Get the RevWalk instance used by this connection.
*
* @return the RevWalk instance used by this connection.
*/
public RevWalk getRevWalk() {
return walk;
}
/**
* Get refs which were advertised to the client.
*
* @return all refs which were advertised to the client, or null if
* {@link #setAdvertisedRefs(Map, Set)} has not been called yet.
*/
public Map
* Intended to be called from a
* {@link org.eclipse.jgit.transport.PreReceiveHook}.
*
* @param 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
* configured {@link #getRefFilter()}. If null, assumes all refs
* were advertised.
* @param additionalHaves
* explicit set of additional haves to claim as advertised. If
* null, assumes the default set of additional haves from the
* repository.
* @throws IOException
* if an IO error occurred
*/
public void setAdvertisedRefs(Map
* 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 {@link #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
* {@link org.eclipse.jgit.transport.AdvertiseRefsHook} or
* {@link org.eclipse.jgit.transport.RefFilter}.
*
* Enabling this feature may imply at least some, if not all, of the same
* functionality performed by {@link #setCheckReceivedObjects(boolean)}.
* Applications are encouraged to enable both features, if desired.
*
* @param b
* {@code true} to enable the additional check.
*/
public void setCheckReferencedObjectsAreReachable(boolean b) {
this.checkReferencedAreReachable = b;
}
/**
* Whether this class expects a bi-directional pipe opened between the
* client and itself.
*
* @return true if this class expects a bi-directional pipe opened between
* the client and itself. The default is true.
*/
public boolean isBiDirectionalPipe() {
return biDirectionalPipe;
}
/**
* Whether 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.
*
* @param 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.
*/
public void setBiDirectionalPipe(boolean twoWay) {
biDirectionalPipe = twoWay;
}
/**
* Whether there is data expected after the pack footer.
*
* @return {@code true} if there is data expected after the pack footer.
*/
public boolean isExpectDataAfterPackFooter() {
return expectDataAfterPackFooter;
}
/**
* Whether there is additional data in InputStream after pack.
*
* @param e
* {@code true} if there is additional data in InputStream after
* pack.
*/
public void setExpectDataAfterPackFooter(boolean e) {
expectDataAfterPackFooter = e;
}
/**
* Whether this instance will verify received objects are formatted
* correctly.
*
* @return {@code true} if this instance will verify received objects are
* formatted correctly. Validating objects requires more CPU time on
* this side of the connection.
*/
public boolean isCheckReceivedObjects() {
return objectChecker != null;
}
/**
* Whether to enable checking received objects
*
* @param check
* {@code true} to enable checking received objects; false to
* assume all received objects are valid.
* @see #setObjectChecker(ObjectChecker)
*/
public void setCheckReceivedObjects(boolean check) {
if (check && objectChecker == null)
setObjectChecker(new ObjectChecker());
else if (!check && objectChecker != null)
setObjectChecker(null);
}
/**
* Set the object checking instance to verify each received object with
*
* @param impl
* if non-null the object checking instance to verify each
* received object with; null to disable object checking.
* @since 3.4
*/
public void setObjectChecker(ObjectChecker impl) {
objectChecker = impl;
}
/**
* Whether the client can request refs to be created.
*
* @return {@code true} if the client can request refs to be created.
*/
public boolean isAllowCreates() {
return allowCreates;
}
/**
* Whether to permit create ref commands to be processed.
*
* @param canCreate
* {@code true} to permit create ref commands to be processed.
*/
public void setAllowCreates(boolean canCreate) {
allowCreates = canCreate;
}
/**
* Whether the client can request refs to be deleted.
*
* @return {@code true} if the client can request refs to be deleted.
*/
public boolean isAllowDeletes() {
return allowAnyDeletes;
}
/**
* Whether to permit delete ref commands to be processed.
*
* @param canDelete
* {@code true} to permit delete ref commands to be processed.
*/
public void setAllowDeletes(boolean canDelete) {
allowAnyDeletes = canDelete;
}
/**
* Whether the client can delete from {@code refs/heads/}.
*
* @return {@code true} if the client can delete from {@code refs/heads/}.
* @since 3.6
*/
public boolean isAllowBranchDeletes() {
return allowBranchDeletes;
}
/**
* Configure whether to permit deletion of branches from the
* {@code refs/heads/} namespace.
*
* @param canDelete
* {@code true} to permit deletion of branches from the
* {@code refs/heads/} namespace.
* @since 3.6
*/
public void setAllowBranchDeletes(boolean canDelete) {
allowBranchDeletes = canDelete;
}
/**
* Whether the client can request non-fast-forward updates of a ref,
* possibly making objects unreachable.
*
* @return {@code true} if the client can request non-fast-forward updates
* of a ref, possibly making objects unreachable.
*/
public boolean isAllowNonFastForwards() {
return allowNonFastForwards;
}
/**
* Configure whether to permit the client to ask for non-fast-forward
* updates of an existing ref.
*
* @param canRewind
* {@code true} to permit the client to ask for non-fast-forward
* updates of an existing ref.
*/
public void setAllowNonFastForwards(boolean canRewind) {
allowNonFastForwards = canRewind;
}
/**
* Whether the client's commands should be performed as a single atomic
* transaction.
*
* @return {@code true} if the client's commands should be performed as a
* single atomic transaction.
* @since 4.4
*/
public boolean isAtomic() {
return atomic;
}
/**
* Configure whether to perform the client's commands as a single atomic
* transaction.
*
* @param atomic
* {@code true} to perform the client's commands as a single
* atomic transaction.
* @since 4.4
*/
public void setAtomic(boolean atomic) {
this.atomic = atomic;
}
/**
* Get identity of the user making the changes in the reflog.
*
* @return identity of the user making the changes in the reflog.
*/
public PersonIdent getRefLogIdent() {
return refLogIdent;
}
/**
* 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.
*
* @param pi
* identity of the user. If null the identity will be
* automatically determined based on the repository
* configuration.
*/
public void setRefLogIdent(PersonIdent pi) {
refLogIdent = pi;
}
/**
* Get the hook used while advertising the refs to the client
*
* @return the hook used while advertising the refs to the client
*/
public AdvertiseRefsHook getAdvertiseRefsHook() {
return advertiseRefsHook;
}
/**
* Get the filter used while advertising the refs to the client
*
* @return the filter used while advertising the refs to the client
*/
public RefFilter getRefFilter() {
return refFilter;
}
/**
* Set the hook used while advertising the refs to the client.
*
* If the {@link org.eclipse.jgit.transport.AdvertiseRefsHook} chooses to
* call {@link #setAdvertisedRefs(Map,Set)}, only refs set by this hook
* and selected by the {@link org.eclipse.jgit.transport.RefFilter}
* will be shown to the client. Clients may still attempt to create or
* update a reference not advertised by the configured
* {@link org.eclipse.jgit.transport.AdvertiseRefsHook}. These attempts
* should be rejected by a matching
* {@link org.eclipse.jgit.transport.PreReceiveHook}.
*
* @param advertiseRefsHook
* the hook; may be null to show all refs.
*/
public void setAdvertiseRefsHook(AdvertiseRefsHook advertiseRefsHook) {
if (advertiseRefsHook != null)
this.advertiseRefsHook = advertiseRefsHook;
else
this.advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
}
/**
* 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
* {@link org.eclipse.jgit.transport.AdvertiseRefsHook} (if applicable).
*
* @param refFilter
* the filter; may be null to show all refs.
*/
public void setRefFilter(RefFilter refFilter) {
this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
}
/**
* Get timeout (in seconds) before aborting an IO operation.
*
* @return timeout (in seconds) before aborting an IO operation.
*/
public int getTimeout() {
return timeout;
}
/**
* Set the timeout before willing to abort an IO call.
*
* @param seconds
* number of seconds to wait (with no data transfer occurring)
* before aborting an IO read or write operation with the
* connected client.
*/
public void setTimeout(int seconds) {
timeout = seconds;
}
/**
* Set the maximum number of command bytes to read from the client.
*
* @param limit
* command limit in bytes; if 0 there is no limit.
* @since 4.7
*/
public void setMaxCommandBytes(long limit) {
maxCommandBytes = limit;
}
/**
* Set the maximum number of command bytes to discard from the client.
*
* Discarding remaining bytes allows this instance to consume the rest of
* the command block and send a human readable over-limit error via the
* side-band channel. If the client sends an excessive number of bytes this
* limit kicks in and the instance disconnects, resulting in a non-specific
* 'pipe closed', 'end of stream', or similar generic error at the client.
*
* When the limit is set to {@code -1} the implementation will default to
* the larger of {@code 3 * maxCommandBytes} or {@code 3 MiB}.
*
* @param limit
* discard limit in bytes; if 0 there is no limit; if -1 the
* implementation tries to set a reasonable default.
* @since 4.7
*/
public void setMaxCommandDiscardBytes(long limit) {
maxDiscardBytes = 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.
*
* @param limit
* the Git object size limit. If zero then there is not limit.
*/
public void setMaxObjectSizeLimit(long limit) {
maxObjectSizeLimit = limit;
}
/**
* Set the maximum allowed pack size.
*
* A pack exceeding this size will be rejected.
*
* @param limit
* the pack size limit, in bytes
* @since 3.3
*/
public void setMaxPackSizeLimit(long limit) {
if (limit < 0)
throw new IllegalArgumentException(
MessageFormat.format(JGitText.get().receivePackInvalidLimit,
Long.valueOf(limit)));
maxPackSizeLimit = limit;
}
/**
* Check whether the client expects a side-band stream.
*
* @return true if the client has advertised a side-band capability, false
* otherwise.
* @throws org.eclipse.jgit.transport.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.
*/
public boolean isSideBand() throws RequestNotYetReadException {
checkRequestWasRead();
return enabledCapabilities.containsKey(CAPABILITY_SIDE_BAND_64K);
}
/**
* Whether clients may request avoiding noisy progress messages.
*
* @return true if clients may request avoiding noisy progress messages.
* @since 4.0
*/
public boolean isAllowQuiet() {
return allowQuiet;
}
/**
* Configure if clients may request the server skip noisy messages.
*
* @param allow
* true to allow clients to request quiet behavior; false to
* refuse quiet behavior and send messages anyway. This may be
* necessary if processing is slow and the client-server network
* connection can timeout.
* @since 4.0
*/
public void setAllowQuiet(boolean allow) {
allowQuiet = allow;
}
/**
* Whether the server supports receiving push options.
*
* @return true if the server supports receiving push options.
* @since 4.5
*/
public boolean isAllowPushOptions() {
return allowPushOptions;
}
/**
* Configure if the server supports receiving push options.
*
* @param allow
* true to optionally accept option strings from the client.
* @since 4.5
*/
public void setAllowPushOptions(boolean allow) {
allowPushOptions = allow;
}
/**
* True if the client wants less verbose output.
*
* @return true if the client has requested the server to be less verbose.
* @throws org.eclipse.jgit.transport.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.
* @since 4.0
*/
public boolean isQuiet() throws RequestNotYetReadException {
checkRequestWasRead();
return quiet;
}
/**
* Set the configuration for push certificate verification.
*
* @param cfg
* new configuration; if this object is null or its
* {@link SignedPushConfig#getCertNonceSeed()} is null, push
* certificate verification will be disabled.
* @since 4.1
*/
public void setSignedPushConfig(SignedPushConfig cfg) {
signedPushConfig = cfg;
}
private PushCertificateParser getPushCertificateParser() {
if (pushCertificateParser == null) {
pushCertificateParser = new PushCertificateParser(db,
signedPushConfig);
}
return pushCertificateParser;
}
/**
* Get the user agent of the client.
*
* If the client is new enough to use {@code agent=} capability that value
* will be returned. Older HTTP clients may also supply their version using
* the HTTP {@code 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
* {@code User-Agent} header value until capabilities have been parsed.
*
* @return user agent supplied by the client. Available only if the client
* is new enough to advertise its user agent.
* @since 4.0
*/
public String getPeerUserAgent() {
if (enabledCapabilities == null || enabledCapabilities.isEmpty()) {
return userAgent;
}
return enabledCapabilities.getOrDefault(OPTION_AGENT, userAgent);
}
/**
* Get all of the command received by the current request.
*
* @return all of the command received by the current request.
*/
public List
* 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.
*
* {@link org.eclipse.jgit.transport.PreReceiveHook}s should always try to
* use
* {@link org.eclipse.jgit.transport.ReceiveCommand#setResult(Result, String)}
* with a result status of
* {@link org.eclipse.jgit.transport.ReceiveCommand.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.
*
* @param what
* string describing the problem identified by the hook. The
* string must not end with an LF, and must not contain an LF.
*/
public void sendError(String what) {
if (refs == null) {
if (advertiseError == null)
advertiseError = new StringBuilder();
advertiseError.append(what).append('\n');
} else {
msgOutWrapper.write(Constants.encode("error: " + what + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
}
}
private void fatalError(String msg) {
if (errOut != null) {
try {
errOut.write(Constants.encode(msg));
errOut.flush();
} catch (IOException e) {
// Ignore write failures
}
} else {
sendError(msg);
}
}
/**
* 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.
*
* @param what
* string describing the problem identified by the hook. The
* string must not end with an LF, and must not contain an LF.
*/
public void sendMessage(String what) {
msgOutWrapper.write(Constants.encode(what + "\n")); //$NON-NLS-1$
}
/**
* Get an underlying stream for sending messages to the client.
*
* @return an underlying stream for sending messages to the client.
*/
public OutputStream getMessageOutputStream() {
return msgOutWrapper;
}
/**
* Get whether or not a pack has been received.
*
* This can be called before calling {@link #getPackSize()} to avoid causing
* {@code IllegalStateException} when the pack size was not set because no
* pack was received.
*
* @return true if a pack has been received.
* @since 5.6
*/
public boolean hasReceivedPack() {
return packSize != null;
}
/**
* Get the size of the received pack file including the index size.
*
* This can only be called if the pack is already received.
*
* @return the size of the received pack including index size
* @throws java.lang.IllegalStateException
* if called before the pack has been received
* @since 3.3
*/
public long getPackSize() {
if (packSize != null)
return packSize.longValue();
throw new IllegalStateException(JGitText.get().packSizeNotSetYet);
}
/**
* Get the commits from the client's shallow file.
*
* @return 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.
*/
private Set
* Only valid after commands are read from the wire.
*
* @return the parsed certificate, or null if push certificates are disabled
* or no cert was presented by the client.
* @since 4.1
*/
public PushCertificate getPushCertificate() {
return pushCert;
}
/**
* Set the push certificate used to verify the pusher's identity.
*
* Should only be called if reconstructing an instance without going through
* the normal {@link #recvCommands()} flow.
*
* @param cert
* the push certificate to set.
* @since 4.1
*/
public void setPushCertificate(PushCertificate cert) {
pushCert = cert;
}
/**
* Gets an unmodifiable view of the option strings associated with the push.
*
* @return an unmodifiable view of pushOptions, or null (if pushOptions is).
* @since 4.5
*/
@Nullable
public List
* Should only be called if reconstructing an instance without going through
* the normal {@link #recvCommands()} flow.
*
* @param options
* the list of options supplied by the client. The
* {@code ReceivePack} instance takes ownership of this list.
* Callers are encouraged to first create a copy if the list may
* be modified later.
* @since 4.5
*/
public void setPushOptions(@Nullable List
* Only valid commands (those which have no obvious errors according to the
* received input and this instance's configuration) are passed into the
* hook. The hook may mark a command with a result of any value other than
* {@link org.eclipse.jgit.transport.ReceiveCommand.Result#NOT_ATTEMPTED} to
* block its execution.
*
* The hook may be called with an empty command collection if the current
* set is completely invalid.
*
* @param h
* the hook instance; may be null to disable the hook.
*/
public void setPreReceiveHook(PreReceiveHook h) {
preReceive = h != null ? h : PreReceiveHook.NULL;
}
/**
* Get the hook invoked after updates occur.
*
* @return the hook invoked after updates occur.
*/
public PostReceiveHook getPostReceiveHook() {
return postReceive;
}
/**
* Set the hook which is invoked after commands are executed.
*
* Only successful commands (type is
* {@link org.eclipse.jgit.transport.ReceiveCommand.Result#OK}) are passed
* into the hook. The hook may be called with an empty command collection if
* the current set all resulted in an error.
*
* @param h
* the hook instance; may be null to disable the hook.
*/
public void setPostReceiveHook(PostReceiveHook h) {
postReceive = h != null ? h : PostReceiveHook.NULL;
}
/**
* Get the current unpack error handler.
*
* @return the current unpack error handler.
* @since 5.8
*/
public UnpackErrorHandler getUnpackErrorHandler() {
return unpackErrorHandler;
}
/**
* Set the unpackErrorHandler
*
* @param unpackErrorHandler
* the unpackErrorHandler
* @since 5.7
*/
public void setUnpackErrorHandler(UnpackErrorHandler unpackErrorHandler) {
this.unpackErrorHandler = unpackErrorHandler;
}
/**
* Get the client session-id
*
* @return The client session-id.
* @since 6.4
*/
public String getClientSID() {
return clientSID;
}
/**
* Execute the receive task on the socket.
*
* @param input
* raw input to read client commands and pack data from. Caller
* must ensure the input is buffered, otherwise read performance
* may suffer.
* @param output
* response back to the Git network client. Caller must ensure
* the output is buffered, otherwise write performance may
* suffer.
* @param 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 java.io.IOException
* if an IO error occurred
*/
public void receive(final InputStream input, final OutputStream output,
final OutputStream messages) throws IOException {
init(input, output, messages);
try {
service();
} catch (PackProtocolException e) {
fatalError(e.getMessage());
throw e;
} catch (InputOverLimitIOException e) {
String msg = JGitText.get().tooManyCommands;
fatalError(msg);
throw new PackProtocolException(msg, e);
} finally {
try {
close();
} finally {
release();
}
}
}
/**
* Execute the receive task on the socket.
*
*
* Same as {@link #receive}, but the exceptions are not reported to the
* client yet.
*
* @param input
* raw input to read client commands and pack data from. Caller
* must ensure the input is buffered, otherwise read performance
* may suffer.
* @param output
* response back to the Git network client. Caller must ensure
* the output is buffered, otherwise write performance may
* suffer.
* @param 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 java.io.IOException
* if an IO error occurred
* @since 5.7
*/
public void receiveWithExceptionPropagation(InputStream input,
OutputStream output, OutputStream messages) throws IOException {
init(input, output, messages);
try {
service();
} finally {
try {
close();
} finally {
release();
}
}
}
private void service() throws IOException {
if (isBiDirectionalPipe()) {
sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
pckOut.flush();
} else
getAdvertisedOrDefaultRefs();
if (hasError())
return;
recvCommands();
if (hasCommands()) {
try (PostReceiveExecutor e = new PostReceiveExecutor()) {
if (needPack()) {
try {
receivePackAndCheckConnectivity();
} catch (IOException | RuntimeException
| SubmoduleValidationException | Error err) {
unlockPack();
unpackErrorHandler.handleUnpackException(err);
throw new UnpackException(err);
}
}
try {
setAtomic(isCapabilityEnabled(CAPABILITY_ATOMIC));
validateCommands();
if (atomic && anyRejects()) {
failPendingCommands();
}
preReceive.onPreReceive(
this, filterCommands(Result.NOT_ATTEMPTED));
if (atomic && anyRejects()) {
failPendingCommands();
}
executeCommands();
} finally {
unlockPack();
}
sendStatusReport(null);
}
autoGc();
}
}
private void autoGc() {
Repository repo = getRepository();
if (!repo.getConfig().getBoolean(ConfigConstants.CONFIG_RECEIVE_SECTION,
ConfigConstants.CONFIG_KEY_AUTOGC, true)) {
return;
}
repo.autoGC(NullProgressMonitor.INSTANCE);
}
static ReceiveCommand parseCommand(String line)
throws PackProtocolException {
if (line == null || line.length() < 83) {
throw new PackProtocolException(
JGitText.get().errorInvalidProtocolWantedOldNewRef);
}
String oldStr = line.substring(0, 40);
String newStr = line.substring(41, 81);
ObjectId oldId, newId;
try {
oldId = ObjectId.fromString(oldStr);
newId = ObjectId.fromString(newStr);
} catch (InvalidObjectIdException e) {
throw new PackProtocolException(
JGitText.get().errorInvalidProtocolWantedOldNewRef, e);
}
String name = line.substring(82);
if (!Repository.isValidRefName(name)) {
throw new PackProtocolException(
JGitText.get().errorInvalidProtocolWantedOldNewRef);
}
return new ReceiveCommand(oldId, newId, name);
}
private class PostReceiveExecutor implements AutoCloseable {
@Override
public void close() {
postReceive.onPostReceive(ReceivePack.this,
filterCommands(Result.OK));
}
}
private class DefaultUnpackErrorHandler implements UnpackErrorHandler {
@Override
public void handleUnpackException(Throwable t) throws IOException {
sendStatusReport(t);
}
}
}