Honor receive.fsckobjects during any fetch connection
[egit/graphgui.git] / org.spearce.jgit / src / org / spearce / jgit / transport / BasePackFetchConnection.java
bloba542eb752e37b7a7612e81558781f42315b83e19
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.transport;
41 import java.io.IOException;
42 import java.util.Collection;
43 import java.util.Date;
45 import org.spearce.jgit.errors.TransportException;
46 import org.spearce.jgit.lib.AnyObjectId;
47 import org.spearce.jgit.lib.MutableObjectId;
48 import org.spearce.jgit.lib.ProgressMonitor;
49 import org.spearce.jgit.lib.Ref;
50 import org.spearce.jgit.revwalk.RevCommit;
51 import org.spearce.jgit.revwalk.RevCommitList;
52 import org.spearce.jgit.revwalk.RevFlag;
53 import org.spearce.jgit.revwalk.RevObject;
54 import org.spearce.jgit.revwalk.RevSort;
55 import org.spearce.jgit.revwalk.RevWalk;
56 import org.spearce.jgit.revwalk.filter.CommitTimeRevFilter;
57 import org.spearce.jgit.revwalk.filter.RevFilter;
59 /**
60 * Fetch implementation using the native Git pack transfer service.
61 * <p>
62 * This is the canonical implementation for transferring objects from the remote
63 * repository to the local repository by talking to the 'git-upload-pack'
64 * service. Objects are packed on the remote side into a pack file and then sent
65 * down the pipe to us.
66 * <p>
67 * This connection requires only a bi-directional pipe or socket, and thus is
68 * easily wrapped up into a local process pipe, anonymous TCP socket, or a
69 * command executed through an SSH tunnel.
70 * <p>
71 * Concrete implementations should just call
72 * {@link #init(java.io.InputStream, java.io.OutputStream)} and
73 * {@link #readAdvertisedRefs()} methods in constructor or before any use. They
74 * should also handle resources releasing in {@link #close()} method if needed.
76 abstract class BasePackFetchConnection extends BasePackConnection implements
77 FetchConnection {
78 /**
79 * Maximum number of 'have' lines to send before giving up.
80 * <p>
81 * During {@link #negotiate(ProgressMonitor)} we send at most this many
82 * commits to the remote peer as 'have' lines without an ACK response before
83 * we give up.
85 private static final int MAX_HAVES = 256;
87 static final String OPTION_INCLUDE_TAG = "include-tag";
89 static final String OPTION_MULTI_ACK = "multi_ack";
91 static final String OPTION_THIN_PACK = "thin-pack";
93 static final String OPTION_SIDE_BAND = "side-band";
95 static final String OPTION_SIDE_BAND_64K = "side-band-64k";
97 static final String OPTION_OFS_DELTA = "ofs-delta";
99 static final String OPTION_SHALLOW = "shallow";
101 private final RevWalk walk;
103 /** All commits that are immediately reachable by a local ref. */
104 private RevCommitList<RevCommit> reachableCommits;
106 /** Marks an object as having all its dependencies. */
107 final RevFlag REACHABLE;
109 /** Marks a commit known to both sides of the connection. */
110 final RevFlag COMMON;
112 /** Marks a commit listed in the advertised refs. */
113 final RevFlag ADVERTISED;
115 private boolean multiAck;
117 private boolean thinPack;
119 private boolean sideband;
121 private boolean includeTags;
123 BasePackFetchConnection(final PackTransport packTransport) {
124 super(packTransport);
125 includeTags = packTransport.getTagOpt() != TagOpt.NO_TAGS;
126 thinPack = packTransport.isFetchThin();
128 walk = new RevWalk(local);
129 reachableCommits = new RevCommitList<RevCommit>();
130 REACHABLE = walk.newFlag("REACHABLE");
131 COMMON = walk.newFlag("COMMON");
132 ADVERTISED = walk.newFlag("ADVERTISED");
134 walk.carry(COMMON);
135 walk.carry(REACHABLE);
136 walk.carry(ADVERTISED);
139 public final void fetch(final ProgressMonitor monitor,
140 final Collection<Ref> want) throws TransportException {
141 markStartedOperation();
142 doFetch(monitor, want);
145 public boolean didFetchIncludeTags() {
146 return false;
149 protected void doFetch(final ProgressMonitor monitor,
150 final Collection<Ref> want) throws TransportException {
151 try {
152 markRefsAdvertised();
153 markReachable(maxTimeWanted(want));
155 if (sendWants(want)) {
156 negotiate(monitor);
158 walk.dispose();
159 reachableCommits = null;
161 receivePack(monitor);
163 } catch (CancelledException ce) {
164 close();
165 return; // Caller should test (or just know) this themselves.
166 } catch (IOException err) {
167 close();
168 throw new TransportException(err.getMessage(), err);
169 } catch (RuntimeException err) {
170 close();
171 throw new TransportException(err.getMessage(), err);
175 private int maxTimeWanted(final Collection<Ref> wants) {
176 int maxTime = 0;
177 for (final Ref r : wants) {
178 try {
179 final RevObject obj = walk.parseAny(r.getObjectId());
180 if (obj instanceof RevCommit) {
181 final int cTime = ((RevCommit) obj).getCommitTime();
182 if (maxTime < cTime)
183 maxTime = cTime;
185 } catch (IOException error) {
186 // We don't have it, but we want to fetch (thus fixing error).
189 return maxTime;
192 private void markReachable(final int maxTime) throws IOException {
193 for (final Ref r : local.getAllRefs().values()) {
194 try {
195 final RevCommit o = walk.parseCommit(r.getObjectId());
196 o.add(REACHABLE);
197 reachableCommits.add(o);
198 } catch (IOException readError) {
199 // If we cannot read the value of the ref skip it.
203 if (maxTime > 0) {
204 // Mark reachable commits until we reach maxTime. These may
205 // wind up later matching up against things we want and we
206 // can avoid asking for something we already happen to have.
208 final Date maxWhen = new Date(maxTime * 1000L);
209 walk.sort(RevSort.COMMIT_TIME_DESC);
210 walk.markStart(reachableCommits);
211 walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
212 for (;;) {
213 final RevCommit c = walk.next();
214 if (c == null)
215 break;
216 if (c.has(ADVERTISED) && !c.has(COMMON)) {
217 // This is actually going to be a common commit, but
218 // our peer doesn't know that fact yet.
220 c.add(COMMON);
221 c.carry(COMMON);
222 reachableCommits.add(c);
228 private boolean sendWants(final Collection<Ref> want) throws IOException {
229 boolean first = true;
230 for (final Ref r : want) {
231 try {
232 if (walk.parseAny(r.getObjectId()).has(REACHABLE)) {
233 // We already have this object. Asking for it is
234 // not a very good idea.
236 continue;
238 } catch (IOException err) {
239 // Its OK, we don't have it, but we want to fix that
240 // by fetching the object from the other side.
243 final StringBuilder line = new StringBuilder(46);
244 line.append("want ");
245 line.append(r.getObjectId().name());
246 if (first) {
247 line.append(enableCapabilities());
248 first = false;
250 line.append('\n');
251 pckOut.writeString(line.toString());
253 pckOut.end();
254 outNeedsEnd = false;
255 return !first;
258 private String enableCapabilities() {
259 final StringBuilder line = new StringBuilder();
260 if (includeTags)
261 includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
262 wantCapability(line, OPTION_OFS_DELTA);
263 multiAck = wantCapability(line, OPTION_MULTI_ACK);
264 if (thinPack)
265 thinPack = wantCapability(line, OPTION_THIN_PACK);
266 if (wantCapability(line, OPTION_SIDE_BAND_64K))
267 sideband = true;
268 else if (wantCapability(line, OPTION_SIDE_BAND))
269 sideband = true;
270 return line.toString();
273 private void negotiate(final ProgressMonitor monitor) throws IOException,
274 CancelledException {
275 final MutableObjectId ackId = new MutableObjectId();
276 int resultsPending = 0;
277 int havesSent = 0;
278 int havesSinceLastContinue = 0;
279 boolean receivedContinue = false;
280 boolean receivedAck = false;
281 boolean sendHaves = true;
283 negotiateBegin();
284 while (sendHaves) {
285 final RevCommit c = walk.next();
286 if (c == null)
287 break;
289 pckOut.writeString("have " + c.getId().name() + "\n");
290 havesSent++;
291 havesSinceLastContinue++;
293 if ((31 & havesSent) != 0) {
294 // We group the have lines into blocks of 32, each marked
295 // with a flush (aka end). This one is within a block so
296 // continue with another have line.
298 continue;
301 if (monitor.isCancelled())
302 throw new CancelledException();
304 pckOut.end();
305 resultsPending++; // Each end will cause a result to come back.
307 if (havesSent == 32) {
308 // On the first block we race ahead and try to send
309 // more of the second block while waiting for the
310 // remote to respond to our first block request.
311 // This keeps us one block ahead of the peer.
313 continue;
316 while (resultsPending > 0) {
317 final PacketLineIn.AckNackResult anr;
319 anr = pckIn.readACK(ackId);
320 resultsPending--;
321 if (anr == PacketLineIn.AckNackResult.NAK) {
322 // More have lines are necessary to compute the
323 // pack on the remote side. Keep doing that.
325 break;
328 if (anr == PacketLineIn.AckNackResult.ACK) {
329 // The remote side is happy and knows exactly what
330 // to send us. There is no further negotiation and
331 // we can break out immediately.
333 multiAck = false;
334 resultsPending = 0;
335 receivedAck = true;
336 sendHaves = false;
337 break;
340 if (anr == PacketLineIn.AckNackResult.ACK_CONTINUE) {
341 // The server knows this commit (ackId). We don't
342 // need to send any further along its ancestry, but
343 // we need to continue to talk about other parts of
344 // our local history.
346 markCommon(walk.parseAny(ackId));
347 receivedAck = true;
348 receivedContinue = true;
349 havesSinceLastContinue = 0;
352 if (monitor.isCancelled())
353 throw new CancelledException();
356 if (receivedContinue && havesSinceLastContinue > MAX_HAVES) {
357 // Our history must be really different from the remote's.
358 // We just sent a whole slew of have lines, and it did not
359 // recognize any of them. Avoid sending our entire history
360 // to them by giving up early.
362 break;
366 // Tell the remote side we have run out of things to talk about.
368 if (monitor.isCancelled())
369 throw new CancelledException();
370 pckOut.writeString("done\n");
371 pckOut.flush();
373 if (!receivedAck) {
374 // Apparently if we have never received an ACK earlier
375 // there is one more result expected from the done we
376 // just sent to the remote.
378 multiAck = false;
379 resultsPending++;
382 while (resultsPending > 0 || multiAck) {
383 final PacketLineIn.AckNackResult anr;
385 anr = pckIn.readACK(ackId);
386 resultsPending--;
388 if (anr == PacketLineIn.AckNackResult.ACK)
389 break; // commit negotiation is finished.
391 if (anr == PacketLineIn.AckNackResult.ACK_CONTINUE) {
392 // There must be a normal ACK following this.
394 multiAck = true;
397 if (monitor.isCancelled())
398 throw new CancelledException();
402 private void negotiateBegin() throws IOException {
403 walk.resetRetain(REACHABLE, ADVERTISED);
404 walk.markStart(reachableCommits);
405 walk.sort(RevSort.COMMIT_TIME_DESC);
406 walk.setRevFilter(new RevFilter() {
407 @Override
408 public RevFilter clone() {
409 return this;
412 @Override
413 public boolean include(final RevWalk walker, final RevCommit c) {
414 final boolean remoteKnowsIsCommon = c.has(COMMON);
415 if (c.has(ADVERTISED)) {
416 // Remote advertised this, and we have it, hence common.
417 // Whether or not the remote knows that fact is tested
418 // before we added the flag. If the remote doesn't know
419 // we have to still send them this object.
421 c.add(COMMON);
423 return !remoteKnowsIsCommon;
428 private void markRefsAdvertised() {
429 for (final Ref r : getRefs()) {
430 markAdvertised(r.getObjectId());
431 if (r.getPeeledObjectId() != null)
432 markAdvertised(r.getPeeledObjectId());
436 private void markAdvertised(final AnyObjectId id) {
437 try {
438 walk.parseAny(id).add(ADVERTISED);
439 } catch (IOException readError) {
440 // We probably just do not have this object locally.
444 private void markCommon(final RevObject obj) {
445 obj.add(COMMON);
446 if (obj instanceof RevCommit)
447 ((RevCommit) obj).carry(COMMON);
450 private void receivePack(final ProgressMonitor monitor) throws IOException {
451 final IndexPack ip;
453 ip = IndexPack.create(local, sideband ? pckIn.sideband(monitor) : in);
454 ip.setFixThin(thinPack);
455 ip.setObjectChecking(transport.isCheckFetchedObjects());
456 ip.index(monitor);
457 ip.renameAndOpenPack();
460 private static class CancelledException extends Exception {
461 private static final long serialVersionUID = 1L;