From 3dc0f206ec31004fc772d154dc04698dfe5ad261 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Fri, 15 Apr 2016 18:49:15 +0200
Subject: [PATCH] optional fetch

---
 .../fim/seibt/gitwrapper/repo/GitWrapper.java | 14 ++++++++---
 .../fim/seibt/gitwrapper/repo/Repository.java | 25 +++++++++++++++++--
 2 files changed, 34 insertions(+), 5 deletions(-)

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 f447875..b51a61b 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 c9d2fad..5bf2620 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.
      *
-- 
GitLab