View Javadoc
1   /*
2    * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
3    * Copyright (C) 2008, Google Inc.
4    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
5    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
6    *
7    * This program and the accompanying materials are made available under the
8    * terms of the Eclipse Distribution License v. 1.0 which is available at
9    * https://www.eclipse.org/org/documents/edl-v10.php.
10   *
11   * SPDX-License-Identifier: BSD-3-Clause
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" }; //$NON-NLS-1$ //$NON-NLS-2$
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())) { //$NON-NLS-1$
65  				File path = FS.DETECTED.resolve(new File("."), uri.getPath()); //$NON-NLS-1$
66  				return new TransportBundleFile(local, uri, path);
67  			}
68  
69  			// This is an ambiguous reference, it could be a bundle file
70  			// or it could be a Git repository. Allow TransportLocal to
71  			// resolve the path and figure out which type it is by testing
72  			// the target.
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())) { //$NON-NLS-1$
81  				File path = FS.DETECTED.resolve(new File("."), uri.getPath()); //$NON-NLS-1$
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  	 * Constructor for TransportBundleFile.
97  	 *
98  	 * @param uri
99  	 *            a {@link org.eclipse.jgit.transport.URIish} object.
100 	 * @param bundlePath
101 	 *            transport bundle path
102 	 */
103 	public TransportBundleFile(URIish uri, File bundlePath) {
104 		super(uri);
105 		bundle = bundlePath;
106 	}
107 
108 	/** {@inheritDoc} */
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 	/** {@inheritDoc} */
125 	@Override
126 	public PushConnection openPush() throws NotSupportedException {
127 		throw new NotSupportedException(
128 				JGitText.get().pushIsNotSupportedForBundleTransport);
129 	}
130 
131 	/** {@inheritDoc} */
132 	@Override
133 	public void close() {
134 		// Resources must be established per-connection.
135 	}
136 
137 }