2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org
.spearce
.jgit
.transport
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
46 import org
.spearce
.jgit
.errors
.TransportException
;
47 import org
.spearce
.jgit
.lib
.Repository
;
48 import org
.spearce
.jgit
.util
.FS
;
51 * Transport that executes the Git "remote side" processes on a local directory.
53 * This transport is suitable for use on the local system, where the caller has
54 * direct read or write access to the remote repository. This implementation
55 * forks a C Git process to provide the remote side access, much as the
56 * {@link TransportGitSsh} implementation causes the remote side to run a C Git
59 class TransportLocal
extends PackTransport
{
60 static boolean canHandle(final URIish uri
) {
61 if (uri
.getHost() != null || uri
.getPort() > 0 || uri
.getUser() != null
62 || uri
.getPass() != null || uri
.getPath() == null)
65 if ("file".equals(uri
.getScheme()) || uri
.getScheme() == null)
66 return FS
.resolve(new File("."), uri
.getPath()).isDirectory();
70 private final File remoteGitDir
;
72 TransportLocal(final Repository local
, final URIish uri
) {
75 File d
= FS
.resolve(new File("."), uri
.getPath()).getAbsoluteFile();
76 if (new File(d
, ".git").isDirectory())
77 d
= new File(d
, ".git");
82 public FetchConnection
openFetch() throws TransportException
{
83 return new LocalFetchConnection();
86 class LocalFetchConnection
extends PackFetchConnection
{
87 private Process uploadPack
;
89 LocalFetchConnection() throws TransportException
{
90 super(TransportLocal
.this);
92 uploadPack
= Runtime
.getRuntime().exec(
93 new String
[] { getOptionUploadPack(), "." }, null,
95 } catch (IOException err
) {
96 throw new TransportException(uri
.toString() + ": "
97 + err
.getMessage(), err
);
100 init(uploadPack
.getInputStream(), uploadPack
.getOutputStream());
101 readAdvertisedRefs();
104 private void startErrorThread() {
105 final InputStream errorStream
= uploadPack
.getErrorStream();
106 new Thread("JGit " + getOptionUploadPack() + " Errors") {
108 final byte[] tmp
= new byte[512];
111 final int n
= errorStream
.read(tmp
);
114 System
.err
.write(tmp
, 0, n
);
117 } catch (IOException err
) {
118 // Ignore errors reading errors.
122 } catch (IOException err2
) {
123 // Ignore errors closing the pipe.
131 public void close() {
134 if (uploadPack
!= null) {
136 uploadPack
.waitFor();
137 } catch (InterruptedException ie
) {
138 // Stop waiting and return anyway.