diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/RepoUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/RepoUtil.java index 334bcd71850290ce017715d3cc6b5ddee9f65719..7fa1fb6571bb6ed7abd4e9da2f54959835c006c0 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/RepoUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/RepoUtil.java @@ -13,9 +13,6 @@ 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.io.SVNRepository; -import org.tmatesoft.svn.core.io.SVNRepositoryFactory; -import org.tmatesoft.svn.core.wc.SVNUpdateClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; import org.tmatesoft.svn.core.wc2.SvnCheckout; import org.tmatesoft.svn.core.wc2.SvnOperationFactory; @@ -23,7 +20,6 @@ import org.tmatesoft.svn.core.wc2.SvnTarget; import java.io.File; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; @@ -35,85 +31,102 @@ public class RepoUtil { public RepoUtil() {} public void cloneRepository(Matcher config, String targetPath, String subDir) { - LOG.debug(String.format("cloning repository: %s", config.group(1))); + LOG.info(String.format("Cloning repository: %s", config.group(1))); File targetDirectory = new File(targetPath); if (targetDirectory.exists()) { - LOG.debug("clone target directory existing yet, deleting now"); + LOG.info("Target directory exists. Deleting..."); FileSystemUtils.deleteRecursively(targetDirectory); } File checkoutDirectory = targetDirectory; - //if an optional directory parameter was given - if(subDir!="") - { - //create companion checkout dir "targetPath"+"_checkout" - checkoutDirectory = new File(targetPath+"_checkout"); + // If an optional subdirectory parameter is provided + if (!subDir.isEmpty()) { + checkoutDirectory = new File(targetPath + "_checkout"); if (checkoutDirectory.exists()) { - LOG.debug("clone checkout directory existing yet, deleting now"); - FileSystemUtils.deleteRecursively(checkoutDirectory); + LOG.info("Checkout directory exists. Deleting..."); + FileSystemUtils.deleteRecursively(checkoutDirectory); } } - + try { - LOG.debug("preparing clone"); - - if (config.group(1).endsWith(".git")) { - CloneCommand cloneCommand = Git.cloneRepository() - .setDirectory(checkoutDirectory) - .setURI(config.group(1)); - - if (!config.group(2).equals("none") && !config.group(3).equals("none")) { - LOG.debug("setting credentials"); - cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(config.group(2), config.group(3))); - } - - LOG.debug("cloning..."); - cloneCommand.call().close(); - } - else { - URL sourceUrl=new URL(config.group(1)); + 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 + 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(); - checkout.setSingleTarget(SvnTarget.fromFile(new File(targetPath))); + + 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)); - - String protocol = url.getProtocol(); + checkout.setDepth(SVNDepth.INFINITY); + String protocol = url.getProtocol(); if (!config.group(2).equals("none") && !config.group(3).equals("none")) { - if (protocol.equals("https")) { - LOG.debug("Setting SVN credentials for HTTPS checkout with username/password"); - operationFactory.setAuthenticationManager(new BasicAuthenticationManager(config.group(2), config.group(3))); - } - else if (protocol.equals("svn+ssh")) { - LOG.debug("Setting credentials for SVN+SSH Authentication with username/password"); - //ISVNAuthenticationManager authManager = new BasicAuthenticationManager(new SVNAuthentication[] { new SVNSSHAuthentication(username, password, -1, false)});//SVNWCUtil.createDefaultAuthenticationManager(username, password); - ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(config.group(2), config.group(3).toCharArray()); - operationFactory.setAuthenticationManager(authManager); - } + 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); + } } - checkout.run(); - LOG.debug("SVN checkout completed successfully."); - } - - //if an optional directory parameter was given - if(subDir!="") - { - //copy appropriate path from checkout directory to target directory - FileSystemUtils.copyRecursively(new File(checkoutDirectory+subDir), new File(targetPath)); + 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; + } } - LOG.debug(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 unit test dir", config.group(1)), e); - } - catch (GitAPIException e) { - LOG.error(String.format("Error while cloning from %s: could not read from Git", config.group(1)), e); - } - catch (SVNException e) { - LOG.error(String.format("Error while cloning from %s: could not read from Svn", config.group(1)), 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); } } }