]> source.dussan.org Git - jgit.git/commitdiff
pgm: Attempt to detect a broken pipe and exit silently 07/9407/2
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 30 Dec 2012 19:57:27 +0000 (20:57 +0100)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 7 Jan 2013 00:05:10 +0000 (01:05 +0100)
When piping output to another program, the other pipe may exit
before we are done. An example is "jgit log|head". The result is
that errno get set to EPIPE. Unfortunately Java does not have
specific exception for this so we have to look at the exception
message and hope that the number of variants are small.

The detection here seem to work on Windows, Linux and OS X and it
seems the message is usually not localized.

Change-Id: Id6968ea7a53ae27ba5496303f1a479e41e41fdcc

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java

index 0d72e639a8c25f741befd5410e6742f757e845e7..69d7d350cfe1fec37399eb92de78246408405006 100644 (file)
@@ -124,6 +124,18 @@ public class Main {
                                err.printStackTrace();
                        System.exit(128);
                } catch (Exception err) {
+                       // Try to detect errno == EPIPE and exit normally if that happens
+                       // There may be issues with operating system versions and locale,
+                       // but we can probably assume that these messages will not be thrown
+                       // under other circumstances.
+                       if (err.getClass() == IOException.class) {
+                               // Linux, OS X
+                               if (err.getMessage().equals("Broken pipe")) //$NON-NLS-1$
+                                       System.exit(0);
+                               // Windows
+                               if (err.getMessage().equals("The pipe is being closed")) //$NON-NLS-1$
+                                       System.exit(0);
+                       }
                        if (!showStackTrace && err.getCause() != null
                                        && err instanceof TransportException)
                                System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage()));