git也有java的api包 可以方便的用java操作提交 建分支的代码管理活动

http://www.cnblogs.com/songshu120/p/6180747.html



public class GitUtilClass {
public static String localRepoPath = "D:/repo";
public static String localRepoGitConfig = "D:/repo/.git";
public static String remoteRepoURI = "git@gitlab.com:wilson/test.git";
public static String localCodeDir = "D:/platplat";

/**
* 新建一个分支并同步到远程仓库
* @param branchName
* @throws IOException
* @throws GitAPIException
*/
public static String newBranch(String branchName){
String newBranchIndex = "refs/heads/"+branchName;
String gitPathURI = "";
Git git;
try {

//检查新建的分支是否已经存在,如果存在则将已存在的分支强制删除并新建一个分支
List<Ref> refs = git.branchList().call();
for (Ref ref : refs) {
if (ref.getName().equals(newBranchIndex)) {
System.out.println("Removing branch before");
git.branchDelete().setBranchNames(branchName).setForce(true)
.call();
break;
}
}
//新建分支
Ref ref = git.branchCreate().setName(branchName).call();
//推送到远程
git.push().add(ref).call();
gitPathURI = remoteRepoURI + " " + "feature/" + branchName;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return gitPathURI;
}

public static void commitFiles() throws IOException, GitAPIException{
String filePath = "";
Git git = Git.open( new File(localRepoGitConfig) );
//创建用户文件的过程
File myfile = new File(filePath);
myfile.createNewFile();
git.add().addFilepattern("pets").call();
//提交
git.commit().setMessage("Added pets").call();
//推送到远程
git.push().call();
}

public static boolean pullBranchToLocal(String cloneURL){
boolean resultFlag = false;
String[] splitURL = cloneURL.split(" ");
String branchName = splitURL[1];
String fileDir = localCodeDir+"/"+branchName;
//检查目标文件夹是否存在
File file = new File(fileDir);
if(file.exists()){
deleteFolder(file);
}
Git git;
try {
git = Git.open( new File(localRepoGitConfig) );
git.cloneRepository().setURI(cloneURL).setDirectory(file).call();
resultFlag = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultFlag;
}

public static void deleteFolder(File file){
if(file.isFile() || file.list().length==0){
file.delete();
}else{
File[] files = file.listFiles();
for(int i=0;i<files.length;i++){
deleteFolder(files[i]);
files[i].delete();
}
}
}

public static void setupRepo() throws GitAPIException{
//建立与远程仓库的联系,仅需要执行一次
Git git = Git.cloneRepository().setURI(remoteRepoURI).setDirectory(new File(localRepoPath)).call();
}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值