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

optional fetch

parent 698c3f92
No related branches found
No related tags found
No related merge requests found
...@@ -56,9 +56,11 @@ public class GitWrapper extends ToolWrapper { ...@@ -56,9 +56,11 @@ public class GitWrapper extends ToolWrapper {
* the directory in which to place the clone of <code>url</code> * the directory in which to place the clone of <code>url</code>
* @param url * @param url
* the git repository to clone * 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 * @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)); LOG.fine(() -> String.format("Cloning '%s'.", url));
Optional<ExecRes> res = exec(parentDir, "clone", url); Optional<ExecRes> res = exec(parentDir, "clone", url);
...@@ -90,6 +92,7 @@ public class GitWrapper extends ToolWrapper { ...@@ -90,6 +92,7 @@ public class GitWrapper extends ToolWrapper {
if (execRes.exitCode == EXIT_SUCCESS) { if (execRes.exitCode == EXIT_SUCCESS) {
LOG.fine(() -> String.format("Cloned '%s' into '%s'.", url, repoPath)); LOG.fine(() -> String.format("Cloned '%s' into '%s'.", url, repoPath));
return new Repository(this, url, repoDir);
} else { } else {
LOG.fine(() -> String.format("Found '%s' which would be produced for '%s'", repoPath, url)); LOG.fine(() -> String.format("Found '%s' which would be produced for '%s'", repoPath, url));
...@@ -99,9 +102,14 @@ public class GitWrapper extends ToolWrapper { ...@@ -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)); 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); return res.map(toRepo);
......
...@@ -63,7 +63,7 @@ public class Repository { ...@@ -63,7 +63,7 @@ public class Repository {
* @param c * @param c
* the {@link Commit} to checkout * the {@link Commit} to checkout
* @return whether the checkout was successful * @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) { public boolean checkout(Commit c) {
...@@ -80,12 +80,33 @@ public class Repository { ...@@ -80,12 +80,33 @@ public class Repository {
LOG.warning(() -> String.format("Checkout of %s failed.", c)); LOG.warning(() -> String.format("Checkout of %s failed.", c));
} }
return failed; return !failed;
}; };
return checkout.map(toBoolean).orElse(false); 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. * Returns the merge commits in this repository.
* *
......
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