JGit:我想获取特定分支中的所有文件和文件夹

huangapple go评论74阅读模式
英文:

JGit : I want to get all files and folders in a particular branch

问题

我看过很多教程,但没有找到获取特定分支中所有文件和文件夹的代码。

我尝试了这段代码:

File src = new File("C:\\Users\\Winfo\\Documents\\GitHub\\WDAS");
org.eclipse.jgit.lib.Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir(src).build();
Git git = new Git(repo);
git.checkout()
    .setName("new-branch")
    .setStartPoint("commit id") // 在这里填入提交的id
    .call();

这会基于提交的id创建一个单独的分支,但我需要根据分支在本地存储库中克隆文件和文件夹列表。

我对JGit不太熟悉,有人可以帮忙满足我的需求吗?提前感谢。

英文:

I have seen a lot of tutorials but I haven't found any code for getting all the files and folders in a particular branch.

I have tried this piece of code

File src = new File("C:\\Users\\Winfo\\Documents\\GitHub\\WDAS");
	org.eclipse.jgit.lib.Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir(src).build();
	Git git = new Git(repo);
	git.checkout()
    .setName("new-branch")
    .setStartPoint("commit id") // commit id here
    .call();

This is creating a separate Branch based on the commit id, but I need to clone the list of files and folders in a local repository based on the branch.

I am new to JGit, could someone help with my requirement. Thanks in advance

答案1

得分: 0

你可以像下面这样执行操作来从特定分支进行拉取:

Git.cloneRepository()
  .setURI("https://github.com/eclipse/jgit.git") // 你的 Git URL
  .setDirectory(new File("/path/to/repo"))
  .setBranchesToClone(Arrays.asList("refs/heads/specific-branch")) // 提供你的分支名称
  .setBranch("refs/heads/specific-branch")
  .call();

更多信息请参阅:这里

英文:

You may do something like this below to pull from a particular branch

Git.cloneRepository()
  .setURI("https://github.com/eclipse/jgit.git") // your git url
  .setDirectory(new File("/path/to/repo")) 
  .setBranchesToClone(Arrays.asList("refs/heads/specific-branch"))// give ur branch name
  .setBranch("refs/heads/specific-branch")
  .call();

For more information: read here

huangapple
  • 本文由 发表于 2020年3月16日 15:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60701641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定