2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Google Inc.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
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
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
;
44 import java
.io
.FileInputStream
;
45 import java
.io
.FileNotFoundException
;
46 import java
.io
.IOException
;
47 import java
.io
.InputStream
;
48 import java
.util
.ArrayList
;
49 import java
.util
.Collection
;
50 import java
.util
.HashSet
;
51 import java
.util
.LinkedHashMap
;
52 import java
.util
.List
;
55 import org
.spearce
.jgit
.errors
.MissingObjectException
;
56 import org
.spearce
.jgit
.errors
.MissingBundlePrerequisiteException
;
57 import org
.spearce
.jgit
.errors
.NotSupportedException
;
58 import org
.spearce
.jgit
.errors
.PackProtocolException
;
59 import org
.spearce
.jgit
.errors
.TransportException
;
60 import org
.spearce
.jgit
.lib
.Constants
;
61 import org
.spearce
.jgit
.lib
.ObjectId
;
62 import org
.spearce
.jgit
.lib
.ProgressMonitor
;
63 import org
.spearce
.jgit
.lib
.Ref
;
64 import org
.spearce
.jgit
.lib
.Repository
;
65 import org
.spearce
.jgit
.revwalk
.RevCommit
;
66 import org
.spearce
.jgit
.revwalk
.RevFlag
;
67 import org
.spearce
.jgit
.revwalk
.RevObject
;
68 import org
.spearce
.jgit
.revwalk
.RevWalk
;
69 import org
.spearce
.jgit
.util
.FS
;
70 import org
.spearce
.jgit
.util
.RawParseUtils
;
73 * Supports fetching from a git bundle (sneaker-net object transport).
75 * Push support for a bundle is complex, as one does not have a peer to
76 * communicate with to decide what the peer already knows. So push is not
77 * supported by the bundle transport.
79 class TransportBundle
extends PackTransport
{
80 static final String V2_BUNDLE_SIGNATURE
= "# v2 git bundle";
82 static boolean canHandle(final URIish uri
) {
83 if (uri
.getHost() != null || uri
.getPort() > 0 || uri
.getUser() != null
84 || uri
.getPass() != null || uri
.getPath() == null)
87 if ("file".equals(uri
.getScheme()) || uri
.getScheme() == null) {
88 final File f
= FS
.resolve(new File("."), uri
.getPath());
89 return f
.isFile() || f
.getName().endsWith(".bundle");
95 private final File bundle
;
97 TransportBundle(final Repository local
, final URIish uri
) {
99 bundle
= FS
.resolve(new File("."), uri
.getPath()).getAbsoluteFile();
103 public FetchConnection
openFetch() throws NotSupportedException
,
105 return new BundleFetchConnection();
109 public PushConnection
openPush() throws NotSupportedException
{
110 throw new NotSupportedException(
111 "Push is not supported for bundle transport");
115 public void close() {
116 // Resources must be established per-connection.
119 class BundleFetchConnection
extends BaseFetchConnection
{
122 RewindBufferedInputStream bin
;
124 final Set
<ObjectId
> prereqs
= new HashSet
<ObjectId
>();
126 BundleFetchConnection() throws TransportException
{
128 in
= new FileInputStream(bundle
);
129 bin
= new RewindBufferedInputStream(in
);
130 } catch (FileNotFoundException err
) {
131 throw new TransportException(uri
, "not found");
135 switch (readSignature()) {
140 throw new TransportException(uri
, "not a bundle");
143 in
.getChannel().position(
144 in
.getChannel().position() - bin
.buffered());
146 } catch (TransportException err
) {
149 } catch (IOException err
) {
151 throw new TransportException(uri
, err
.getMessage(), err
);
152 } catch (RuntimeException err
) {
154 throw new TransportException(uri
, err
.getMessage(), err
);
158 private int readSignature() throws IOException
{
159 final String rev
= readLine(new byte[1024]);
160 if (V2_BUNDLE_SIGNATURE
.equals(rev
))
162 throw new TransportException(uri
, "not a bundle");
165 private void readBundleV2() throws IOException
{
166 final byte[] hdrbuf
= new byte[1024];
167 final LinkedHashMap
<String
, Ref
> avail
= new LinkedHashMap
<String
, Ref
>();
169 String line
= readLine(hdrbuf
);
170 if (line
.length() == 0)
173 if (line
.charAt(0) == '-') {
174 prereqs
.add(ObjectId
.fromString(line
.substring(1, 41)));
178 final String name
= line
.substring(41, line
.length());
179 final ObjectId id
= ObjectId
.fromString(line
.substring(0, 40));
180 final Ref prior
= avail
.put(name
, new Ref(Ref
.Storage
.NETWORK
,
183 throw duplicateAdvertisement(name
);
188 private PackProtocolException
duplicateAdvertisement(final String name
) {
189 return new PackProtocolException(uri
,
190 "duplicate advertisements of " + name
);
193 private String
readLine(final byte[] hdrbuf
) throws IOException
{
194 bin
.mark(hdrbuf
.length
);
195 final int cnt
= bin
.read(hdrbuf
);
197 while (lf
< cnt
&& hdrbuf
[lf
] != '\n')
201 if (lf
< cnt
&& hdrbuf
[lf
] == '\n')
203 return RawParseUtils
.decode(Constants
.CHARSET
, hdrbuf
, 0, lf
);
207 protected void doFetch(final ProgressMonitor monitor
,
208 final Collection
<Ref
> want
) throws TransportException
{
209 verifyPrerequisites();
211 final IndexPack ip
= IndexPack
.create(local
, in
);
214 ip
.renameAndOpenPack();
215 } catch (IOException err
) {
217 throw new TransportException(uri
, err
.getMessage(), err
);
218 } catch (RuntimeException err
) {
220 throw new TransportException(uri
, err
.getMessage(), err
);
224 private void verifyPrerequisites() throws TransportException
{
225 if (prereqs
.isEmpty())
228 final RevWalk rw
= new RevWalk(local
);
229 final RevFlag PREREQ
= rw
.newFlag("PREREQ");
230 final RevFlag SEEN
= rw
.newFlag("SEEN");
232 final List
<ObjectId
> missing
= new ArrayList
<ObjectId
>();
233 final List
<RevObject
> commits
= new ArrayList
<RevObject
>();
234 for (final ObjectId p
: prereqs
) {
236 final RevCommit c
= rw
.parseCommit(p
);
237 if (!c
.has(PREREQ
)) {
241 } catch (MissingObjectException notFound
) {
243 } catch (IOException err
) {
244 throw new TransportException(uri
, "Cannot read commit "
248 if (!missing
.isEmpty())
249 throw new MissingBundlePrerequisiteException(uri
, missing
);
251 for (final Ref r
: local
.getAllRefs().values()) {
253 rw
.markStart(rw
.parseCommit(r
.getObjectId()));
254 } catch (IOException readError
) {
255 // If we cannot read the value of the ref skip it.
259 int remaining
= commits
.size();
262 while ((c
= rw
.next()) != null) {
265 if (--remaining
== 0)
269 } catch (IOException err
) {
270 throw new TransportException(uri
, "Cannot read object", err
);
274 for (final RevObject o
: commits
) {
278 throw new MissingBundlePrerequisiteException(uri
, missing
);
283 public void close() {
287 } catch (IOException ie
) {
288 // Ignore close failures.
296 class RewindBufferedInputStream
extends BufferedInputStream
{
297 RewindBufferedInputStream(final InputStream src
) {
302 return (count
- pos
);