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

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