diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java similarity index 75% rename from src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java rename to src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java index aa70b2ab1b17b733ca3abf45a55b5868b1ed8562..08ae56e91db07d77e0de6de9f41f1a0ef5a8de36 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Git.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java @@ -12,12 +12,15 @@ import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; -public class Git { +/** + * A wrapper for executing git commands on the command line. + */ +public class GitWrapper { public static final int EXIT_SUCCESS = 0; public static final int EXIT_FAIL = 1; - private static final Logger LOG = Logger.getLogger(Git.class.getCanonicalName()); + private static final Logger LOG = Logger.getLogger(GitWrapper.class.getCanonicalName()); 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\\."); @@ -29,10 +32,26 @@ public class Git { private String git; - public Git(String git) { + /** + * 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; } + /** + * Attempts to clone the git repository at the given <code>url</code> into a directory under <code>parentDir</code>. + * If <code>parentDir</code> already contains a git repository with the name that would be produced when cloning + * <code>url</code> it is assumed to be a clone of <code>url</code> and returned as a {@link Repository}. + * + * @param parentDir + * the directory in which to place the clone of <code>url</code> + * @param url + * the git repository to clone + * @return the resulting {@link Repository} or an empty {@link Optional} if there is an error + */ public Optional<Repository> clone(File parentDir, String url) { LOG.fine(() -> String.format("Cloning '%s'.", url)); @@ -100,6 +119,16 @@ public class Git { return status.map(res -> res.code == EXIT_SUCCESS).orElse(false); } + /** + * Executes 'git <parameters>' and returns the exit code and the output of the command. If there is an + * exception executing the command an empty {@link Optional} will be returned. + * + * @param workingDirectory + * the working directory for the command execution + * @param parameters + * the git parameters + * @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]; diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java index 4218b22346a6347f918fb3e22abafc1ae10e2fbf..111895c46b67fd14318885a4ae0bb170f1e0f0a0 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java @@ -4,13 +4,13 @@ import java.io.File; public class Repository { - private Git git; + private GitWrapper gitWrapper; private String url; private File dir; - Repository(Git git, String url, File dir) { - this.git = git; + Repository(GitWrapper gitWrapper, String url, File dir) { + this.gitWrapper = gitWrapper; this.url = url; this.dir = dir; }