1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jgit.transport;
15
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.InputStream;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.LinkedHashSet;
23 import java.util.Set;
24
25 import org.eclipse.jgit.errors.NotSupportedException;
26 import org.eclipse.jgit.errors.TransportException;
27 import org.eclipse.jgit.internal.JGitText;
28 import org.eclipse.jgit.lib.Repository;
29 import org.eclipse.jgit.util.FS;
30
31 class TransportBundleFile extends Transport implements TransportBundle {
32 static final TransportProtocol PROTO_BUNDLE = new TransportProtocol() {
33 private final String[] schemeNames = { "bundle", "file" };
34
35 private final Set<String> schemeSet = Collections
36 .unmodifiableSet(new LinkedHashSet<>(Arrays
37 .asList(schemeNames)));
38
39 @Override
40 public String getName() {
41 return JGitText.get().transportProtoBundleFile;
42 }
43
44 @Override
45 public Set<String> getSchemes() {
46 return schemeSet;
47 }
48
49 @Override
50 public boolean canHandle(URIish uri, Repository local, String remoteName) {
51 if (uri.getPath() == null
52 || uri.getPort() > 0
53 || uri.getUser() != null
54 || uri.getPass() != null
55 || uri.getHost() != null
56 || (uri.getScheme() != null && !getSchemes().contains(uri.getScheme())))
57 return false;
58 return true;
59 }
60
61 @Override
62 public Transport open(URIish uri, Repository local, String remoteName)
63 throws NotSupportedException, TransportException {
64 if ("bundle".equals(uri.getScheme())) {
65 File path = FS.DETECTED.resolve(new File("."), uri.getPath());
66 return new TransportBundleFile(local, uri, path);
67 }
68
69
70
71
72
73
74 return TransportLocal.PROTO_LOCAL.open(uri, local, remoteName);
75 }
76
77 @Override
78 public Transport open(URIish uri) throws NotSupportedException,
79 TransportException {
80 if ("bundle".equals(uri.getScheme())) {
81 File path = FS.DETECTED.resolve(new File("."), uri.getPath());
82 return new TransportBundleFile(uri, path);
83 }
84 return TransportLocal.PROTO_LOCAL.open(uri);
85 }
86 };
87
88 private final File bundle;
89
90 TransportBundleFile(Repository local, URIish uri, File bundlePath) {
91 super(local, uri);
92 bundle = bundlePath;
93 }
94
95
96
97
98
99
100
101
102
103 public TransportBundleFile(URIish uri, File bundlePath) {
104 super(uri);
105 bundle = bundlePath;
106 }
107
108
109 @Override
110 public FetchConnection openFetch() throws NotSupportedException,
111 TransportException {
112 final InputStream src;
113 try {
114 src = new FileInputStream(bundle);
115 } catch (FileNotFoundException err) {
116 TransportException te = new TransportException(uri,
117 JGitText.get().notFound);
118 te.initCause(err);
119 throw te;
120 }
121 return new BundleFetchConnection(this, src);
122 }
123
124
125 @Override
126 public PushConnection openPush() throws NotSupportedException {
127 throw new NotSupportedException(
128 JGitText.get().pushIsNotSupportedForBundleTransport);
129 }
130
131
132 @Override
133 public void close() {
134
135 }
136
137 }