]> source.dussan.org Git - jgit.git/commitdiff
Check for write errors in standard out and exit with error 31/7631/9
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Fri, 14 Sep 2012 16:48:56 +0000 (18:48 +0200)
committerShawn O. Pearce <spearce@spearce.org>
Sun, 16 Sep 2012 18:12:47 +0000 (11:12 -0700)
The underlying problem is that System.out is a PrintWriter and
as such it does not throw exceptions on error, but rather just
sets a flag and continues.

This changes replaces the use of System.out with a PrintWriter-like
writer that does not catch error, but instead throw them to the
caller.

Bug: 366243
Change-Id: I44405edc4416e943b87f09a0f6ed041c6c51b046

42 files changed:
org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java
org.eclipse.jgit.pgm/META-INF/MANIFEST.MF
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties
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/Blame.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Config.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Daemon.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTree.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Log.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsRemote.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsTree.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeBase.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/RevList.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevParse.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ShowRef.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.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/Version.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/MakeCacheTree.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadDirCache.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowCacheTree.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowDirCache.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/eclipse/Iplog.java
org.eclipse.jgit/src/org/eclipse/jgit/util/io/ThrowingPrintWriter.java [new file with mode: 0644]

index 0e8ebc6fa1ca30a686ab39d5459aadb225f43147..61007f98b2963560b9aa599d99c06690ca64383b 100644 (file)
@@ -15,6 +15,7 @@ Import-Package: org.eclipse.jgit.api;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.revwalk;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.storage.file;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.util;version="[2.1.0,2.2.0)",
+ org.eclipse.jgit.util.io;version="[2.1.0,2.2.0)",
  org.hamcrest.core;bundle-version="[1.1.0,2.0.0)",
  org.junit;version="[4.4.0,5.0.0)",
  org.kohsuke.args4j;version="[2.0.12,2.1.0)",
index 28339c71e3f90c73b4f0cb86f42c3cfaef601b22..78e752d44e412991d7596064be9d0112ad3090ed 100644 (file)
  */
 package org.eclipse.jgit.pgm;
 
-import java.io.BufferedWriter;
 import java.io.ByteArrayOutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -84,21 +81,20 @@ public class CLIGitCommand {
                clp.parseArgument(argv);
 
                final TextBuiltin cmd = bean.getSubcommand();
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               cmd.outs = baos;
                if (cmd.requiresRepository())
                        cmd.init(db, null);
                else
                        cmd.init(null, null);
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();
-               cmd.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
-                               baos)));
                try {
                        cmd.execute(bean.getArguments().toArray(
                                        new String[bean.getArguments().size()]));
                } catch (Die e) {
                        return IO.readLines(e.getMessage());
                } finally {
-                       if (cmd.out != null)
-                               cmd.out.flush();
+                       if (cmd.outw != null)
+                               cmd.outw.flush();
                }
                return IO.readLines(baos.toString());
        }
index 0f837b2977dfa98280f294ef40fa793e3660e1f8..00946ee158f8af78cf488232bb7e085df026bf68 100644 (file)
@@ -28,6 +28,7 @@ Import-Package: org.eclipse.jgit.api;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.treewalk;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.treewalk.filter;version="[2.1.0,2.2.0)",
  org.eclipse.jgit.util;version="[2.1.0,2.2.0)",
+ org.eclipse.jgit.util.io;version="[2.1.0,2.2.0)",
  org.kohsuke.args4j;version="[2.0.12,2.1.0)",
  org.kohsuke.args4j.spi;version="[2.0.12,2.1.0)"
 Bundle-ActivationPolicy: lazy
index 0291f5ce26d3b3889ce1a9c8508ddd1f010f6ba0..9fb0eba8fbbacf0d1cf44c2cc642ed6ac7a30e0f 100644 (file)
@@ -152,6 +152,7 @@ tagLabel=tag
 taggerInfo=Tagger: {0} <{1}>
 timeInMilliSeconds={0} ms
 tooManyRefsGiven=Too many refs given
+unknownIoErrorStdout=An unknown I/O error occurred on standard output
 unknownMergeStrategy=unknown merge strategy {0} specified
 unmergedPaths=Unmerged paths:
 unsupportedOperation=Unsupported operation: {0}
index 5d31637cc42abaec3dfe49f9fbaa5e8b7cd57d4a..4633187d77d530a997d19ec4203996937f41a0c9 100644 (file)
@@ -63,7 +63,7 @@ abstract class AbstractFetchCommand extends TextBuiltin {
        @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beMoreVerbose")
        private boolean verbose;
 
-       protected void showFetchResult(final FetchResult r) {
+       protected void showFetchResult(final FetchResult r) throws IOException {
                ObjectReader reader = db.newObjectReader();
                try {
                        boolean shownURI = false;
@@ -77,13 +77,13 @@ abstract class AbstractFetchCommand extends TextBuiltin {
                                final String dst = abbreviateRef(u.getLocalName(), true);
 
                                if (!shownURI) {
-                                       out.println(MessageFormat.format(CLIText.get().fromURI,
+                                       outw.println(MessageFormat.format(CLIText.get().fromURI,
                                                        r.getURI()));
                                        shownURI = true;
                                }
 
-                               out.format(" %c %-17s %-10s -> %s", type, longType, src, dst);
-                               out.println();
+                               outw.format(" %c %-17s %-10s -> %s", type, longType, src, dst);
+                               outw.println();
                        }
                } finally {
                        reader.release();
index cad4b6331493573395cccf5b56852a0c3b6862b8..f137b10acaacee1ae41cc2a84bbfbc0c5a4613ea 100644 (file)
@@ -86,21 +86,23 @@ class AmazonS3Client extends TextBuiltin {
                        int len = c.getContentLength();
                        final InputStream in = c.getInputStream();
                        try {
+                               outw.flush();
                                final byte[] tmp = new byte[2048];
                                while (len > 0) {
                                        final int n = in.read(tmp);
                                        if (n < 0)
                                                throw new EOFException(MessageFormat.format(CLIText.get().expectedNumberOfbytes, len));
-                                       System.out.write(tmp, 0, n);
+                                       outs.write(tmp, 0, n);
                                        len -= n;
                                }
+                               outs.flush();
                        } finally {
                                in.close();
                        }
 
                } else if ("ls".equals(op) || "list".equals(op)) {
                        for (final String k : s3.list(bucket, key))
-                               System.out.println(k);
+                               outw.println(k);
 
                } else if ("rm".equals(op) || "delete".equals(op)) {
                        s3.delete(bucket, key);
index 162f433ffafd84ec3726593324d4e16d02fe9ad6..a67c35d9337d1701e3ebfa6cc716fc83273728e9 100644 (file)
@@ -210,17 +210,18 @@ class Blame extends TextBuiltin {
                                        authorWidth, dateWidth);
 
                        for (int line = begin; line < end; line++) {
-                               out.print(abbreviate(blame.getSourceCommit(line)));
+                               outw.print(abbreviate(blame.getSourceCommit(line)));
                                if (showSourcePath)
-                                       out.format(pathFmt, path(line));
+                                       outw.format(pathFmt, path(line));
                                if (showSourceLine)
-                                       out.format(numFmt, blame.getSourceLine(line) + 1);
+                                       outw.format(numFmt, blame.getSourceLine(line) + 1);
                                if (!noAuthor)
-                                       out.format(authorFmt, author(line), date(line));
-                               out.format(lineFmt, line + 1);
-                               out.flush();
-                               blame.getResultContents().writeLine(System.out, line);
-                               out.print('\n');
+                                       outw.format(authorFmt, author(line), date(line));
+                               outw.format(lineFmt, line + 1);
+                               outw.flush();
+                               blame.getResultContents().writeLine(outs, line);
+                               outs.flush();
+                               outw.print('\n');
                        }
                } finally {
                        generator.release();
index 5fb4a963e180a8af4c80033099d90e62d6ef3c6e..c20924d3a69ed394c13259ef56c9aff6f76547da 100644 (file)
@@ -215,18 +215,18 @@ class Branch extends TextBuiltin {
 
        private void printHead(final ObjectReader reader, final String ref,
                        final boolean isCurrent, final Ref refObj) throws Exception {
-               out.print(isCurrent ? '*' : ' ');
-               out.print(' ');
-               out.print(ref);
+               outw.print(isCurrent ? '*' : ' ');
+               outw.print(' ');
+               outw.print(ref);
                if (verbose) {
                        final int spaces = maxNameLength - ref.length() + 1;
-                       out.format("%" + spaces + "s", "");
+                       outw.format("%" + spaces + "s", "");
                        final ObjectId objectId = refObj.getObjectId();
-                       out.print(reader.abbreviate(objectId).name());
-                       out.print(' ');
-                       out.print(rw.parseCommit(objectId).getShortMessage());
+                       outw.print(reader.abbreviate(objectId).name());
+                       outw.print(' ');
+                       outw.print(rw.parseCommit(objectId).getShortMessage());
                }
-               out.println();
+               outw.println();
        }
 
        private void delete(boolean force) throws IOException {
@@ -247,9 +247,9 @@ class Branch extends TextBuiltin {
                        } else if (result == Result.NEW)
                                throw die(MessageFormat.format(CLIText.get().branchNotFound, branch));
                        if (remote)
-                               out.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, branch));
+                               outw.println(MessageFormat.format(CLIText.get().deletedRemoteBranch, branch));
                        else if (verbose)
-                               out.println(MessageFormat.format(CLIText.get().deletedBranch, branch));
+                               outw.println(MessageFormat.format(CLIText.get().deletedBranch, branch));
                }
        }
 }
index 85b91ab9483ecddf8213578ae19e8398f2872b25..ac838472334f0ad4735ccd37f2043dac0d35e19a 100644 (file)
@@ -217,6 +217,7 @@ public class CLIText extends TranslationBundle {
        /***/ public String taggerInfo;
        /***/ public String timeInMilliSeconds;
        /***/ public String tooManyRefsGiven;
+       /***/ public char[] unknownIoErrorStdout;
        /***/ public String unknownMergeStrategy;
        /***/ public String unmergedPaths;
        /***/ public String unsupportedOperation;
index ee1ded09a641e11adf33e6db954d073ee3ae4128..c356e184a48e3efbf4908a9fde99bb1a38307e65 100644 (file)
@@ -84,20 +84,22 @@ class Checkout extends TextBuiltin {
                        String oldBranch = db.getBranch();
                        Ref ref = command.call();
                        if (Repository.shortenRefName(ref.getName()).equals(oldBranch)) {
-                               out.println(MessageFormat.format(CLIText.get().alreadyOnBranch,
+                               outw.println(MessageFormat.format(
+                                               CLIText.get().alreadyOnBranch,
                                                name));
                                return;
                        }
                        if (createBranch)
-                               out.println(MessageFormat.format(
+                               outw.println(MessageFormat.format(
                                                CLIText.get().switchedToNewBranch,
                                                Repository.shortenRefName(ref.getName())));
                        else
-                               out.println(MessageFormat.format(
+                               outw.println(MessageFormat.format(
                                                CLIText.get().switchedToBranch,
                                                Repository.shortenRefName(ref.getName())));
                } catch (RefNotFoundException e) {
-                       out.println(MessageFormat.format(CLIText.get().pathspecDidNotMatch,
+                       outw.println(MessageFormat.format(
+                                       CLIText.get().pathspecDidNotMatch,
                                        name));
                } catch (RefAlreadyExistsException e) {
                        throw die(MessageFormat.format(CLIText.get().branchAlreadyExists,
index de430294fbbda2791612bc6836f4d823298877ee..31d8ec8e12ff1aad3c1124504580c8dbdb400885 100644 (file)
@@ -55,8 +55,6 @@ import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheCheckout;
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
-import org.eclipse.jgit.errors.NotSupportedException;
-import org.eclipse.jgit.errors.TransportException;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefComparator;
@@ -116,10 +114,10 @@ class Clone extends AbstractFetchCommand {
                dstcfg.save();
                db = dst;
 
-               out.print(MessageFormat.format(
+               outw.print(MessageFormat.format(
                                CLIText.get().initializedEmptyGitRepositoryIn, gitdir));
-               out.println();
-               out.flush();
+               outw.println();
+               outw.flush();
 
                saveRemote(uri);
                final FetchResult r = runFetch();
@@ -139,8 +137,7 @@ class Clone extends AbstractFetchCommand {
                dstcfg.save();
        }
 
-       private FetchResult runFetch() throws NotSupportedException,
-                       URISyntaxException, TransportException {
+       private FetchResult runFetch() throws URISyntaxException, IOException {
                final Transport tn = Transport.open(db, remoteName);
                final FetchResult r;
                try {
index dccf33de08ff919d3db817df1cf41d7f300c4ecf..0963286e9f5d2e694505ddbf09d085e002a94e9c 100644 (file)
@@ -109,7 +109,7 @@ class Commit extends TextBuiltin {
                        if (branchName.startsWith(Constants.R_HEADS))
                                branchName = branchName.substring(Constants.R_HEADS.length());
                }
-               out.println("[" + branchName + " " + commit.name() + "] "
+               outw.println("[" + branchName + " " + commit.name() + "] "
                                + commit.getShortMessage());
        }
 }
index f2cd9205803dd32120d8f193935b9c7b0b70a318..cf5539f74711ea6c496f661729775e24400f5f76 100644 (file)
@@ -103,7 +103,7 @@ class Config extends TextBuiltin {
                        Set<String> names = config.getNames(section);
                        for (String name : names) {
                                for (String value : config.getStringList(section, null, name))
-                                       out.println(section + "." + name + "=" + value);
+                                       outw.println(section + "." + name + "=" + value);
                        }
                        if (names.isEmpty()) {
                                for (String subsection : config.getSubsections(section)) {
@@ -111,7 +111,7 @@ class Config extends TextBuiltin {
                                        for (String name : names) {
                                                for (String value : config.getStringList(section,
                                                                subsection, name))
-                                                       out.println(section + "." + subsection + "."
+                                                       outw.println(section + "." + subsection + "."
                                                                        + name + "=" + value);
                                        }
                                }
index 9b637cdb319a7ba5cc3a96239eca338bca8ad35b..c75da9f89ba3ef061589adb1eef0dde850d9a991 100644 (file)
@@ -127,7 +127,7 @@ class Daemon extends TextBuiltin {
 
                final FileResolver<DaemonClient> resolver = new FileResolver<DaemonClient>();
                for (final File f : directory) {
-                       out.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
+                       outw.println(MessageFormat.format(CLIText.get().exporting, f.getAbsolutePath()));
                        resolver.exportDirectory(f);
                }
                resolver.setExportAll(exportAll);
@@ -152,7 +152,7 @@ class Daemon extends TextBuiltin {
                        service(d, n).setOverridable(false);
 
                d.start();
-               out.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
+               outw.println(MessageFormat.format(CLIText.get().listeningOn, d.getAddress()));
        }
 
        private DaemonService service(final org.eclipse.jgit.transport.Daemon d,
index 5cc058950cf41a3eb5bb878d1bd6bf9eb11716d8..56564c71c2263475584499695a54e91bc7181869 100644 (file)
@@ -49,7 +49,7 @@ import static org.eclipse.jgit.lib.Constants.HEAD;
 import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
 
 import java.io.BufferedOutputStream;
-import java.io.PrintWriter;
+import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -69,13 +69,14 @@ import org.eclipse.jgit.treewalk.AbstractTreeIterator;
 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
 import org.eclipse.jgit.treewalk.FileTreeIterator;
 import org.eclipse.jgit.treewalk.filter.TreeFilter;
+import org.eclipse.jgit.util.io.ThrowingPrintWriter;
 import org.kohsuke.args4j.Argument;
 import org.kohsuke.args4j.Option;
 
 @Command(common = true, usage = "usage_ShowDiffs")
 class Diff extends TextBuiltin {
        private final DiffFormatter diffFmt = new DiffFormatter( //
-                       new BufferedOutputStream(System.out));
+                       new BufferedOutputStream(outs));
 
        @Argument(index = 0, metaVar = "metaVar_treeish")
        private AbstractTreeIterator oldTree;
@@ -202,8 +203,8 @@ class Diff extends TextBuiltin {
                        }
 
                        if (showNameAndStatusOnly) {
-                               nameStatus(out, diffFmt.scan(oldTree, newTree));
-                               out.flush();
+                               nameStatus(outw, diffFmt.scan(oldTree, newTree));
+                               outw.flush();
 
                        } else {
                                diffFmt.format(oldTree, newTree);
@@ -214,7 +215,8 @@ class Diff extends TextBuiltin {
                }
        }
 
-       static void nameStatus(PrintWriter out, List<DiffEntry> files) {
+       static void nameStatus(ThrowingPrintWriter out, List<DiffEntry> files)
+                       throws IOException {
                for (DiffEntry ent : files) {
                        switch (ent.getChangeType()) {
                        case ADD:
index ed82c4b86228cb436a35cecd181799d8c3e8e4c2..018bad069417bda2ee8058f8a4aa9736dfa08c52 100644 (file)
@@ -82,19 +82,19 @@ class DiffTree extends TextBuiltin {
                final int nTree = walk.getTreeCount();
                while (walk.next()) {
                        for (int i = 1; i < nTree; i++)
-                               out.print(':');
+                               outw.print(':');
                        for (int i = 0; i < nTree; i++) {
                                final FileMode m = walk.getFileMode(i);
                                final String s = m.toString();
                                for (int pad = 6 - s.length(); pad > 0; pad--)
-                                       out.print('0');
-                               out.print(s);
-                               out.print(' ');
+                                       outw.print('0');
+                               outw.print(s);
+                               outw.print(' ');
                        }
 
                        for (int i = 0; i < nTree; i++) {
-                               out.print(walk.getObjectId(i).name());
-                               out.print(' ');
+                               outw.print(walk.getObjectId(i).name());
+                               outw.print(' ');
                        }
 
                        char chg = 'M';
@@ -108,11 +108,11 @@ class DiffTree extends TextBuiltin {
                                else if (m0 != m1 && walk.idEqual(0, 1))
                                        chg = 'T';
                        }
-                       out.print(chg);
+                       outw.print(chg);
 
-                       out.print('\t');
-                       out.print(walk.getPathString());
-                       out.println();
+                       outw.print('\t');
+                       outw.print(walk.getPathString());
+                       outw.println();
                }
        }
 }
index e1e38e5bc80d777424d31da2a740e6dd7509020c..ab498873d9653eed2bc2054fcc38eb282d788849 100644 (file)
@@ -72,7 +72,7 @@ class Init extends TextBuiltin {
                if (gitdir != null)
                        command.setDirectory(new File(gitdir));
                Repository repository = command.call().getRepository();
-               out.println(MessageFormat.format(
+               outw.println(MessageFormat.format(
                                CLIText.get().initializedEmptyGitRepositoryIn, repository
                                                .getDirectory().getAbsolutePath()));
        }
index f0b2ca90c85c4b3b9bd01f555cb6b3b8f97a7e6e..987021e4f9694091386d9943e5c29b4d3d68e4f7 100644 (file)
@@ -80,7 +80,7 @@ class Log extends RevWalkTextBuiltin {
                        Format.DEFAULT);
 
        private final DiffFormatter diffFmt = new DiffFormatter( //
-                       new BufferedOutputStream(System.out));
+                       new BufferedOutputStream(outs));
 
        private Map<AnyObjectId, Set<Ref>> allRefsByPeeledObjectId;
 
@@ -231,43 +231,43 @@ class Log extends RevWalkTextBuiltin {
 
        @Override
        protected void show(final RevCommit c) throws Exception {
-               out.print(CLIText.get().commitLabel);
-               out.print(" ");
-               c.getId().copyTo(outbuffer, out);
+               outw.print(CLIText.get().commitLabel);
+               outw.print(" ");
+               c.getId().copyTo(outbuffer, outw);
                if (decorate) {
                        Collection<Ref> list = allRefsByPeeledObjectId.get(c);
                        if (list != null) {
-                               out.print(" (");
+                               outw.print(" (");
                                for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
-                                       out.print(i.next().getName());
+                                       outw.print(i.next().getName());
                                        if (i.hasNext())
-                                               out.print(" ");
+                                               outw.print(" ");
                                }
-                               out.print(")");
+                               outw.print(")");
                        }
                }
-               out.println();
+               outw.println();
 
                final PersonIdent author = c.getAuthorIdent();
-               out.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
-               out.println(MessageFormat.format(CLIText.get().dateInfo,
+               outw.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
+               outw.println(MessageFormat.format(CLIText.get().dateInfo,
                                dateFormatter.formatDate(author)));
 
-               out.println();
+               outw.println();
                final String[] lines = c.getFullMessage().split("\n");
                for (final String s : lines) {
-                       out.print("    ");
-                       out.print(s);
-                       out.println();
+                       outw.print("    ");
+                       outw.print(s);
+                       outw.println();
                }
 
-               out.println();
+               outw.println();
                if (showNotes(c))
-                       out.println();
+                       outw.println();
 
                if (c.getParentCount() == 1 && (showNameAndStatusOnly || showPatch))
                        showDiff(c);
-               out.flush();
+               outw.flush();
        }
 
        /**
@@ -315,23 +315,23 @@ class Log extends RevWalkTextBuiltin {
                if (blobId == null)
                        return false;
                if (emptyLine)
-                       out.println();
-               out.print("Notes");
+                       outw.println();
+               outw.print("Notes");
                if (label != null) {
-                       out.print(" (");
-                       out.print(label);
-                       out.print(")");
+                       outw.print(" (");
+                       outw.print(label);
+                       outw.print(")");
                }
-               out.println(":");
+               outw.println(":");
                try {
                        RawText rawText = new RawText(argWalk.getObjectReader()
                                        .open(blobId).getCachedBytes(Integer.MAX_VALUE));
                        for (int i = 0; i < rawText.size(); i++) {
-                               out.print("    ");
-                               out.println(rawText.getString(i));
+                               outw.print("    ");
+                               outw.println(rawText.getString(i));
                        }
                } catch (LargeObjectException e) {
-                       out.println(MessageFormat.format(
+                       outw.println(MessageFormat.format(
                                        CLIText.get().noteObjectTooLargeToPrint, blobId.name()));
                }
                return true;
@@ -342,12 +342,12 @@ class Log extends RevWalkTextBuiltin {
                final RevTree b = c.getTree();
 
                if (showNameAndStatusOnly)
-                       Diff.nameStatus(out, diffFmt.scan(a, b));
+                       Diff.nameStatus(outw, diffFmt.scan(a, b));
                else {
-                       out.flush();
+                       outw.flush();
                        diffFmt.format(a, b);
                        diffFmt.flush();
                }
-               out.println();
+               outw.println();
        }
 }
index e750e2dea8e951df3227e460b23ead736c623787..d59451c55e44f785dff448d88207f8977e08383d 100644 (file)
@@ -45,6 +45,8 @@
 
 package org.eclipse.jgit.pgm;
 
+import java.io.IOException;
+
 import org.kohsuke.args4j.Argument;
 import org.kohsuke.args4j.Option;
 import org.eclipse.jgit.lib.AnyObjectId;
@@ -77,10 +79,11 @@ class LsRemote extends TextBuiltin {
                }
        }
 
-       private void show(final AnyObjectId id, final String name) {
-               out.print(id.name());
-               out.print('\t');
-               out.print(name);
-               out.println();
+       private void show(final AnyObjectId id, final String name)
+                       throws IOException {
+               outw.print(id.name());
+               outw.print('\t');
+               outw.print(name);
+               outw.println();
        }
 }
index 6878d8ef480e8f70e9af4b8e0b10f384cf68782d..16480d9dbfceeee8a3b80dd148b6ef8c1ec61871 100644 (file)
@@ -68,17 +68,17 @@ class LsTree extends TextBuiltin {
                while (walk.next()) {
                        final FileMode mode = walk.getFileMode(0);
                        if (mode == FileMode.TREE)
-                               out.print('0');
-                       out.print(mode);
-                       out.print(' ');
-                       out.print(Constants.typeString(mode.getObjectType()));
+                               outw.print('0');
+                       outw.print(mode);
+                       outw.print(' ');
+                       outw.print(Constants.typeString(mode.getObjectType()));
 
-                       out.print(' ');
-                       out.print(walk.getObjectId(0).name());
+                       outw.print(' ');
+                       outw.print(walk.getObjectId(0).name());
 
-                       out.print('\t');
-                       out.print(walk.getPathString());
-                       out.println();
+                       outw.print('\t');
+                       outw.print(walk.getPathString());
+                       outw.println();
                }
        }
 }
index 654eed2cd9d5f11ae31ca7fd7848cfd06e60986f..6c5cd2767c2898987cb40ee47e832b7c514e5c83 100644 (file)
@@ -137,6 +137,15 @@ public class Main {
                        err.printStackTrace();
                        System.exit(1);
                }
+               if (System.out.checkError()) {
+                       System.err.println(CLIText.get().unknownIoErrorStdout);
+                       System.exit(1);
+               }
+               if (System.err.checkError()) {
+                       // No idea how to present an error here, most likely disk full or
+                       // broken pipe
+                       System.exit(1);
+               }
        }
 
        private void execute(final String[] argv) throws Exception {
@@ -190,8 +199,8 @@ public class Main {
                try {
                        cmd.execute(arguments.toArray(new String[arguments.size()]));
                } finally {
-                       if (cmd.out != null)
-                               cmd.out.flush();
+                       if (cmd.outw != null)
+                               cmd.outw.flush();
                }
        }
 
index 4cd82b8225625186f0595357ff65bee7e6d40e96..da0f8b868a883e870099fd90d738593d7e27f56f 100644 (file)
@@ -88,13 +88,13 @@ class Merge extends TextBuiltin {
                switch (result.getMergeStatus()) {
                case ALREADY_UP_TO_DATE:
                case FAST_FORWARD:
-                       out.println(result.getMergeStatus().toString());
+                       outw.println(result.getMergeStatus().toString());
                        break;
                case CONFLICTING:
                        for (String collidingPath : result.getConflicts().keySet())
-                               out.println(MessageFormat.format(CLIText.get().mergeConflict,
+                               outw.println(MessageFormat.format(CLIText.get().mergeConflict,
                                                collidingPath));
-                       out.println(CLIText.get().mergeFailed);
+                       outw.println(CLIText.get().mergeFailed);
                        break;
                case FAILED:
                        for (Map.Entry<String, MergeFailureReason> entry : result
@@ -102,21 +102,21 @@ class Merge extends TextBuiltin {
                                switch (entry.getValue()) {
                                case DIRTY_WORKTREE:
                                case DIRTY_INDEX:
-                                       out.println(CLIText.get().dontOverwriteLocalChanges);
-                                       out.println("        " + entry.getKey());
+                                       outw.println(CLIText.get().dontOverwriteLocalChanges);
+                                       outw.println("        " + entry.getKey());
                                        break;
                                case COULD_NOT_DELETE:
-                                       out.println(CLIText.get().cannotDeleteFile);
-                                       out.println("        " + entry.getKey());
+                                       outw.println(CLIText.get().cannotDeleteFile);
+                                       outw.println("        " + entry.getKey());
                                        break;
                                }
                        break;
                case MERGED:
-                       out.println(MessageFormat.format(CLIText.get().mergeMadeBy,
+                       outw.println(MessageFormat.format(CLIText.get().mergeMadeBy,
                                        mergeStrategy.getName()));
                        break;
                case NOT_SUPPORTED:
-                       out.println(MessageFormat.format(
+                       outw.println(MessageFormat.format(
                                        CLIText.get().unsupportedOperation, result.toString()));
                }
        }
index f59fd431585bca46688293d3fc5c757d9c709db7..34aa9b2feda2acfca098fca81b7bbb46185b99be 100644 (file)
@@ -74,7 +74,7 @@ class MergeBase extends TextBuiltin {
                        final RevCommit b = argWalk.next();
                        if (b == null)
                                break;
-                       out.println(b.getId().name());
+                       outw.println(b.getId().name());
                }
        }
 }
index 677e7332c3956883a71026f78751f549e6890b8b..f4665172502a462fa55ced76bc47f40f23a5ae15 100644 (file)
@@ -132,7 +132,7 @@ class Push extends TextBuiltin {
        }
 
        private void printPushResult(final ObjectReader reader, final URIish uri,
-                       final PushResult result) {
+                       final PushResult result) throws IOException {
                shownURI = false;
                boolean everythingUpToDate = true;
 
@@ -160,14 +160,15 @@ class Push extends TextBuiltin {
 
                AbstractFetchCommand.showRemoteMessages(result.getMessages());
                if (everythingUpToDate)
-                       out.println(CLIText.get().everythingUpToDate);
+                       outw.println(CLIText.get().everythingUpToDate);
        }
 
        private void printRefUpdateResult(final ObjectReader reader,
-                       final URIish uri, final PushResult result, final RemoteRefUpdate rru) {
+                       final URIish uri, final PushResult result, final RemoteRefUpdate rru)
+                       throws IOException {
                if (!shownURI) {
                        shownURI = true;
-                       out.println(MessageFormat.format(CLIText.get().pushTo, uri));
+                       outw.println(MessageFormat.format(CLIText.get().pushTo, uri));
                }
 
                final String remoteName = rru.getRemoteName();
@@ -247,16 +248,17 @@ class Push extends TextBuiltin {
        }
 
        private void printUpdateLine(final char flag, final String summary,
-                       final String srcRef, final String destRef, final String message) {
-               out.format(" %c %-17s", flag, summary);
+                       final String srcRef, final String destRef, final String message)
+                       throws IOException {
+               outw.format(" %c %-17s", flag, summary);
 
                if (srcRef != null)
-                       out.format(" %s ->", abbreviateRef(srcRef, true));
-               out.format(" %s", abbreviateRef(destRef, true));
+                       outw.format(" %s ->", abbreviateRef(srcRef, true));
+               outw.format(" %s", abbreviateRef(destRef, true));
 
                if (message != null)
-                       out.format(" (%s)", message);
+                       outw.format(" (%s)", message);
 
-               out.println();
+               outw.println();
        }
 }
index 7a276172202e84d74c8db0f38357c08bf8ebd95c..dd6de1d667076d07a6572c29f47a2d16df03d514 100644 (file)
@@ -75,6 +75,6 @@ class ReceivePack extends TextBuiltin {
                }
 
                rp = new org.eclipse.jgit.transport.ReceivePack(db);
-               rp.receive(System.in, System.out, System.err);
+               rp.receive(System.in, outs, System.err);
        }
 }
index 1b8c180f912c65071d2840a6062d0ba3cad56b36..bab502b791cdc58e6f4b8f3136fbf5d70c7d7332 100644 (file)
@@ -53,29 +53,29 @@ class RevList extends RevWalkTextBuiltin {
        @Override
        protected void show(final RevCommit c) throws Exception {
                if (c.has(RevFlag.UNINTERESTING))
-                       out.print('-');
-               c.getId().copyTo(outbuffer, out);
+                       outw.print('-');
+               c.getId().copyTo(outbuffer, outw);
                if (parents)
                        for (int i = 0; i < c.getParentCount(); i++) {
-                               out.print(' ');
-                               c.getParent(i).getId().copyTo(outbuffer, out);
+                               outw.print(' ');
+                               c.getParent(i).getId().copyTo(outbuffer, outw);
                        }
-               out.println();
+               outw.println();
        }
 
        @Override
        protected void show(final ObjectWalk ow, final RevObject obj)
                        throws Exception {
                if (obj.has(RevFlag.UNINTERESTING))
-                       out.print('-');
-               obj.getId().copyTo(outbuffer, out);
+                       outw.print('-');
+               obj.getId().copyTo(outbuffer, outw);
                final String path = ow.getPathString();
                if (path != null) {
-                       out.print(' ');
-                       out.print(path);
+                       outw.print(' ');
+                       outw.print(path);
                } else if (obj instanceof RevTree)
-                       out.print(' ');
-               out.println();
+                       outw.print(' ');
+               outw.println();
 
        }
 }
index 98eb2ce86b1e8d153068a9fcdae8b40903f2da80..b564107b9e7f76bbd91e3ace2f716b2c62be99ab 100644 (file)
@@ -65,10 +65,10 @@ class RevParse extends TextBuiltin {
                if (all) {
                        Map<String, Ref> allRefs = db.getAllRefs();
                        for (final Ref r : allRefs.values())
-                               out.println(r.getObjectId().name());
+                               outw.println(r.getObjectId().name());
                } else {
                        for (final ObjectId o : commits)
-                               out.println(o.name());
+                               outw.println(o.name());
                }
        }
 }
index 5eeb21492064044c496db6b145d5dea142394099..13dad7a9a06791b393a826522717e7d9bb4400c1 100644 (file)
@@ -198,16 +198,16 @@ class Show extends TextBuiltin {
                                        break;
 
                                case Constants.OBJ_TREE:
-                                       out.print("tree ");
-                                       out.print(objectName);
-                                       out.println();
-                                       out.println();
+                                       outw.print("tree ");
+                                       outw.print(objectName);
+                                       outw.println();
+                                       outw.println();
                                        show((RevTree) obj);
                                        break;
 
                                case Constants.OBJ_BLOB:
                                        db.open(obj, obj.getType()).copyTo(System.out);
-                                       System.out.flush();
+                                       outw.flush();
                                        break;
 
                                default:
@@ -223,32 +223,32 @@ class Show extends TextBuiltin {
                }
        }
 
-       private void show(RevTag tag) {
-               out.print(CLIText.get().tagLabel);
-               out.print(" ");
-               out.print(tag.getTagName());
-               out.println();
+       private void show(RevTag tag) throws IOException {
+               outw.print(CLIText.get().tagLabel);
+               outw.print(" ");
+               outw.print(tag.getTagName());
+               outw.println();
 
                final PersonIdent tagger = tag.getTaggerIdent();
                if (tagger != null) {
-                       out.println(MessageFormat.format(CLIText.get().taggerInfo,
+                       outw.println(MessageFormat.format(CLIText.get().taggerInfo,
                                        tagger.getName(), tagger.getEmailAddress()));
 
                        final TimeZone taggerTZ = tagger.getTimeZone();
                        fmt.setTimeZone(taggerTZ != null ? taggerTZ : myTZ);
-                       out.println(MessageFormat.format(CLIText.get().dateInfo,
+                       outw.println(MessageFormat.format(CLIText.get().dateInfo,
                                        fmt.format(tagger.getWhen())));
                }
 
-               out.println();
+               outw.println();
                final String[] lines = tag.getFullMessage().split("\n");
                for (final String s : lines) {
-                       out.print("    ");
-                       out.print(s);
-                       out.println();
+                       outw.print("    ");
+                       outw.print(s);
+                       outw.println();
                }
 
-               out.println();
+               outw.println();
        }
 
        private void show(RevTree obj) throws MissingObjectException,
@@ -258,45 +258,45 @@ class Show extends TextBuiltin {
                walk.addTree(obj);
 
                while (walk.next()) {
-                       out.print(walk.getPathString());
+                       outw.print(walk.getPathString());
                        final FileMode mode = walk.getFileMode(0);
                        if (mode == FileMode.TREE)
-                               out.print('/');
-                       out.println();
+                               outw.print("/");
+                       outw.println();
                }
        }
 
        private void show(RevWalk rw, final RevCommit c) throws Exception {
                char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
 
-               out.print(CLIText.get().commitLabel);
-               out.print(" ");
-               c.getId().copyTo(outbuffer, out);
-               out.println();
+               outw.print(CLIText.get().commitLabel);
+               outw.print(" ");
+               c.getId().copyTo(outbuffer, outw);
+               outw.println();
 
                final PersonIdent author = c.getAuthorIdent();
-               out.println(MessageFormat.format(CLIText.get().authorInfo,
+               outw.println(MessageFormat.format(CLIText.get().authorInfo,
                                author.getName(), author.getEmailAddress()));
 
                final TimeZone authorTZ = author.getTimeZone();
                fmt.setTimeZone(authorTZ != null ? authorTZ : myTZ);
-               out.println(MessageFormat.format(CLIText.get().dateInfo,
+               outw.println(MessageFormat.format(CLIText.get().dateInfo,
                                fmt.format(author.getWhen())));
 
-               out.println();
+               outw.println();
                final String[] lines = c.getFullMessage().split("\n");
                for (final String s : lines) {
-                       out.print("    ");
-                       out.print(s);
-                       out.println();
+                       outw.print("    ");
+                       outw.print(s);
+                       outw.println();
                }
 
-               out.println();
+               outw.println();
                if (c.getParentCount() == 1) {
                        rw.parseHeaders(c.getParent(0));
                        showDiff(c);
                }
-               out.flush();
+               outw.flush();
        }
 
        private void showDiff(RevCommit c) throws IOException {
@@ -304,12 +304,12 @@ class Show extends TextBuiltin {
                final RevTree b = c.getTree();
 
                if (showNameAndStatusOnly)
-                       Diff.nameStatus(out, diffFmt.scan(a, b));
+                       Diff.nameStatus(outw, diffFmt.scan(a, b));
                else {
-                       out.flush();
+                       outw.flush();
                        diffFmt.format(a, b);
                        diffFmt.flush();
                }
-               out.println();
+               outw.println();
        }
 }
index d34f373db2c6a267cbf143859626f9aedc202f2c..3e0f66109798e86df178540cd85f1520f2178aea 100644 (file)
@@ -45,6 +45,7 @@
 
 package org.eclipse.jgit.pgm;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.SortedMap;
 
@@ -71,10 +72,11 @@ class ShowRef extends TextBuiltin {
                return RefComparator.sort(all.values());
        }
 
-       private void show(final AnyObjectId id, final String name) {
-               out.print(id.name());
-               out.print('\t');
-               out.print(name);
-               out.println();
+       private void show(final AnyObjectId id, final String name)
+                       throws IOException {
+               outw.print(id.name());
+               outw.print('\t');
+               outw.print(name);
+               outw.println();
        }
 }
index 1489e47d1cf65507e2b4c5f5e3a59f6aef7a9834..81143f6b86b7551ba736a6da2872fe67a8771b79 100644 (file)
@@ -91,7 +91,7 @@ class Tag extends TextBuiltin {
                        ListTagCommand command = git.tagList();
                        List<Ref> list = command.call();
                        for (Ref ref : list) {
-                               out.println(Repository.shortenRefName(ref.getName()));
+                               outw.println(Repository.shortenRefName(ref.getName()));
                        }
                }
        }
index 2988dfc54bb596673458bb37a1cacbd16d9a5642..fe8907bc20ab2194fc815407343eb733145cfb62 100644 (file)
@@ -49,7 +49,10 @@ import static org.eclipse.jgit.lib.Constants.R_REMOTES;
 import static org.eclipse.jgit.lib.Constants.R_TAGS;
 
 import java.io.BufferedWriter;
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.text.MessageFormat;
@@ -59,6 +62,7 @@ import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.pgm.opt.CmdLineParser;
 import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.util.io.ThrowingPrintWriter;
 import org.kohsuke.args4j.CmdLineException;
 import org.kohsuke.args4j.Option;
 
@@ -79,7 +83,17 @@ public abstract class TextBuiltin {
        @Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" })
        private boolean help;
 
+       /** Writer to output to, typically this is standard output. */
+       protected ThrowingPrintWriter outw;
+
        /** Stream to output to, typically this is standard output. */
+       protected OutputStream outs;
+
+       /**
+        * Stream to output to, typically this is standard output.
+        *
+        * @deprecated Use outw instead
+        */
        protected PrintWriter out;
 
        /** Git repository the command was invoked within. */
@@ -114,12 +128,16 @@ public abstract class TextBuiltin {
                        final String outputEncoding = repository != null ? repository
                                        .getConfig()
                                        .getString("i18n", null, "logOutputEncoding") : null;
+                       if (outs == null)
+                               outs = new FileOutputStream(FileDescriptor.out);
+                       BufferedWriter bufw;
                        if (outputEncoding != null)
-                               out = new PrintWriter(new BufferedWriter(
-                                               new OutputStreamWriter(System.out, outputEncoding)));
+                               bufw = new BufferedWriter(new OutputStreamWriter(outs,
+                                               outputEncoding));
                        else
-                               out = new PrintWriter(new BufferedWriter(
-                                               new OutputStreamWriter(System.out)));
+                               bufw = new BufferedWriter(new OutputStreamWriter(outs));
+                       out = new PrintWriter(bufw);
+                       outw = new ThrowingPrintWriter(bufw);
                } catch (IOException e) {
                        throw die(CLIText.get().cannotCreateOutputStream);
                }
index d4e2bcec76c56f72579794ee984d1a3dae5d2bfd..df7f1c3c8c68324174eecc59ea0f14a8b1921ee3 100644 (file)
@@ -81,6 +81,6 @@ class UploadPack extends TextBuiltin {
                up = new org.eclipse.jgit.transport.UploadPack(db);
                if (0 <= timeout)
                        up.setTimeout(timeout);
-               up.upload(System.in, System.out, System.err);
+               up.upload(System.in, outs, System.err);
        }
 }
index 9b51b871fcb785cb4e310afd0f8a80af5f3d2815..cd7f5116b8fa6f892b1d1c4e7f59140cf52ed1ba 100644 (file)
@@ -53,7 +53,7 @@ class Version extends TextBuiltin {
                if (pkg == null || pkg.getImplementationVersion() == null)
                        throw die(CLIText.get().cannotReadPackageInformation);
 
-               out.println(MessageFormat.format(CLIText.get().jgitVersion, pkg.getImplementationVersion()));
+               outw.println(MessageFormat.format(CLIText.get().jgitVersion, pkg.getImplementationVersion()));
        }
 
        @Override
index f98cdfa4af9534dff0160634f09be5fecd4f2ea0..ef297322dfeaef7ded8ed5d493aa555640ec5e69 100644 (file)
@@ -245,29 +245,29 @@ class DiffAlgorithms extends TextBuiltin {
                        File parent = db.getDirectory().getParentFile();
                        if (name.equals(Constants.DOT_GIT) && parent != null)
                                name = parent.getName();
-                       out.println(name + ": start at " + startId.name());
+                       outw.println(name + ": start at " + startId.name());
                }
 
-               out.format("  %12d files,     %8d commits\n", files, commits);
-               out.format("  N=%10d min lines, %8d max lines\n", minN, maxN);
+               outw.format("  %12d files,     %8d commits\n", files, commits);
+               outw.format("  N=%10d min lines, %8d max lines\n", minN, maxN);
 
-               out.format("%-25s %12s ( %12s  %12s )\n", //
+               outw.format("%-25s %12s ( %12s  %12s )\n", //
                                "Algorithm", "Time(ns)", "Time(ns) on", "Time(ns) on");
-               out.format("%-25s %12s ( %12s  %12s )\n", //
+               outw.format("%-25s %12s ( %12s  %12s )\n", //
                                "", "", "N=" + minN, "N=" + maxN);
-               out.println("-----------------------------------------------------"
+               outw.println("-----------------------------------------------------"
                                + "----------------");
 
                for (Test test : all) {
-                       out.format("%-25s %12d ( %12d  %12d )", //
+                       outw.format("%-25s %12d ( %12d  %12d )", //
                                        test.algorithm.name, //
                                        test.runningTimeNanos, //
                                        test.minN.runningTimeNanos, //
                                        test.maxN.runningTimeNanos);
-                       out.println();
+                       outw.println();
                }
-               out.println();
-               out.flush();
+               outw.println();
+               outw.flush();
        }
 
        private static boolean isFile(TreeWalk tw, int ithTree) {
index 709b45a17b3cb9ec8c8171e246c1cba4eafa0966..3961a7c1560e05b9d628201555b7668e54694c77 100644 (file)
@@ -44,6 +44,7 @@
 
 package org.eclipse.jgit.pgm.debug;
 
+import java.io.IOException;
 import java.text.MessageFormat;
 
 import org.eclipse.jgit.dircache.DirCache;
@@ -59,8 +60,8 @@ class MakeCacheTree extends TextBuiltin {
                show(tree);
        }
 
-       private void show(final DirCacheTree tree) {
-               out.println(MessageFormat.format(CLIText.get().cacheTreePathInfo
+       private void show(final DirCacheTree tree) throws IOException {
+               outw.println(MessageFormat.format(CLIText.get().cacheTreePathInfo
                                , tree.getPathString(), tree.getEntrySpan(), tree.getChildCount()));
 
                for (int i = 0; i < tree.getChildCount(); i++)
index 0ca050880eafc576a4789b30ef96cd8f9b2b2939..83dacce6b5bddd84fc77d653d13d9d7e698fa6ad 100644 (file)
@@ -57,7 +57,7 @@ class ReadDirCache extends TextBuiltin {
                for (int i = 0; i < cnt; i++)
                        db.readDirCache();
                final long end = System.currentTimeMillis();
-               out.print(" ");
-               out.println(MessageFormat.format(CLIText.get().averageMSPerRead, (end - start) / cnt));
+               outw.print(" ");
+               outw.println(MessageFormat.format(CLIText.get().averageMSPerRead, (end - start) / cnt));
        }
 }
index c49aefbf2fe1b1300ce513effd5ccc7b6b7b0ca8..7b4b36aa423090bf25f804711e0eb56ed7e65db8 100644 (file)
@@ -44,6 +44,7 @@
 
 package org.eclipse.jgit.pgm.debug;
 
+import java.io.IOException;
 import java.text.MessageFormat;
 
 import org.eclipse.jgit.dircache.DirCache;
@@ -61,8 +62,8 @@ class ShowCacheTree extends TextBuiltin {
                show(tree);
        }
 
-       private void show(final DirCacheTree tree) {
-               out.println(MessageFormat.format(CLIText.get().cacheTreePathInfo
+       private void show(final DirCacheTree tree) throws IOException {
+               outw.println(MessageFormat.format(CLIText.get().cacheTreePathInfo
                                , tree.getPathString(), tree.getEntrySpan(), tree.getChildCount()));
 
                for (int i = 0; i < tree.getChildCount(); i++)
index fb3e3e65bfbecdb8fd03c68320fe86db92f7d5b2..a710103e9d8aaf31e29c3574905340c5f5d1eedc 100644 (file)
@@ -68,17 +68,17 @@ class ShowDirCache extends TextBuiltin {
                        final Date mtime = new Date(ent.getLastModified());
                        final int stage = ent.getStage();
 
-                       out.print(mode);
-                       out.format(" %6d", len);
-                       out.print(' ');
-                       out.print(fmt.format(mtime));
-                       out.print(' ');
-                       out.print(ent.getObjectId().name());
-                       out.print(' ');
-                       out.print(stage);
-                       out.print('\t');
-                       out.print(ent.getPathString());
-                       out.println();
+                       outw.print(mode);
+                       outw.format(" %6d", len);
+                       outw.print(' ');
+                       outw.print(fmt.format(mtime));
+                       outw.print(' ');
+                       outw.print(ent.getObjectId().name());
+                       outw.print(' ');
+                       outw.print(stage);
+                       outw.print('\t');
+                       outw.print(ent.getPathString());
+                       outw.println();
                }
        }
 }
index ddeec330f7f71a43c8d5c576e6cc64cd22e411f7..c852d41638f43af853b3076d71a14ebf5304e012 100644 (file)
@@ -87,7 +87,7 @@ class ShowPackDelta extends TextBuiltin {
                        throw die("Object " + obj.name() + " is not a delta");
                }
 
-               out.println(BinaryDelta.format(delta));
+               outw.println(BinaryDelta.format(delta));
        }
 
        private byte[] getDelta(ObjectReader reader, RevObject obj)
index e7051a1bdb0e78d35e664fd828a6dbe9a5c2588e..ab80037da3e0f0290b3f73f6b2914d69af8bb871 100644 (file)
@@ -344,26 +344,26 @@ class TextHashFunctions extends TextBuiltin {
                        File parent = db.getDirectory().getParentFile();
                        if (name.equals(Constants.DOT_GIT) && parent != null)
                                name = parent.getName();
-                       out.println(name + ":");
+                       outw.println(name + ":");
                }
-               out.format("  %6d files; %5d avg. unique lines/file\n", //
+               outw.format("  %6d files; %5d avg. unique lines/file\n", //
                                fileCnt, //
                                lineCnt / fileCnt);
-               out.format("%-20s %-15s %9s\n", "Hash", "Fold", "Max Len");
-               out.println("-----------------------------------------------");
+               outw.format("%-20s %-15s %9s\n", "Hash", "Fold", "Max Len");
+               outw.println("-----------------------------------------------");
                String lastHashName = null;
                for (Function fun : all) {
                        String hashName = fun.hash.name;
                        if (hashName.equals(lastHashName))
                                hashName = "";
-                       out.format("%-20s %-15s %9d\n", //
+                       outw.format("%-20s %-15s %9d\n", //
                                        hashName, //
                                        fun.fold.name, //
                                        fun.maxChainLength);
                        lastHashName = fun.hash.name;
                }
-               out.println();
-               out.flush();
+               outw.println();
+               outw.flush();
        }
 
        private void testOne(Function fun, RawText txt, int[] elements, int cnt) {
index 86b93a0f84dbac5ba7a9ec85301cc40da512a705..5784fecd8d2738b74778c0c01541e2a425e3778c 100644 (file)
@@ -114,8 +114,8 @@ class Iplog extends TextBuiltin {
                                lf.unlock();
                        }
                } else {
-                       log.writeTo(System.out);
-                       System.out.flush();
+                       log.writeTo(outs);
+                       outs.flush();
                }
        }
 }
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/ThrowingPrintWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/ThrowingPrintWriter.java
new file mode 100644 (file)
index 0000000..1c5c2b6
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2012, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * 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.util.io;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.eclipse.jgit.util.SystemReader;
+
+/**
+ * An alternative PrintWriter that doesn't catch exceptions.
+ */
+public class ThrowingPrintWriter extends Writer {
+
+       private final Writer out;
+
+       private final String LF;
+
+       /**
+        * Construct a JGitPrintWriter
+        *
+        * @param out
+        *            the underlying {@link Writer}
+        */
+       public ThrowingPrintWriter(Writer out) {
+               this.out = out;
+               LF = AccessController.doPrivileged(new PrivilegedAction<String>() {
+                       public String run() {
+                               return SystemReader.getInstance().getProperty("line.separator");
+                       }
+               });
+       }
+
+       @Override
+       public void write(char[] cbuf, int off, int len) throws IOException {
+               out.write(cbuf, off, len);
+       }
+
+       @Override
+       public void flush() throws IOException {
+               out.flush();
+       }
+
+       @Override
+       public void close() throws IOException {
+               out.close();
+       }
+
+       /**
+        * Print a string and terminate with a line feed.
+        *
+        * @param s
+        * @throws IOException
+        */
+       public void println(String s) throws IOException {
+               print(s + LF);
+       }
+
+       /**
+        * Print a platform dependent new line
+        *
+        * @throws IOException
+        */
+       public void println() throws IOException {
+               print(LF);
+       }
+
+       /**
+        * Print a char
+        *
+        * @param value
+        * @throws IOException
+        */
+       public void print(char value) throws IOException {
+               print(String.valueOf(value));
+       }
+
+       /**
+        * Print an int as string
+        *
+        * @param value
+        * @throws IOException
+        */
+       public void print(int value) throws IOException {
+               print(String.valueOf(value));
+       }
+
+       /**
+        * Print a long as string
+        *
+        * @param value
+        * @throws IOException
+        */
+       public void print(long value) throws IOException {
+               print(String.valueOf(value));
+       }
+
+       /**
+        * Print a short as string
+        *
+        * @param value
+        * @throws IOException
+        */
+       public void print(short value) throws IOException {
+               print(String.valueOf(value));
+       }
+
+       /**
+        * Print a formatted message according to
+        * {@link String#format(String, Object...)}.
+        *
+        * @param fmt
+        * @param args
+        * @throws IOException
+        */
+       public void format(String fmt, Object... args) throws IOException {
+               print(String.format(fmt, args));
+       }
+
+       /**
+        * Print an object's toString representations
+        *
+        * @param any
+        * @throws IOException
+        */
+       public void print(Object any) throws IOException {
+               out.write(String.valueOf(any));
+       }
+}