You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransportLocal.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008-2010, Google Inc.
  4. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  6. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.transport;
  48. import java.io.BufferedInputStream;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.util.Collections;
  54. import java.util.Map;
  55. import java.util.Set;
  56. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  57. import org.eclipse.jgit.errors.NotSupportedException;
  58. import org.eclipse.jgit.errors.TransportException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.lib.RepositoryBuilder;
  62. import org.eclipse.jgit.lib.RepositoryCache;
  63. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  64. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  65. import org.eclipse.jgit.util.FS;
  66. import org.eclipse.jgit.util.io.MessageWriter;
  67. import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
  68. import org.eclipse.jgit.util.io.StreamCopyThread;
  69. /**
  70. * Transport to access a local directory as though it were a remote peer.
  71. * <p>
  72. * This transport is suitable for use on the local system, where the caller has
  73. * direct read or write access to the "remote" repository.
  74. * <p>
  75. * By default this transport works by spawning a helper thread within the same
  76. * JVM, and processes the data transfer using a shared memory buffer between the
  77. * calling thread and the helper thread. This is a pure-Java implementation
  78. * which does not require forking an external process.
  79. * <p>
  80. * However, during {@link #openFetch()}, if the Transport has configured
  81. * {@link Transport#getOptionUploadPack()} to be anything other than
  82. * <code>"git-upload-pack"</code> or <code>"git upload-pack"</code>, this
  83. * implementation will fork and execute the external process, using an operating
  84. * system pipe to transfer data.
  85. * <p>
  86. * Similarly, during {@link #openPush()}, if the Transport has configured
  87. * {@link Transport#getOptionReceivePack()} to be anything other than
  88. * <code>"git-receive-pack"</code> or <code>"git receive-pack"</code>, this
  89. * implementation will fork and execute the external process, using an operating
  90. * system pipe to transfer data.
  91. */
  92. class TransportLocal extends Transport implements PackTransport {
  93. static final TransportProtocol PROTO_LOCAL = new TransportProtocol() {
  94. @Override
  95. public String getName() {
  96. return JGitText.get().transportProtoLocal;
  97. }
  98. public Set<String> getSchemes() {
  99. return Collections.singleton("file"); //$NON-NLS-1$
  100. }
  101. @Override
  102. public boolean canHandle(URIish uri, Repository local, String remoteName) {
  103. if (uri.getPath() == null
  104. || uri.getPort() > 0
  105. || uri.getUser() != null
  106. || uri.getPass() != null
  107. || uri.getHost() != null
  108. || (uri.getScheme() != null && !getSchemes().contains(uri.getScheme())))
  109. return false;
  110. return true;
  111. }
  112. @Override
  113. public Transport open(URIish uri, Repository local, String remoteName)
  114. throws NoRemoteRepositoryException {
  115. File localPath = local.isBare() ? local.getDirectory() : local.getWorkTree();
  116. File path = local.getFS().resolve(localPath, uri.getPath());
  117. // If the reference is to a local file, C Git behavior says
  118. // assume this is a bundle, since repositories are directories.
  119. if (path.isFile())
  120. return new TransportBundleFile(local, uri, path);
  121. File gitDir = RepositoryCache.FileKey.resolve(path, local.getFS());
  122. if (gitDir == null)
  123. throw new NoRemoteRepositoryException(uri, JGitText.get().notFound);
  124. return new TransportLocal(local, uri, gitDir);
  125. }
  126. public Transport open(URIish uri) throws NotSupportedException,
  127. TransportException {
  128. File path = FS.DETECTED.resolve(new File("."), uri.getPath()); //$NON-NLS-1$
  129. // If the reference is to a local file, C Git behavior says
  130. // assume this is a bundle, since repositories are directories.
  131. if (path.isFile())
  132. return new TransportBundleFile(uri, path);
  133. File gitDir = RepositoryCache.FileKey.resolve(path, FS.DETECTED);
  134. if (gitDir == null)
  135. throw new NoRemoteRepositoryException(uri,
  136. JGitText.get().notFound);
  137. return new TransportLocal(uri, gitDir);
  138. }
  139. };
  140. private final File remoteGitDir;
  141. TransportLocal(Repository local, URIish uri, File gitDir) {
  142. super(local, uri);
  143. remoteGitDir = gitDir;
  144. }
  145. TransportLocal(URIish uri, File gitDir) {
  146. super(uri);
  147. remoteGitDir = gitDir;
  148. }
  149. UploadPack createUploadPack(final Repository dst) {
  150. return new UploadPack(dst);
  151. }
  152. ReceivePack createReceivePack(final Repository dst) {
  153. return new ReceivePack(dst);
  154. }
  155. private Repository openRepo() throws TransportException {
  156. try {
  157. return new RepositoryBuilder().setGitDir(remoteGitDir).build();
  158. } catch (IOException err) {
  159. throw new TransportException(uri, JGitText.get().notAGitDirectory);
  160. }
  161. }
  162. @Override
  163. public FetchConnection openFetch() throws TransportException {
  164. final String up = getOptionUploadPack();
  165. if (!"git-upload-pack".equals(up) //$NON-NLS-1$
  166. && !"git upload-pack".equals(up)) //$NON-NLS-1$
  167. return new ForkLocalFetchConnection();
  168. UploadPackFactory<Void> upf = new UploadPackFactory<Void>() {
  169. @Override
  170. public UploadPack create(Void req, Repository db) {
  171. return createUploadPack(db);
  172. }
  173. };
  174. return new InternalFetchConnection<Void>(this, upf, null, openRepo());
  175. }
  176. @Override
  177. public PushConnection openPush() throws TransportException {
  178. final String rp = getOptionReceivePack();
  179. if (!"git-receive-pack".equals(rp) //$NON-NLS-1$
  180. && !"git receive-pack".equals(rp)) //$NON-NLS-1$
  181. return new ForkLocalPushConnection();
  182. ReceivePackFactory<Void> rpf = new ReceivePackFactory<Void>() {
  183. @Override
  184. public ReceivePack create(Void req, Repository db) {
  185. return createReceivePack(db);
  186. }
  187. };
  188. return new InternalPushConnection<Void>(this, rpf, null, openRepo());
  189. }
  190. @Override
  191. public void close() {
  192. // Resources must be established per-connection.
  193. }
  194. protected Process spawn(final String cmd)
  195. throws TransportException {
  196. try {
  197. String[] args = { "." }; //$NON-NLS-1$
  198. ProcessBuilder proc = local.getFS().runInShell(cmd, args);
  199. proc.directory(remoteGitDir);
  200. // Remove the same variables CGit does.
  201. Map<String, String> env = proc.environment();
  202. env.remove("GIT_ALTERNATE_OBJECT_DIRECTORIES"); //$NON-NLS-1$
  203. env.remove("GIT_CONFIG"); //$NON-NLS-1$
  204. env.remove("GIT_CONFIG_PARAMETERS"); //$NON-NLS-1$
  205. env.remove("GIT_DIR"); //$NON-NLS-1$
  206. env.remove("GIT_WORK_TREE"); //$NON-NLS-1$
  207. env.remove("GIT_GRAFT_FILE"); //$NON-NLS-1$
  208. env.remove("GIT_INDEX_FILE"); //$NON-NLS-1$
  209. env.remove("GIT_NO_REPLACE_OBJECTS"); //$NON-NLS-1$
  210. return proc.start();
  211. } catch (IOException err) {
  212. throw new TransportException(uri, err.getMessage(), err);
  213. }
  214. }
  215. class ForkLocalFetchConnection extends BasePackFetchConnection {
  216. private Process uploadPack;
  217. private Thread errorReaderThread;
  218. ForkLocalFetchConnection() throws TransportException {
  219. super(TransportLocal.this);
  220. final MessageWriter msg = new MessageWriter();
  221. setMessageWriter(msg);
  222. uploadPack = spawn(getOptionUploadPack());
  223. final InputStream upErr = uploadPack.getErrorStream();
  224. errorReaderThread = new StreamCopyThread(upErr, msg.getRawStream());
  225. errorReaderThread.start();
  226. InputStream upIn = uploadPack.getInputStream();
  227. OutputStream upOut = uploadPack.getOutputStream();
  228. upIn = new BufferedInputStream(upIn);
  229. upOut = new SafeBufferedOutputStream(upOut);
  230. init(upIn, upOut);
  231. readAdvertisedRefs();
  232. }
  233. @Override
  234. public void close() {
  235. super.close();
  236. if (uploadPack != null) {
  237. try {
  238. uploadPack.waitFor();
  239. } catch (InterruptedException ie) {
  240. // Stop waiting and return anyway.
  241. } finally {
  242. uploadPack = null;
  243. }
  244. }
  245. if (errorReaderThread != null) {
  246. try {
  247. errorReaderThread.join();
  248. } catch (InterruptedException e) {
  249. // Stop waiting and return anyway.
  250. } finally {
  251. errorReaderThread = null;
  252. }
  253. }
  254. }
  255. }
  256. class ForkLocalPushConnection extends BasePackPushConnection {
  257. private Process receivePack;
  258. private Thread errorReaderThread;
  259. ForkLocalPushConnection() throws TransportException {
  260. super(TransportLocal.this);
  261. final MessageWriter msg = new MessageWriter();
  262. setMessageWriter(msg);
  263. receivePack = spawn(getOptionReceivePack());
  264. final InputStream rpErr = receivePack.getErrorStream();
  265. errorReaderThread = new StreamCopyThread(rpErr, msg.getRawStream());
  266. errorReaderThread.start();
  267. InputStream rpIn = receivePack.getInputStream();
  268. OutputStream rpOut = receivePack.getOutputStream();
  269. rpIn = new BufferedInputStream(rpIn);
  270. rpOut = new SafeBufferedOutputStream(rpOut);
  271. init(rpIn, rpOut);
  272. readAdvertisedRefs();
  273. }
  274. @Override
  275. public void close() {
  276. super.close();
  277. if (receivePack != null) {
  278. try {
  279. receivePack.waitFor();
  280. } catch (InterruptedException ie) {
  281. // Stop waiting and return anyway.
  282. } finally {
  283. receivePack = null;
  284. }
  285. }
  286. if (errorReaderThread != null) {
  287. try {
  288. errorReaderThread.join();
  289. } catch (InterruptedException e) {
  290. // Stop waiting and return anyway.
  291. } finally {
  292. errorReaderThread = null;
  293. }
  294. }
  295. }
  296. }
  297. }