Handle peeling of loose refs.
[egit/chris.git] / org.spearce.jgit / src / org / spearce / jgit / transport / BasePackConnection.java
blobe9df30e3bb000a4e55ebde92539b2da9e270faad
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 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);
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;
144 if (avail.isEmpty()) {
145 final int nul = line.indexOf('\0');
146 if (nul >= 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)
156 break;
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
162 continue;
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);
171 if (prior == null)
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));
180 } else {
181 final Ref prior;
182 prior = avail.put(name, new Ref(Ref.Storage.NETWORK, name, id));
183 if (prior != null)
184 throw duplicateAdvertisement(name);
187 available(avail);
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
197 * possibly why.
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))
209 return false;
210 if (b.length() > 0)
211 b.append(' ');
212 b.append(option);
213 return true;
216 private PackProtocolException duplicateAdvertisement(final String name) {
217 return new PackProtocolException(uri, "duplicate advertisements of "
218 + name);
221 @Override
222 public void close() {
223 if (out != null) {
224 try {
225 if (outNeedsEnd)
226 pckOut.end();
227 out.close();
228 } catch (IOException err) {
229 // Ignore any close errors.
230 } finally {
231 out = null;
232 pckOut = null;
236 if (in != null) {
237 try {
238 in.close();
239 } catch (IOException err) {
240 // Ignore any close errors.
241 } finally {
242 in = null;
243 pckIn = null;