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 /** Capability tokens advertised by the remote side. */
87 private final Set
<String
> remoteCapablities
= new HashSet
<String
>();
89 BasePackConnection(final PackTransport packTransport
) {
90 local
= packTransport
.local
;
91 uri
= packTransport
.uri
;
94 protected void init(final InputStream myIn
, final OutputStream myOut
) {
95 in
= myIn
instanceof BufferedInputStream ? myIn
96 : new BufferedInputStream(myIn
);
97 out
= myOut
instanceof BufferedOutputStream ? myOut
98 : new BufferedOutputStream(myOut
);
100 pckIn
= new PacketLineIn(in
);
101 pckOut
= new PacketLineOut(out
);
104 protected void readAdvertisedRefs() throws TransportException
{
106 readAdvertisedRefsImpl();
107 } catch (TransportException err
) {
110 } catch (IOException err
) {
112 throw new TransportException(err
.getMessage(), err
);
113 } catch (RuntimeException err
) {
115 throw new TransportException(err
.getMessage(), err
);
119 private void readAdvertisedRefsImpl() throws IOException
{
120 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
125 line
= pckIn
.readString();
126 } catch (EOFException eof
) {
128 throw new TransportException(uri
+ " not found.");
132 if (avail
.isEmpty()) {
133 final int nul
= line
.indexOf('\0');
135 // The first line (if any) may contain "hidden"
136 // capability values after a NUL byte.
137 for (String c
: line
.substring(nul
+ 1).split(" "))
138 remoteCapablities
.add(c
);
139 line
= line
.substring(0, nul
);
142 if (line
.equals("capabilties^{}")) {
143 // special line from git-receive-pack to show
144 // capabilities when there are no refs to advertise
149 if (line
.length() == 0)
152 String name
= line
.substring(41, line
.length());
153 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
154 if (name
.endsWith("^{}")) {
155 name
= name
.substring(0, name
.length() - 3);
156 final Ref prior
= avail
.get(name
);
158 throw new PackProtocolException(uri
+ ": advertisement of "
159 + name
+ "^{} came before " + name
);
161 if (prior
.getPeeledObjectId() != null)
162 throw duplicateAdvertisement(name
+ "^{}");
164 avail
.put(name
, new Ref(name
, prior
.getObjectId(), id
));
166 final Ref prior
= avail
.put(name
, new Ref(name
, id
));
168 throw duplicateAdvertisement(name
);
174 protected boolean isCapableOf(final String option
) {
175 return remoteCapablities
.contains(option
);
178 protected boolean wantCapability(final StringBuilder b
, final String option
) {
179 if (!isCapableOf(option
))
187 private PackProtocolException
duplicateAdvertisement(final String name
) {
188 return new PackProtocolException(uri
+ ": duplicate advertisements of "
193 public void close() {
198 } catch (IOException err
) {
199 // Ignore any close errors.
209 } catch (IOException err
) {
210 // Ignore any close errors.