Disambiguate pkt-line "0000" from "0004"
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / transport / BasePackConnection.java
blob0382d2bfcf28b579097d303611e639f3b44dc8fb
1 /*
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>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
10 * conditions are met:
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
23 * written permission.
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;
50 import java.util.Set;
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;
59 /**
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 Transport 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 transport = (Transport)packTransport;
101 local = transport.local;
102 uri = transport.uri;
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);
113 outNeedsEnd = true;
116 protected void readAdvertisedRefs() throws TransportException {
117 try {
118 readAdvertisedRefsImpl();
119 } catch (TransportException err) {
120 close();
121 throw err;
122 } catch (IOException err) {
123 close();
124 throw new TransportException(err.getMessage(), err);
125 } catch (RuntimeException err) {
126 close();
127 throw new TransportException(err.getMessage(), err);
131 private void readAdvertisedRefsImpl() throws IOException {
132 final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
133 for (;;) {
134 String line;
136 try {
137 line = pckIn.readString();
138 } catch (EOFException eof) {
139 if (avail.isEmpty())
140 throw noRepository();
141 throw eof;
143 if (line == PacketLineIn.END)
144 break;
146 if (avail.isEmpty()) {
147 final int nul = line.indexOf('\0');
148 if (nul >= 0) {
149 // The first line (if any) may contain "hidden"
150 // capability values after a NUL byte.
151 for (String c : line.substring(nul + 1).split(" "))
152 remoteCapablities.add(c);
153 line = line.substring(0, nul);
157 String name = line.substring(41, line.length());
158 if (avail.isEmpty() && name.equals("capabilities^{}")) {
159 // special line from git-receive-pack to show
160 // capabilities when there are no refs to advertise
161 continue;
164 final ObjectId id = ObjectId.fromString(line.substring(0, 40));
165 if (name.equals(".have")) {
166 additionalHaves.add(id);
167 } else if (name.endsWith("^{}")) {
168 name = name.substring(0, name.length() - 3);
169 final Ref prior = avail.get(name);
170 if (prior == null)
171 throw new PackProtocolException(uri, "advertisement of "
172 + name + "^{} came before " + name);
174 if (prior.getPeeledObjectId() != null)
175 throw duplicateAdvertisement(name + "^{}");
177 avail.put(name, new Ref(Ref.Storage.NETWORK, name, prior
178 .getObjectId(), id, true));
179 } else {
180 final Ref prior;
181 prior = avail.put(name, new Ref(Ref.Storage.NETWORK, name, id));
182 if (prior != null)
183 throw duplicateAdvertisement(name);
186 available(avail);
190 * Create an exception to indicate problems finding a remote repository. The
191 * caller is expected to throw the returned exception.
193 * Subclasses may override this method to provide better diagnostics.
195 * @return a TransportException saying a repository cannot be found and
196 * possibly why.
198 protected TransportException noRepository() {
199 return new NoRemoteRepositoryException(uri, "not found.");
202 protected boolean isCapableOf(final String option) {
203 return remoteCapablities.contains(option);
206 protected boolean wantCapability(final StringBuilder b, final String option) {
207 if (!isCapableOf(option))
208 return false;
209 b.append(' ');
210 b.append(option);
211 return true;
214 private PackProtocolException duplicateAdvertisement(final String name) {
215 return new PackProtocolException(uri, "duplicate advertisements of "
216 + name);
219 @Override
220 public void close() {
221 if (out != null) {
222 try {
223 if (outNeedsEnd)
224 pckOut.end();
225 out.close();
226 } catch (IOException err) {
227 // Ignore any close errors.
228 } finally {
229 out = null;
230 pckOut = null;
234 if (in != null) {
235 try {
236 in.close();
237 } catch (IOException err) {
238 // Ignore any close errors.
239 } finally {
240 in = null;
241 pckIn = null;