JGit有哪些常用的方法
生活随笔
收集整理的這篇文章主要介紹了
JGit有哪些常用的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇文章主要介紹“JGit有哪些常用的方法”,在日常操作中,相信很多人在JGit有哪些常用的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JGit有哪些常用的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
package;
importjava.io.File;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.commons.lang3.StringUtils;
importorg.eclipse.jgit.api.AddCommand;
importorg.eclipse.jgit.api.Git;
importorg.eclipse.jgit.diff.DiffEntry;
importorg.eclipse.jgit.diff.DiffEntry.ChangeType;
importorg.eclipse.jgit.dircache.DirCache;
importorg.eclipse.jgit.revwalk.RevCommit;
importorg.eclipse.jgit.transport.CredentialsProvider;
importorg.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
importorg.eclipse.jgit.treewalk.filter.PathFilterGroup;
publicclassGitService{
privateGitgit;
privateCredentialsProvidercredentialsProvider;
privatestaticStringgitFile=".git";
privatestaticStringrepoName="repo/";
GitService(Gitgit){
this.git=git;
}
GitService(StringrepositoryUrl,Stringusername,Stringpwd,StringlocalRepo,StringbranchName)throwsException{
this.credentialsProvider=newUsernamePasswordCredentialsProvider(username,pwd);
initRepo(credentialsProvider,repositoryUrl,localRepo,branchName);
}
publicvoidinitRepo(CredentialsProvidercredentialsProvider,StringrepositoryUrl,StringlocalRepo,StringbranchName)throwsException{
// if(StringUtils.isEmpty(repoDir)||StringUtils.isEmpty(localRepo)||StringUtils.isEmpty(branchName)){
// //error
// return;
// }
// FilelocalDir=newFile(localRepo);
// //initandclonetherepo
// if(!newFile(repoDir+File.separator+gitFile).exists()){
// Git.init().setDirectory(localDir).call();
// Git.cloneRepository().setURI(repoDir).setBranch(branchName)
// .setDirectory(newFile(localRepo)).call();
// }
createLocalRepo(localRepo);
if(!newFile(localRepo+gitFile).exists()){
this.git=Git.cloneRepository().setCredentialsProvider(credentialsProvider).setURI(repositoryUrl).setBranch(branchName)
.setDirectory(newFile(localRepo)).call();
}else{
this.git=Git.open(newFile(localRepo));
}
}
privatestaticvoidcreateLocalRepo(StringrepositoryDir){
Filefile=newFile(repositoryDir);
if(!file.exists()){
file.mkdirs();
}
}
publicvoidpullRepo(StringbranchName)throwsException{
git.pull().setRemoteBranchName(branchName).setCredentialsProvider(credentialsProvider).call();
}
publicvoidresetLocalRepo(StringrepoDir,StringlocalRepo,StringbranchName)throwsException{
if(StringUtils.isEmpty(repoDir)||StringUtils.isEmpty(localRepo)||StringUtils.isEmpty(branchName)){
//error
return;
}
Filefile=newFile(localRepo);
if(file.canWrite()&&file.exists()){
file.delete();
}
file.mkdirs();
initRepo(credentialsProvider,repoDir,localRepo,branchName);
}
publicvoidpushRepo(List<String>files)throwsException{
if(!files.isEmpty()){
AddCommandaddCmd=git.add();
for(Stringfile:files){
addCmd.addFilepattern(file);
}
addCmd.call();
git.commit().setMessage("Authomatedpushthecode").call();
git.push().setCredentialsProvider(credentialsProvider).call();
}else{
//noupdates
}
Stringfile="repo/sourcTest.java";
// git.add().addFilepattern(file).call();
git.commit().setOnly(file).setMessage("Automatedpushthecode");
git.push().call();
}
publicstaticvoidmain(String[]args)throwsException{
StringrepoUrl="";
Stringuser="";
StringlocalRepoDir=ProjectUser.getPath(user)+repoName;
Stringusername="";
Stringpwd="";
StringbranchName="";
// Gitgit=JGitService.getGitByPwd(repoUrl,username,pwd,localRepoDir,branchName);
// GitServiceservice=newGitService(git);
// service.initRepo(repoUrl,localRepoDir,branchName);
// service.pullRepo(branchName);
// List<String>files=newArrayList<>();
// files.add("repo/test/TestPush.java");
// files.add(".+");
// service.pushRepo(files);
test();
}
publicstaticvoidtest()throwsException{
StringrepoUrl="";
Stringuser="";
StringlocalRepoDir=ProjectUser.getPath(user)+repoName;
Stringusername="";
Stringpwd="";
StringbranchName="";
CredentialsProvidercredentialsProvider=newUsernamePasswordCredentialsProvider(username,
pwd);
Gitgit=JGitService.getGitByPwd(repoUrl,username,pwd,localRepoDir,branchName);
// GitServiceservice=newGitService(git);
// DirCacheindex=git.add().addFilepattern("test/").call();
AddCommandaddCmd=git.add();
addCmd.addFilepattern("sourcTest.java");
DirCacheindex=addCmd.call();
intcount=index.getEntryCount();
System.out.println("Updatedcount:"+count);
git.commit().setMessage("Automatedpushthecode").call();
git.push().setCredentialsProvider(credentialsProvider).call();
}
}
總結
以上是生活随笔為你收集整理的JGit有哪些常用的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是pnp问题_pnp什么意思
- 下一篇: 实现tree系统命令