2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org
.spearce
.jgit
.transport
;
42 import java
.io
.BufferedInputStream
;
43 import java
.io
.BufferedOutputStream
;
44 import java
.io
.EOFException
;
45 import java
.io
.IOException
;
46 import java
.io
.InputStream
;
47 import java
.io
.OutputStream
;
48 import java
.util
.HashSet
;
49 import java
.util
.LinkedHashMap
;
52 import org
.spearce
.jgit
.errors
.NoRemoteRepositoryException
;
53 import org
.spearce
.jgit
.errors
.PackProtocolException
;
54 import org
.spearce
.jgit
.errors
.TransportException
;
55 import org
.spearce
.jgit
.lib
.ObjectId
;
56 import org
.spearce
.jgit
.lib
.Ref
;
57 import org
.spearce
.jgit
.lib
.Repository
;
60 * Base helper class for pack-based operations implementations. Provides partial
61 * implementation of pack-protocol - refs advertising and capabilities support,
62 * and some other helper methods.
64 * @see BasePackFetchConnection
65 * @see BasePackPushConnection
67 abstract class BasePackConnection
extends BaseConnection
{
69 /** The repository this transport fetches into, or pushes out of. */
70 protected final Repository local
;
72 /** Remote repository location. */
73 protected final URIish uri
;
75 /** A transport connected to {@link #uri}. */
76 protected final PackTransport transport
;
78 /** Buffered input stream reading from the remote. */
79 protected InputStream in
;
81 /** Buffered output stream sending to the remote. */
82 protected OutputStream out
;
84 /** Packet line decoder around {@link #in}. */
85 protected PacketLineIn pckIn
;
87 /** Packet line encoder around {@link #out}. */
88 protected PacketLineOut pckOut
;
90 /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
91 protected boolean outNeedsEnd
;
93 /** Capability tokens advertised by the remote side. */
94 private final Set
<String
> remoteCapablities
= new HashSet
<String
>();
96 /** Extra objects the remote has, but which aren't offered as refs. */
97 protected final Set
<ObjectId
> additionalHaves
= new HashSet
<ObjectId
>();
99 BasePackConnection(final PackTransport packTransport
) {
100 local
= packTransport
.local
;
101 uri
= packTransport
.uri
;
102 transport
= packTransport
;
105 protected void init(final InputStream myIn
, final OutputStream myOut
) {
106 in
= myIn
instanceof BufferedInputStream ? myIn
107 : new BufferedInputStream(myIn
, IndexPack
.BUFFER_SIZE
);
108 out
= myOut
instanceof BufferedOutputStream ? myOut
109 : new BufferedOutputStream(myOut
);
111 pckIn
= new PacketLineIn(in
);
112 pckOut
= new PacketLineOut(out
);
116 protected void readAdvertisedRefs() throws TransportException
{
118 readAdvertisedRefsImpl();
119 } catch (TransportException err
) {
122 } catch (IOException err
) {
124 throw new TransportException(err
.getMessage(), err
);
125 } catch (RuntimeException err
) {
127 throw new TransportException(err
.getMessage(), err
);
131 private void readAdvertisedRefsImpl() throws IOException
{
132 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
137 line
= pckIn
.readString();
138 } catch (EOFException eof
) {
140 throw noRepository();
144 if (avail
.isEmpty()) {
145 final int nul
= line
.indexOf('\0');
147 // The first line (if any) may contain "hidden"
148 // capability values after a NUL byte.
149 for (String c
: line
.substring(nul
+ 1).split(" "))
150 remoteCapablities
.add(c
);
151 line
= line
.substring(0, nul
);
155 if (line
.length() == 0)
158 String name
= line
.substring(41, line
.length());
159 if (avail
.isEmpty() && name
.equals("capabilities^{}")) {
160 // special line from git-receive-pack to show
161 // capabilities when there are no refs to advertise
165 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
166 if (name
.equals(".have")) {
167 additionalHaves
.add(id
);
168 } else if (name
.endsWith("^{}")) {
169 name
= name
.substring(0, name
.length() - 3);
170 final Ref prior
= avail
.get(name
);
172 throw new PackProtocolException(uri
, "advertisement of "
173 + name
+ "^{} came before " + name
);
175 if (prior
.getPeeledObjectId() != null)
176 throw duplicateAdvertisement(name
+ "^{}");
178 avail
.put(name
, new Ref(Ref
.Storage
.NETWORK
, name
, prior
179 .getObjectId(), id
, true));
182 prior
= avail
.put(name
, new Ref(Ref
.Storage
.NETWORK
, name
, id
));
184 throw duplicateAdvertisement(name
);
191 * Create an exception to indicate problems finding a remote repository. The
192 * caller is expected to throw the returned exception.
194 * Subclasses may override this method to provide better diagnostics.
196 * @return a TransportException saying a repository cannot be found and
199 protected TransportException
noRepository() {
200 return new NoRemoteRepositoryException(uri
, "not found.");
203 protected boolean isCapableOf(final String option
) {
204 return remoteCapablities
.contains(option
);
207 protected boolean wantCapability(final StringBuilder b
, final String option
) {
208 if (!isCapableOf(option
))
216 private PackProtocolException
duplicateAdvertisement(final String name
) {
217 return new PackProtocolException(uri
, "duplicate advertisements of "
222 public void close() {
228 } catch (IOException err
) {
229 // Ignore any close errors.
239 } catch (IOException err
) {
240 // Ignore any close errors.