Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / TransportLocal.java
blobcde648d08186eb14ac53b72d138a158c48e91430
1 /*
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>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
10 * conditions are met:
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
23 * written permission.
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;
42 import java.io.File;
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;
50 /**
51 * Transport that executes the Git "remote side" processes on a local directory.
52 * <p>
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
57 * process.
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)
63 return false;
65 if ("file".equals(uri.getScheme()) || uri.getScheme() == null)
66 return FS.resolve(new File("."), uri.getPath()).isDirectory();
67 return false;
70 private final File remoteGitDir;
72 TransportLocal(final Repository local, final URIish uri) {
73 super(local, uri);
75 File d = FS.resolve(new File("."), uri.getPath()).getAbsoluteFile();
76 if (new File(d, ".git").isDirectory())
77 d = new File(d, ".git");
78 remoteGitDir = d;
81 @Override
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);
91 try {
92 uploadPack = Runtime.getRuntime().exec(
93 new String[] { getOptionUploadPack(), "." }, null,
94 remoteGitDir);
95 } catch (IOException err) {
96 throw new TransportException(uri.toString() + ": "
97 + err.getMessage(), err);
99 startErrorThread();
100 init(uploadPack.getInputStream(), uploadPack.getOutputStream());
101 readAdvertisedRefs();
104 private void startErrorThread() {
105 final InputStream errorStream = uploadPack.getErrorStream();
106 new Thread("JGit " + getOptionUploadPack() + " Errors") {
107 public void run() {
108 final byte[] tmp = new byte[512];
109 try {
110 for (;;) {
111 final int n = errorStream.read(tmp);
112 if (n < 0)
113 break;
114 System.err.write(tmp, 0, n);
115 System.err.flush();
117 } catch (IOException err) {
118 // Ignore errors reading errors.
119 } finally {
120 try {
121 errorStream.close();
122 } catch (IOException err2) {
123 // Ignore errors closing the pipe.
127 }.start();
130 @Override
131 public void close() {
132 super.close();
134 if (uploadPack != null) {
135 try {
136 uploadPack.waitFor();
137 } catch (InterruptedException ie) {
138 // Stop waiting and return anyway.
139 } finally {
140 uploadPack = null;