JGitUtil.java 2.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
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 java.io.File;
13
import java.io.IOException;
14
15
16
17
18
19
20
21
22
import java.util.regex.Matcher;

@Component
public class JGitUtil {

    private static final Logger LOG = LogManager.getLogger(JGitUtil.class);

    public JGitUtil() {}

23
    public void cloneRepository(Matcher config, String targetPath, String subDir) {
24
25
26
27
28
29
30
31
        LOG.debug(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");
            FileSystemUtils.deleteRecursively(targetDirectory);
        }

32
33
34
35
36
37
        File checkoutDirectory = targetDirectory;
        //if an optional directory parameter was given
        if(subDir!="")
        {
        	//create companion checkout dir "targetPath"+"_checkout"
            checkoutDirectory = new File(targetPath+"_checkout");
38
            if (checkoutDirectory.exists()) {
39
40
41
            	LOG.debug("clone checkout directory existing yet, deleting now");
            	FileSystemUtils.deleteRecursively(checkoutDirectory);
            }
42
        }
43
        
44
45
46
47
48
49
50
51
        try {
            LOG.debug("preparing clone");
            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");
52
                cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(config.group(2), config.group(3)));
53
54
55
            }

            LOG.debug("cloning...");
56
            cloneCommand.call().close();
57
            
58
59
60
61
            //if an optional directory parameter was given
            if(subDir!="")
            {
            	//copy appropriate path from checkout directory to target directory
Lückemeyer's avatar
Lückemeyer committed
62
            	FileSystemUtils.copyRecursively(new File(checkoutDirectory+subDir), new File(targetPath));
63
            }
Lückemeyer's avatar
Lückemeyer committed
64
            LOG.debug(String.format("cloned from %s to %s", config.group(1), targetDirectory));            
65
66
67
        }
        catch (IOException e) {
        	LOG.error(String.format("Error while cloning from %s: could not copy to unit test dir", config.group(1)), e);
68
69
        }
        catch (GitAPIException e) {
70
            LOG.error(String.format("Error while cloning from %s: could not read from Git", config.group(1)), e);
71
72
73
        }
    }
}