From 698c3f92e1f4e43020bdad9484676a5501cc2ce5 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Fri, 15 Apr 2016 10:48:48 +0200
Subject: [PATCH] move copy method to Repository and return new Repository
 objects representing the copies

---
 .../fim/seibt/gitwrapper/repo/Repository.java | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

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 258d747..c9d2fad 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Repository.java
@@ -15,6 +15,7 @@ import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
 import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor.ExecRes;
+import org.apache.commons.io.FileUtils;
 
 /**
  * A git {@link Repository}.
@@ -195,6 +196,32 @@ public class Repository {
         return revParse.map(toHash);
     }
 
+    /**
+     * Copies this {@link Repository} to the given <code>destination</code> directory.
+     *
+     * @param destination
+     *         the destination directory
+     * @return a copy of this {@link Repository} at the new location
+     * @see FileUtils#copyDirectory(File, File)
+     */
+    public Optional<Repository> copy(File destination) {
+
+        try {
+            if (destination.exists() && destination.isDirectory()) {
+                LOG.warning(() -> String.format("%s already exists. Merging source and destination directories.", destination));
+            }
+
+            FileUtils.copyDirectory(dir, destination);
+
+            LOG.fine(() -> String.format("Copied %s to %s.", this, destination));
+        } catch (IOException e) {
+            LOG.log(Level.WARNING, e, () -> String.format("Failed to copy %s to %s.", dir, destination));
+            return Optional.empty();
+        }
+
+        return Optional.of(new Repository(git, url, destination));
+    }
+
     /**
      * Returns the {@link GitWrapper} used by this {@link Repository}.
      *
-- 
GitLab