Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TransportLocal.java 12KB

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