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 f447875e08859bbdf905f144cfa8a6c1f742b2d8..b51a61b33960d3e0390242bc851f8fdf0f561f86 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java @@ -56,9 +56,11 @@ public class GitWrapper extends ToolWrapper { * the directory in which to place the clone of <code>url</code> * @param url * the git repository to clone + * @param fetch + * whether to perform a fetch of the {@link Repository} if a clone of it was already found on disk * @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, boolean fetch) { LOG.fine(() -> String.format("Cloning '%s'.", url)); Optional<ExecRes> res = exec(parentDir, "clone", url); @@ -90,6 +92,7 @@ public class GitWrapper extends ToolWrapper { if (execRes.exitCode == EXIT_SUCCESS) { LOG.fine(() -> String.format("Cloned '%s' into '%s'.", url, repoPath)); + return new Repository(this, url, repoDir); } else { LOG.fine(() -> String.format("Found '%s' which would be produced for '%s'", repoPath, url)); @@ -99,9 +102,14 @@ public class GitWrapper extends ToolWrapper { } LOG.fine(() -> String.format("'%s' is a git repository. Assuming it is a clone of '%s'.", repoPath, url)); - } + Repository repo = new Repository(this, url, repoDir); + + if (fetch && !repo.fetch()) { + LOG.warning("Could not fetch " + repo + ". It may be out of date."); + } - return new Repository(this, url, repoDir); + return repo; + } }; return res.map(toRepo); 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 c9d2fad45a7c44782f7ad9ca8ffcc4700b0d3de4..5bf26202fa98605aba2f44175afddccd8886843f 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java @@ -63,7 +63,7 @@ public class Repository { * @param c * the {@link Commit} to checkout * @return whether the checkout was successful - * @see <a href=https://git-scm.com/docs/git-checkout>git-checkout</a> + * @see <a href=https://git-scm.com/docs/git-checkout>git checkout</a> */ public boolean checkout(Commit c) { @@ -80,12 +80,33 @@ public class Repository { LOG.warning(() -> String.format("Checkout of %s failed.", c)); } - return failed; + return !failed; }; return checkout.map(toBoolean).orElse(false); } + /** + * Performs a fetch in this {@link Repository}. + * + * @return whether the fetch was successful + * @see <a href=https://git-scm.com/docs/git-fetch>git fetch</a> + */ + public boolean fetch() { + Optional<ExecRes> fetch = git.exec(dir, "fetch", "--all", "--tags"); //TODO add "--prune"? + Function<ExecRes, Boolean> toBoolean = res -> { + boolean failed = git.failed(res); + + if (failed) { + LOG.warning(() -> String.format("Fetch of %s failed.", this)); + } + + return !failed; + }; + + return fetch.map(toBoolean).orElse(false); + } + /** * Returns the merge commits in this repository. *