2 * Copyright (C) 2008-2010, Google Inc.
3 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * and other copyright owners as documented in the project's IP log.
8 * This program and the accompanying materials are made available
9 * under the terms of the Eclipse Distribution License v1.0 which
10 * accompanies this distribution, is reproduced below, and is
11 * available at http://www.eclipse.org/org/documents/edl-v10.php
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials provided
25 * with the distribution.
27 * - Neither the name of the Eclipse Foundation, Inc. nor the
28 * names of its contributors may be used to endorse or promote
29 * products derived from this software without specific prior
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 package org
.eclipse
.jgit
.transport
;
49 import java
.io
.EOFException
;
50 import java
.io
.IOException
;
51 import java
.io
.InputStream
;
52 import java
.io
.OutputStream
;
53 import java
.util
.HashSet
;
54 import java
.util
.LinkedHashMap
;
57 import org
.eclipse
.jgit
.errors
.NoRemoteRepositoryException
;
58 import org
.eclipse
.jgit
.errors
.PackProtocolException
;
59 import org
.eclipse
.jgit
.errors
.RemoteRepositoryException
;
60 import org
.eclipse
.jgit
.errors
.TransportException
;
61 import org
.eclipse
.jgit
.lib
.ObjectId
;
62 import org
.eclipse
.jgit
.lib
.ObjectIdRef
;
63 import org
.eclipse
.jgit
.lib
.Ref
;
64 import org
.eclipse
.jgit
.lib
.Repository
;
65 import org
.eclipse
.jgit
.util
.io
.InterruptTimer
;
66 import org
.eclipse
.jgit
.util
.io
.TimeoutInputStream
;
67 import org
.eclipse
.jgit
.util
.io
.TimeoutOutputStream
;
70 * Base helper class for pack-based operations implementations. Provides partial
71 * implementation of pack-protocol - refs advertising and capabilities support,
72 * and some other helper methods.
74 * @see BasePackFetchConnection
75 * @see BasePackPushConnection
77 abstract class BasePackConnection
extends BaseConnection
{
79 /** The repository this transport fetches into, or pushes out of. */
80 protected final Repository local
;
82 /** Remote repository location. */
83 protected final URIish uri
;
85 /** A transport connected to {@link #uri}. */
86 protected final Transport transport
;
88 /** Low-level input stream, if a timeout was configured. */
89 protected TimeoutInputStream timeoutIn
;
91 /** Low-level output stream, if a timeout was configured. */
92 protected TimeoutOutputStream timeoutOut
;
94 /** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
95 private InterruptTimer myTimer
;
97 /** Input stream reading from the remote. */
98 protected InputStream in
;
100 /** Output stream sending to the remote. */
101 protected OutputStream out
;
103 /** Packet line decoder around {@link #in}. */
104 protected PacketLineIn pckIn
;
106 /** Packet line encoder around {@link #out}. */
107 protected PacketLineOut pckOut
;
109 /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
110 protected boolean outNeedsEnd
;
112 /** True if this is a stateless RPC connection. */
113 protected boolean statelessRPC
;
115 /** Capability tokens advertised by the remote side. */
116 private final Set
<String
> remoteCapablities
= new HashSet
<String
>();
118 /** Extra objects the remote has, but which aren't offered as refs. */
119 protected final Set
<ObjectId
> additionalHaves
= new HashSet
<ObjectId
>();
121 BasePackConnection(final PackTransport packTransport
) {
122 transport
= (Transport
) packTransport
;
123 local
= transport
.local
;
128 * Configure this connection with the directional pipes.
131 * input stream to receive data from the peer. Caller must ensure
132 * the input is buffered, otherwise read performance may suffer.
134 * output stream to transmit data to the peer. Caller must ensure
135 * the output is buffered, otherwise write performance may
138 protected final void init(InputStream myIn
, OutputStream myOut
) {
139 final int timeout
= transport
.getTimeout();
141 final Thread caller
= Thread
.currentThread();
142 myTimer
= new InterruptTimer(caller
.getName() + "-Timer");
143 timeoutIn
= new TimeoutInputStream(myIn
, myTimer
);
144 timeoutOut
= new TimeoutOutputStream(myOut
, myTimer
);
145 timeoutIn
.setTimeout(timeout
* 1000);
146 timeoutOut
.setTimeout(timeout
* 1000);
154 pckIn
= new PacketLineIn(in
);
155 pckOut
= new PacketLineOut(out
);
160 * Reads the advertised references through the initialized stream.
162 * Subclass implementations may call this method only after setting up the
163 * input and output streams with {@link #init(InputStream, OutputStream)}.
165 * If any errors occur, this connection is automatically closed by invoking
166 * {@link #close()} and the exception is wrapped (if necessary) and thrown
167 * as a {@link TransportException}.
169 * @throws TransportException
170 * the reference list could not be scanned.
172 protected void readAdvertisedRefs() throws TransportException
{
174 readAdvertisedRefsImpl();
175 } catch (TransportException err
) {
178 } catch (IOException err
) {
180 throw new TransportException(err
.getMessage(), err
);
181 } catch (RuntimeException err
) {
183 throw new TransportException(err
.getMessage(), err
);
187 private void readAdvertisedRefsImpl() throws IOException
{
188 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
193 line
= pckIn
.readString();
194 } catch (EOFException eof
) {
196 throw noRepository();
199 if (line
== PacketLineIn
.END
)
202 if (line
.startsWith("ERR ")) {
203 // This is a customized remote service error.
204 // Users should be informed about it.
205 throw new RemoteRepositoryException(uri
, line
.substring(4));
208 if (avail
.isEmpty()) {
209 final int nul
= line
.indexOf('\0');
211 // The first line (if any) may contain "hidden"
212 // capability values after a NUL byte.
213 for (String c
: line
.substring(nul
+ 1).split(" "))
214 remoteCapablities
.add(c
);
215 line
= line
.substring(0, nul
);
219 String name
= line
.substring(41, line
.length());
220 if (avail
.isEmpty() && name
.equals("capabilities^{}")) {
221 // special line from git-receive-pack to show
222 // capabilities when there are no refs to advertise
226 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
227 if (name
.equals(".have")) {
228 additionalHaves
.add(id
);
229 } else if (name
.endsWith("^{}")) {
230 name
= name
.substring(0, name
.length() - 3);
231 final Ref prior
= avail
.get(name
);
233 throw new PackProtocolException(uri
, "advertisement of "
234 + name
+ "^{} came before " + name
);
236 if (prior
.getPeeledObjectId() != null)
237 throw duplicateAdvertisement(name
+ "^{}");
239 avail
.put(name
, new ObjectIdRef
.PeeledTag(
240 Ref
.Storage
.NETWORK
, name
, prior
.getObjectId(), id
));
242 final Ref prior
= avail
.put(name
, new ObjectIdRef
.PeeledNonTag(
243 Ref
.Storage
.NETWORK
, name
, id
));
245 throw duplicateAdvertisement(name
);
252 * Create an exception to indicate problems finding a remote repository. The
253 * caller is expected to throw the returned exception.
255 * Subclasses may override this method to provide better diagnostics.
257 * @return a TransportException saying a repository cannot be found and
260 protected TransportException
noRepository() {
261 return new NoRemoteRepositoryException(uri
, "not found.");
264 protected boolean isCapableOf(final String option
) {
265 return remoteCapablities
.contains(option
);
268 protected boolean wantCapability(final StringBuilder b
, final String option
) {
269 if (!isCapableOf(option
))
276 private PackProtocolException
duplicateAdvertisement(final String name
) {
277 return new PackProtocolException(uri
, "duplicate advertisements of "
282 public void close() {
288 } catch (IOException err
) {
289 // Ignore any close errors.
299 } catch (IOException err
) {
300 // Ignore any close errors.
307 if (myTimer
!= null) {