From aaf1d04d214c114a7c1d70bada0f00254fe7eac0 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Wed, 13 Apr 2016 15:00:53 +0200
Subject: [PATCH] build generic ProcessExecutor class

---
 .../gitwrapper/process/ProcessExecutor.java   | 60 +++++++++++++++++++
 .../fim/seibt/gitwrapper/repo/GitWrapper.java | 34 +----------
 2 files changed, 62 insertions(+), 32 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 eff1f7f..90c6330 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java
@@ -1,7 +1,17 @@
 package de.uni_passau.fim.seibt.gitwrapper.process;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Optional;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.io.IOUtils;
+
 public class ProcessExecutor {
 
+    private static final Logger LOG = Logger.getLogger(ProcessExecutor.class.getCanonicalName());
+
     /**
      * The result of a command line execution.
      */
@@ -9,4 +19,54 @@ public class ProcessExecutor {
         public int exitCode;
         public String output;
     }
+
+    public static Optional<ExecRes> exec(String cmd, File workingDir, boolean redirectError, String... parameters) {
+
+        if (parameters == null) {
+            return exec(workingDir, redirectError, cmd);
+        }
+
+        String[] cmdArray = new String[parameters.length + 1];
+
+        cmdArray[0] = cmd;
+        System.arraycopy(parameters, 0, cmdArray, 1, parameters.length);
+
+        return exec(workingDir, redirectError, cmdArray);
+    }
+
+    public static Optional<ExecRes> exec(File workingDir, boolean redirectError, String... command) {
+
+        if (command == null) {
+            return Optional.empty();
+        }
+
+        ProcessBuilder builder = new ProcessBuilder();
+
+        builder.command(command);
+        builder.directory(workingDir);
+        builder.redirectErrorStream(redirectError);
+
+        return exec(builder);
+    }
+
+    public static Optional<ExecRes> exec(ProcessBuilder builder) {
+        String cmd = String.join(" ", builder.command());
+        LOG.fine(() -> String.format("Executing '%s'.", cmd));
+
+        try {
+            ExecRes res = new ExecRes();
+            Process p = builder.start();
+
+            res.output = IOUtils.toString(p.getInputStream());
+            res.exitCode = p.waitFor();
+
+            LOG.fine(() -> String.format("Execution of '%s' returned exit code %d.", cmd, res.exitCode));
+            LOG.finest(() -> String.format("Execution of '%s' output:%n%s", cmd, res.output));
+
+            return Optional.of(res);
+        } catch (IOException | InterruptedException e) {
+            LOG.log(Level.SEVERE, e, () -> String.format("Exception executing '%s'.", cmd));
+            return Optional.empty();
+        }
+    }
 }
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 bf57484..1a577ca 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java
@@ -1,17 +1,15 @@
 package de.uni_passau.fim.seibt.gitwrapper.repo;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.Optional;
 import java.util.Scanner;
 import java.util.function.Function;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor;
 import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor.ExecRes;
-import org.apache.commons.io.IOUtils;
 
 /**
  * A wrapper for executing git commands on the command line.
@@ -126,34 +124,6 @@ public class GitWrapper {
      * @return the result of the execution
      */
     public Optional<ExecRes> exec(File workingDirectory, String... parameters) {
-        ProcessBuilder b = new ProcessBuilder();
-        String[] cmdArray = new String[parameters.length + 1];
-
-        cmdArray[0] = git;
-        System.arraycopy(parameters, 0, cmdArray, 1, parameters.length);
-
-        b.command(cmdArray);
-        b.directory(workingDirectory);
-        b.redirectErrorStream(true);
-
-        String cmd = String.join(" ", b.command());
-
-        LOG.fine(() -> String.format("Executing '%s'.", cmd));
-
-        try {
-            ExecRes res = new ExecRes();
-            Process p = b.start();
-
-            res.output = IOUtils.toString(p.getInputStream());
-            res.exitCode = p.waitFor();
-
-            LOG.fine(() -> String.format("Execution of '%s' returned exit code %d.", cmd, res.exitCode));
-            LOG.finest(() -> String.format("Execution of '%s' output:%n%s", cmd, res.output));
-
-            return Optional.of(res);
-        } catch (IOException | InterruptedException e) {
-            LOG.log(Level.SEVERE, e, () -> String.format("Exception executing '%s'.", cmd));
-            return Optional.empty();
-        }
+        return ProcessExecutor.exec(git, workingDirectory, true, parameters);
     }
 }
-- 
GitLab