diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolNotWorkingException.java b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolNotWorkingException.java new file mode 100644 index 0000000000000000000000000000000000000000..1150130f94a01d019467c8070c68300480536639 --- /dev/null +++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolNotWorkingException.java @@ -0,0 +1,25 @@ +package de.uni_passau.fim.seibt.gitwrapper.process; + +/** + * An {@link Exception} indicating that a wrapped tool is not working. + */ +public class ToolNotWorkingException extends Exception { + + public ToolNotWorkingException() {} + + public ToolNotWorkingException(String message) { + super(message); + } + + public ToolNotWorkingException(String message, Throwable cause) { + super(message, cause); + } + + public ToolNotWorkingException(Throwable cause) { + super(cause); + } + + public ToolNotWorkingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java new file mode 100644 index 0000000000000000000000000000000000000000..6370a816b377a58be903e5d694f447456a6d8a3a --- /dev/null +++ b/src/de/uni_passau/fim/seibt/gitwrapper/process/ToolWrapper.java @@ -0,0 +1,16 @@ +package de.uni_passau.fim.seibt.gitwrapper.process; + +public abstract class ToolWrapper { + + protected String cmd; + + public ToolWrapper(String cmd) throws ToolNotWorkingException { + this.cmd = cmd; + + if (!isWorking()) { + throw new ToolNotWorkingException(cmd + " is not working."); + } + } + + protected abstract boolean isWorking(); +} 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 ea8f7adc470776b661fe1865f94669302a0137ac..5d4d758fd715c6a70194530ff6968481c3988500 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java @@ -10,11 +10,13 @@ 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 de.uni_passau.fim.seibt.gitwrapper.process.ToolNotWorkingException; +import de.uni_passau.fim.seibt.gitwrapper.process.ToolWrapper; /** * A wrapper for executing git commands. */ -public class GitWrapper { +public class GitWrapper extends ToolWrapper { public static final int EXIT_SUCCESS = 0; public static final int EXIT_FAIL = 1; @@ -24,15 +26,22 @@ public class GitWrapper { private static final Pattern CLONING_INTO = Pattern.compile("Cloning into '(.*)'\\.\\.\\."); private static final Pattern ALREADY_EXISTS = Pattern.compile("fatal: destination path '(.*)' already exists and is not an empty directory\\."); - private String git; + private static final String GIT_VERSION_C = "--version"; + private static final String GIT_VERSION_PREFIX = "git version"; /** * Constructs a new {@link GitWrapper} * * @param git the git command, e.g. '/usr/bin/git' or just 'git' */ - public GitWrapper(String git) { - this.git = git; + public GitWrapper(String git) throws ToolNotWorkingException { + super(git); + } + + @Override + protected boolean isWorking() { + Function<ExecRes, Boolean> checkPrefix = execRes -> execRes.output.startsWith(GIT_VERSION_PREFIX); + return ProcessExecutor.exec(cmd, new File("."), true, GIT_VERSION_C).map(checkPrefix).orElse(false); } /** @@ -124,6 +133,6 @@ public class GitWrapper { * @return the result of the execution */ public Optional<ExecRes> exec(File workingDirectory, String... parameters) { - return ProcessExecutor.exec(git, workingDirectory, true, parameters); + return ProcessExecutor.exec(cmd, workingDirectory, true, parameters); } }