git clone --depth=1

This commit is contained in:
jiangzhongxiang 2019-08-01 15:36:37 +08:00
parent dc9a792ed8
commit 2ca09eacd6
2 changed files with 24 additions and 22 deletions

View File

@ -79,7 +79,7 @@ public class GameService {
* @param tpiRepoName
* Tpi版本库名称需要这个参数的原因是TPM的仓库名字和TPI的名字并不总是相同的
*/
public void gitClone(String tpiWorkSpace, String tpmGitURL, String remoteName, String tpiRepoName)
public void gitClone(String tpiWorkSpace, String tpmGitURL, String remoteName, String tpiRepoName, String depth)
throws GameException {
logger.debug("tpiWorkSpace: {}, tpmGitURL: {}, remoteName: {}, tpiRepoName: {}", tpiWorkSpace, tpmGitURL,
remoteName, tpiRepoName);
@ -105,7 +105,7 @@ public class GameService {
}
// 执行克隆操作
String cmd = "cd " + tpiWorkSpace + " && git clone -o " + remoteName + " "+ tpmGitURL
String cmd = "cd " + tpiWorkSpace + " && git clone -o " + remoteName + " " + depth + " " + tpmGitURL
+ " && cd " + tpmRepoName + " && git config user.email educoder@163.com && git config user.name educoder";
JSONObject result = ShellUtil.executeAndGetExitStatus(cmd, 3);
@ -136,26 +136,27 @@ public class GameService {
String tpiID = path.substring(path.lastIndexOf("_") + 1);
// pull 操作之前如果tpi本地版本库不存在则克隆主机名为origin
int isRepoExist = ShellUtil.executeAndGetExitStatus("ls " + tpiRepoPath).getInteger("exitStatus");
String cmd = "ls " + tpiRepoPath;
int isRepoExist = ShellUtil.executeAndGetExitStatus(cmd).getInteger("exitStatus");
if (isRepoExist != 0) {
logger.warn("版本库不存在,先克隆! tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
gitClone(path, tpiGitURL, "origin", tpiRepoName);
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
}
// pull之前先判断版本库的origin主机是否存在若不存在则先创建
JSONObject remoteExist = ShellUtil
.executeAndGetExitStatus("cd " + tpiRepoPath + " && git remote | grep ^origin$");
cmd = "cd " + tpiRepoPath + " && git remote | grep ^origin$";
JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd);
if (remoteExist.getInteger("exitStatus") != 0) {
if (remoteExist.getString("out").contains("fatal")) {
logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL {}", tpiID, tpiGitURL);
logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL: {}, 检查cmd: {}", tpiID, tpiGitURL, cmd);
ShellUtil.execute("cd " + tpiRepoPath + " && git init");
}
logger.warn("origin主机名不存在先添加主机名及其git地址remoteName: {}, tpiGitURL: {}, tpiID: {}", "origin", tpiGitURL, tpiID);
JSONObject addRemote = ShellUtil
.executeAndGetExitStatus("cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL, 3);
cmd = "cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL;
JSONObject addRemote = ShellUtil.executeAndGetExitStatus(cmd, 3);
if (addRemote.getInteger("exitStatus") != 0) {
logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}, cmd: {}", tpiGitURL, tpiID, cmd);
throw new GameException("origin主机名不存在无法完成pull操作,tpiID:" + tpiID);
} else {
logger.debug("origin主机名不存在,添加主机名成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
@ -181,7 +182,7 @@ public class GameService {
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpiGitURL, "origin", tpiRepoName);
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
logger.debug("gitPull冲突重新clone结束, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
@ -234,17 +235,18 @@ public class GameService {
public void gitPullFromTpm(String path, String tpmGitURL, String tpiRepoName, String tpiID) throws GameException {
logger.debug("path: {}, tpmGitURL: {}, tpiRepoName: {}", path, tpmGitURL, tpiRepoName);
String tpiRepoPath = path + File.separator + tpiRepoName;
// pull 操作之前如果本地版本库不存在则克隆主机名为remote_origin
String cmd = "ls " + tpiRepoPath;
int isRepoExist = ShellUtil.executeAndGetExitStatus(cmd).getInteger("exitStatus");
if (isRepoExist != 0) {
logger.warn("版本库不存在,先克隆! tpmGitURL: {}, tpiID: {}", tpmGitURL, tpiID);
gitClone(path, tpmGitURL, "remote_origin", tpiRepoName);
String tpiRepoPath = path + File.separator + tpiRepoName;
try {
FileUtils.forceDelete(new File(tpiRepoPath));
logger.info("同步代码库先删除浅克隆,tpiID [{}] tpiRepoPath: {}", tpiID, tpiRepoPath);
} catch (IOException e1) {
logger.error("同步代码库先删除浅克隆,tpiID [{}] tpiRepoPath: {}, error: {}", tpiID, tpiRepoPath, e1.getMessage());
}
cmd = "cd " + tpiRepoPath + " && git remote | grep ^remote_origin$";
// pull 操作之前如果本地版本库不存在则克隆主机名为remote_origin
gitClone(path, tpmGitURL, "remote_origin", tpiRepoName, "");
String cmd = "cd " + tpiRepoPath + " && git remote | grep ^remote_origin$";
JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd);
if (remoteExist.getInteger("exitStatus") != 0) {
if (remoteExist.getString("out").contains("fatal")) {
@ -281,7 +283,7 @@ public class GameService {
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpmGitURL, "remote_origin", tpiRepoName);
gitClone(path, tpmGitURL, "remote_origin", tpiRepoName, "");
logger.debug("gitPull冲突重新clone结束, tpmGitURL: {}, tpiID: {}", tpmGitURL, tpiID);
} else {

View File

@ -25,7 +25,7 @@ public class CodeCloneJob implements Runnable {
try {
logger.info("下载tpi {}代码,tpmGitUrl {}", codeClone.getTpiID(), codeClone.getTpmGitURL());
gameService.gitClone(codeClone.getTpiWorkSpace(), codeClone.getTpmGitURL(), codeClone.getRemoteName(),
codeClone.getTpiRepoName());
codeClone.getTpiRepoName(), "--depth=1");
} catch (Throwable e) {
logger.error("下载代码" + codeClone.getTpiID() + "出错", e);
}