From 68798d38f715f497748ec491b430d3b7eda8a165 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Thu, 17 Nov 2016 15:03:32 +0100
Subject: [PATCH] Add convenience methods for checking whether an ExecRes
 failed or succeeded. Split up GitWrapper#failed.

---
 .../gitwrapper/process/ProcessExecutor.java   | 22 ++++++++++++++++++-
 .../seibt/gitwrapper/process/ToolWrapper.java |  2 --
 .../fim/seibt/gitwrapper/repo/GitWrapper.java | 19 ++++++++++++----
 3 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java b/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java
index 36e2bef..75ae757 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java
@@ -3,7 +3,6 @@ package de.uni_passau.fim.seibt.gitwrapper.process;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -39,6 +38,8 @@ public class ProcessExecutor {
      */
     public static final class ExecRes {
 
+        public static final int EXIT_SUCCESS = 0;
+
         /**
          * The exit code of the process that produced this {@link ExecRes}.
          */
@@ -100,6 +101,25 @@ public class ProcessExecutor {
 
             return stdErrTrimmed;
         }
+
+        /**
+         * Returns whether the exit code contained in this {@link ExecRes} is {@value ExecRes#EXIT_SUCCESS}.
+         *
+         * @return whether the exit code in this {@link ExecRes} indicates successful execution
+         */
+        public boolean succeeded() {
+            return exitCode == EXIT_SUCCESS;
+        }
+
+        /**
+         * Returns whether the exit code contained in this {@link ExecRes} is anything other than
+         * {@value ExecRes#EXIT_SUCCESS}.
+         *
+         * @return whether the exit code in this {@link ExecRes} indicates a failed execution
+         */
+        public boolean failed() {
+            return exitCode != EXIT_SUCCESS;
+        }
     }
 
     /**
diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java
index f960c2b..3d44b03 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java
@@ -14,8 +14,6 @@ import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor.ExecRes;
  */
 public abstract class ToolWrapper {
 
-    protected static final int EXIT_SUCCESS = 0;
-
     protected String cmd;
 
     /**
diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java
index 89beddd..84e88d2 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java
@@ -110,7 +110,7 @@ public class GitWrapper extends ToolWrapper {
             String firstLine = sc.nextLine();
             Matcher matcher;
 
-            if (execRes.exitCode == EXIT_SUCCESS) {
+            if (execRes.succeeded()) {
                 matcher = CLONING_INTO.matcher(firstLine);
             } else {
                 matcher = ALREADY_EXISTS.matcher(firstLine);
@@ -125,7 +125,7 @@ public class GitWrapper extends ToolWrapper {
             File repoDir = new File(parentDir, name);
             String repoPath = repoDir.getAbsolutePath();
 
-            if (execRes.exitCode == EXIT_SUCCESS) {
+            if (execRes.succeeded()) {
                 LOG.fine(() -> String.format("Cloned '%s' into '%s'.", url, repoPath));
                 return new Repository(this, url, repoDir);
             } else {
@@ -222,8 +222,19 @@ public class GitWrapper extends ToolWrapper {
      * @return whether the git command failed
      */
     public boolean failed(ExecRes res) {
-        return res.exitCode != EXIT_SUCCESS || res.getStdOutTrimmed().startsWith(FATAL_PREFIX)
-                                            || res.getStdOutTrimmed().startsWith(ERROR_PREFIX);
+        return res.failed() || failedPrefix(res);
+    }
+
+    /**
+     * Returns whether the given git command failed by checking whether the output starts with either
+     * "{@value FATAL_PREFIX}" or "{@value ERROR_PREFIX}". This method ignores the exit code.
+     *
+     * @param res
+     *         the {@link ExecRes} to check
+     * @return whether the git command failed
+     */
+    public boolean failedPrefix(ExecRes res) {
+        return res.getStdOutTrimmed().startsWith(FATAL_PREFIX) || res.getStdOutTrimmed().startsWith(ERROR_PREFIX);
     }
 
     /**
-- 
GitLab