JGit 常用方法示例
最编程
2024-03-09 20:43:07
...
【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告
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
推荐阅读
-
PX4 安装教程(七)几种常用遥控器的使用方法
-
FFmpeg 命令行参数 ⑦ ( 使用 FFmpeg 提取 PCM 音频数据 | PCM 音频格式 | 提取 PCM 音频格式常用参数 | 查询文档方法 )
-
python 读取和保存音频数据的几种常用方法-2.
-
计算 24 点的常用方法
-
一种结构设计模式,允许在对象中动态添加新行为。它通过创建一个封装器来实现这一目的,即把对象放入一个装饰器类中,然后把这个装饰器类放入另一个装饰器类中,以此类推,形成一个封装器链。这样,我们就可以在不改变原始对象的情况下动态添加新行为或修改原始行为。 在 Java 中,实现装饰器设计模式的步骤如下: 定义一个接口或抽象类作为被装饰对象的基类。 公共接口 Component { void operation; } } 在本例中,我们定义了一个名为 Component 的接口,该接口包含一个名为 operation 的抽象方法,该方法定义了被装饰对象的基本行为。 定义一个实现基类方法的具体装饰对象。 公共类 ConcreteComponent 实现 Component { public class ConcreteComponent implements Component { @Override public void operation { System.out.println("ConcreteComponent is doing something...") ; } } 定义一个抽象装饰器类,该类继承于基类,并将装饰对象作为一个属性。 公共抽象类装饰器实现组件 { protected Component 组件 public Decorator(Component component) { this.component = component; } } @Override public void operation { component.operation; } } } 在这个示例中,我们定义了一个名为 Decorator 的抽象类,它继承了 Component 接口,并将被装饰对象作为一个属性。在操作方法中,我们调用了被装饰对象上的同名方法。 定义一个具体的装饰器类,继承自抽象装饰器类并实现增强逻辑。 公共类 ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component 组件) { super(component); } } public void operation { super.operation System.out.println("ConcreteDecoratorA 正在添加新行为......") ; } } 在本例中,我们定义了一个名为 ConcreteDecoratorA 的具体装饰器类,它继承自装饰器抽象类,并实现了操作方法的增强逻辑。在操作方法中,我们首先调用被装饰对象上的同名方法,然后添加新行为。 使用装饰器增强被装饰对象。 公共类 Main { public static void main(String args) { Component 组件 = new ConcreteComponent; component = new ConcreteDecoratorA(component); 组件操作 } } 在这个示例中,我们首先创建了一个被装饰对象 ConcreteComponent,然后通过 ConcreteDecoratorA 类创建了一个装饰器,并将被装饰对象作为参数传递。最后,调用装饰器的操作方法,实现对被装饰对象的增强。 使用场景 在 Java 中,装饰器模式被广泛使用,尤其是在 I/O 中。Java 中的 I/O 库使用装饰器模式实现了不同数据流之间的转换和增强。 让我们打开文件 a.txt,从中读取数据。InputStream 是一个抽象类,FileInputStream 是专门用于读取文件流的子类。BufferedInputStream 是一个支持缓存的数据读取类,可以提高数据读取的效率,具体代码如下: @Test public void testIO throws Exception { InputStream inputStream = new FileInputStream("C:/bbb/a.txt"); // 实现包装 inputStream = new BufferedInputStream(inputStream); byte bytes = new byte[1024]; int len; while((len = inputStream.read(bytes)) != -1){ System.out.println(new String(bytes, 0, len)); } } } } 其中 BufferedInputStream 对读取数据进行了增强。 这样看来,装饰器设计模式和代理模式似乎有点相似,接下来让我们讨论一下它们之间的区别。 第三,与代理模式的区别: 代理模式的目的是控制对对象的访问,它在对象外部提供一个代理对象来控制对原对象的访问。代理对象和原始对象通常实现相同的接口或继承相同的类,以确保两者可以相互替换。 装饰器模式的目的是动态增强对象的功能,而这是通过对象内部的包装器来实现的。在装饰器模式中,装饰器类和被装饰对象通常实现相同的接口或继承自相同的类,以确保两者可以相互替代。装饰器模式也被称为封装器模式。 在代理模式中,代理类附加了与原类无关的功能。
-
将汉字转换为拼音和缩写的 JavaScript 方法示例
-
Oracle导出导入dmp等文件类型多表数据的常用方法、遇到的常见问题及解决办法(exp无效sql????)
-
七言绝句和示例:第一种方法 8 首诗,第二种方法 54 首诗
-
c# ListView 控件的常用属性、方法和事件
-
理解动态类型、静态类型;强类型、弱类型的常用方法