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
.PackProtocolException
;
53 import org
.spearce
.jgit
.errors
.TransportException
;
54 import org
.spearce
.jgit
.lib
.ObjectId
;
55 import org
.spearce
.jgit
.lib
.Ref
;
56 import org
.spearce
.jgit
.lib
.Repository
;
59 * Base helper class for pack-based operations implementations. Provides partial
60 * implementation of pack-protocol - refs advertising and capabilities support,
61 * and some other helper methods.
63 * @see BasePackFetchConnection
64 * @see BasePackPushConnection
66 abstract class BasePackConnection
extends BaseConnection
{
68 /** The repository this transport fetches into, or pushes out of. */
69 protected final Repository local
;
71 /** Remote repository location. */
72 protected final URIish uri
;
74 /** Buffered input stream reading from the remote. */
75 protected InputStream in
;
77 /** Buffered output stream sending to the remote. */
78 protected OutputStream out
;
80 /** Packet line decoder around {@link #in}. */
81 protected PacketLineIn pckIn
;
83 /** Packet line encoder around {@link #out}. */
84 protected PacketLineOut pckOut
;
86 /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
87 protected boolean outNeedsEnd
;
89 /** Capability tokens advertised by the remote side. */
90 private final Set
<String
> remoteCapablities
= new HashSet
<String
>();
92 BasePackConnection(final PackTransport packTransport
) {
93 local
= packTransport
.local
;
94 uri
= packTransport
.uri
;
97 protected void init(final InputStream myIn
, final OutputStream myOut
) {
98 in
= myIn
instanceof BufferedInputStream ? myIn
99 : new BufferedInputStream(myIn
);
100 out
= myOut
instanceof BufferedOutputStream ? myOut
101 : new BufferedOutputStream(myOut
);
103 pckIn
= new PacketLineIn(in
);
104 pckOut
= new PacketLineOut(out
);
108 protected void readAdvertisedRefs() throws TransportException
{
110 readAdvertisedRefsImpl();
111 } catch (TransportException err
) {
114 } catch (IOException err
) {
116 throw new TransportException(err
.getMessage(), err
);
117 } catch (RuntimeException err
) {
119 throw new TransportException(err
.getMessage(), err
);
123 private void readAdvertisedRefsImpl() throws IOException
{
124 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
129 line
= pckIn
.readString();
130 } catch (EOFException eof
) {
132 throw new TransportException(uri
, "not found.");
136 if (avail
.isEmpty()) {
137 final int nul
= line
.indexOf('\0');
139 // The first line (if any) may contain "hidden"
140 // capability values after a NUL byte.
141 for (String c
: line
.substring(nul
+ 1).split(" "))
142 remoteCapablities
.add(c
);
143 line
= line
.substring(0, nul
);
146 if (line
.equals("capabilties^{}")) {
147 // special line from git-receive-pack to show
148 // capabilities when there are no refs to advertise
153 if (line
.length() == 0)
156 String name
= line
.substring(41, line
.length());
157 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
158 if (name
.endsWith("^{}")) {
159 name
= name
.substring(0, name
.length() - 3);
160 final Ref prior
= avail
.get(name
);
162 throw new PackProtocolException(uri
, "advertisement of "
163 + name
+ "^{} came before " + name
);
165 if (prior
.getPeeledObjectId() != null)
166 throw duplicateAdvertisement(name
+ "^{}");
168 avail
.put(name
, new Ref(Ref
.Storage
.NETWORK
, name
, prior
169 .getObjectId(), id
));
172 prior
= avail
.put(name
, new Ref(Ref
.Storage
.NETWORK
, name
, id
));
174 throw duplicateAdvertisement(name
);
180 protected boolean isCapableOf(final String option
) {
181 return remoteCapablities
.contains(option
);
184 protected boolean wantCapability(final StringBuilder b
, final String option
) {
185 if (!isCapableOf(option
))
193 private PackProtocolException
duplicateAdvertisement(final String name
) {
194 return new PackProtocolException(uri
, "duplicate advertisements of "
199 public void close() {
205 } catch (IOException err
) {
206 // Ignore any close errors.
216 } catch (IOException err
) {
217 // Ignore any close errors.