Fix push to empty repository
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / transport / BasePackConnection.java
blob2d145a63513b90ee838f0425891c86f0427df468
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 BasePackConnection(final PackTransport packTransport) {
97 local = packTransport.local;
98 uri = packTransport.uri;
99 transport = packTransport;
102 protected void init(final InputStream myIn, final OutputStream myOut) {
103 in = myIn instanceof BufferedInputStream ? myIn
104 : new BufferedInputStream(myIn, IndexPack.BUFFER_SIZE);
105 out = myOut instanceof BufferedOutputStream ? myOut
106 : new BufferedOutputStream(myOut);
108 pckIn = new PacketLineIn(in);
109 pckOut = new PacketLineOut(out);
110 outNeedsEnd = true;
113 protected void readAdvertisedRefs() throws TransportException {
114 try {
115 readAdvertisedRefsImpl();
116 } catch (TransportException err) {
117 close();
118 throw err;
119 } catch (IOException err) {
120 close();
121 throw new TransportException(err.getMessage(), err);
122 } catch (RuntimeException err) {
123 close();
124 throw new TransportException(err.getMessage(), err);
128 private void readAdvertisedRefsImpl() throws IOException {
129 final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
130 for (;;) {
131 String line;
133 try {
134 line = pckIn.readString();
135 } catch (EOFException eof) {
136 if (avail.isEmpty())
137 throw noRepository();
138 throw eof;
141 if (avail.isEmpty()) {
142 final int nul = line.indexOf('\0');
143 if (nul >= 0) {
144 // The first line (if any) may contain "hidden"
145 // capability values after a NUL byte.
146 for (String c : line.substring(nul + 1).split(" "))
147 remoteCapablities.add(c);
148 line = line.substring(0, nul);
152 if (line.length() == 0)
153 break;
155 String name = line.substring(41, line.length());
156 if (avail.isEmpty() && name.equals("capabilities^{}")) {
157 // special line from git-receive-pack to show
158 // capabilities when there are no refs to advertise
159 continue;
162 final ObjectId id = ObjectId.fromString(line.substring(0, 40));
163 if (name.endsWith("^{}")) {
164 name = name.substring(0, name.length() - 3);
165 final Ref prior = avail.get(name);
166 if (prior == null)
167 throw new PackProtocolException(uri, "advertisement of "
168 + name + "^{} came before " + name);
170 if (prior.getPeeledObjectId() != null)
171 throw duplicateAdvertisement(name + "^{}");
173 avail.put(name, new Ref(Ref.Storage.NETWORK, name, prior
174 .getObjectId(), id));
175 } else {
176 final Ref prior;
177 prior = avail.put(name, new Ref(Ref.Storage.NETWORK, name, id));
178 if (prior != null)
179 throw duplicateAdvertisement(name);
182 available(avail);
186 * Create an exception to indicate problems finding a remote repository. The
187 * caller is expected to throw the returned exception.
189 * Subclasses may override this method to provide better diagnostics.
191 * @return a TransportException saying a repository cannot be found and
192 * possibly why.
194 protected TransportException noRepository() {
195 return new NoRemoteRepositoryException(uri, "not found.");
198 protected boolean isCapableOf(final String option) {
199 return remoteCapablities.contains(option);
202 protected boolean wantCapability(final StringBuilder b, final String option) {
203 if (!isCapableOf(option))
204 return false;
205 if (b.length() > 0)
206 b.append(' ');
207 b.append(option);
208 return true;
211 private PackProtocolException duplicateAdvertisement(final String name) {
212 return new PackProtocolException(uri, "duplicate advertisements of "
213 + name);
216 @Override
217 public void close() {
218 if (out != null) {
219 try {
220 if (outNeedsEnd)
221 pckOut.end();
222 out.close();
223 } catch (IOException err) {
224 // Ignore any close errors.
225 } finally {
226 out = null;
227 pckOut = null;
231 if (in != null) {
232 try {
233 in.close();
234 } catch (IOException err) {
235 // Ignore any close errors.
236 } finally {
237 in = null;
238 pckIn = null;