From 7c73b56da547a84a6ce9ae1724c95874f4c8e776 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Fri, 15 Apr 2016 00:20:35 +0200
Subject: [PATCH] checkout and doc formatting

---
 .../fim/seibt/gitwrapper/repo/Repository.java | 35 +++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

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 4d103af..258d747 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) {
-- 
GitLab