使用JGit对上一次提交进行文件差异

我正在尝试使用JGit来获取从上次提交到最近未提交的更改的文件差异。我该如何使用JGit?(使用命令行将是的输出git diff HEAD)

经过几次讨论(link1,link2),我附带了一段代码,该代码可以查找未提交的文件,但无法获取文件的区别

Repository db = new FileRepository("/path/to/git");

Git git = new Git(db);

AbstractTreeIterator oldTreeParser = this.prepareTreeParser(db, Constants.HEAD);

List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).call();

for (DiffEntry entry : diff) {

System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());

DiffFormatter formatter = new DiffFormatter(System.out);

formatter.setRepository(db);

formatter.format(entry);

}

更新

这个问题是很久以前的。我现有的for确实显示了未提交的代码。prepareTreeParser在显示差异的上下文中,我正在使用的当前代码是:

public void gitDiff() throws Exception {

Repository db = new FileRepository("/path/to/git" + DEFAULT_GIT);

Git git = new Git(db);

ByteArrayOutputStream out = new ByteArrayOutputStream();

DiffFormatter formatter = new DiffFormatter( out );

formatter.setRepository(git.getRepository());

AbstractTreeIterator commitTreeIterator = prepareTreeParser(git.getRepository(), Constants.HEAD);

FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );

List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );

for( DiffEntry entry : diffEntries ) {

System.out.println("DIFF Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());

formatter.format(entry);

String diffText = out.toString("UTF-8");

System.out.println(diffText);

out.reset();

}

git.close();

db.close();

// This code is untested. It is slighting different for the code I am using in production,

// but it should be very easy to adapt it for your needs

}

private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws Exception {

Ref head = repository.getRef(ref);

RevWalk walk = new RevWalk(repository);

RevCommit commit = walk.parseCommit(head.getObjectId());

RevTree tree = walk.parseTree(commit.getTree().getId());

CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();

ObjectReader oldReader = repository.newObjectReader();

try {

oldTreeParser.reset(oldReader, tree.getId());

} finally {

oldReader.release();

}

return oldTreeParser;

}

回答:

以下设置适用于我:

DiffFormatter formatter = new DiffFormatter( System.out );

formatter.setRepository( git.getRepository() );

AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(), Constants.HEAD );

FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );

List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );

for( DiffEntry entry : diffEntries ) {

System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() );

formatter.format( entry );

}

通过可以访问未提交的更改FileTreeIterator。使用formatter.scan()代替DiffCommand的优点是格式化程序已正确设置以处理FileTreeIterator。否则MissingObjectException,当格式化程序尝试从存储库中的工作树中查找更改时,您将得到。

以上是 使用JGit对上一次提交进行文件差异 的全部内容, 来源链接: utcz.com/qa/428742.html

回到顶部