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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.BufferedOutputStream;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.io.PipedInputStream;
  55. import java.io.PipedOutputStream;
  56. import org.eclipse.jgit.JGitText;
  57. import org.eclipse.jgit.errors.NotSupportedException;
  58. import org.eclipse.jgit.errors.TransportException;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.storage.file.FileRepository;
  62. import org.eclipse.jgit.util.FS;
  63. import org.eclipse.jgit.util.io.MessageWriter;
  64. import org.eclipse.jgit.util.io.StreamCopyThread;
  65. /**
  66. * Transport to access a local directory as though it were a remote peer.
  67. * <p>
  68. * This transport is suitable for use on the local system, where the caller has
  69. * direct read or write access to the "remote" repository.
  70. * <p>
  71. * By default this transport works by spawning a helper thread within the same
  72. * JVM, and processes the data transfer using a shared memory buffer between the
  73. * calling thread and the helper thread. This is a pure-Java implementation
  74. * which does not require forking an external process.
  75. * <p>
  76. * However, during {@link #openFetch()}, if the Transport has configured
  77. * {@link Transport#getOptionUploadPack()} to be anything other than
  78. * <code>"git-upload-pack"</code> or <code>"git upload-pack"</code>, this
  79. * implementation will fork and execute the external process, using an operating
  80. * system pipe to transfer data.
  81. * <p>
  82. * Similarly, during {@link #openPush()}, if the Transport has configured
  83. * {@link Transport#getOptionReceivePack()} to be anything other than
  84. * <code>"git-receive-pack"</code> or <code>"git receive-pack"</code>, this
  85. * implementation will fork and execute the external process, using an operating
  86. * system pipe to transfer data.
  87. */
  88. class TransportLocal extends Transport implements PackTransport {
  89. private static final String PWD = ".";
  90. static boolean canHandle(final URIish uri, FS fs) {
  91. if (uri.getHost() != null || uri.getPort() > 0 || uri.getUser() != null
  92. || uri.getPass() != null || uri.getPath() == null)
  93. return false;
  94. if ("file".equals(uri.getScheme()) || uri.getScheme() == null)
  95. return fs.resolve(new File(PWD), uri.getPath()).isDirectory();
  96. return false;
  97. }
  98. private final File remoteGitDir;
  99. TransportLocal(final Repository local, final URIish uri) {
  100. super(local, uri);
  101. File d = local.getFS().resolve(new File(PWD), uri.getPath()).getAbsoluteFile();
  102. if (new File(d, Constants.DOT_GIT).isDirectory())
  103. d = new File(d, Constants.DOT_GIT);
  104. remoteGitDir = d;
  105. }
  106. UploadPack createUploadPack(final Repository dst) {
  107. return new UploadPack(dst);
  108. }
  109. ReceivePack createReceivePack(final Repository dst) {
  110. return new ReceivePack(dst);
  111. }
  112. @Override
  113. public FetchConnection openFetch() throws TransportException {
  114. final String up = getOptionUploadPack();
  115. if ("git-upload-pack".equals(up) || "git upload-pack".equals(up))
  116. return new InternalLocalFetchConnection();
  117. return new ForkLocalFetchConnection();
  118. }
  119. @Override
  120. public PushConnection openPush() throws NotSupportedException,
  121. TransportException {
  122. final String rp = getOptionReceivePack();
  123. if ("git-receive-pack".equals(rp) || "git receive-pack".equals(rp))
  124. return new InternalLocalPushConnection();
  125. return new ForkLocalPushConnection();
  126. }
  127. @Override
  128. public void close() {
  129. // Resources must be established per-connection.
  130. }
  131. protected Process spawn(final String cmd)
  132. throws TransportException {
  133. try {
  134. final String[] args;
  135. if (cmd.startsWith("git-")) {
  136. args = new String[] { "git", cmd.substring(4), PWD };
  137. } else {
  138. final int gitspace = cmd.indexOf("git ");
  139. if (gitspace >= 0) {
  140. final String git = cmd.substring(0, gitspace + 3);
  141. final String subcmd = cmd.substring(gitspace + 4);
  142. args = new String[] { git, subcmd, PWD };
  143. } else {
  144. args = new String[] { cmd, PWD };
  145. }
  146. }
  147. return Runtime.getRuntime().exec(args, null, remoteGitDir);
  148. } catch (IOException err) {
  149. throw new TransportException(uri, err.getMessage(), err);
  150. }
  151. }
  152. class InternalLocalFetchConnection extends BasePackFetchConnection {
  153. private Thread worker;
  154. InternalLocalFetchConnection() throws TransportException {
  155. super(TransportLocal.this);
  156. final Repository dst;
  157. try {
  158. dst = new FileRepository(remoteGitDir);
  159. } catch (IOException err) {
  160. throw new TransportException(uri, JGitText.get().notAGitDirectory);
  161. }
  162. final PipedInputStream in_r;
  163. final PipedOutputStream in_w;
  164. final PipedInputStream out_r;
  165. final PipedOutputStream out_w;
  166. try {
  167. in_r = new PipedInputStream();
  168. in_w = new PipedOutputStream(in_r);
  169. out_r = new PipedInputStream() {
  170. // The client (BasePackFetchConnection) can write
  171. // a huge burst before it reads again. We need to
  172. // force the buffer to be big enough, otherwise it
  173. // will deadlock both threads.
  174. {
  175. buffer = new byte[MIN_CLIENT_BUFFER];
  176. }
  177. };
  178. out_w = new PipedOutputStream(out_r);
  179. } catch (IOException err) {
  180. dst.close();
  181. throw new TransportException(uri, JGitText.get().cannotConnectPipes, err);
  182. }
  183. worker = new Thread("JGit-Upload-Pack") {
  184. public void run() {
  185. try {
  186. final UploadPack rp = createUploadPack(dst);
  187. rp.upload(out_r, in_w, null);
  188. } catch (IOException err) {
  189. // Client side of the pipes should report the problem.
  190. err.printStackTrace();
  191. } catch (RuntimeException err) {
  192. // Clients side will notice we went away, and report.
  193. err.printStackTrace();
  194. } finally {
  195. try {
  196. out_r.close();
  197. } catch (IOException e2) {
  198. // Ignore close failure, we probably crashed above.
  199. }
  200. try {
  201. in_w.close();
  202. } catch (IOException e2) {
  203. // Ignore close failure, we probably crashed above.
  204. }
  205. dst.close();
  206. }
  207. }
  208. };
  209. worker.start();
  210. init(in_r, out_w);
  211. readAdvertisedRefs();
  212. }
  213. @Override
  214. public void close() {
  215. super.close();
  216. if (worker != null) {
  217. try {
  218. worker.join();
  219. } catch (InterruptedException ie) {
  220. // Stop waiting and return anyway.
  221. } finally {
  222. worker = null;
  223. }
  224. }
  225. }
  226. }
  227. class ForkLocalFetchConnection extends BasePackFetchConnection {
  228. private Process uploadPack;
  229. private Thread errorReaderThread;
  230. ForkLocalFetchConnection() throws TransportException {
  231. super(TransportLocal.this);
  232. final MessageWriter msg = new MessageWriter();
  233. setMessageWriter(msg);
  234. uploadPack = spawn(getOptionUploadPack());
  235. final InputStream upErr = uploadPack.getErrorStream();
  236. errorReaderThread = new StreamCopyThread(upErr, msg.getRawStream());
  237. errorReaderThread.start();
  238. InputStream upIn = uploadPack.getInputStream();
  239. OutputStream upOut = uploadPack.getOutputStream();
  240. upIn = new BufferedInputStream(upIn);
  241. upOut = new BufferedOutputStream(upOut);
  242. init(upIn, upOut);
  243. readAdvertisedRefs();
  244. }
  245. @Override
  246. public void close() {
  247. super.close();
  248. if (uploadPack != null) {
  249. try {
  250. uploadPack.waitFor();
  251. } catch (InterruptedException ie) {
  252. // Stop waiting and return anyway.
  253. } finally {
  254. uploadPack = null;
  255. }
  256. }
  257. if (errorReaderThread != null) {
  258. try {
  259. errorReaderThread.join();
  260. } catch (InterruptedException e) {
  261. // Stop waiting and return anyway.
  262. } finally {
  263. errorReaderThread = null;
  264. }
  265. }
  266. }
  267. }
  268. class InternalLocalPushConnection extends BasePackPushConnection {
  269. private Thread worker;
  270. InternalLocalPushConnection() throws TransportException {
  271. super(TransportLocal.this);
  272. final Repository dst;
  273. try {
  274. dst = new FileRepository(remoteGitDir);
  275. } catch (IOException err) {
  276. throw new TransportException(uri, JGitText.get().notAGitDirectory);
  277. }
  278. final PipedInputStream in_r;
  279. final PipedOutputStream in_w;
  280. final PipedInputStream out_r;
  281. final PipedOutputStream out_w;
  282. try {
  283. in_r = new PipedInputStream();
  284. in_w = new PipedOutputStream(in_r);
  285. out_r = new PipedInputStream();
  286. out_w = new PipedOutputStream(out_r);
  287. } catch (IOException err) {
  288. dst.close();
  289. throw new TransportException(uri, JGitText.get().cannotConnectPipes, err);
  290. }
  291. worker = new Thread("JGit-Receive-Pack") {
  292. public void run() {
  293. try {
  294. final ReceivePack rp = createReceivePack(dst);
  295. rp.receive(out_r, in_w, System.err);
  296. } catch (IOException err) {
  297. // Client side of the pipes should report the problem.
  298. } catch (RuntimeException err) {
  299. // Clients side will notice we went away, and report.
  300. } finally {
  301. try {
  302. out_r.close();
  303. } catch (IOException e2) {
  304. // Ignore close failure, we probably crashed above.
  305. }
  306. try {
  307. in_w.close();
  308. } catch (IOException e2) {
  309. // Ignore close failure, we probably crashed above.
  310. }
  311. dst.close();
  312. }
  313. }
  314. };
  315. worker.start();
  316. init(in_r, out_w);
  317. readAdvertisedRefs();
  318. }
  319. @Override
  320. public void close() {
  321. super.close();
  322. if (worker != null) {
  323. try {
  324. worker.join();
  325. } catch (InterruptedException ie) {
  326. // Stop waiting and return anyway.
  327. } finally {
  328. worker = null;
  329. }
  330. }
  331. }
  332. }
  333. class ForkLocalPushConnection extends BasePackPushConnection {
  334. private Process receivePack;
  335. private Thread errorReaderThread;
  336. ForkLocalPushConnection() throws TransportException {
  337. super(TransportLocal.this);
  338. final MessageWriter msg = new MessageWriter();
  339. setMessageWriter(msg);
  340. receivePack = spawn(getOptionReceivePack());
  341. final InputStream rpErr = receivePack.getErrorStream();
  342. errorReaderThread = new StreamCopyThread(rpErr, msg.getRawStream());
  343. errorReaderThread.start();
  344. InputStream rpIn = receivePack.getInputStream();
  345. OutputStream rpOut = receivePack.getOutputStream();
  346. rpIn = new BufferedInputStream(rpIn);
  347. rpOut = new BufferedOutputStream(rpOut);
  348. init(rpIn, rpOut);
  349. readAdvertisedRefs();
  350. }
  351. @Override
  352. public void close() {
  353. super.close();
  354. if (receivePack != null) {
  355. try {
  356. receivePack.waitFor();
  357. } catch (InterruptedException ie) {
  358. // Stop waiting and return anyway.
  359. } finally {
  360. receivePack = null;
  361. }
  362. }
  363. if (errorReaderThread != null) {
  364. try {
  365. errorReaderThread.join();
  366. } catch (InterruptedException e) {
  367. // Stop waiting and return anyway.
  368. } finally {
  369. errorReaderThread = null;
  370. }
  371. }
  372. }
  373. }
  374. }