使用JGit
private Git git=null;
public void CommitCode(String proName,String proPath) throws IOException, NoFilepatternException, GitAPIException {
try{
Repository existingRepo = new FileRepositoryBuilder().setGitDir(new File(proPath+"\\.git")).build();
git = new Git(existingRepo);
//true if no differences exist between the working-tree, the index, and the current HEAD, false if differences do exist
if(git.status().call().isClean()==false){
git.add().addFilepattern(".").call();
SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat hm = new SimpleDateFormat("HHmm");
git.commit().setMessage(proName+"_"+ymd.format(new Date())+"_"+hm.format(new Date())).call();
git.push().call();
MyLog.logger.info("------succeed add,commit,push files . to repository at " + existingRepo.getDirectory());
}else{ //clean
MyLog.logger.info("\n-------code is clean------");
}
}finally{
if(git !=null){
git.close();
}
}
}