2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.transport
;
41 import java
.io
.BufferedInputStream
;
43 import java
.io
.FileInputStream
;
44 import java
.io
.FileNotFoundException
;
45 import java
.io
.IOException
;
46 import java
.io
.InputStream
;
47 import java
.util
.Collection
;
48 import java
.util
.HashSet
;
49 import java
.util
.LinkedHashMap
;
52 import org
.spearce
.jgit
.errors
.NotSupportedException
;
53 import org
.spearce
.jgit
.errors
.PackProtocolException
;
54 import org
.spearce
.jgit
.errors
.TransportException
;
55 import org
.spearce
.jgit
.lib
.Constants
;
56 import org
.spearce
.jgit
.lib
.ObjectId
;
57 import org
.spearce
.jgit
.lib
.ProgressMonitor
;
58 import org
.spearce
.jgit
.lib
.Ref
;
59 import org
.spearce
.jgit
.lib
.Repository
;
60 import org
.spearce
.jgit
.util
.FS
;
63 * Supports fetching from a git bundle (sneaker-net object transport).
65 * Push support for a bundle is complex, as one does not have a peer to
66 * communicate with to decide what the peer already knows. So push is not
67 * supported by the bundle transport.
69 class TransportBundle
extends PackTransport
{
70 static final String V2_BUNDLE_SIGNATURE
= "# v2 git bundle";
72 static boolean canHandle(final URIish uri
) {
73 if (uri
.getHost() != null || uri
.getPort() > 0 || uri
.getUser() != null
74 || uri
.getPass() != null || uri
.getPath() == null)
77 if ("file".equals(uri
.getScheme()) || uri
.getScheme() == null) {
78 final File f
= FS
.resolve(new File("."), uri
.getPath());
79 return f
.isFile() || f
.getName().endsWith(".bundle");
85 private final File bundle
;
87 TransportBundle(final Repository local
, final URIish uri
) {
89 bundle
= FS
.resolve(new File("."), uri
.getPath()).getAbsoluteFile();
93 public FetchConnection
openFetch() throws NotSupportedException
,
95 return new BundleFetchConnection();
99 public PushConnection
openPush() throws NotSupportedException
{
100 throw new NotSupportedException(
101 "Push is not supported for bundle transport");
104 class BundleFetchConnection
extends BaseFetchConnection
{
107 RewindBufferedInputStream bin
;
109 final Set
<ObjectId
> prereqs
= new HashSet
<ObjectId
>();
111 BundleFetchConnection() throws TransportException
{
113 in
= new FileInputStream(bundle
);
114 bin
= new RewindBufferedInputStream(in
);
115 } catch (FileNotFoundException err
) {
116 throw new TransportException(bundle
.getPath() + ": not found");
120 switch (readSignature()) {
125 throw new TransportException(bundle
.getPath()
129 in
.getChannel().position(
130 in
.getChannel().position() - bin
.buffered());
132 } catch (TransportException err
) {
135 } catch (IOException err
) {
137 throw new TransportException(bundle
.getPath() + ": "
138 + err
.getMessage(), err
);
139 } catch (RuntimeException err
) {
141 throw new TransportException(bundle
.getPath() + ": "
142 + err
.getMessage(), err
);
146 private int readSignature() throws IOException
{
147 final String rev
= readLine(new byte[1024]);
148 if (V2_BUNDLE_SIGNATURE
.equals(rev
))
150 throw new TransportException(bundle
.getPath() + ": not a bundle");
153 private void readBundleV2() throws IOException
{
154 final byte[] hdrbuf
= new byte[1024];
155 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
157 String line
= readLine(hdrbuf
);
158 if (line
.length() == 0)
161 if (line
.charAt(0) == '-') {
162 prereqs
.add(ObjectId
.fromString(line
.substring(1, 41)));
166 final String name
= line
.substring(41, line
.length());
167 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
168 final Ref prior
= avail
.put(name
, new Ref(name
, id
));
170 throw duplicateAdvertisement(name
);
175 private PackProtocolException
duplicateAdvertisement(final String name
) {
176 return new PackProtocolException("duplicate advertisements of "
180 private String
readLine(final byte[] hdrbuf
) throws IOException
{
181 bin
.mark(hdrbuf
.length
);
182 final int cnt
= bin
.read(hdrbuf
);
184 while (lf
< cnt
&& hdrbuf
[lf
] != '\n')
188 if (lf
< cnt
&& hdrbuf
[lf
] == '\n')
190 return new String(hdrbuf
, 0, lf
, Constants
.CHARACTER_ENCODING
);
194 protected void doFetch(final ProgressMonitor monitor
,
195 final Collection
<Ref
> want
) throws TransportException
{
197 final IndexPack ip
= IndexPack
.create(local
, in
);
200 ip
.renameAndOpenPack();
201 } catch (IOException err
) {
203 throw new TransportException(err
.getMessage(), err
);
204 } catch (RuntimeException err
) {
206 throw new TransportException(err
.getMessage(), err
);
211 public void close() {
215 } catch (IOException ie
) {
216 // Ignore close failures.
224 class RewindBufferedInputStream
extends BufferedInputStream
{
225 RewindBufferedInputStream(final InputStream src
) {
230 return (count
- pos
);