Commit 8aabe7e6 authored by Lückemeyer's avatar Lückemeyer
Browse files

added solution idea comments for issue #8

parent b80f7087
Pipeline #9075 passed with stage
package de.hftstuttgart.dtabackend.utils; package de.hftstuttgart.dtabackend.utils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import java.io.File; import java.io.File;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@Component @Component
public class JGitUtil { public class JGitUtil {
private static final Logger LOG = LogManager.getLogger(JGitUtil.class); private static final Logger LOG = LogManager.getLogger(JGitUtil.class);
public JGitUtil() {} public JGitUtil() {}
public void cloneRepository(Matcher config, String targetPath) { public void cloneRepository(Matcher config, String targetPath) {
LOG.debug(String.format("cloning repository: %s", config.group(1))); LOG.debug(String.format("cloning repository: %s", config.group(1)));
File targetDirectory = new File(targetPath); File targetDirectory = new File(targetPath);
if (targetDirectory.exists()) { if (targetDirectory.exists()) {
LOG.debug("clone target directory existing yet, deleting now"); LOG.debug("clone target directory existing yet, deleting now");
FileSystemUtils.deleteRecursively(targetDirectory); FileSystemUtils.deleteRecursively(targetDirectory);
} }
try { //create companion checkout dir "targetPath"+"_checkout"
LOG.debug("preparing clone"); File checkoutDirectory = new File(targetPath+"_checkout");
CloneCommand cloneCommand = Git.cloneRepository() if (targetDirectory.exists()) {
.setDirectory(targetDirectory) LOG.debug("clone checkout directory existing yet, deleting now");
.setURI(config.group(1)); FileSystemUtils.deleteRecursively(checkoutDirectory);
}
if (!config.group(2).equals("none") && !config.group(3).equals("none")) {
LOG.debug("setting credentials"); try {
cloneCommand.setCredentialsProvider( //check group(1) for possible directory
new UsernamePasswordCredentialsProvider(config.group(2), config.group(3))); //if(!config.group(1).endsWith(".git"))
} //cut off the directory part
//pos=instr(".git/")
LOG.debug("cloning..."); //cloneURI=config.group(1).substr(1, pos+3)
cloneCommand.call() //else
.close(); //cloneURI=config.group(1)
} LOG.debug("preparing clone");
catch (GitAPIException e) { CloneCommand cloneCommand = Git.cloneRepository()
LOG.error(String.format("Error while cloning from %s", config.group(1)), e); .setDirectory(checkoutDirectory)
} //.setURI(cloneURI)
.setURI(config.group(1));
LOG.debug(String.format("cloned from %s to %s", config.group(1), targetDirectory));
} 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();
//copy appropriate path from checkout directory to target directory
//if(!config.group(1).endsWith(".git"))
//copy checkout+config.group(1).substr(pos+4) to target directory
//else
//copy checkout directory to target directory directly
}
catch (GitAPIException e) {
LOG.error(String.format("Error while cloning from %s", config.group(1)), e);
}
LOG.debug(String.format("cloned from %s to %s", config.group(1), targetDirectory));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment