我已经成功地掌握了jGit文件的基础知识,包括连接到repos以及添加,提交甚至循环文件的提交消息.
File gitDir = new File("/Users/myname/Sites/helloworld/.git");
RepositoryBuilder builder = new RepositoryBuilder();
Repository repository;
repository = builder.setGitDir(gitDir).readEnvironment()
.findGitDir().build();
Git git = new Git(repository);
RevWalk walk = new RevWalk(repository);
RevCommit commit = null;
// Add all files
// AddCommand add = git.add();
// add.addFilepattern(".").call();
// Commit them
// CommitCommand commit = git.commit();
// commit.setMessage("Commiting from java").call();
Iterable<RevCommit> logs = git.log().call();
Iterator<RevCommit> i = logs.iterator();
while (i.hasNext()) {
commit = walk.parseCommit( i.next() );
System.out.println( commit.getFullMessage() );
}
Run Code Online (Sandbox Code Playgroud)
我接下来要做的是能够获取单个文件的所有提交消息,然后能够将单个文件还原回特定的参考/时间点.
我正在尝试使用jgit的api使用以下代码执行git pull/push
org.eclipse.jgit.api.Git.open(theRepoFile).pull().call()
Run Code Online (Sandbox Code Playgroud)
但我得到例外
JSchException Auth fail
com.jcraft.jsch.Session.connect (Session.java:461)
org.eclipse.jgit.transport.JschConfigSessionFactory.getSession (JschConfigSessionFactory.java:116)
org.eclipse.jgit.transport.SshTransport.getSession (SshTransport.java:121)
org.eclipse.jgit.transport.TransportGitSsh$SshPushConnection.<init> (TransportGitSsh.java:306)
org.eclipse.jgit.transport.TransportGitSsh.openPush (TransportGitSsh.java:152)
org.eclipse.jgit.transport.PushProcess.execute (PushProcess.java:130)
org.eclipse.jgit.transport.Transport.push (Transport.java:1127)
org.eclipse.jgit.api.PushCommand.call (PushCommand.java:153)
Run Code Online (Sandbox Code Playgroud)
即使使用cgit pull and push工作.
我尝试检查SO以获取示例代码
但上面的问题没有提供一个完整的编码示例,说明使用通常通过ssh密钥进行身份验证的远程仓库执行git pull所需的操作.应该有一种方法来获取凭证信息~/.ssh/或等效的窗口.
何时尝试执行以下方法(使用JGIT库)
private void pullRepo() throws IOException,GitAPIException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException{
Git git = new Git(localRepo);
git.pull().call();
}
Run Code Online (Sandbox Code Playgroud)
我得到以下运行时异常:
org.eclipse.jgit.api.errors.NoHeadException: Pull on repository without HEAD currently not supported
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:161)
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个问题吗?我使用的localRepo与我用于cloneRepository方法的方法相同,完全正常.
谢谢,bgvv1983
假设以下代码片段被提交到Git存储库:
int test(){
int a = 3;
int b = 4;
int c = a + b;
return c;
}
Run Code Online (Sandbox Code Playgroud)
并在以后更新为
int test(){
return 7;
}
Run Code Online (Sandbox Code Playgroud)
我目前有一个方法,它使用JGit API来访问提交上述内容的Git存储库,并输出一个类似于以下内容的字符串:
int test(){
-int a = 3;
-int b = 4;
-int c = a + b;
-return c;
+return 7;
}
Run Code Online (Sandbox Code Playgroud)
现在,我的要求已经改变,并且只想知道更改行的行号.所以我想要以下内容:
2 -int a = 3;
3 -int b = 4;
4 -int c = a + b;
5 -return c;
2 +return 7;
Run Code Online (Sandbox Code Playgroud)
基本上,GitHub应用程序在进行更新时提供的信息相同.
任何帮助将不胜感激 :)
如何计算 - /+行的片段:
String …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用jgit连接到github使用指定的ssh密钥文件(即不在〜/ .ssh /中).
不幸的是,我不确定如何JschConfigSessionFactory正确使用.我尝试创建一个类似于本文中的设置:使用JGit键来安全地访问Git存储库
我使用git调用git.push().setRemote(remotePath).call();但是,我收到此错误(日志中省略了特定的存储库):
org.eclipse.jgit.api.errors.TransportException: https://github.com/user/repo: not authorized
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:160)
at gitio.GitInterface.pushToRemote(GitInterface.java:145)
at engine.GitInterfaceTester.main(GitInterfaceTester.java:25)
Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/user/repo: not authorized
at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:479)
at org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:396)
at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154)
at org.eclipse.jgit.transport.Transport.push(Transport.java:1173)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:156)
... 2 more
Run Code Online (Sandbox Code Playgroud)
我注意到JschConfigSessionFactory实际上没有实际调用自定义覆盖方法.这几乎肯定是问题的原因......但不知道为什么不调用它们; 我将自定义传递JschConfigSessionFactory给SshSessionFactory使用SshSessionFactory.setInstance(sessionFactory);
有人知道我做错了什么吗?
如何使用JGit获取存储库中的所有分支?我们来看一个示例存储库.我们可以看到,它有5个分支.
在这里我找到了这个例子:
int c = 0;
List<Ref> call = new Git(repository).branchList().call();
for (Ref ref : call) {
System.out.println("Branch: " + ref + " " + ref.getName() + " "
+ ref.getObjectId().getName());
c++;
}
System.out.println("Number of branches: " + c);
Run Code Online (Sandbox Code Playgroud)
但我得到的只是:
Branch: Ref[refs/heads/master=d766675da9e6bf72f09f320a92b48fa529ffefdc] refs/heads/master d766675da9e6bf72f09f320a92b48fa529ffefdc
Number of branches: 1
Branch: master
Run Code Online (Sandbox Code Playgroud) 我试图在文件的两次提交之间显示一个git diff.基本上,我这样做是为https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java
您可以在https://github.com/svenhornberg/JGitDiff下查看我的完整代码
public class RepoDiff {
public void compare(byte[] fileContentOld, byte[] fileContentNew) {
try {
Repository repository = createNewRepository();
Git git = new Git(repository);
// create the file
commitFileContent(fileContentOld, repository, git, true);
commitFileContent(fileContentNew, repository, git, false);
// The {tree} will return the underlying tree-id instead of the commit-id itself!
ObjectId oldHead = repository.resolve("HEAD^^^^{tree}"); //here is my nullpointer
ObjectId head = repository.resolve("HEAD^{tree}");
System.out.println("Printing diff between tree: " + oldHead + " and " + head);
// prepare …Run Code Online (Sandbox Code Playgroud) 有没有办法配置egit使用你的本机(OS)git而不是jgit实现?如果没有,有没有替代git Eclipse插件?
编辑#1 - 我应该注意,AWS CodeCommit使用来自.gitconfig的auth 的凭证助手:
[credential]
helper = !/usr/local/bin/aws --profile CodeCommitProfile codecommit credential-helper $@
UseHttpPath = true
Run Code Online (Sandbox Code Playgroud)
我猜这是CodeCommit特有的,而不是jgit.
我正在使用IntelliJ的注释功能在编辑器中查看最后一次更改文件中的行.
现在我使用JGit读取相同的注释,它们有所不同.对我来说,似乎Intellij检查提交之间的行没有被更改,仍然使用旧的提交消息.JGit没有看到它,因此发出了另一条消息.
任何人都可以确认JGit责备和IntelliJ的行为有何不同?是什么原因,我如何强制IntelliJ像JGit一样行事?也许IntelliJ忽略了空白变化?
我正在使用IntelliJ 15.0.1和JGit 4.1.1
一个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进行提交?