importjava.io.File;importjava.util.List;importorg.eclipse.jgit.api.Git;importorg.eclipse.jgit.api.ListBranchCommand;importorg.eclipse.jgit.api.errors.GitAPIException;importorg.eclipse.jgit.lib.Ref;importorg.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;public classGitFromXLY {public static voidmain(String[] args) {
String baseUrl= "http://git/url";
String module= args[0];
String user= "username";
String pass= "password";
String branch= args[1];
String revision= args[2];
GitFromXLY gfxly= newGitFromXLY(module, user, pass, branch, revision);int getPullCode = gfxly.pull(baseUrl +module);if (getPullCode == 0) {
System.out.println("检出代码成功===0");
}else if (getPullCode == 1) {
System.exit(1);
}else if (getPullCode == 2) {
System.exit(2);
}else if (getPullCode == 3) {
System.exit(3);
}else if (getPullCode == 4) {
System.exit(4);
}else{
System.out.println("检出代码未知异常===5");
System.exit(5);
}int getBranchCode =gfxly.checkoutBranch();if (getBranchCode == 0) {
System.out.println("检出分支成功===0");
}else if (getPullCode == 6) {
System.exit(6);
}else{
System.out.println("检出分支未知异常===7");
System.exit(7);
}int getRevisionCode =gfxly.checkoutRevision();if (getRevisionCode == 0) {
System.out.println("检出版本成功===0");
}else if (getPullCode == 8) {
System.exit(8);
}else{
System.out.println("检出版本未知异常===9");
System.exit(9);
}
}privateString module;privateString user;privateString pass;privateString branch;privateString revision;privateString git_config;publicGitFromXLY(String module, String user, String pass, String branch, String revision){this.module =module;this.user =user;this.pass =pass;this.branch =branch;this.revision =revision;this.git_config = "./basedir/" + module + "/.git";
}/*** 通过url拉取代码
*@paramgitUrl
*@return
*/
public intpull(String gitUrl){
String pullMsg= "";//标记拉取代码的标志
int pullFlag = 0;//提供用户名和密码的验证
UsernamePasswordCredentialsProvider provider = newUsernamePasswordCredentialsProvider(this.user, this.pass);//指定要加载的代码路径
File dir = new File("./basedir/" + this.module);//判断代码路径下是否有内容,如果有就删除
if(dir.exists()){
deleteFolder(dir);
}
Git git= null;try{
git=Git.cloneRepository().setURI(gitUrl)
.setDirectory(dir).setCredentialsProvider(provider).call();
pullMsg= "检出代码成功 success";
}catch(org.eclipse.jgit.api.errors.TransportException e){
e.printStackTrace();
pullMsg= "用户名NAME或密码PASSWORD错误或远程链接URL错误 failed";
pullFlag= 1;
}catch(org.eclipse.jgit.api.errors.JGitInternalException e) {
e.printStackTrace();
pullMsg= "已经存在了项目的下载目录,并且目录正在被操作 failed";
pullFlag= 2;
}catch(GitAPIException e) {
e.printStackTrace();
pullMsg= "调用GitAPI异常,failed";
pullFlag= 3;
}catch(NoClassDefFoundError e) {
e.printStackTrace();
pullMsg= "未找到相应的类文件异常,failed";
pullFlag= 4;
}finally{
System.out.println(pullMsg+"--code--"+pullFlag);if (git != null) {
git.close();
}
}returnpullFlag;
}/*** 检出分支
*@parambranchName
*@return
*/
public intcheckoutBranch(){
String checkoutMsg= "";int checkoutFlag = 0;if (this.branch.equals("master")) {
checkoutMsg= "Check out code OK. ->" + this.branch;
System.out.println(checkoutMsg+"--code--"+checkoutFlag);returncheckoutFlag;
}
Git git= null;try{
git= Git.open( new File(this.git_config) );//列出所有的分支名称
List branchList =git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();for(Ref ref : branchList){if (this.branch.equals(ref.getName())) {
System.out.println("代码分支列表中存在给定分支");
}
}
git.checkout().setName("origin/" + this.branch).setForce(true).call();
checkoutMsg= "检出分支代码 success! code OK ->" + this.branch;
}catch(Exception e) {
e.printStackTrace();
checkoutMsg= "检出分支代码 failed ! ->" + this.branch;
checkoutFlag= 6;
}finally{
System.out.println(checkoutMsg+"--code--"+checkoutFlag);if (git != null) {
git.close();
}
}returncheckoutFlag;
}/*** 检出代码
*@paramrevision
*@return
*/
public intcheckoutRevision(){
String checkoutMsg= "";int checkoutFlag = 0;if (this.revision == null || this.revision.length() == 0) {
checkoutMsg= "Check out code OK. ->" + this.revision;
System.out.println(checkoutMsg+"--code--"+checkoutFlag);returncheckoutFlag;
}
Git git= null;try{
git= Git.open( new File(this.git_config) );
git.checkout().setName(this.revision ).setForce(true).call();
checkoutMsg= "检出代码版本 success! code OK. ->" + this.revision;
}catch(Exception e) {
e.printStackTrace();
checkoutMsg= "检出代码版本 failed ! ->" + this.revision;
checkoutFlag= 8;
}finally{
System.out.println(checkoutMsg+"--code--"+checkoutFlag);if (git != null) {
git.close();
}
}returncheckoutFlag;
}/*** 删除目录
*@paramfile*/
private voiddeleteFolder(File file){try{if (file.isFile() || file.list().length==0) {
file.delete();
}else{
File[] files=file.listFiles();for(File getFile: files) {
deleteFolder(getFile);
getFile.delete();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}