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 258d7470a8888b959aaf67ad2953e908c2b25916..c9d2fad45a7c44782f7ad9ca8ffcc4700b0d3de4 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}. *