import static java.lang.Character.valueOf;
import java.io.IOException;
-import java.io.PrintWriter;
import java.text.MessageFormat;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.TrackingRefUpdate;
+import org.eclipse.jgit.util.io.ThrowingPrintWriter;
import org.kohsuke.args4j.Option;
abstract class AbstractFetchCommand extends TextBuiltin {
} finally {
reader.release();
}
- showRemoteMessages(r.getMessages());
+ showRemoteMessages(errw, r.getMessages());
}
- static void showRemoteMessages(String pkt) {
- PrintWriter writer = new PrintWriter(System.err);
+ static void showRemoteMessages(ThrowingPrintWriter writer, String pkt) throws IOException {
while (0 < pkt.length()) {
final int lf = pkt.indexOf('\n');
final int cr = pkt.indexOf('\r');
final OutputStream os = s3.beginPut(bucket, key, null, null);
final byte[] tmp = new byte[2048];
int n;
- while ((n = System.in.read(tmp)) > 0)
+ while ((n = ins.read(tmp)) > 0)
os.write(tmp, 0, n);
os.close();
@Override
protected void run() throws Exception {
- BufferedInputStream in = new BufferedInputStream(System.in);
+ BufferedInputStream in = new BufferedInputStream(ins);
ObjectInserter inserter = db.newObjectInserter();
try {
PackParser p = inserter.newPackParser(in);
printRefUpdateResult(reader, uri, result, rru);
}
- AbstractFetchCommand.showRemoteMessages(result.getMessages());
+ AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
if (everythingUpToDate)
outw.println(CLIText.get().everythingUpToDate);
}
}
rp = new org.eclipse.jgit.transport.ReceivePack(db);
- rp.receive(System.in, outs, System.err);
+ rp.receive(ins, outs, errs);
}
}
final int n = walkLoop();
if (count) {
final long end = System.currentTimeMillis();
- System.err.print(n);
- System.err.print(' ');
- System.err
- .println(MessageFormat.format(
+ errw.print(n);
+ errw.print(' ');
+ errw.println(MessageFormat.format(
CLIText.get().timeInMilliSeconds,
Long.valueOf(end - start)));
}
import java.io.BufferedWriter;
import java.io.FileDescriptor;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
@Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" })
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.
*
@Deprecated
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. */
protected Repository db;
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);
- BufferedWriter bufw;
+ 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));
+ out = new PrintWriter(outbufw);
+ outw = new ThrowingPrintWriter(outbufw);
+ BufferedWriter errbufw;
if (outputEncoding != null)
- bufw = new BufferedWriter(new OutputStreamWriter(outs,
+ errbufw = new BufferedWriter(new OutputStreamWriter(errs,
outputEncoding));
else
- bufw = new BufferedWriter(new OutputStreamWriter(outs));
- out = new PrintWriter(bufw);
- outw = new ThrowingPrintWriter(bufw);
+ errbufw = new BufferedWriter(new OutputStreamWriter(errs));
+ errw = new ThrowingPrintWriter(errbufw);
} catch (IOException e) {
throw die(CLIText.get().cannotCreateOutputStream);
}
*
* @param args
* 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);
try {
clp.parseArgument(args);
} catch (CmdLineException err) {
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);
}
}
* Print the usage line
*
* @param clp
+ * @throws IOException
*/
- public void printUsageAndExit(final CmdLineParser clp) {
+ public void printUsageAndExit(final CmdLineParser clp) throws IOException {
printUsageAndExit("", clp); //$NON-NLS-1$
}
*
* @param message
* @param clp
+ * @throws IOException
*/
- public void printUsageAndExit(final String message, final CmdLineParser clp) {
- PrintWriter writer = new PrintWriter(System.err);
- writer.println(message);
- writer.print("jgit "); //$NON-NLS-1$
- writer.print(commandName);
- clp.printSingleLineUsage(writer, getResourceBundle());
- writer.println();
-
- writer.println();
- clp.printUsage(writer, getResourceBundle());
- writer.println();
-
- writer.flush();
+ public void printUsageAndExit(final String message, final CmdLineParser clp) throws IOException {
+ errw.println(message);
+ errw.print("jgit "); //$NON-NLS-1$
+ errw.print(commandName);
+ clp.printSingleLineUsage(errw, getResourceBundle());
+ errw.println();
+
+ errw.println();
+ clp.printUsage(errw, getResourceBundle());
+ errw.println();
+
+ errw.flush();
System.exit(1);
}
up = new org.eclipse.jgit.transport.UploadPack(db);
if (0 <= timeout)
up.setTimeout(timeout);
- up.upload(System.in, outs, System.err);
+ up.upload(ins, outs, errs);
}
}
@Override
protected void run() throws Exception {
if (!really && !db.getRefDatabase().getRefs(ALL).isEmpty()) {
- System.err.println(
+ errw.println(
MessageFormat.format(CLIText.get().fatalThisProgramWillDestroyTheRepository
, db.getDirectory().getAbsolutePath(), REALLY));
throw die(CLIText.get().needApprovalToDestroyCurrentRepository);
rw.parseAny(id);
} catch (MissingObjectException mue) {
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;
}
throw new MissingObjectException(id, type);
package org.eclipse.jgit.pgm.debug;
+import java.io.IOException;
import java.net.URL;
-import org.kohsuke.args4j.Option;
import org.eclipse.jgit.pgm.Command;
import org.eclipse.jgit.pgm.CommandCatalog;
import org.eclipse.jgit.pgm.CommandRef;
import org.eclipse.jgit.pgm.TextBuiltin;
import org.eclipse.jgit.pgm.internal.CLIText;
+import org.eclipse.jgit.util.io.ThrowingPrintWriter;
+import org.kohsuke.args4j.Option;
@Command(usage = "usage_displayAListOfAllRegisteredJgitCommands")
class ShowCommands extends TextBuiltin {
width += 2;
for (final CommandRef c : list) {
- System.err.print(c.isCommon() ? '*' : ' ');
- System.err.print(' ');
+ errw.print(c.isCommon() ? '*' : ' ');
+ errw.print(' ');
- System.err.print(c.getName());
+ errw.print(c.getName());
for (int i = c.getName().length(); i < width; i++)
- System.err.print(' ');
+ errw.print(' ');
- pretty.print(c);
- System.err.println();
+ pretty.print(errw, c);
+ errw.println();
}
- System.err.println();
+ errw.println();
}
static enum Format {
/** */
USAGE {
- void print(final CommandRef c) {
+ void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
String usage = c.getUsage();
if (usage != null && usage.length() > 0)
- System.err.print(CLIText.get().resourceBundle().getString(usage));
+ err.print(CLIText.get().resourceBundle().getString(usage));
}
},
/** */
CLASSES {
- void print(final CommandRef c) {
- System.err.print(c.getImplementationClassName());
+ void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
+ err.print(c.getImplementationClassName());
}
},
/** */
URLS {
- void print(final CommandRef c) {
+ void print(ThrowingPrintWriter err, final CommandRef c) throws IOException {
final ClassLoader ldr = c.getImplementationClassLoader();
String cn = c.getImplementationClassName();
final URL url = ldr.getResource(cn);
if (url == null) {
- System.err.print(CLIText.get().notFound);
+ err.print(CLIText.get().notFound);
return;
}
if (rn.endsWith(cn))
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;
}
}