An error occurred while loading the file. Please try again.
-
mamunozgil authored08131b4e
package de.hftstuttgart.dtabackend.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.wc2.SvnCheckout;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnTarget;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
@Component
public class RepoUtil {
private static final Logger LOG = LogManager.getLogger(RepoUtil.class);
public RepoUtil() {}
public void cloneRepository(Matcher config, String targetPath, String subDir) {
LOG.info(String.format("Cloning repository: %s", config.group(1)));
File targetDirectory = new File(targetPath);
if (targetDirectory.exists()) {
LOG.info("Target directory exists. Deleting...");
FileSystemUtils.deleteRecursively(targetDirectory);
}
File checkoutDirectory = targetDirectory;
// If an optional subdirectory parameter is provided
if (!subDir.isEmpty()) {
checkoutDirectory = new File(targetPath + "_checkout");
if (checkoutDirectory.exists()) {
LOG.info("Checkout directory exists. Deleting...");
FileSystemUtils.deleteRecursively(checkoutDirectory);
}
}
try {
LOG.info("Preparing to clone...");
if (config.group(1).endsWith(".git")) {
// Git cloning
CloneCommand cloneCommand = Git.cloneRepository()
.setDirectory(checkoutDirectory)
.setURI(config.group(1));
if (!config.group(2).equals("none") && !config.group(3).equals("none")) {
LOG.info("Setting Git credentials...");
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(config.group(2), config.group(3)));
}
LOG.info("Cloning Git repository...");
cloneCommand.call().close();
} else {
// SVN Checkout
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
URL sourceUrl = new URL(config.group(1));
SVNURL url = SVNURL.create(sourceUrl.getProtocol(), null, sourceUrl.getHost(), sourceUrl.getPort(), sourceUrl.getPath(), false);
SvnOperationFactory operationFactory = new SvnOperationFactory();
SvnCheckout checkout = operationFactory.createCheckout();
String directoryString = checkoutDirectory.toString();
File directory = new File(directoryString);
if (!directory.exists()) {
LOG.info("Target directory does not exist. Creating: " + directoryString);
if (!directory.mkdirs()) {
throw new IOException("Failed to create target directory: " + directoryString);
}
}
checkout.setSingleTarget(SvnTarget.fromFile(directory));
checkout.setSource(SvnTarget.fromURL(url));
checkout.setDepth(SVNDepth.INFINITY);
String protocol = url.getProtocol();
if (!config.group(2).equals("none") && !config.group(3).equals("none")) {
if (protocol.equalsIgnoreCase("https")) {
LOG.info("Setting SVN credentials for HTTPS...");
operationFactory.setAuthenticationManager(BasicAuthenticationManager.newInstance(config.group(2), config.group(3).toCharArray()));
} else if (protocol.equalsIgnoreCase("svn+ssh")) {
LOG.info("Setting SVN credentials for SVN+SSH...");
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(config.group(2), config.group(3).toCharArray());
operationFactory.setAuthenticationManager(authManager);
}
}
try {
LOG.info("Performing SVN checkout...");
checkout.run();
LOG.info("SVN checkout completed successfully.");
} catch (Exception e) {
LOG.error("Error during SVN checkout: " + e.getMessage(), e);
throw e;
}
}
// If an optional subdirectory parameter is provided
if (!subDir.isEmpty()) {
File subDirPath = new File(checkoutDirectory, subDir);
if (!subDirPath.exists() || !subDirPath.isDirectory()) {
LOG.error("Specified subdirectory does not exist in the repository: " + subDirPath.getPath());
throw new IllegalArgumentException("Invalid subdirectory: " + subDirPath.getPath());
}
LOG.info("Copying subdirectory to target...");
FileSystemUtils.copyRecursively(subDirPath, new File(targetPath));
}
LOG.info(String.format("Cloned from %s via %s to %s", config.group(1), checkoutDirectory + subDir, targetDirectory));
} catch (IOException e) {
LOG.error(String.format("Error while cloning from %s: could not copy to target directory", config.group(1)), e);
} catch (GitAPIException e) {
LOG.error(String.format("Error while cloning Git repository %s: %s", config.group(1), e.getMessage()), e);
} catch (SVNException e) {
LOG.error(String.format("Error while cloning SVN repository %s: %s", config.group(1), e.getMessage()), e);
}
}
}