Give NoRemoteRepositoryException better message in BasePackConnection
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / BasePackConnection.java
blobde0c7b6842bfcbe8c433ea6c30ec36d4cd465700
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 /** Buffered input stream reading from the remote. */
76 protected InputStream in;
78 /** Buffered output stream sending to the remote. */
79 protected OutputStream out;
81 /** Packet line decoder around {@link #in}. */
82 protected PacketLineIn pckIn;
84 /** Packet line encoder around {@link #out}. */
85 protected PacketLineOut pckOut;
87 /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
88 protected boolean outNeedsEnd;
90 /** Capability tokens advertised by the remote side. */
91 private final Set<String> remoteCapablities = new HashSet<String>();
93 BasePackConnection(final PackTransport packTransport) {
94 local = packTransport.local;
95 uri = packTransport.uri;
98 protected void init(final InputStream myIn, final OutputStream myOut) {
99 in = myIn instanceof BufferedInputStream ? myIn
100 : new BufferedInputStream(myIn);
101 out = myOut instanceof BufferedOutputStream ? myOut
102 : new BufferedOutputStream(myOut);
104 pckIn = new PacketLineIn(in);
105 pckOut = new PacketLineOut(out);
106 outNeedsEnd = true;
109 protected void readAdvertisedRefs() throws TransportException {
110 try {
111 readAdvertisedRefsImpl();
112 } catch (TransportException err) {
113 close();
114 throw err;
115 } catch (IOException err) {
116 close();
117 throw new TransportException(err.getMessage(), err);
118 } catch (RuntimeException err) {
119 close();
120 throw new TransportException(err.getMessage(), err);
124 private void readAdvertisedRefsImpl() throws IOException {
125 final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
126 for (;;) {
127 String line;
129 try {
130 line = pckIn.readString();
131 } catch (EOFException eof) {
132 if (avail.isEmpty()) {
133 String service = "unknown";
134 if (this instanceof PushConnection)
135 service = "push";
136 else if (this instanceof FetchConnection)
137 service = "fetch";
138 throw new NoRemoteRepositoryException(uri, service
139 + " service not found.");
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);
154 if (line.equals("capabilities^{}")) {
155 // special line from git-receive-pack to show
156 // capabilities when there are no refs to advertise
157 continue;
161 if (line.length() == 0)
162 break;
164 String name = line.substring(41, line.length());
165 final ObjectId id = ObjectId.fromString(line.substring(0, 40));
166 if (name.endsWith("^{}")) {
167 name = name.substring(0, name.length() - 3);
168 final Ref prior = avail.get(name);
169 if (prior == null)
170 throw new PackProtocolException(uri, "advertisement of "
171 + name + "^{} came before " + name);
173 if (prior.getPeeledObjectId() != null)
174 throw duplicateAdvertisement(name + "^{}");
176 avail.put(name, new Ref(Ref.Storage.NETWORK, name, prior
177 .getObjectId(), id));
178 } else {
179 final Ref prior;
180 prior = avail.put(name, new Ref(Ref.Storage.NETWORK, name, id));
181 if (prior != null)
182 throw duplicateAdvertisement(name);
185 available(avail);
188 protected boolean isCapableOf(final String option) {
189 return remoteCapablities.contains(option);
192 protected boolean wantCapability(final StringBuilder b, final String option) {
193 if (!isCapableOf(option))
194 return false;
195 if (b.length() > 0)
196 b.append(' ');
197 b.append(option);
198 return true;
201 private PackProtocolException duplicateAdvertisement(final String name) {
202 return new PackProtocolException(uri, "duplicate advertisements of "
203 + name);
206 @Override
207 public void close() {
208 if (out != null) {
209 try {
210 if (outNeedsEnd)
211 pckOut.end();
212 out.close();
213 } catch (IOException err) {
214 // Ignore any close errors.
215 } finally {
216 out = null;
217 pckOut = null;
221 if (in != null) {
222 try {
223 in.close();
224 } catch (IOException err) {
225 // Ignore any close errors.
226 } finally {
227 in = null;
228 pckIn = null;