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

doc

parent 7ae542a9
No related branches found
No related tags found
No related merge requests found
...@@ -12,12 +12,15 @@ import java.util.regex.Pattern; ...@@ -12,12 +12,15 @@ import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils; 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_SUCCESS = 0;
public static final int EXIT_FAIL = 1; 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 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 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 { ...@@ -29,10 +32,26 @@ public class Git {
private String 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; 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) { public Optional<Repository> clone(File parentDir, String url) {
LOG.fine(() -> String.format("Cloning '%s'.", url)); LOG.fine(() -> String.format("Cloning '%s'.", url));
...@@ -100,6 +119,16 @@ public class Git { ...@@ -100,6 +119,16 @@ public class Git {
return status.map(res -> res.code == EXIT_SUCCESS).orElse(false); return status.map(res -> res.code == EXIT_SUCCESS).orElse(false);
} }
/**
* Executes 'git &lt;parameters&gt' 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) { public Optional<ExecRes> exec(File workingDirectory, String... parameters) {
ProcessBuilder b = new ProcessBuilder(); ProcessBuilder b = new ProcessBuilder();
String[] cmdArray = new String[parameters.length + 1]; String[] cmdArray = new String[parameters.length + 1];
......
...@@ -4,13 +4,13 @@ import java.io.File; ...@@ -4,13 +4,13 @@ import java.io.File;
public class Repository { public class Repository {
private Git git; private GitWrapper gitWrapper;
private String url; private String url;
private File dir; private File dir;
Repository(Git git, String url, File dir) { Repository(GitWrapper gitWrapper, String url, File dir) {
this.git = git; this.gitWrapper = gitWrapper;
this.url = url; this.url = url;
this.dir = dir; this.dir = dir;
} }
......
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