JGit 常用方法示例

  • Post author:
  • Post category:其他


package ;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;

public class GitService {

	private Git git;
	private CredentialsProvider credentialsProvider;
	private static String gitFile = ".git";
	private static String repoName = "repo/";

	GitService(Git git){
		this.git = git;
	}
	
	GitService(String repositoryUrl, String username, String pwd, String localRepo,String branchName) throws Exception{
		this.credentialsProvider = new UsernamePasswordCredentialsProvider(username,pwd);
		initRepo(credentialsProvider,repositoryUrl,localRepo,branchName);
	}

	public void initRepo(CredentialsProvider credentialsProvider,String repositoryUrl, String localRepo, String branchName) throws Exception{
//		if(StringUtils.isEmpty(repoDir)||StringUtils.isEmpty(localRepo)||StringUtils.isEmpty(branchName)){
//			//error
//			return;
//		}
//		File localDir = new File(localRepo);  
//		//init and clone the repo
//		if (!new File(repoDir + File.separator + gitFile).exists()) {  
//			Git.init().setDirectory(localDir).call();
//			Git.cloneRepository().setURI(repoDir).setBranch(branchName)
//			.setDirectory(new File(localRepo)).call();
//		}  
		createLocalRepo(localRepo);
		if (!new File(localRepo + gitFile).exists()) {  
			this.git = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setURI(repositoryUrl).setBranch(branchName)
					.setDirectory(new File(localRepo)).call();
		}else{
			this.git = Git.open( new File(localRepo));
		}
	}
	
	private static void createLocalRepo(String repositoryDir){
		File file = new File(repositoryDir);
		if(!file.exists()){
			file.mkdirs();
		}
	}

	public void pullRepo(String branchName) throws Exception{
		git.pull().setRemoteBranchName(branchName).setCredentialsProvider(credentialsProvider).call();
	}

	public void resetLocalRepo(String repoDir, String localRepo, String branchName) throws Exception{
		if(StringUtils.isEmpty(repoDir)||StringUtils.isEmpty(localRepo)||StringUtils.isEmpty(branchName)){
			//error
			return;
		}
		File file = new File(localRepo);
		if(file.canWrite()&&file.exists()){
			file.delete();
		}
		file.mkdirs();
		initRepo(credentialsProvider,repoDir,localRepo,branchName);
	}

	public void pushRepo(List<String> files) throws Exception{
		if(!files.isEmpty()){
			AddCommand addCmd = git.add();  
			for (String file : files) {  
				addCmd.addFilepattern(file);  
			}  
			addCmd.call();
			git.commit().setMessage("Authomated push the code").call();
			git.push().setCredentialsProvider(credentialsProvider).call();
		}else{
			// no updates
		}
		String file = "repo/sourcTest.java";
//		git.add().addFilepattern(file).call();
		git.commit().setOnly(file).setMessage("Automated push the code");
		git.push().call();
	}
	
	public static void main(String[] args) throws Exception{
		String repoUrl = "";
		String user = "";
		String localRepoDir = ProjectUser.getPath(user)+repoName;
		String username = "";
		String pwd = "";
		String branchName = "";
		
//		Git git = JGitService.getGitByPwd(repoUrl, username, pwd, localRepoDir,branchName);
//		GitService service = new GitService(git);
//		service.initRepo(repoUrl,localRepoDir,branchName);
		
//		service.pullRepo(branchName);
		
//		List<String> files = new ArrayList<>();
//		files.add("repo/test/TestPush.java");
//		files.add(".+");
//		service.pushRepo(files);
		test();
	}
	
	public static void test() throws Exception{
		String repoUrl = "";
		String user = "";
		String localRepoDir = ProjectUser.getPath(user)+repoName;
		String username = "";
		String pwd = "";
		String branchName = "";
		CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username,
				pwd);
		
		Git git = JGitService.getGitByPwd(repoUrl, username, pwd, localRepoDir,branchName);
//		GitService service = new GitService(git);
		
//		DirCache index = git.add().addFilepattern("test/").call();
		AddCommand addCmd = git.add();
		addCmd.addFilepattern("sourcTest.java");
		DirCache index = addCmd.call();
		int count = index.getEntryCount();
		System.out.println("Updated count: "+count);
		git.commit().setMessage("Automated push the code").call();
		git.push().setCredentialsProvider(credentialsProvider).call();
		
		
	}
}

以上仅为测试代码,main方法测试了代码init\clone\commit & push

转载于:https://my.oschina.net/slightScenery/blog/3073991