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 d13cbaf7b85f98ad8157da53d4dee9665907a8f1..52036d24dd4f792fdbbbee50e41430c018cae48b 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ProcessExecutor.java @@ -2,9 +2,11 @@ package de.uni_passau.fim.seibt.gitwrapper.process; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Stream; import org.apache.commons.io.IOUtils; @@ -42,12 +44,7 @@ public class ProcessExecutor { 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); + return exec(workingDir, redirectError, merge(false, parameters, cmd)); } /** @@ -92,8 +89,8 @@ public class ProcessExecutor { ExecRes res = new ExecRes(); Process p = builder.start(); - res.output = IOUtils.toString(p.getInputStream()); res.exitCode = p.waitFor(); + res.output = IOUtils.toString(p.getInputStream()); 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.trim())); @@ -104,4 +101,33 @@ public class ProcessExecutor { return Optional.empty(); } } + + /** + * Returns an array representing the concatenation of <code>arr</code> and <code>varArgs</code> in the order defined + * by <code>av</code>. If one of <code>arr</code> and <code>varArgs</code> is null, the other one is returned. + * + * @param av + * if true returns [arr] + [varArgs] otherwise returns [varArgs] + [arr] + * @param arr + * the array + * @param varArgs + * the variable number of arguments + * @return the concatenation + */ + public static String[] merge(boolean av, String[] arr, String... varArgs) { + + if (arr != null && varArgs == null) { + return arr; + } else if (arr == null && varArgs != null) { + return varArgs; + } else if (arr == null && varArgs == null){ + throw new IllegalArgumentException("Both arr and varArgs are null!"); + } + + if (av) { + return Stream.concat(Arrays.stream(arr), Arrays.stream(varArgs)).toArray(String[]::new); + } else { + return Stream.concat(Arrays.stream(varArgs), Arrays.stream(arr)).toArray(String[]::new); + } + } }