tl;博士 JGit checkout在命令行git checkout正常工作时抛出异常
我目前正在尝试使用JGit从Java(用于工作)检查来自在线Git存储库的某些修订.我目前的方法是(我对Git很新,来自SVN背景,所以这可能是错的):
基本上,我希望能够将临时文件夹的内容与任何修订版交换.使用命令行界面我已经能够使用git checkout master和git checkout dylanbranch(其中dylanbranch是我在我自己的克隆上使用任意选择的版本制作的分支),但是我的Java代码试图做同样的事情失败:
Git git = Git.open(new File("checkout")); //checkout is the folder with .git
git.checkout().setName("master").call(); //succeeds
git.checkout().setName("dylanbranch").call(); //fails
Run Code Online (Sandbox Code Playgroud)
并且打印到控制台的例外:
Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: Checkout conflict with files:
src/sizzle
test/qunit
at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:211)
at com.avi.scm.git.BranchCheckout.main(BranchCheckout.java:30)
Caused by: org.eclipse.jgit.errors.CheckoutConflictException: Checkout conflict with files:
src/sizzle
test/qunit
at org.eclipse.jgit.dircache.DirCacheCheckout.checkout(DirCacheCheckout.java:387)
at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:162)
... 1 more
Run Code Online (Sandbox Code Playgroud)
我可以验证有问题的文件是否被标记为已删除,而不是通过使用进行提交,git status虽然我不确定为什么会有这些更改,并且它们会在我切换回主分支时返回.尽管如此,我仍然可以使用命令行Git成功更改工作树.
所以我的问题:为什么JGit在命令行git时不能用于这个应用程序?感谢任何其他有用的信息 - 教育我:)
更新我一直在使用jQuery存储库进行测试,并注意到JGit的一些问题:当我使用"master"分支时, …
我用java编写的git客户端遇到了一些困难.我正在使用jGit库通过git服务器上的ssh进行连接.问题是我不知道如何指定私钥的路径和密码.我在jGit文档中找不到有关我需要调用哪些函数的任何示例.从我读到的jGit库使用JSch使用ssh连接到服务器,JSch支持私钥和密码.有没有人有这方面的经验或有一些工作代码?
谢谢
一个JGit-初学者问题:
我使用JGit从存储库中读取文件(BLOB)并操纵其内容.之后,我想将具有相同文件名的新内容作为新提交写回存储库.但是如何使用JGit提交新内容?
我的伪代码:
String gitUrl = "path/to/repository/.git";
Repository repository = new FileRepository(gitUrl);
String filename = "test/seppl.txt";
blobId = getIdOf(filename);
ObjectLoader object = repository.open(blobId, Constants.OBJ_BLOB);
ObjectStream is = object.openStream();
String newContent = processStream(is);
// How to commit the newContent in filename?
Run Code Online (Sandbox Code Playgroud)
我是否必须将newContent文件写入文件并使用AddCommand和CommitCommand提交此文件?或者我可以使用相同的文件名将"即时"字符串写入存储库吗?
Web中是否有任何例子如何使用JGit进行提交?
我正在使用jgit安全地访问GitHub中的存储库.我做了以下操作来生成GitHub和我的客户端代码之间的安全通信密钥.
生成密钥对:
ssh-keygen -t rsa
Run Code Online (Sandbox Code Playgroud)使用帐户设置 - > SSH密钥 - >添加SSH密钥,将公钥添加到GitHub帐户
将步骤1中生成的私钥添加到本地主机:
ssh-add id_rsa
Run Code Online (Sandbox Code Playgroud)执行此操作后,当我尝试访问GitHub并进行克隆时,我仍然会收到以下错误:
org.eclipse.jgit.api.errors.TransportException: git@github.com:test/test_repo.git: UnknownHostKey: github.com. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码:
String localPath, remotePath;
Repository localRepo;
Git git;
localPath = <path_to_local_repository>;
remotePath = "git@github.com:test/test_repo.git";
try {
localRepo = new FileRepository(localPath + "/.git");
} catch (IOException e) {
e.printStackTrace();
}
git = new Git(localRepo);
CloneCommand cloneCmd = git.cloneRepository().
setURI(remotePath).
setDirectory(new File(localPath));
try {
cloneCmd.call();
} catch (GitAPIException e) {
log.error("git clone …Run Code Online (Sandbox Code Playgroud) 这个问题与这个问题相反:JGit如何从RevCom中获取SHA1?.
如果我将特定提交的SHA1 ID作为字符串给出,我如何在JGit中获取ObjectId或关联RevCommit?
这是一个可能的答案,它遍历所有RevCommits:
RevCommit findCommit(String SHAId)
{
Iterable<RevCommit> commits = git_.log().call();
for (RevCommit commit: commits)
{
if (commit.getName().equals(SHAId))
return commit;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
有没有比上面这个实现更好的东西?
我正在使用jGit克隆远程现有仓库,遵循指南:
我正在使用CFML作为我的例子:
Git = createObject( 'java', 'org.eclipse.jgit.api.Git' );
localPath = createObject( 'java', 'java.io.File' ).init( expandPath( 'temp' ) );
result = Git.cloneRepository()
.setURI( 'https://github.com/github/testrepo.git' )
.setDirectory( localPath )
.call();
result.close();
Run Code Online (Sandbox Code Playgroud)
克隆工作得很好,但在temp\.git\objects\pack我停止Java进程之前,文件锁不会在"pack"文件中发布.
然后我也注意到API文档对于结果.close()方法的行为似乎有些过于谨慎:http:
//download.eclipse.org/jgit/site/4.0.1.201506240215-r/apidocs/org/eclipse/jgit/ LIB/Repository.html#close()方法
减少使用次数,并可能关闭资源.
也许?那是什么意思?为了"放弃任何底层资源",我需要做什么,如AutoCloseable该.close()方法帮助实现的接口中指定的那样?
在SO上有几个类似的问题,但没有一个涉及使用静态方法org.eclipse.jgit.api.Git来克隆新的repo.
我使用以下代码从Java克隆git repo.我需要存储克隆的最新版本哈希.
localRepo = new FileRepository(path);
git = new Git(localRepo);
Git.cloneRepository().setURI(url).setBranch("master")
.setDirectory(new File(path)).call();
git.close();
Run Code Online (Sandbox Code Playgroud)
有关获取修订哈希的任何线索吗?
*在此问题的底部添加了最终工作计划*
我能够使用org.eclipse.egit.github.core java库向GitHub添加一个文件(代码示例来自这里:https://gist.github.com/Detelca/2337731)
但我无法给出像"folder1\myfile.txt"或"folder1\folder2\myfile.txt"这样的路径
我试图找到一个简单的例子来添加文件夹下的文件,但实际上无法找到它.
有关此示例的任何帮助吗?
下面是我正在使用的代码:(配置详细信息,如用户名,reponame在方法addFileToGH()中)
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.CommitUser;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.TypedResource;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;
public class GHFileWriter {
public static void main(String[] args) {
try {
new GHFileWriter().addFileToGH("myfile.txt", "some file content");
//new GHFileWriter().addFolderToGH("folder1");
} catch (IOException e) {
// TODO …Run Code Online (Sandbox Code Playgroud)