Add an input stream and an error stream to TextBuiltin base class

Leverage these streams to remove calls to System.in and System.err

Bug: 413522
Change-Id: I8396f3e273c93e23861e8bcfb2ab8182fb09220d
Signed-off-by: Guillaume Nodet <gnodet@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Guillaume Nodet 2014-01-28 10:19:59 +01:00 committed by Matthias Sohn
parent c8d2040722
commit c0574fe680
10 changed files with 86 additions and 50 deletions

View File

@ -50,7 +50,6 @@
import static java.lang.Character.valueOf; import static java.lang.Character.valueOf;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat; import java.text.MessageFormat;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
@ -60,6 +59,7 @@
import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.transport.FetchResult; import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.TrackingRefUpdate; import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.util.io.ThrowingPrintWriter;
import org.kohsuke.args4j.Option; import org.kohsuke.args4j.Option;
abstract class AbstractFetchCommand extends TextBuiltin { abstract class AbstractFetchCommand extends TextBuiltin {
@ -92,11 +92,10 @@ protected void showFetchResult(final FetchResult r) throws IOException {
} finally { } finally {
reader.release(); reader.release();
} }
showRemoteMessages(r.getMessages()); showRemoteMessages(errw, r.getMessages());
} }
static void showRemoteMessages(String pkt) { static void showRemoteMessages(ThrowingPrintWriter writer, String pkt) throws IOException {
PrintWriter writer = new PrintWriter(System.err);
while (0 < pkt.length()) { while (0 < pkt.length()) {
final int lf = pkt.indexOf('\n'); final int lf = pkt.indexOf('\n');
final int cr = pkt.indexOf('\r'); final int cr = pkt.indexOf('\r');

View File

@ -116,7 +116,7 @@ protected void run() throws Exception {
final OutputStream os = s3.beginPut(bucket, key, null, null); final OutputStream os = s3.beginPut(bucket, key, null, null);
final byte[] tmp = new byte[2048]; final byte[] tmp = new byte[2048];
int n; int n;
while ((n = System.in.read(tmp)) > 0) while ((n = ins.read(tmp)) > 0)
os.write(tmp, 0, n); os.write(tmp, 0, n);
os.close(); os.close();

View File

@ -62,7 +62,7 @@ class IndexPack extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
BufferedInputStream in = new BufferedInputStream(System.in); BufferedInputStream in = new BufferedInputStream(ins);
ObjectInserter inserter = db.newObjectInserter(); ObjectInserter inserter = db.newObjectInserter();
try { try {
PackParser p = inserter.newPackParser(in); PackParser p = inserter.newPackParser(in);

View File

@ -161,7 +161,7 @@ private void printPushResult(final ObjectReader reader, final URIish uri,
printRefUpdateResult(reader, uri, result, rru); printRefUpdateResult(reader, uri, result, rru);
} }
AbstractFetchCommand.showRemoteMessages(result.getMessages()); AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
if (everythingUpToDate) if (everythingUpToDate)
outw.println(CLIText.get().everythingUpToDate); outw.println(CLIText.get().everythingUpToDate);
} }

View File

@ -76,6 +76,6 @@ protected void run() throws Exception {
} }
rp = new org.eclipse.jgit.transport.ReceivePack(db); rp = new org.eclipse.jgit.transport.ReceivePack(db);
rp.receive(System.in, outs, System.err); rp.receive(ins, outs, errs);
} }
} }

View File

@ -199,10 +199,9 @@ else if (revLimiter.size() > 1)
final int n = walkLoop(); final int n = walkLoop();
if (count) { if (count) {
final long end = System.currentTimeMillis(); final long end = System.currentTimeMillis();
System.err.print(n); errw.print(n);
System.err.print(' '); errw.print(' ');
System.err errw.println(MessageFormat.format(
.println(MessageFormat.format(
CLIText.get().timeInMilliSeconds, CLIText.get().timeInMilliSeconds,
Long.valueOf(end - start))); Long.valueOf(end - start)));
} }

View File

@ -50,8 +50,10 @@
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -84,6 +86,13 @@ public abstract class TextBuiltin {
@Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" }) @Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" })
private boolean help; private boolean help;
/**
* Input stream, typically this is standard input.
*
* @since 3.4
*/
protected InputStream ins;
/** /**
* Writer to output to, typically this is standard output. * Writer to output to, typically this is standard output.
* *
@ -106,6 +115,20 @@ public abstract class TextBuiltin {
@Deprecated @Deprecated
protected PrintWriter out; protected PrintWriter out;
/**
* Error writer, typically this is standard error.
*
* @since 3.4
*/
protected ThrowingPrintWriter errw;
/**
* Error output stream, typically this is standard error.
*
* @since 3.4
*/
protected OutputStream errs;
/** Git repository the command was invoked within. */ /** Git repository the command was invoked within. */
protected Repository db; protected Repository db;
@ -137,16 +160,27 @@ protected void init(final Repository repository, final String gitDir) {
try { try {
final String outputEncoding = repository != null ? repository final String outputEncoding = repository != null ? repository
.getConfig().getString("i18n", null, "logOutputEncoding") : null; //$NON-NLS-1$ //$NON-NLS-2$ .getConfig().getString("i18n", null, "logOutputEncoding") : null; //$NON-NLS-1$ //$NON-NLS-2$
if (ins == null)
ins = new FileInputStream(FileDescriptor.in);
if (outs == null) if (outs == null)
outs = new FileOutputStream(FileDescriptor.out); outs = new FileOutputStream(FileDescriptor.out);
BufferedWriter bufw; if (errs == null)
errs = new FileOutputStream(FileDescriptor.err);
BufferedWriter outbufw;
if (outputEncoding != null) if (outputEncoding != null)
bufw = new BufferedWriter(new OutputStreamWriter(outs, outbufw = new BufferedWriter(new OutputStreamWriter(outs,
outputEncoding)); outputEncoding));
else else
bufw = new BufferedWriter(new OutputStreamWriter(outs)); outbufw = new BufferedWriter(new OutputStreamWriter(outs));
out = new PrintWriter(bufw); out = new PrintWriter(outbufw);
outw = new ThrowingPrintWriter(bufw); 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) { } catch (IOException e) {
throw die(CLIText.get().cannotCreateOutputStream); throw die(CLIText.get().cannotCreateOutputStream);
} }
@ -184,14 +218,15 @@ public final void execute(String[] args) throws Exception {
* *
* @param args * @param args
* the arguments supplied on the command line, if any. * the arguments supplied on the command line, if any.
* @throws IOException
*/ */
protected void parseArguments(final String[] args) { protected void parseArguments(final String[] args) throws IOException {
final CmdLineParser clp = new CmdLineParser(this); final CmdLineParser clp = new CmdLineParser(this);
try { try {
clp.parseArgument(args); clp.parseArgument(args);
} catch (CmdLineException err) { } catch (CmdLineException err) {
if (!help) { if (!help) {
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); this.errw.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
System.exit(1); System.exit(1);
} }
} }
@ -207,8 +242,9 @@ protected void parseArguments(final String[] args) {
* Print the usage line * Print the usage line
* *
* @param clp * @param clp
* @throws IOException
*/ */
public void printUsageAndExit(final CmdLineParser clp) { public void printUsageAndExit(final CmdLineParser clp) throws IOException {
printUsageAndExit("", clp); //$NON-NLS-1$ printUsageAndExit("", clp); //$NON-NLS-1$
} }
@ -217,20 +253,20 @@ public void printUsageAndExit(final CmdLineParser clp) {
* *
* @param message * @param message
* @param clp * @param clp
* @throws IOException
*/ */
public void printUsageAndExit(final String message, final CmdLineParser clp) { public void printUsageAndExit(final String message, final CmdLineParser clp) throws IOException {
PrintWriter writer = new PrintWriter(System.err); errw.println(message);
writer.println(message); errw.print("jgit "); //$NON-NLS-1$
writer.print("jgit "); //$NON-NLS-1$ errw.print(commandName);
writer.print(commandName); clp.printSingleLineUsage(errw, getResourceBundle());
clp.printSingleLineUsage(writer, getResourceBundle()); errw.println();
writer.println();
writer.println(); errw.println();
clp.printUsage(writer, getResourceBundle()); clp.printUsage(errw, getResourceBundle());
writer.println(); errw.println();
writer.flush(); errw.flush();
System.exit(1); System.exit(1);
} }

View File

@ -82,6 +82,6 @@ protected void run() throws Exception {
up = new org.eclipse.jgit.transport.UploadPack(db); up = new org.eclipse.jgit.transport.UploadPack(db);
if (0 <= timeout) if (0 <= timeout)
up.setTimeout(timeout); up.setTimeout(timeout);
up.upload(System.in, outs, System.err); up.upload(ins, outs, errs);
} }
} }

View File

@ -117,7 +117,7 @@ class RebuildCommitGraph extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
if (!really && !db.getRefDatabase().getRefs(ALL).isEmpty()) { if (!really && !db.getRefDatabase().getRefs(ALL).isEmpty()) {
System.err.println( errw.println(
MessageFormat.format(CLIText.get().fatalThisProgramWillDestroyTheRepository MessageFormat.format(CLIText.get().fatalThisProgramWillDestroyTheRepository
, db.getDirectory().getAbsolutePath(), REALLY)); , db.getDirectory().getAbsolutePath(), REALLY));
throw die(CLIText.get().needApprovalToDestroyCurrentRepository); throw die(CLIText.get().needApprovalToDestroyCurrentRepository);
@ -294,7 +294,7 @@ private Map<String, Ref> computeNewRefs() throws IOException {
rw.parseAny(id); rw.parseAny(id);
} catch (MissingObjectException mue) { } catch (MissingObjectException mue) {
if (!Constants.TYPE_COMMIT.equals(type)) { if (!Constants.TYPE_COMMIT.equals(type)) {
System.err.println(MessageFormat.format(CLIText.get().skippingObject, type, name)); errw.println(MessageFormat.format(CLIText.get().skippingObject, type, name));
continue; continue;
} }
throw new MissingObjectException(id, type); throw new MissingObjectException(id, type);

View File

@ -43,14 +43,16 @@
package org.eclipse.jgit.pgm.debug; package org.eclipse.jgit.pgm.debug;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import org.kohsuke.args4j.Option;
import org.eclipse.jgit.pgm.Command; import org.eclipse.jgit.pgm.Command;
import org.eclipse.jgit.pgm.CommandCatalog; import org.eclipse.jgit.pgm.CommandCatalog;
import org.eclipse.jgit.pgm.CommandRef; import org.eclipse.jgit.pgm.CommandRef;
import org.eclipse.jgit.pgm.TextBuiltin; import org.eclipse.jgit.pgm.TextBuiltin;
import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.util.io.ThrowingPrintWriter;
import org.kohsuke.args4j.Option;
@Command(usage = "usage_displayAListOfAllRegisteredJgitCommands") @Command(usage = "usage_displayAListOfAllRegisteredJgitCommands")
class ShowCommands extends TextBuiltin { class ShowCommands extends TextBuiltin {
@ -67,39 +69,39 @@ protected void run() throws Exception {
width += 2; width += 2;
for (final CommandRef c : list) { for (final CommandRef c : list) {
System.err.print(c.isCommon() ? '*' : ' '); errw.print(c.isCommon() ? '*' : ' ');
System.err.print(' '); errw.print(' ');
System.err.print(c.getName()); errw.print(c.getName());
for (int i = c.getName().length(); i < width; i++) for (int i = c.getName().length(); i < width; i++)
System.err.print(' '); errw.print(' ');
pretty.print(c); pretty.print(errw, c);
System.err.println(); errw.println();
} }
System.err.println(); errw.println();
} }
static enum Format { static enum Format {
/** */ /** */
USAGE { USAGE {
void print(final CommandRef c) { void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
String usage = c.getUsage(); String usage = c.getUsage();
if (usage != null && usage.length() > 0) if (usage != null && usage.length() > 0)
System.err.print(CLIText.get().resourceBundle().getString(usage)); err.print(CLIText.get().resourceBundle().getString(usage));
} }
}, },
/** */ /** */
CLASSES { CLASSES {
void print(final CommandRef c) { void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
System.err.print(c.getImplementationClassName()); err.print(c.getImplementationClassName());
} }
}, },
/** */ /** */
URLS { URLS {
void print(final CommandRef c) { void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
final ClassLoader ldr = c.getImplementationClassLoader(); final ClassLoader ldr = c.getImplementationClassLoader();
String cn = c.getImplementationClassName(); String cn = c.getImplementationClassName();
@ -107,7 +109,7 @@ void print(final CommandRef c) {
final URL url = ldr.getResource(cn); final URL url = ldr.getResource(cn);
if (url == null) { if (url == null) {
System.err.print(CLIText.get().notFound); err.print(CLIText.get().notFound);
return; return;
} }
@ -115,10 +117,10 @@ void print(final CommandRef c) {
if (rn.endsWith(cn)) if (rn.endsWith(cn))
rn = rn.substring(0, rn.length() - cn.length()); rn = rn.substring(0, rn.length() - cn.length());
System.err.print(rn); err.print(rn);
} }
}; };
abstract void print(CommandRef c); abstract void print(ThrowingPrintWriter err, CommandRef c) throws IOException;
} }
} }