]> source.dussan.org Git - jgit.git/commitdiff
Add an input stream and an error stream to TextBuiltin base class 81/21181/6
authorGuillaume Nodet <gnodet@gmail.com>
Tue, 28 Jan 2014 09:19:59 +0000 (10:19 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Sun, 30 Mar 2014 21:25:09 +0000 (23:25 +0200)
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>
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/AbstractFetchCommand.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/AmazonS3Client.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/IndexPack.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Push.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ReceivePack.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/UploadPack.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowCommands.java

index 1fe39da8a6c39b5a979f53a5bdde860b3f0a023e..d226df21bb481b49e5cd833fe8f93ac90b08e78f 100644 (file)
@@ -50,7 +50,6 @@ package org.eclipse.jgit.pgm;
 import static java.lang.Character.valueOf;
 
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.text.MessageFormat;
 
 import org.eclipse.jgit.lib.Constants;
@@ -60,6 +59,7 @@ import org.eclipse.jgit.lib.RefUpdate;
 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 {
@@ -92,11 +92,10 @@ 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');
index 5e66c360120af225f61a833d88b3f268925265dd..e0a9244a8f08c09cfcf09c03a3d43758c9feab35 100644 (file)
@@ -116,7 +116,7 @@ class AmazonS3Client extends TextBuiltin {
                        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();
 
index 9fc5882426d8ebd476eff5cd94ee33f3437ac0cc..42114062b1af8ab818ea7d060647ac068945d756 100644 (file)
@@ -62,7 +62,7 @@ class IndexPack extends TextBuiltin {
 
        @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);
index b252de8e60431d404b3366237c20f0202fae6893..c7c27b4c41de7030eed46aff91ae0a6fe35b73d0 100644 (file)
@@ -161,7 +161,7 @@ class Push extends TextBuiltin {
                                printRefUpdateResult(reader, uri, result, rru);
                }
 
-               AbstractFetchCommand.showRemoteMessages(result.getMessages());
+               AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
                if (everythingUpToDate)
                        outw.println(CLIText.get().everythingUpToDate);
        }
index bc8f497164d51be1875c81cfd0b041c37c87a701..d7f895a3009f13ef8e93508b090237c022b76aee 100644 (file)
@@ -76,6 +76,6 @@ class ReceivePack extends TextBuiltin {
                }
 
                rp = new org.eclipse.jgit.transport.ReceivePack(db);
-               rp.receive(System.in, outs, System.err);
+               rp.receive(ins, outs, errs);
        }
 }
index 847bf7fc1ab92a719aeabd039a465294f80bd064..e23fb356af4d8857beb0cc1ec443d5a1d50de2da 100644 (file)
@@ -199,10 +199,9 @@ abstract class RevWalkTextBuiltin extends TextBuiltin {
                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)));
                }
index 7f7ef8d4d1531b7d0ebf5ecd0f0baabdeaeea886..3308fda0e38ce93ad9fea6420f85a3a573ae6194 100644 (file)
@@ -50,8 +50,10 @@ import static org.eclipse.jgit.lib.Constants.R_TAGS;
 
 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;
@@ -84,6 +86,13 @@ public abstract class TextBuiltin {
        @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.
         *
@@ -106,6 +115,20 @@ public abstract class TextBuiltin {
        @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;
 
@@ -137,16 +160,27 @@ public abstract class TextBuiltin {
                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);
                }
@@ -184,14 +218,15 @@ public abstract class TextBuiltin {
         *
         * @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);
                        }
                }
@@ -207,8 +242,9 @@ public abstract class TextBuiltin {
         * 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$
        }
 
@@ -217,20 +253,20 @@ public abstract class TextBuiltin {
         *
         * @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);
        }
 
index 4ace0aacd7a8dec1f897868e0e200cff0bc09c1f..447374d69947b55d9ce3dc8595c3f283e2376f86 100644 (file)
@@ -82,6 +82,6 @@ class UploadPack extends TextBuiltin {
                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);
        }
 }
index cd0236cc04c071e431f859a1ee4826abb0901317..214093983966704bf3bfb5f58338051c008736c6 100644 (file)
@@ -117,7 +117,7 @@ class RebuildCommitGraph extends TextBuiltin {
        @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);
@@ -294,7 +294,7 @@ class RebuildCommitGraph extends TextBuiltin {
                                        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);
index 4f6d1503b336d1392ef5560260216b21bcfc4ba4..aa258073b61f7e6b820e5a869056467c93196b0a 100644 (file)
 
 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 {
@@ -67,39 +69,39 @@ 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();
@@ -107,7 +109,7 @@ class ShowCommands extends TextBuiltin {
 
                                final URL url = ldr.getResource(cn);
                                if (url == null) {
-                                       System.err.print(CLIText.get().notFound);
+                                       err.print(CLIText.get().notFound);
                                        return;
                                }
 
@@ -115,10 +117,10 @@ class ShowCommands extends TextBuiltin {
                                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;
        }
 }