简体   繁体   English

使用JGit对最后一次提交的文件差异

[英]File diff against the last commit with JGit

I am trying to use JGit to get the differences of a file from the last commit to the most recent uncommitted changes. 我正在尝试使用JGit来获取上次提交的文件与最近未提交的更改之间的差异。 How can I do this with JGit? 我怎么能用JGit做到这一点? (using the command line would be the output of git diff HEAD ) (使用命令行将是git diff HEAD的输出)

Following several discussions ( link1 , link2 ) I come with a piece of code that is able to find the files that are uncommited, but it I cannot get the difference of the files 经过几次讨论( link1link2 )后,我带来了一段代码,能够找到未提交的文件,但是我无法区分文件

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

}

UPDATE UPDATE

This issue was a long time ago. 这个问题很久以前了。 My existing for does display the uncommitted code. 我现有的用于显示未提交的代码。 The current code that I am using for prepareTreeParser , in the context of displaying the difference, is: 我在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;
}

The following setup works for me: 以下设置适用于我:

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

The uncommitted changes are made accessible trough the FileTreeIterator . 通过FileTreeIterator可以访问未提交的更改。 Using formatter.scan() instead of the DiffCommand has the advantage that the formatter is set up properly to handle the FileTreeIterator . 使用formatter.scan()而不是DiffCommand的优点是格式化器已正确设置以处理FileTreeIterator Otherwise you will get MissingObjectException s as the formatter tries to locate changes from the work tree in the repository. 否则,当格式化程序尝试从存储库中的工作树中查找更改时,您将获得MissingObjectException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM