有没有办法以编程方式从Java远程git存储库下载单个文件?
我能够使用SVNKit做一些与Subversion类似的东西,我已经看到有一个纯粹的java实现的git(eclipse的JGit)可能能够做类似的事情,所以我希望有一个肯定的答案; 虽然从我对git如何工作的理解 - 只允许来自本地存储库的更新 - 这可能证明是有问题的.
我正在研究一个基于gui的辅助工具,它将:
所有这一切都是在没有使用ngit(jgit的.NET端口)安装git的情况下完成的.
但我无法弄清楚如何设置跟踪来跟踪主设备到原点/主设备并使用ngit开发到原点/开发.
我可以轻松地做到
git branch --set-upstream master origin/master
Run Code Online (Sandbox Code Playgroud)
但是,我希望避免本地git安装的依赖.
我是GIT的新手,想要使用JGit创建存储库并将其从Java maven项目中的其他操作中删除.我想知道需要哪些依赖项.
以下链接我读了一篇回合JGit http://wiki.eclipse.org/JGit/User_Guide#Getting_Started
我正在使用以下代码从github中检出一个存储库。
private String url = "https://github.com/organization/project.git";
Git repo = Git.cloneRepository().setURI(url).setDirectory(directory).setCloneAllBranches(true).call();
for (Ref b : repo.branchList().call()) {
System.out.println("(standard): cloned branch " + b.getName());
}
Run Code Online (Sandbox Code Playgroud)
我正在使用代码
Git git = Git.open(checkout); //checkout is the folder with .git
git.pull().call(); //succeeds
Run Code Online (Sandbox Code Playgroud)
如果我去支店
Git git = Git.open(new File(checkout)); //checkout is the folder with .git
System.out.println(git.getRepository().getFullBranch());
CheckoutCommand checkout = git.checkout();
Ref call = checkout.setName("kalees").call();
Run Code Online (Sandbox Code Playgroud)
引发org.eclipse.jgit.api.errors.RefNotFoundException:引用kalees无法解析。
这是什么问题,如果我指定“ master”而不是“ kalees”,它可以正常工作。结帐特定分支我应该怎么做?
如果我使用代码
git.checkout().setCreateBranch(true).setName("refs/remotes/origin/kalees");
Run Code Online (Sandbox Code Playgroud)
它签出kalees分支。但是当我做拉动操作时
git.pull().call();
Run Code Online (Sandbox Code Playgroud)
它抛出org.eclipse.jgit.api.errors.DetachedHeadException:HEAD被分离。这是结帐问题还是出库问题,可能是什么?
使用JGit,我想获得一个列表,其中列出了使用git log --name-status可能发生的更改。
这可能吗?如果是这样,您该怎么做?
我需要列出远程Git存储库的标签,并通过JGit 3.2.0 API按创建日期对它们进行排序。
找不到使用lsremote的方法,所以我只能按名称排序:
System.out.println("Listing remote repository " + REMOTE_URL);
Collection<Ref> tags = Git.lsRemoteRepository()
.setTags(true)
.setRemote(REMOTE_URL)
.call();
ArrayList<Ref> taglist = new ArrayList<>(tags);
Collections.sort(taglist, new Comparator<Ref>()
{
public int compare(Ref o1, Ref o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (Ref ref : taglist) {
System.out.println("Ref: " + ref.getName());
System.out.println("ObjectId : " + ref.getObjectId());
System.out.println("Ref short: " + Repository.shortenRefName(ref.getName()));
}
}
Run Code Online (Sandbox Code Playgroud)
如何按创建日期对标签进行排序?
克隆存储库后,它可以与本地存储库一起使用:
// open a cloned repository
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File(localPath + "/.git"))
.readEnvironment()
.findGitDir() …Run Code Online (Sandbox Code Playgroud) JGit是否支持Git Credentials,如下所示:http://git-scm.com/docs/gitcredentials?
我还没有找到任何关于这个主题的内容.
我需要创建一个历史文件,详细说明所有标签和每个标签的所有提交.
我试图调用getTags()存储库对象并使用那些对象id,但它们不是提交id.
我也试图getAllRefsByPeeledObjectId()在存储库上使用它确实带回了提交但我不能将它们与标签相关联.
有任何想法吗?
对于给定的提交,我想获取父提交树,以便我可以继续比较更改.我发现父RevCommit对象上的getTree()始终返回null.
ObjectId lastCommitId = repository.resolve(Constants.HEAD);
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(lastCommitId);
List<RevCommit> parents = new ArrayList<>();
for(RevCommit parent : commit.getParents()) {
parents.add(parent);
}
if ( parents.get(0).getTree() == null ) {
System.out.println("first parent tree was null");
}
Run Code Online (Sandbox Code Playgroud)
我这是错误的方式吗?父对象是浅层副本吗?我必须做一些事情来填充树属性?
我确实让它按照以下方式工作,但仍然想知道这是否是正确的方法.
ObjectId lastCommitId = repository.resolve(Constants.HEAD);
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(lastCommitId);
List<RevCommit> parents = new ArrayList<>();
for(RevCommit parent : commit.getParents()) {
RevCommit deepCopy = revWalk.parseCommit(parent.getId());
parents.add(deepCopy);
}
if ( parents.get(0).getTree() != null ) {
System.out.println(parents.get(0).getTree().toString());
} else …Run Code Online (Sandbox Code Playgroud) 我正在使用JGit并希望从远程存储库拉到我的本地存储库.
第一个approch是克隆存储库,并且工作正常:
CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, password);
try (Git result = Git.cloneRepository()
.setURI("http://172.20.1.2/team/myrepo.git")
.setDirectory(new File("c:\\temp\\gittest"))
.setCredentialsProvider(cp)
.call()) {
System.out.println("Having repository: " + result.getRepository().getDirectory());
}
Run Code Online (Sandbox Code Playgroud)
但是在第二次调用之后,不需要再次克隆存储库.所以我觉得我需要拉
Git git = Git.open(new File("c:\\temp\\gittest"));
git.pull().call();
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
org.eclipse.jgit.api.errors.TransportException: http://172.20.1.2/team/myrepo.git: Authentication is required but no CredentialsProvider has been registered
Run Code Online (Sandbox Code Playgroud)
我不知道在哪里可以传递pull命令的凭据.