Skip to content
Snippets Groups Projects
Commit 4e59366a authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

implement ToolWrapper, check whether the command is working in GitWrapper

parent b7335620
No related branches found
No related tags found
No related merge requests found
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);
}
}
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();
}
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment