如何使用JGit显示提交之间的更改 [英] How to show changes between commits with JGit
问题描述
我试图在文件的两次提交之间显示一个git diff。基本上,我是在 https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java
I am trying to show a git diff between two commits for a file. Basically, I did it as in https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java
您可以在 https://github.com/svenhornberg/JGitDiff <下查看我的完整代码/ a>
You can see my full code under 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 the two iterators to compute the diff between
ObjectReader reader = repository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
// finally get the list of changed files
List<DiffEntry> diffs= new Git(repository).diff()
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();
for (DiffEntry entry : diffs) {
System.out.println("Entry: " + entry);
}
System.out.println("Done");
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Adds and optionally commits fileContent to a repository
* @param commit True if file should be committed, False if not
* @throws Exception catch all for testing
*/
private void commitFileContent(byte[] fileContent, Repository repository, Git git, boolean commit) throws Exception {
File myfile = new File(repository.getDirectory().getParent(), "testfile");
myfile.createNewFile();
FileOutputStream fos = new FileOutputStream (myfile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(fileContent);
baos.writeTo(fos);
// run the add
git.add().addFilepattern("testfile").call();
if(commit) {
// and then commit the changes
git.commit().setMessage("Added fileContent").call();
}
fos.close();
}
/**
* Helperfunction from
* https://github.com/centic9/jgit-cookbook
*/
public static Repository createNewRepository() throws IOException {
// prepare a new folder
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
// create the directory
Repository repository = FileRepositoryBuilder.create(new File(
localPath, ".git"));
repository.create();
return repository;
}
}
代码会产生以下消息:
Printing diff between tree: null and AnyObjectId[c11a3a58c23b0dd6e541c4bcd553197772626bc6]
java.lang.NullPointerException
at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.index(UnpackedObjectCache.java:146)
at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.contains(UnpackedObjectCache.java:109)
at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache.isUnpacked(UnpackedObjectCache.java:64)
at org.eclipse.jgit.internal.storage.file.ObjectDirectory.openObject(ObjectDirectory.java:367)
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:145)
at org.eclipse.jgit.treewalk.CanonicalTreeParser.reset(CanonicalTreeParser.java:202)
at javadiff.RepoDiff.compare(RepoDiff.java:40)
at javadiff.GitDiff.main(GitDiff.java:30)
关注ng line必须为false,但它类似于jgit-cookbook中的示例
ObjectId oldHead = repository.resolve("HEAD^^^^{tree}"); //here is my nullpointer
我测试过 HEAD ^ 1 {Tree}
但这不起作用,看起来 {tree}
必须在字符串中才能解析树而不是提交ID。任何让它运作的想法?
I tested HEAD^1{Tree}
but this does not work, it looks like {tree}
must be in the string for resolving a tree instead of a commit id. Any ideas to get it working?
推荐答案
要获得头部提交树,请致电
To obtain the tree of the head commit, call
git.getRepository().resolve( "HEAD^{tree}" )
并获取HEAD提交的父树,请致电
and to obtain the tree of the parent of the HEAD commit, call
git.getRepository().resolve( "HEAD~1^{tree}" )
搜索'Git如果你对更多细节感兴趣,可以使用caret和tilde。
Search for 'Git caret and tilde' if you are interested in more details.
总结一下,这里有一个片段来计算两次提交的差异:
To summarize, here goes a snippet that computes the diff of two commits:
File file = new File( git.getRepository().getWorkTree(), "file.txt" );
writeFile( file, "first version" );
RevCommit newCommit = commitChanges();
writeFile( file, "second version" );
RevCommit oldCommit = commitChanges();
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
ObjectId oldTree = git.getRepository().resolve( "HEAD^{tree}" ); // equals newCommit.getTree()
oldTreeIter.reset( reader, oldTree );
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
ObjectId newTree = git.getRepository().resolve( "HEAD~1^{tree}" ); // equals oldCommit.getTree()
newTreeIter.reset( reader, newTree );
DiffFormatter df = new DiffFormatter( new ByteArrayOutputStream() );
df.setRepository( git.getRepository() );
List<DiffEntry> entries = df.scan( oldTreeIter, newTreeIter );
for( DiffEntry entry : entries ) {
System.out.println( entry );
}
private RevCommit commitChanges() throws GitAPIException {
git.add().addFilepattern( "." ).call();
return git.commit().setMessage( "commit message" ).call();
}
private static void writeFile( File file, String content ) throws IOException {
FileOutputStream outputStream = new FileOutputStream( file );
outputStream.write( content.getBytes( "UTF-8" ) );
outputStream.close();
}
有关显示提交之间更改的进一步注意事项,您可能需要阅读此内容 - 深入讨论JGit的差异API,可以在这里找到: http:// www .codeaffine.com / 2016/06/16 / jgit-diff /
For further considerations about showing changes between commits, you may want to read this in-depth discussion of JGit's diff APIs that can be found here: http://www.codeaffine.com/2016/06/16/jgit-diff/
这篇关于如何使用JGit显示提交之间的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!