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 4d103af3c80246d750088f84cd14556b39ff473c..258d7470a8888b959aaf67ad2953e908c2b25916 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java @@ -56,6 +56,35 @@ public class Repository { this.commits = new HashMap<>(); } + /** + * Performs a checkout of the given {@link Commit}. + * + * @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> + */ + public boolean checkout(Commit c) { + + if (!commits.containsValue(c)) { + LOG.warning(() -> String.format("Checkout failed. %s is not part of %s.", c, this)); + return false; + } + + Optional<ExecRes> checkout = git.exec(dir, "checkout", c.getId()); + Function<ExecRes, Boolean> toBoolean = res -> { + boolean failed = git.failed(res); + + if (failed) { + LOG.warning(() -> String.format("Checkout of %s failed.", c)); + } + + return failed; + }; + + return checkout.map(toBoolean).orElse(false); + } + /** * Returns the merge commits in this repository. * @@ -85,7 +114,8 @@ public class Repository { * Returns a {@link Commit} for the given ID. The caller must ensure that the ID is a full SHA1 hash of a * commit that exists in this repository. * - * @param id the ID for the {@link Commit} + * @param id + * the ID for the {@link Commit} * @return the {@link Commit} */ Commit getCommitUnchecked(String id) { @@ -96,7 +126,8 @@ public class Repository { * Returns a {@link Commit} for the given ID. If the given ID does not designate a commit that exists in this * {@link Repository} an empty {@link Optional} will be returned. The ID will be resolved to a full SHA1 hash. * - * @param id the ID of the commit + * @param id + * the ID of the commit * @return the {@link Commit} or an empty {@link Optional} if the ID is invalid or an exception occurs */ public Optional<Commit> getCommit(String id) {