]> source.dussan.org Git - jgit.git/commitdiff
Avoid calls to System.exit() and throw an exception instead 82/21182/7
authorGuillaume Nodet <gnodet@gmail.com>
Fri, 21 Mar 2014 14:53:44 +0000 (15:53 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 1 Apr 2014 22:15:15 +0000 (00:15 +0200)
The exception is caught in Main and System.exit() is called.

Bug: 413522
Change-Id: Ibe68f7104d4fd55b832000a7840c07a169e7dd58
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/Die.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java

index a2f4060821b70b350bfeef433b4c667ddc0a92e3..f07df1a4b5d6b91ab4314d5938644478a6da315d 100644 (file)
@@ -54,6 +54,8 @@ package org.eclipse.jgit.pgm;
 public class Die extends RuntimeException {
        private static final long serialVersionUID = 1L;
 
+       private boolean aborted;
+
        /**
         * Construct a new message explaining what has gone wrong.
         *
@@ -75,4 +77,25 @@ public class Die extends RuntimeException {
        public Die(final String why, final Throwable cause) {
                super(why, cause);
        }
+
+       /**
+        * Construct a new exception reflecting the fact that the
+        * command execution has been aborted before running.
+        *
+        * @param aborted boolean indicating the fact the execution has been aborted
+        * @since 3.4
+        */
+       public Die(boolean aborted) {
+               this.aborted = aborted;
+       }
+
+       /**
+        * Check if this exception should cause the execution to be aborted.
+        *
+        * @return boolean indicating that the execution should be aborted
+        * @since 3.4
+        */
+       public boolean isAborted() {
+               return aborted;
+       }
 }
index 3648ffd8e4e4289cd4769ead75d304fb0e31a5e5..94591393afd8eddcd113ac6d70ca34a00001ba22 100644 (file)
@@ -123,6 +123,8 @@ public class Main {
                        configureHttpProxy();
                        execute(argv);
                } catch (Die err) {
+                       if (err.isAborted())
+                               System.exit(1);
                        System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
                        if (showStackTrace)
                                err.printStackTrace();
index 3308fda0e38ce93ad9fea6420f85a3a573ae6194..8e8b82fe07b0f75f3a2950682cdf1d877c2b24ed 100644 (file)
@@ -227,7 +227,7 @@ public abstract class TextBuiltin {
                } catch (CmdLineException err) {
                        if (!help) {
                                this.errw.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
-                               System.exit(1);
+                               throw die(true);
                        }
                }
 
@@ -267,7 +267,7 @@ public abstract class TextBuiltin {
                errw.println();
 
                errw.flush();
-               System.exit(1);
+               throw die(true);
        }
 
        /**
@@ -324,6 +324,16 @@ public abstract class TextBuiltin {
                return new Die(why, cause);
        }
 
+       /**
+        * @param aborted
+        *            boolean indicating that the execution has been aborted before running
+        * @return a runtime exception the caller is expected to throw
+        * @since 3.4
+        */
+       protected static Die die(boolean aborted) {
+               return new Die(aborted);
+       }
+
        String abbreviateRef(String dst, boolean abbreviateRemote) {
                if (dst.startsWith(R_HEADS))
                        dst = dst.substring(R_HEADS.length());