123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017 |
- /*
- * Copyright (C) 2008-2010, Google Inc.
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- package org.eclipse.jgit.transport;
-
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- import org.eclipse.jgit.JGitText;
- import org.eclipse.jgit.errors.CorruptObjectException;
- import org.eclipse.jgit.errors.IncorrectObjectTypeException;
- import org.eclipse.jgit.errors.MissingObjectException;
- import org.eclipse.jgit.errors.PackProtocolException;
- import org.eclipse.jgit.lib.Constants;
- import org.eclipse.jgit.lib.NullProgressMonitor;
- import org.eclipse.jgit.lib.ObjectId;
- import org.eclipse.jgit.lib.ProgressMonitor;
- import org.eclipse.jgit.lib.Ref;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.revwalk.AsyncRevObjectQueue;
- import org.eclipse.jgit.revwalk.DepthWalk;
- import org.eclipse.jgit.revwalk.ObjectWalk;
- import org.eclipse.jgit.revwalk.RevCommit;
- import org.eclipse.jgit.revwalk.RevFlag;
- import org.eclipse.jgit.revwalk.RevFlagSet;
- import org.eclipse.jgit.revwalk.RevObject;
- import org.eclipse.jgit.revwalk.RevTag;
- import org.eclipse.jgit.revwalk.RevWalk;
- import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
- import org.eclipse.jgit.storage.pack.PackConfig;
- import org.eclipse.jgit.storage.pack.PackWriter;
- import org.eclipse.jgit.transport.BasePackFetchConnection.MultiAck;
- import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
- import org.eclipse.jgit.util.io.InterruptTimer;
- import org.eclipse.jgit.util.io.TimeoutInputStream;
- import org.eclipse.jgit.util.io.TimeoutOutputStream;
-
- /**
- * Implements the server side of a fetch connection, transmitting objects.
- */
- public class UploadPack {
- static final String OPTION_INCLUDE_TAG = BasePackFetchConnection.OPTION_INCLUDE_TAG;
-
- static final String OPTION_MULTI_ACK = BasePackFetchConnection.OPTION_MULTI_ACK;
-
- static final String OPTION_MULTI_ACK_DETAILED = BasePackFetchConnection.OPTION_MULTI_ACK_DETAILED;
-
- static final String OPTION_THIN_PACK = BasePackFetchConnection.OPTION_THIN_PACK;
-
- static final String OPTION_SIDE_BAND = BasePackFetchConnection.OPTION_SIDE_BAND;
-
- static final String OPTION_SIDE_BAND_64K = BasePackFetchConnection.OPTION_SIDE_BAND_64K;
-
- static final String OPTION_OFS_DELTA = BasePackFetchConnection.OPTION_OFS_DELTA;
-
- static final String OPTION_NO_PROGRESS = BasePackFetchConnection.OPTION_NO_PROGRESS;
-
- static final String OPTION_NO_DONE = BasePackFetchConnection.OPTION_NO_DONE;
-
- static final String OPTION_SHALLOW = BasePackFetchConnection.OPTION_SHALLOW;
-
- /** Database we read the objects from. */
- private final Repository db;
-
- /** Revision traversal support over {@link #db}. */
- private final RevWalk walk;
-
- /** Configuration to pass into the PackWriter. */
- private PackConfig packConfig;
-
- /** Timeout in seconds to wait for client interaction. */
- private int timeout;
-
- /**
- * Is the client connection a bi-directional socket or pipe?
- * <p>
- * 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.
- * <p>
- * 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;
-
- /** Timer to manage {@link #timeout}. */
- private InterruptTimer timer;
-
- private InputStream rawIn;
-
- private OutputStream rawOut;
-
- private PacketLineIn pckIn;
-
- private PacketLineOut pckOut;
-
- /** The refs we advertised as existing at the start of the connection. */
- private Map<String, Ref> refs;
-
- /** Filter used while advertising the refs to the client. */
- private RefFilter refFilter;
-
- /** Hook handling the various upload phases. */
- private PreUploadHook preUploadHook = PreUploadHook.NULL;
-
- /** Capabilities requested by the client. */
- private final Set<String> options = new HashSet<String>();
-
- /** Raw ObjectIds the client has asked for, before validating them. */
- private final Set<ObjectId> wantIds = new HashSet<ObjectId>();
-
- /** Objects the client wants to obtain. */
- private final Set<RevObject> wantAll = new HashSet<RevObject>();
-
- /** Objects on both sides, these don't have to be sent. */
- private final Set<RevObject> commonBase = new HashSet<RevObject>();
-
- /** Shallow commits the client already has. */
- private final Set<ObjectId> clientShallowCommits = new HashSet<ObjectId>();
-
- /** Shallow commits on the client which are now becoming unshallow */
- private final List<ObjectId> unshallowCommits = new ArrayList<ObjectId>();
-
- /** Desired depth from the client on a shallow request. */
- private int depth;
-
- /** Commit time of the oldest common commit, in seconds. */
- private int oldestTime;
-
- /** null if {@link #commonBase} should be examined again. */
- private Boolean okToGiveUp;
-
- private boolean sentReady;
-
- /** Objects we sent in our advertisement list, clients can ask for these. */
- private Set<ObjectId> advertised;
-
- /** Marked on objects the client has asked us to give them. */
- private final RevFlag WANT;
-
- /** Marked on objects both we and the client have. */
- private final RevFlag PEER_HAS;
-
- /** Marked on objects in {@link #commonBase}. */
- private final RevFlag COMMON;
-
- /** Objects where we found a path from the want list to a common base. */
- private final RevFlag SATISFIED;
-
- private final RevFlagSet SAVE;
-
- private MultiAck multiAck = MultiAck.OFF;
-
- private boolean noDone;
-
- private PackWriter.Statistics statistics;
-
- private UploadPackLogger logger;
-
- /**
- * Create a new pack upload for an open repository.
- *
- * @param copyFrom
- * the source repository.
- */
- public UploadPack(final Repository copyFrom) {
- db = copyFrom;
- walk = new RevWalk(db);
- walk.setRetainBody(false);
-
- WANT = walk.newFlag("WANT");
- PEER_HAS = walk.newFlag("PEER_HAS");
- COMMON = walk.newFlag("COMMON");
- SATISFIED = walk.newFlag("SATISFIED");
- walk.carry(PEER_HAS);
-
- SAVE = new RevFlagSet();
- SAVE.add(WANT);
- SAVE.add(PEER_HAS);
- SAVE.add(COMMON);
- SAVE.add(SATISFIED);
- refFilter = RefFilter.DEFAULT;
- }
-
- /** @return the repository this upload is reading from. */
- public final Repository getRepository() {
- return db;
- }
-
- /** @return the RevWalk instance used by this connection. */
- public final RevWalk getRevWalk() {
- return walk;
- }
-
- /** @return all refs which were advertised to the client. */
- public final Map<String, Ref> getAdvertisedRefs() {
- if (refs == null) {
- refs = refFilter.filter(db.getAllRefs());
- }
- return refs;
- }
-
- /** @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(final int seconds) {
- timeout = seconds;
- }
-
- /**
- * @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;
- }
-
- /**
- * @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(final boolean twoWay) {
- biDirectionalPipe = twoWay;
- }
-
- /** @return the filter used while advertising the refs to the client */
- public RefFilter getRefFilter() {
- return refFilter;
- }
-
- /**
- * Set the filter used while advertising the refs to the client.
- * <p>
- * Only refs allowed by this filter will be sent to the client. This can
- * be used by a server to restrict the list of references the client can
- * obtain through clone or fetch, effectively limiting the access to only
- * certain refs.
- *
- * @param refFilter
- * the filter; may be null to show all refs.
- */
- public void setRefFilter(final RefFilter refFilter) {
- this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
- }
-
- /** @return the configured upload hook. */
- public PreUploadHook getPreUploadHook() {
- return preUploadHook;
- }
-
- /**
- * Set the hook that controls how this instance will behave.
- *
- * @param hook
- * the hook; if null no special actions are taken.
- */
- public void setPreUploadHook(PreUploadHook hook) {
- preUploadHook = hook != null ? hook : PreUploadHook.NULL;
- }
-
- /**
- * Set the configuration used by the pack generator.
- *
- * @param pc
- * configuration controlling packing parameters. If null the
- * source repository's settings will be used.
- */
- public void setPackConfig(PackConfig pc) {
- this.packConfig = pc;
- }
-
- /**
- * Set the logger.
- *
- * @param logger
- * the logger instance. If null, no logging occurs.
- */
- public void setLogger(UploadPackLogger logger) {
- this.logger = logger;
- }
-
- /**
- * Execute the upload task on the socket.
- *
- * @param input
- * raw input to read client commands from. Caller must ensure the
- * input is buffered, otherwise read performance may suffer.
- * @param 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.
- * @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 IOException
- */
- public void upload(final InputStream input, final OutputStream output,
- final OutputStream messages) throws IOException {
- try {
- rawIn = input;
- rawOut = output;
-
- if (timeout > 0) {
- final Thread caller = Thread.currentThread();
- timer = new InterruptTimer(caller.getName() + "-Timer");
- TimeoutInputStream i = new TimeoutInputStream(rawIn, timer);
- TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
- i.setTimeout(timeout * 1000);
- o.setTimeout(timeout * 1000);
- rawIn = i;
- rawOut = o;
- }
-
- pckIn = new PacketLineIn(rawIn);
- pckOut = new PacketLineOut(rawOut);
- service();
- } finally {
- walk.release();
- if (timer != null) {
- try {
- timer.terminate();
- } finally {
- timer = null;
- }
- }
- }
- }
-
- /**
- * Get the PackWriter's statistics if a pack was sent to the client.
- *
- * @return statistics about pack output, if a pack was sent. Null if no pack
- * was sent, such as during the negotation phase of a smart HTTP
- * connection, or if the client was already up-to-date.
- */
- public PackWriter.Statistics getPackStatistics() {
- return statistics;
- }
-
- private void service() throws IOException {
- if (biDirectionalPipe)
- sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
- else {
- advertised = new HashSet<ObjectId>();
- for (Ref ref : getAdvertisedRefs().values()) {
- if (ref.getObjectId() != null)
- advertised.add(ref.getObjectId());
- }
- }
-
- boolean sendPack;
- try {
- recvWants();
- if (wantIds.isEmpty()) {
- preUploadHook.onBeginNegotiateRound(this, wantIds, 0);
- preUploadHook.onEndNegotiateRound(this, wantIds, 0, 0, false);
- return;
- }
-
- if (options.contains(OPTION_MULTI_ACK_DETAILED)) {
- multiAck = MultiAck.DETAILED;
- noDone = options.contains(OPTION_NO_DONE);
- } else if (options.contains(OPTION_MULTI_ACK))
- multiAck = MultiAck.CONTINUE;
- else
- multiAck = MultiAck.OFF;
-
- if (depth != 0)
- processShallow();
- sendPack = negotiate();
- } catch (PackProtocolException err) {
- reportErrorDuringNegotiate(err.getMessage());
- throw err;
-
- } catch (UploadPackMayNotContinueException err) {
- if (!err.isOutput() && err.getMessage() != null) {
- try {
- pckOut.writeString("ERR " + err.getMessage() + "\n");
- err.setOutput();
- } catch (Throwable err2) {
- // Ignore this secondary failure (and not mark output).
- }
- }
- throw err;
-
- } catch (IOException err) {
- reportErrorDuringNegotiate(JGitText.get().internalServerError);
- throw err;
- } catch (RuntimeException err) {
- reportErrorDuringNegotiate(JGitText.get().internalServerError);
- throw err;
- } catch (Error err) {
- reportErrorDuringNegotiate(JGitText.get().internalServerError);
- throw err;
- }
-
- if (sendPack)
- sendPack();
- }
-
- private void reportErrorDuringNegotiate(String msg) {
- try {
- pckOut.writeString("ERR " + msg + "\n");
- } catch (Throwable err) {
- // Ignore this secondary failure.
- }
- }
-
- private void processShallow() throws IOException {
- DepthWalk.RevWalk depthWalk =
- new DepthWalk.RevWalk(walk.getObjectReader(), depth);
-
- // Find all the commits which will be shallow
- for (ObjectId o : wantIds) {
- try {
- depthWalk.markRoot(depthWalk.parseCommit(o));
- } catch (IncorrectObjectTypeException notCommit) {
- // Ignore non-commits in this loop.
- }
- }
-
- RevCommit o;
- while ((o = depthWalk.next()) != null) {
- DepthWalk.Commit c = (DepthWalk.Commit) o;
-
- // Commits at the boundary which aren't already shallow in
- // the client need to be marked as such
- if (c.getDepth() == depth && !clientShallowCommits.contains(c))
- pckOut.writeString("shallow " + o.name());
-
- // Commits not on the boundary which are shallow in the client
- // need to become unshallowed
- if (c.getDepth() < depth && clientShallowCommits.contains(c)) {
- unshallowCommits.add(c.copy());
- pckOut.writeString("unshallow " + c.name());
- }
- }
-
- pckOut.end();
- }
-
- /**
- * Generate an advertisement of available refs and capabilities.
- *
- * @param adv
- * the advertisement formatter.
- * @throws IOException
- * the formatter failed to write an advertisement.
- * @throws UploadPackMayNotContinueException
- * the hook denied advertisement.
- */
- public void sendAdvertisedRefs(final RefAdvertiser adv) throws IOException,
- UploadPackMayNotContinueException {
- try {
- preUploadHook.onPreAdvertiseRefs(this);
- } catch (UploadPackMayNotContinueException fail) {
- if (fail.getMessage() != null) {
- adv.writeOne("ERR " + fail.getMessage());
- fail.setOutput();
- }
- throw fail;
- }
-
- adv.init(db);
- adv.advertiseCapability(OPTION_INCLUDE_TAG);
- adv.advertiseCapability(OPTION_MULTI_ACK_DETAILED);
- adv.advertiseCapability(OPTION_MULTI_ACK);
- adv.advertiseCapability(OPTION_OFS_DELTA);
- adv.advertiseCapability(OPTION_SIDE_BAND);
- adv.advertiseCapability(OPTION_SIDE_BAND_64K);
- adv.advertiseCapability(OPTION_THIN_PACK);
- adv.advertiseCapability(OPTION_NO_PROGRESS);
- adv.advertiseCapability(OPTION_SHALLOW);
- if (!biDirectionalPipe)
- adv.advertiseCapability(OPTION_NO_DONE);
- adv.setDerefTags(true);
- advertised = adv.send(getAdvertisedRefs());
- adv.end();
- }
-
- private void recvWants() throws IOException {
- boolean isFirst = true;
- for (;;) {
- String line;
- try {
- line = pckIn.readString();
- } catch (EOFException eof) {
- if (isFirst)
- break;
- throw eof;
- }
-
- if (line == PacketLineIn.END)
- break;
-
- if (line.startsWith("deepen ")) {
- depth = Integer.parseInt(line.substring(7));
- continue;
- }
-
- if (line.startsWith("shallow ")) {
- clientShallowCommits.add(ObjectId.fromString(line.substring(8)));
- continue;
- }
-
- if (!line.startsWith("want ") || line.length() < 45)
- throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "want", line));
-
- if (isFirst && line.length() > 45) {
- String opt = line.substring(45);
- if (opt.startsWith(" "))
- opt = opt.substring(1);
- for (String c : opt.split(" "))
- options.add(c);
- line = line.substring(0, 45);
- }
-
- wantIds.add(ObjectId.fromString(line.substring(5)));
- isFirst = false;
- }
- }
-
- private boolean negotiate() throws IOException {
- okToGiveUp = Boolean.FALSE;
-
- ObjectId last = ObjectId.zeroId();
- List<ObjectId> peerHas = new ArrayList<ObjectId>(64);
- for (;;) {
- String line;
- try {
- line = pckIn.readString();
- } catch (EOFException eof) {
- // EOF on stateless RPC (aka smart HTTP) and non-shallow request
- // means the client asked for the updated shallow/unshallow data,
- // disconnected, and will try another request with actual want/have.
- // Don't report the EOF here, its a bug in the protocol that the client
- // just disconnects without sending an END.
- if (!biDirectionalPipe && depth > 0)
- return false;
- throw eof;
- }
-
- if (line == PacketLineIn.END) {
- last = processHaveLines(peerHas, last);
- if (commonBase.isEmpty() || multiAck != MultiAck.OFF)
- pckOut.writeString("NAK\n");
- if (noDone && sentReady) {
- pckOut.writeString("ACK " + last.name() + "\n");
- return true;
- }
- if (!biDirectionalPipe)
- return false;
- pckOut.flush();
-
- } else if (line.startsWith("have ") && line.length() == 45) {
- peerHas.add(ObjectId.fromString(line.substring(5)));
-
- } else if (line.equals("done")) {
- last = processHaveLines(peerHas, last);
-
- if (commonBase.isEmpty())
- pckOut.writeString("NAK\n");
-
- else if (multiAck != MultiAck.OFF)
- pckOut.writeString("ACK " + last.name() + "\n");
-
- return true;
-
- } else {
- throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "have", line));
- }
- }
- }
-
- private ObjectId processHaveLines(List<ObjectId> peerHas, ObjectId last)
- throws IOException {
- preUploadHook.onBeginNegotiateRound(this, wantIds, peerHas.size());
- if (peerHas.isEmpty())
- return last;
-
- List<ObjectId> toParse = peerHas;
- HashSet<ObjectId> peerHasSet = null;
- boolean needMissing = false;
- sentReady = false;
-
- if (wantAll.isEmpty() && !wantIds.isEmpty()) {
- // We have not yet parsed the want list. Parse it now.
- peerHasSet = new HashSet<ObjectId>(peerHas);
- int cnt = wantIds.size() + peerHasSet.size();
- toParse = new ArrayList<ObjectId>(cnt);
- toParse.addAll(wantIds);
- toParse.addAll(peerHasSet);
- needMissing = true;
- }
-
- int haveCnt = 0;
- AsyncRevObjectQueue q = walk.parseAny(toParse, needMissing);
- try {
- for (;;) {
- RevObject obj;
- try {
- obj = q.next();
- } catch (MissingObjectException notFound) {
- ObjectId id = notFound.getObjectId();
- if (wantIds.contains(id)) {
- String msg = MessageFormat.format(
- JGitText.get().wantNotValid, id.name());
- throw new PackProtocolException(msg, notFound);
- }
- continue;
- }
- if (obj == null)
- break;
-
- // If the object is still found in wantIds, the want
- // list wasn't parsed earlier, and was done in this batch.
- //
- if (wantIds.remove(obj)) {
- if (!advertised.contains(obj)) {
- String msg = MessageFormat.format(
- JGitText.get().wantNotValid, obj.name());
- throw new PackProtocolException(msg);
- }
-
- if (!obj.has(WANT)) {
- obj.add(WANT);
- wantAll.add(obj);
- }
-
- if (!(obj instanceof RevCommit))
- obj.add(SATISFIED);
-
- if (obj instanceof RevTag) {
- RevObject target = walk.peel(obj);
- if (target instanceof RevCommit) {
- if (!target.has(WANT)) {
- target.add(WANT);
- wantAll.add(target);
- }
- }
- }
-
- if (!peerHasSet.contains(obj))
- continue;
- }
-
- last = obj;
- haveCnt++;
-
- if (obj instanceof RevCommit) {
- RevCommit c = (RevCommit) obj;
- if (oldestTime == 0 || c.getCommitTime() < oldestTime)
- oldestTime = c.getCommitTime();
- }
-
- if (obj.has(PEER_HAS))
- continue;
-
- obj.add(PEER_HAS);
- if (obj instanceof RevCommit)
- ((RevCommit) obj).carry(PEER_HAS);
- addCommonBase(obj);
-
- // If both sides have the same object; let the client know.
- //
- switch (multiAck) {
- case OFF:
- if (commonBase.size() == 1)
- pckOut.writeString("ACK " + obj.name() + "\n");
- break;
- case CONTINUE:
- pckOut.writeString("ACK " + obj.name() + " continue\n");
- break;
- case DETAILED:
- pckOut.writeString("ACK " + obj.name() + " common\n");
- break;
- }
- }
- } finally {
- q.release();
- }
- int missCnt = peerHas.size() - haveCnt;
-
- // If we don't have one of the objects but we're also willing to
- // create a pack at this point, let the client know so it stops
- // telling us about its history.
- //
- boolean didOkToGiveUp = false;
- if (0 < missCnt) {
- for (int i = peerHas.size() - 1; i >= 0; i--) {
- ObjectId id = peerHas.get(i);
- if (walk.lookupOrNull(id) == null) {
- didOkToGiveUp = true;
- if (okToGiveUp()) {
- switch (multiAck) {
- case OFF:
- break;
- case CONTINUE:
- pckOut.writeString("ACK " + id.name() + " continue\n");
- break;
- case DETAILED:
- pckOut.writeString("ACK " + id.name() + " ready\n");
- sentReady = true;
- break;
- }
- }
- break;
- }
- }
- }
-
- if (multiAck == MultiAck.DETAILED && !didOkToGiveUp && okToGiveUp()) {
- ObjectId id = peerHas.get(peerHas.size() - 1);
- sentReady = true;
- pckOut.writeString("ACK " + id.name() + " ready\n");
- sentReady = true;
- }
-
- preUploadHook.onEndNegotiateRound(this, wantAll, haveCnt, missCnt, sentReady);
- peerHas.clear();
- return last;
- }
-
- private void addCommonBase(final RevObject o) {
- if (!o.has(COMMON)) {
- o.add(COMMON);
- commonBase.add(o);
- okToGiveUp = null;
- }
- }
-
- private boolean okToGiveUp() throws PackProtocolException {
- if (okToGiveUp == null)
- okToGiveUp = Boolean.valueOf(okToGiveUpImp());
- return okToGiveUp.booleanValue();
- }
-
- private boolean okToGiveUpImp() throws PackProtocolException {
- if (commonBase.isEmpty())
- return false;
-
- try {
- for (RevObject obj : wantAll) {
- if (!wantSatisfied(obj))
- return false;
- }
- return true;
- } catch (IOException e) {
- throw new PackProtocolException(JGitText.get().internalRevisionError, e);
- }
- }
-
- private boolean wantSatisfied(final RevObject want) throws IOException {
- if (want.has(SATISFIED))
- return true;
-
- walk.resetRetain(SAVE);
- walk.markStart((RevCommit) want);
- if (oldestTime != 0)
- walk.setRevFilter(CommitTimeRevFilter.after(oldestTime * 1000L));
- for (;;) {
- final RevCommit c = walk.next();
- if (c == null)
- break;
- if (c.has(PEER_HAS)) {
- addCommonBase(c);
- want.add(SATISFIED);
- return true;
- }
- }
- return false;
- }
-
- private void sendPack() throws IOException {
- final boolean sideband = options.contains(OPTION_SIDE_BAND)
- || options.contains(OPTION_SIDE_BAND_64K);
-
- if (!biDirectionalPipe) {
- // Ensure the request was fully consumed. Any remaining input must
- // be a protocol error. If we aren't at EOF the implementation is broken.
- int eof = rawIn.read();
- if (0 <= eof)
- throw new CorruptObjectException(MessageFormat.format(
- JGitText.get().expectedEOFReceived,
- "\\x" + Integer.toHexString(eof)));
- }
-
- if (sideband) {
- try {
- sendPack(true);
- } catch (UploadPackMayNotContinueException noPack) {
- // This was already reported on (below).
- throw noPack;
- } catch (IOException err) {
- if (reportInternalServerErrorOverSideband())
- throw new UploadPackInternalServerErrorException(err);
- else
- throw err;
- } catch (RuntimeException err) {
- if (reportInternalServerErrorOverSideband())
- throw new UploadPackInternalServerErrorException(err);
- else
- throw err;
- } catch (Error err) {
- if (reportInternalServerErrorOverSideband())
- throw new UploadPackInternalServerErrorException(err);
- else
- throw err;
- }
- } else {
- sendPack(false);
- }
- }
-
- private boolean reportInternalServerErrorOverSideband() {
- try {
- SideBandOutputStream err = new SideBandOutputStream(
- SideBandOutputStream.CH_ERROR,
- SideBandOutputStream.SMALL_BUF,
- rawOut);
- err.write(Constants.encode(JGitText.get().internalServerError));
- err.flush();
- return true;
- } catch (Throwable cannotReport) {
- // Ignore the reason. This is a secondary failure.
- return false;
- }
- }
-
- private void sendPack(final boolean sideband) throws IOException {
- ProgressMonitor pm = NullProgressMonitor.INSTANCE;
- OutputStream packOut = rawOut;
- SideBandOutputStream msgOut = null;
-
- if (sideband) {
- int bufsz = SideBandOutputStream.SMALL_BUF;
- if (options.contains(OPTION_SIDE_BAND_64K))
- bufsz = SideBandOutputStream.MAX_BUF;
-
- packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA,
- bufsz, rawOut);
- if (!options.contains(OPTION_NO_PROGRESS)) {
- msgOut = new SideBandOutputStream(
- SideBandOutputStream.CH_PROGRESS, bufsz, rawOut);
- pm = new SideBandProgressMonitor(msgOut);
- }
- }
-
- try {
- if (wantAll.isEmpty()) {
- preUploadHook.onSendPack(this, wantIds, commonBase);
- } else {
- preUploadHook.onSendPack(this, wantAll, commonBase);
- }
- } catch (UploadPackMayNotContinueException noPack) {
- if (sideband && noPack.getMessage() != null) {
- noPack.setOutput();
- SideBandOutputStream err = new SideBandOutputStream(
- SideBandOutputStream.CH_ERROR,
- SideBandOutputStream.SMALL_BUF, rawOut);
- err.write(Constants.encode(noPack.getMessage()));
- err.flush();
- }
- throw noPack;
- }
-
- PackConfig cfg = packConfig;
- if (cfg == null)
- cfg = new PackConfig(db);
- final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
- try {
- pw.setUseCachedPacks(true);
- pw.setReuseDeltaCommits(true);
- pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
- pw.setThin(options.contains(OPTION_THIN_PACK));
- pw.setReuseValidatingObjects(false);
-
- if (commonBase.isEmpty()) {
- Set<ObjectId> tagTargets = new HashSet<ObjectId>();
- for (Ref ref : refs.values()) {
- if (ref.getPeeledObjectId() != null)
- tagTargets.add(ref.getPeeledObjectId());
- else if (ref.getObjectId() == null)
- continue;
- else if (ref.getName().startsWith(Constants.R_HEADS))
- tagTargets.add(ref.getObjectId());
- }
- pw.setTagTargets(tagTargets);
- }
-
- if (depth > 0)
- pw.setShallowPack(depth, unshallowCommits);
-
- RevWalk rw = walk;
- if (wantAll.isEmpty()) {
- pw.preparePack(pm, wantIds, commonBase);
- } else {
- walk.reset();
-
- ObjectWalk ow = walk.toObjectWalkWithSameObjects();
- pw.preparePack(pm, ow, wantAll, commonBase);
- rw = ow;
- }
-
- if (options.contains(OPTION_INCLUDE_TAG)) {
- for (Ref ref : refs.values()) {
- ObjectId objectId = ref.getObjectId();
-
- // If the object was already requested, skip it.
- if (wantAll.isEmpty()) {
- if (wantIds.contains(objectId))
- continue;
- } else {
- RevObject obj = rw.lookupOrNull(objectId);
- if (obj != null && obj.has(WANT))
- continue;
- }
-
- if (!ref.isPeeled())
- ref = db.peel(ref);
-
- ObjectId peeledId = ref.getPeeledObjectId();
- if (peeledId == null)
- continue;
-
- objectId = ref.getObjectId();
- if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
- pw.addObject(rw.parseAny(objectId));
- }
- }
-
- pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
- statistics = pw.getStatistics();
-
- if (msgOut != null) {
- String msg = pw.getStatistics().getMessage() + '\n';
- msgOut.write(Constants.encode(msg));
- msgOut.flush();
- }
-
- } finally {
- pw.release();
- }
-
- if (sideband)
- pckOut.end();
-
- if (logger != null && statistics != null)
- logger.onPackStatistics(statistics);
- }
- }
|