diff options
Diffstat (limited to 'org.eclipse.jgit.pgm/src')
-rw-r--r-- | org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java | 88 | ||||
-rw-r--r-- | org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/SshDriver.java | 56 |
2 files changed, 118 insertions, 26 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java index 7e5b5451b6..c4b4018b8f 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java @@ -44,6 +44,9 @@ package org.eclipse.jgit.pgm; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SECTION_I18N; +import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_LOG_OUTPUT_ENCODING; import static org.eclipse.jgit.lib.Constants.R_HEADS; import static org.eclipse.jgit.lib.Constants.R_REMOTES; import static org.eclipse.jgit.lib.Constants.R_TAGS; @@ -56,14 +59,20 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.charset.Charset; import java.text.MessageFormat; import java.util.ResourceBundle; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.pgm.internal.CLIText; +import org.eclipse.jgit.pgm.internal.SshDriver; import org.eclipse.jgit.pgm.opt.CmdLineParser; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.transport.SshSessionFactory; +import org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory; +import org.eclipse.jgit.transport.sshd.JGitKeyCache; +import org.eclipse.jgit.transport.sshd.SshdSessionFactory; import org.eclipse.jgit.util.io.ThrowingPrintWriter; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.Option; @@ -85,6 +94,9 @@ public abstract class TextBuiltin { @Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" }) private boolean help; + @Option(name = "--ssh", usage = "usage_sshDriver") + private SshDriver sshDriver = SshDriver.JSCH; + /** * Input stream, typically this is standard input. * @@ -168,6 +180,30 @@ public abstract class TextBuiltin { } /** + * Get the log output encoding specified in the repository's + * {@code i18n.logOutputEncoding} configuration. + * + * @param repository + * the repository. + * @return Charset corresponding to {@code i18n.logOutputEncoding}, or + * {@code UTF_8}. + */ + private Charset getLogOutputEncodingCharset(Repository repository) { + if (repository != null) { + String logOutputEncoding = repository.getConfig().getString( + CONFIG_SECTION_I18N, null, CONFIG_KEY_LOG_OUTPUT_ENCODING); + if (logOutputEncoding != null) { + try { + return Charset.forName(logOutputEncoding); + } catch (IllegalArgumentException e) { + throw die(CLIText.get().cannotCreateOutputStream); + } + } + } + return UTF_8; + } + + /** * Initialize the command to work with a repository. * * @param repository @@ -177,32 +213,18 @@ public abstract class TextBuiltin { * {@code repository} is null. */ protected void init(Repository repository, String gitDir) { - try { - final String outputEncoding = repository != null ? repository - .getConfig().getString("i18n", null, "logOutputEncoding") : null; //$NON-NLS-1$ //$NON-NLS-2$ - if (ins == null) - ins = new FileInputStream(FileDescriptor.in); - if (outs == null) - outs = new FileOutputStream(FileDescriptor.out); - if (errs == null) - errs = new FileOutputStream(FileDescriptor.err); - BufferedWriter outbufw; - if (outputEncoding != null) - outbufw = new BufferedWriter(new OutputStreamWriter(outs, - outputEncoding)); - else - outbufw = new BufferedWriter(new OutputStreamWriter(outs)); - outw = new ThrowingPrintWriter(outbufw); - BufferedWriter errbufw; - if (outputEncoding != null) - errbufw = new BufferedWriter(new OutputStreamWriter(errs, - outputEncoding)); - else - errbufw = new BufferedWriter(new OutputStreamWriter(errs)); - errw = new ThrowingPrintWriter(errbufw); - } catch (IOException e) { - throw die(CLIText.get().cannotCreateOutputStream); - } + Charset charset = getLogOutputEncodingCharset(repository); + + if (ins == null) + ins = new FileInputStream(FileDescriptor.in); + if (outs == null) + outs = new FileOutputStream(FileDescriptor.out); + if (errs == null) + errs = new FileOutputStream(FileDescriptor.err); + outw = new ThrowingPrintWriter(new BufferedWriter( + new OutputStreamWriter(outs, charset))); + errw = new ThrowingPrintWriter(new BufferedWriter( + new OutputStreamWriter(errs, charset))); if (repository != null && repository.getDirectory() != null) { db = repository; @@ -225,6 +247,20 @@ public abstract class TextBuiltin { */ public final void execute(String[] args) throws Exception { parseArguments(args); + switch (sshDriver) { + case APACHE: { + SshdSessionFactory factory = new SshdSessionFactory( + new JGitKeyCache(), new DefaultProxyDataFactory()); + Runtime.getRuntime() + .addShutdownHook(new Thread(() -> factory.close())); + SshSessionFactory.setInstance(factory); + break; + } + case JSCH: + default: + SshSessionFactory.setInstance(null); + break; + } run(); } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/SshDriver.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/SshDriver.java new file mode 100644 index 0000000000..7d0423b014 --- /dev/null +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/SshDriver.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.pgm.internal; + +/** + * Simple enumeration for the available built-in ssh clients. + */ +public enum SshDriver { + + /** Default client: use JSch. */ + JSCH, + + /** Use the Apache MINA sshd client from org.eclipse.jgit.ssh.apache. */ + APACHE; + +} |