最近在做一个项目中需要将系统中操作的SQL自动化维护到远程服务器上,git是一个非常强大的代码维护工具,遂对其自动化push做了一些研究,供大家学习参考。
java git api - jgit的资料还是比较多的,而且git的操作比svn更容易理解,所以毅然决然的在git的道路上越走越远。
如果你想在一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit 。 JGit 是一个用 Java 写成的功能相对健全的 Git 的实现,它在 Java 社区中被广泛使用。 JGit 项目由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit 。
非常好的例子:https://github.com/centic9/jgit-cookbook
下面是我实现的代码,分别包含了如下的功能:
1、在本地文件夹建立起与远程仓库的连接(clone远程仓库到本地)
2 创建本地仓库
3、根据主干master新建分支并同步到远程
4 切换分支
5、提交并push代码到远程
6、从远程pull代码到本地
所需依赖包:
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.11.0.201803080745-r</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.archive</artifactId>
<version>4.11.0.201803080745-r</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
java文件:
package com.rz.commons.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Git操作工具类
*/
public class JGitUtil {
private static String LOCAL_REPO_PATH ;
private static String LOCAL_REPOGIT_CONFIG;
private static String REMOTE_REPO_URI;
private static String INIT_LOCAL_CODE_DIR;
private static String LOCAL_CODE_CT_SQL_DIR;
private static String BRANCH_NAME ;
private static String GIT_USERNAME;
private static String GIT_PASSWORD ;
public enum SqlTypeEnum{
SQL_CALC,EMAIL,MYSQL_TO_HIVE,HIVE_TO_MYSQL
}
final static Logger LOG = LoggerFactory.getLogger(JGitUtil.class);
final static PropertyUtil prop = PropertyUtil.getInstance();
static{
LOCAL_REPO_PATH = prop.getProperty("local_repo_path"); // D:/workspace/project
LOCAL_REPOGIT_CONFIG = prop.getProperty("local_repogit_config"); // D:/workspace/project/.git
REMOTE_REPO_URI = prop.getProperty("remote_repo_uri"); // [email protected]:xxx/project.git
INIT_LOCAL_CODE_DIR = prop.getProperty("init_local_code_dir"); // D:/workspace
LOCAL_CODE_CT_SQL_DIR = prop.getProperty("local_code_ct_sql_dir"); // sqlpath/
BRANCH_NAME = prop.getProperty("branch_name"); // v1.0
GIT_USERNAME = prop.getProperty("git_username"); // admin
GIT_PASSWORD = prop.getProperty("git_password"); // admin
}
public static void main(String[] args) {
writeFileToGit(SqlTypeEnum.EMAIL,"xxx","-- 测试hehe \n select * from dual;","test_测试_201806071322","test");
}
/**
* sql脚本文件同步到git仓库
* @param qte SQl类型
* @param loginName 系统登录名
* @param fileName 文件名
* @param fileContent 文件内容
* @param comment 提交说明
* @return
*/
public static boolean writeFileToGit(SqlTypeEnum qte,String loginName,String sqlConent,String fileName,String comment){
JGitUtil.pull();
String dest =LOCAL_CODE_CT_SQL_DIR+qte.name().toLowerCase();
String path = LOCAL_REPO_PATH+"/"+dest;
File f =new File(path);
if(!f.exists()){
f.mkdirs();
}
dest=dest+"/"+fileName;
path=path+"/"+fileName;
comment=loginName+" option of "+comment;
return true==JGitUtil.createFile(sqlConent, path)==JGitUtil.commitAndPush(dest,comment);
}
/**
* 根据主干master新建分支并同步到远程仓库
* @param branchName 分支名
* @throws IOException
* @throws GitAPIException
*/
public static String newBranch(String branchName) throws IOException{
// String newBranchIndex = "refs/heads/"+branchName;
String gitPathURI = "";
// Git git = null;
// 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 (GitAPIException e) {
// e.printStackTrace();
// }
return gitPathURI;
}
/**
* 添加文件
* @param fileName
* @return
*/
public static boolean addFile(String fileName) {
boolean addFileFlag=true;
try (
Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );
// Git git2 = new Git(new FileRepository(localRepoGitConfig));
) {
// File myFile = new File(git.getRepository().getDirectory().getParent(),filePath);
// if(!myFile.createNewFile()) {
// throw new IOException("Could not create file " + myFile);
// }
//add file to git
String filePath=LOCAL_CODE_CT_SQL_DIR+fileName;
git.add().addFilepattern(INIT_LOCAL_CODE_DIR).call();
System.out.println("Added file " + filePath + " to repository at " + git.getRepository().getDirectory());
}catch (Exception e) {
e.printStackTrace();
addFileFlag=false;
}
return addFileFlag;
}
/**
* 提交代码到本地仓库
* @param filePath 文件位置(相对仓库位置:a/b/file)
* @param comment 提交git内容描述
* @return
*/
public static boolean commitFile(String comment) {
boolean commitFileFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
//提交代码到本地仓库
git.commit().setMessage(comment).call();
LOG.info("Committed to repository at " + git.getRepository().getDirectory());
}catch (Exception e) {
e.printStackTrace();
commitFileFlag=false;
LOG.error("commitFile error! \n"+e.getMessage());
}
return commitFileFlag;
}
public static boolean push() {
boolean pushFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
//提交代码到本地仓库
git.push().call();
LOG.info("push " + git.getRepository()+File.separator+git.getRepository().getBranch());
}catch (Exception e) {
e.printStackTrace();
pushFlag=false;
LOG.error("push error! \n"+e.getMessage());
}
return pushFlag;
}
/**
* 提交并推送代码至远程服务器
* @param filePath 提交文件路径(相对路径)
* @param desc 提交描述
* @return
*/
public static boolean commitAndPush(String filePath,String desc){
boolean commitAndPushFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
// //创建用户文件的过程
// File myfile = new File(filePath);
// myfile.createNewFile();
UsernamePasswordCredentialsProvider provider =new UsernamePasswordCredentialsProvider(GIT_USERNAME,GIT_PASSWORD);
git.add().addFilepattern(filePath).call();
//提交
git.commit().setMessage(desc).call();
//推送到远程
if(StringUtils.isBlank(GIT_USERNAME) || StringUtils.isBlank(GIT_PASSWORD)){
git.push().setCredentialsProvider(provider).call();
}else{
git.push().call();
}
LOG.info("Commit And Push file " + filePath + " to repository at " + git.getRepository().getDirectory());
}catch (Exception e) {
e.printStackTrace();
commitAndPushFlag=false;
LOG.error("Commit And Push error! \n"+e.getMessage());
}
return commitAndPushFlag;
}
/**
* 拉取远程代码
* @param remoteBranchName
* @return 远程分支名
*/
public static boolean pull(){
return pull(BRANCH_NAME);
}
public static boolean pull(String remoteBranchName){
boolean pullFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
// UsernamePasswordCredentialsProvider provider =new UsernamePasswordCredentialsProvider(GIT_USERNAME,GIT_PASSWORD);
git.pull()
.setRemoteBranchName(remoteBranchName)
// .setCredentialsProvider(provider)
.call();
}catch (Exception e) {
e.printStackTrace();
pullFlag=false;
}
return pullFlag;
}
public static boolean checkout(String branchName){
boolean checkoutFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
git.checkout().setName("refs/heads/"+branchName).setForce(true).call();
}catch (Exception e) {
e.printStackTrace();
checkoutFlag=false;
}
return checkoutFlag;
}
public static boolean checkout(){
return checkout(BRANCH_NAME);
}
/**
* 从远程获取最新版本到本地 不会自动合并 merge
* @param branchName
* @return
*/
public static boolean fetch(){
boolean fetchFlag=true;
try ( Git git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );) {
git.fetch().setCheckFetchedObjects(true).call();
}catch (Exception e) {
e.printStackTrace();
fetchFlag=false;
}
return fetchFlag;
}
/**
* 拉取新创建的分支到本地
* @param cloneURL
* @return
*/
@SuppressWarnings("static-access")
public static boolean pullNewBranchToLocal(String cloneURL){
boolean resultFlag = false;
String[] splitURL = cloneURL.split(" ");
String branchName = splitURL[1];
String fileDir = INIT_LOCAL_CODE_DIR+"/"+branchName;
//检查目标文件夹是否存在
File file = new File(fileDir);
if(file.exists()){
deleteFolder(file);
}
Git git;
try {
git = Git.open( new File(LOCAL_REPOGIT_CONFIG) );
git.cloneRepository().setURI(cloneURL).setDirectory(file).call();
resultFlag = true;
} catch (IOException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
return resultFlag;
}
private 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();
}
}
}
/**
* 生成文件写内容
* @param content 文件内容
* @param filePath 文件名称
*/
@SuppressWarnings("unused")
private static boolean createFile(String content,String filePath){
//删除前一天临时目录
// File af = new File(filePath+File.separator+DateUtil.getAgoBackDate(-1));
// if (af.exists()) {
// deleteFolder(af);
// }
// //创建临时存储目录
// File f = new File(filePath+File.separator+DateUtil.getAgoBackDate(0));
// if (!f.exists()) {
// f.mkdirs();
// }
// if (!fileName.endsWith(".sql")) {
// fileName+=".sql";
// }
boolean createFileFlag=true;
File file =new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
createFileFlag=false;
}
}
try(BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),Charsets.UTF_8));) {
bw.write(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
createFileFlag=false;
} catch (IOException e) {
e.printStackTrace();
createFileFlag=false;
}
return createFileFlag;
}
/**
* 创建本地新仓库
* @param repoPath 仓库地址 D:/workspace/TestGitRepository
* @return
* @throws IOException
*/
public static Repository createNewRepository(String repoPath) throws IOException {
File localPath = new File(repoPath);
// create the directory
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
repository.create();
return repository;
}
/**
* 创建仓库,仅需要执行一次
*/
public static boolean setupRepository(){
boolean setupRepositoryFlag=true;
try {
//设置远程服务器上的用户名和密码
UsernamePasswordCredentialsProvider provider =new UsernamePasswordCredentialsProvider(GIT_USERNAME,GIT_PASSWORD);
if(StringUtils.isBlank(GIT_USERNAME) || StringUtils.isBlank(GIT_P点击打开链接ASSWORD)){
Git git =Git.cloneRepository().setURI(REMOTE_REPO_URI) //设置远程URI
.setBranch("master") //设置clone下来的分支,默认master
.setDirectory(new File(LOCAL_REPO_PATH)) //设置下载存放路径
.call();
}else{
Git git =Git.cloneRepository().setURI(REMOTE_REPO_URI) //设置远程URI
.setBranch("master") //设置clone下来的分支,默认master
.setDirectory(new File(LOCAL_REPO_PATH)) //设置下载存放路径
.setCredentialsProvider(provider) //设置权限验证
.call();
}
} catch (Exception e) {
e.printStackTrace();
setupRepositoryFlag=false;
}
return setupRepositoryFlag;
}
}
点击打开链接ASSWORD)){
Git git =Git.cloneRepository().setURI(REMOTE_REPO_URI) //设置远程URI
.setBranch("master") //设置clone下来的分支,默认master
.setDirectory(new File(LOCAL_REPO_PATH)) //设置下载存放路径
.call();
}else{
Git git =Git.cloneRepository().setURI(REMOTE_REPO_URI) //设置远程URI
.setBranch("master") //设置clone下来的分支,默认master
.setDirectory(new File(LOCAL_REPO_PATH)) //设置下载存放路径
.setCredentialsProvider(provider) //设置权限验证
.call();
}
} catch (Exception e) {
e.printStackTrace();
setupRepositoryFlag=false;
}
return setupRepositoryFlag;
}
}
另,如果你想将 Git 整合进你的应用程序的话,一般来说你有三种可能的选择:启动一个 shell 来使用 Git 的命令行工具;使用 Libgit2;或者使用 JGit。见 资料
每个部件使用不同的缓冲区对象实现机械臂模拟动画1.initShaders() 函数创建步骤2.相关API介绍2.1创建着色器对象 gl.createShader()2.2指定着色器代码 gl.shaderSource()1.initShaders() 函数创建步骤从第一个demo开始一直使用了一个着色器初始化函数initShaders(),但是一直没有详细说明,接下来我们了解一下它是怎么把字符串形式的 GLSL ES 代码编译成可在GPU上运行的着色器程序,该函数大致可分为以下七个步骤:创建着色器对
卷积神经网络(CNN)介绍传统CNN的强大之处就在于它的多层结构能自动学习特征,并且可以学习到多个层次的特征:较浅的卷积层感知域较小,学习到一些局部特征;较深的卷积层感知域较大,能够学习到更加抽象的特征。这些抽象特征对物体的方向、大小、位置等敏感性更低,从而有助于识别性能的提高。缺点:抽象的特征对分类很有帮助,可以判断出一幅图像中的物体属于哪一类。但由于抽象特征丢失了物体的一些细节,不能很...
https://www.cnblogs.com/jifeng/p/4676863.html写得很全面,根本无需补充http://blog.mkfree.com/posts/515835d1975a30cc561dc35dspring-data-redis API:http://docs.spring.io/spring-data/redis/docs/1.5.1.RELEASE/api/首先跟大家...
引言: 书上关于next和nextval的修正值方法比较难理解,所以我这里讲解自己的方法。这里我就不介绍关于字符串匹配中KMP的优点,也不强调next的修正值比next的值好在哪,我们就说方法就行了。一、next求解。来,首先给我们一个序列j 1 2 3 4 5 6 7 8 模式串 a b a a...
配置我的配置文件如下C:\Users\jerry\.gitconfig[diff] tool = bc3[difftool] prompt = false[difftool "bc3"] cmd = \"D:/Program Files/Beyond Compare 3.3.8.16340/bcomp.exe\" \"$LOCAL\" \"$REMOTE\" ...
package com.ldy.util;import java.util.Properties;import org.apache.commons.lang3.StringUtils;import org.apache.log4j.Logger;import org.springframework.mail.SimpleMailMessage;import org.springfra
一、JUnit概述1.1 简介JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。JUnit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功...
转移指令转移指令(JMP)允许程序员跳过一段程序,跳转到存储器的任何位置执行下一条指令。条件转移则允许程序员根据对数值的测试做出决定。这些数值测试的结果保存在标志位中,再由条件转移指令检测它们。1.无条件转移指令可以分为短转移、近转移、远转移。短转移-2字节指令,允许在+127和-128字节范围内转移近转移-3字节指令,允许在+32kb和-32kb范围内转移远转移-5字节指...
arc117C假设三种颜色分别为0,1,2,那么操作相当于在模3意义下取-x-y,满足结合律,直接组合数算一下即可。D考虑将最后的点权按照从小到大的顺序排列,那么只需要考虑相邻的距离是否满足条件。因此对于相邻的直接取它们的距离即可。等价于求一个排列,点权即为上一个加上两者之间距离,等价于求从某个点开始到某个点结束经过所有点的路径长度最小,那么只有一条路径不会经过两遍,取直径即可。E考虑最后的折线,一层一层DP即可,设f[i][j][k]f[i][j][k]f[i][j][k]表示当
越来越多的我发现自己在我的一些较大的Web项目中丢失了文件。有很多内容要处理 - HTML视图,几个派生CSS页面,页面级CSS,脚本库,应用程序范围的脚本和页面特定的脚本文件等等。幸运的是,我使用Resharper和Ctrl-T转到任何自动完成你到任何文件,快速键入,成员。令人敬畏的除了当我忘记 - 或者当我不太确定我正在寻找的名字。项目导航仍然很重要。有时在一个项目工作时,我似...
好的。下面是一个基本的示例程序:首先,你需要在系统中安装 onnxruntime 和 opencv-python 库:pipinstall onnxruntimepip install opencv-python然后,你可以使用下面的代码来调用 USB 摄像头并使用 ONNX Runtime 进行推理加速:import cv2import onnxruntime as rt# 读...
简要:众所周知,开发软件最好的环境是在MAC下;为此在黑苹果上,用Thinkphp开发;在过程中出现,找不到model以及数据库类型错误;为此向大家分享解决办法,希望能够帮助困惑的伙伴们,如果有不对或者好的建议告知下;~!一、Model找不到(MODULE_NOT_EXIST)用惯Thinkphp框架的朋友,都清楚;报错原因:找不到模块;但这种情况(MODULE_NOT_EXIST :后面没带控制