1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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
| import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.RuntimeUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit;
@Slf4j public class GitHandler {
private final String checkoutDirectory;
public GitHandler(String checkoutDirectory) { this.checkoutDirectory = checkoutDirectory; }
public void cloneRepository(String gitUrl, String name) throws Exception { String gitUrl2 = "http://root:XpaKdb4Fjy2609@" + gitUrl.replace("http://", ""); log.info("cloneRepository gitUrl: {}", gitUrl2); executeGitClone(gitUrl2, name);
}
public void pushToNewRepository(String projectName, String newGitUrl) throws IOException, InterruptedException { String gitUrl = "http://root:glpat-ssMtpGJRMTn1NLFvpwAC@" + newGitUrl.replace("http://", ""); log.info("pushToNewRepository gitUrl: {}", gitUrl); executeGitPush(gitUrl, projectName); }
public void executeGitClone(String gitUrl, String name) throws Exception { String fileName = String.format("%s%s", checkoutDirectory, name); File file = new File(fileName); if (file.exists()) { log.info("{} already exists", fileName); return; } String command = String.format("git clone --bare %s %s", gitUrl, fileName); log.info("executeGitClone command: {}", command);
Process exec = RuntimeUtil.exec(command); int pid = RuntimeUtil.getPid(); log.info("executeGitClone pid: {}", pid); exec.waitFor(120, TimeUnit.SECONDS); log.info("executeGitClone exitValue: {}", exec.exitValue()); String result = RuntimeUtil.getResult(exec, CharsetUtil.systemCharset()); log.info("executeGitClone result: {}", result); ThreadUtil.sleep(3000); }
public void executeGitPush(String newGitUrl, String projectName) throws InterruptedException { String ss = String.format("cd /d %s%s && git push --mirror %s", checkoutDirectory, projectName, newGitUrl); String[] command = { "cmd.exe", "/c", ss }; log.info("executeGitPush command: {}", StringUtils.join(command, ",")); Process exec = RuntimeUtil.exec(command); exec.waitFor(120, TimeUnit.SECONDS); log.info("executeGitPush exitValue: {}", exec.exitValue()); String result = RuntimeUtil.getResult(exec, CharsetUtil.systemCharset()); log.info("executeGitPush result: {}", result); ThreadUtil.sleep(3000); } }
|