java中显示提交_如何使用JGit显示提交之间的更改

我试图显示文件的两次提交之间的git diff。基本上,我是按照https://github.com/centic9/jgit-

cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java中的说明进行操作的

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 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)

下面的行必须为false,但这类似于jgit-cookbook中的示例

ObjectId oldHead = repository.resolve("HEAD^^^^{tree}"); //here is my nullpointer

我测试了HEAD^1{Tree}一下,但是这行不通,看起来{tree}必须在字符串中才能解析树而不是提交ID。有什么想法可以使其正常工作吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值