GitHook: use generic OutputStream instead of PrintStream

Change-Id: I15e64dc963c9d27dc9c8de4976dd63f74b918b15
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2021-02-09 02:34:10 +01:00
parent 7cc1a52c66
commit 5b528474f5
5 changed files with 49 additions and 28 deletions

View File

@ -24,4 +24,34 @@
</message_arguments> </message_arguments>
</filter> </filter>
</resource> </resource>
<resource path="src/org/eclipse/jgit/util/FS.java" type="org.eclipse.jgit.util.FS">
<filter id="338792546">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.FS"/>
<message_argument value="internalRunHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)"/>
</message_arguments>
</filter>
<filter id="338792546">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.FS"/>
<message_argument value="runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/jgit/util/FS_POSIX.java" type="org.eclipse.jgit.util.FS_POSIX">
<filter id="338792546">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.FS_POSIX"/>
<message_argument value="runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/jgit/util/FS_Win32_Cygwin.java" type="org.eclipse.jgit.util.FS_Win32_Cygwin">
<filter id="338792546">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.FS_Win32_Cygwin"/>
<message_argument value="runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)"/>
</message_arguments>
</filter>
</resource>
</component> </component>

View File

@ -13,8 +13,7 @@
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import org.eclipse.jgit.api.errors.AbortedByHookException; import org.eclipse.jgit.api.errors.AbortedByHookException;
@ -44,12 +43,12 @@ abstract class GitHook<T> implements Callable<T> {
/** /**
* The output stream to be used by the hook. * The output stream to be used by the hook.
*/ */
private final PrintStream outputStream; private final OutputStream outputStream;
/** /**
* The error stream to be used by the hook. * The error stream to be used by the hook.
*/ */
private final PrintStream errorStream; private final OutputStream errorStream;
/** /**
* Constructor for GitHook. * Constructor for GitHook.
@ -63,7 +62,7 @@ abstract class GitHook<T> implements Callable<T> {
* The output stream the hook must use. {@code null} is allowed, * The output stream the hook must use. {@code null} is allowed,
* in which case the hook will use {@code System.out}. * in which case the hook will use {@code System.out}.
*/ */
protected GitHook(Repository repo, PrintStream outputStream) { protected GitHook(Repository repo, OutputStream outputStream) {
this(repo, outputStream, null); this(repo, outputStream, null);
} }
@ -79,8 +78,8 @@ protected GitHook(Repository repo, PrintStream outputStream) {
* The error stream the hook must use. {@code null} is allowed, * The error stream the hook must use. {@code null} is allowed,
* in which case the hook will use {@code System.err}. * in which case the hook will use {@code System.err}.
*/ */
protected GitHook(Repository repo, PrintStream outputStream, protected GitHook(Repository repo, OutputStream outputStream,
PrintStream errorStream) { OutputStream errorStream) {
this.repo = repo; this.repo = repo;
this.outputStream = outputStream; this.outputStream = outputStream;
this.errorStream = errorStream; this.errorStream = errorStream;
@ -137,7 +136,7 @@ protected String getStdinArgs() {
* @return The output stream the hook must use. Never {@code null}, * @return The output stream the hook must use. Never {@code null},
* {@code System.out} is returned by default. * {@code System.out} is returned by default.
*/ */
protected PrintStream getOutputStream() { protected OutputStream getOutputStream() {
return outputStream == null ? System.out : outputStream; return outputStream == null ? System.out : outputStream;
} }
@ -147,7 +146,7 @@ protected PrintStream getOutputStream() {
* @return The error stream the hook must use. Never {@code null}, * @return The error stream the hook must use. Never {@code null},
* {@code System.err} is returned by default. * {@code System.err} is returned by default.
*/ */
protected PrintStream getErrorStream() { protected OutputStream getErrorStream() {
return errorStream == null ? System.err : errorStream; return errorStream == null ? System.err : errorStream;
} }
@ -161,20 +160,13 @@ protected void doRun() throws AbortedByHookException {
final ByteArrayOutputStream errorByteArray = new ByteArrayOutputStream(); final ByteArrayOutputStream errorByteArray = new ByteArrayOutputStream();
final TeeOutputStream stderrStream = new TeeOutputStream(errorByteArray, final TeeOutputStream stderrStream = new TeeOutputStream(errorByteArray,
getErrorStream()); getErrorStream());
PrintStream hookErrRedirect = null;
try {
hookErrRedirect = new PrintStream(stderrStream, false,
UTF_8.name());
} catch (UnsupportedEncodingException e) {
// UTF-8 is guaranteed to be available
}
Repository repository = getRepository(); Repository repository = getRepository();
FS fs = repository.getFS(); FS fs = repository.getFS();
if (fs == null) { if (fs == null) {
fs = FS.DETECTED; fs = FS.DETECTED;
} }
ProcessResult result = fs.runHookIfPresent(repository, getHookName(), ProcessResult result = fs.runHookIfPresent(repository, getHookName(),
getParameters(), getOutputStream(), hookErrRedirect, getParameters(), getOutputStream(), stderrStream,
getStdinArgs()); getStdinArgs());
if (result.isExecutedWithError()) { if (result.isExecutedWithError()) {
throw new AbortedByHookException( throw new AbortedByHookException(

View File

@ -22,7 +22,6 @@
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.AccessDeniedException; import java.nio.file.AccessDeniedException;
@ -1873,18 +1872,18 @@ public ProcessResult runHookIfPresent(Repository repository,
* @throws org.eclipse.jgit.api.errors.JGitInternalException * @throws org.eclipse.jgit.api.errors.JGitInternalException
* if we fail to run the hook somehow. Causes may include an * if we fail to run the hook somehow. Causes may include an
* interrupted process or I/O errors. * interrupted process or I/O errors.
* @since 4.0 * @since 5.11
*/ */
public ProcessResult runHookIfPresent(Repository repository, public ProcessResult runHookIfPresent(Repository repository,
final String hookName, final String hookName,
String[] args, PrintStream outRedirect, PrintStream errRedirect, String[] args, OutputStream outRedirect, OutputStream errRedirect,
String stdinArgs) throws JGitInternalException { String stdinArgs) throws JGitInternalException {
return new ProcessResult(Status.NOT_SUPPORTED); return new ProcessResult(Status.NOT_SUPPORTED);
} }
/** /**
* See * See
* {@link #runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)} * {@link #runHookIfPresent(Repository, String, String[], OutputStream, OutputStream, String)}
* . Should only be called by FS supporting shell scripts execution. * . Should only be called by FS supporting shell scripts execution.
* *
* @param repository * @param repository
@ -1909,11 +1908,11 @@ public ProcessResult runHookIfPresent(Repository repository,
* @throws org.eclipse.jgit.api.errors.JGitInternalException * @throws org.eclipse.jgit.api.errors.JGitInternalException
* if we fail to run the hook somehow. Causes may include an * if we fail to run the hook somehow. Causes may include an
* interrupted process or I/O errors. * interrupted process or I/O errors.
* @since 4.0 * @since 5.11
*/ */
protected ProcessResult internalRunHookIfPresent(Repository repository, protected ProcessResult internalRunHookIfPresent(Repository repository,
final String hookName, String[] args, PrintStream outRedirect, final String hookName, String[] args, OutputStream outRedirect,
PrintStream errRedirect, String stdinArgs) OutputStream errRedirect, String stdinArgs)
throws JGitInternalException { throws JGitInternalException {
File hookFile = findHook(repository, hookName); File hookFile = findHook(repository, hookName);
if (hookFile == null || hookName == null) { if (hookFile == null || hookName == null) {

View File

@ -16,7 +16,7 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileStore; import java.nio.file.FileStore;
@ -268,7 +268,7 @@ String shellQuote(String cmd) {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public ProcessResult runHookIfPresent(Repository repository, String hookName, public ProcessResult runHookIfPresent(Repository repository, String hookName,
String[] args, PrintStream outRedirect, PrintStream errRedirect, String[] args, OutputStream outRedirect, OutputStream errRedirect,
String stdinArgs) throws JGitInternalException { String stdinArgs) throws JGitInternalException {
return internalRunHookIfPresent(repository, hookName, args, outRedirect, return internalRunHookIfPresent(repository, hookName, args, outRedirect,
errRedirect, stdinArgs); errRedirect, stdinArgs);

View File

@ -13,7 +13,7 @@
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File; import java.io.File;
import java.io.PrintStream; import java.io.OutputStream;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.ArrayList; import java.util.ArrayList;
@ -139,7 +139,7 @@ public String relativize(String base, String other) {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public ProcessResult runHookIfPresent(Repository repository, String hookName, public ProcessResult runHookIfPresent(Repository repository, String hookName,
String[] args, PrintStream outRedirect, PrintStream errRedirect, String[] args, OutputStream outRedirect, OutputStream errRedirect,
String stdinArgs) throws JGitInternalException { String stdinArgs) throws JGitInternalException {
return internalRunHookIfPresent(repository, hookName, args, outRedirect, return internalRunHookIfPresent(repository, hookName, args, outRedirect,
errRedirect, stdinArgs); errRedirect, stdinArgs);