JGit Show remote changes like git diff --name-status master origin/master

孤者浪人 提交于 2019-12-24 05:14:23

问题


I'm trying a bit with JGIT. It's work very well, but I haven't found out how to show changes on a remote git before makeing a pull. In the terminal I solved my problem with the following steps:

  1. git fetch

  2. git diff --name-status master origin/master

How can I get the remote changes with JGit? Has anyone a idea?

I think the thread How do I do the equivalent of "git diff --name-status" with jgit? can help, but I don't know how to get the remote HEAD.


回答1:


I found out how to get the remote HEAD (FETCH_HEAD). Now my Code is as follows:

git.fetch().call();
Repository repo = git.getRepository();      
ObjectId fetchHead = repo.resolve("FETCH_HEAD^{tree}");
ObjectId head = repo.resolve("HEAD^{tree}");

ObjectReader reader = repo.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, head);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, fetchHead);
List<DiffEntry> diffs= git.diff().setShowNameAndStatusOnly(true)
                        .setNewTree(newTreeIter)
                        .setOldTree(oldTreeIter)
                        .call();
for(DiffEntry entry : diffs) {
    System.out.println(entry.toString());
}

It shows me remote changes like I wanted, but it works not correct. If I create a new File in my working-copy and commit the file (without to push), then the diff show me that the new file is deleted in the FETCH_HEAD. How can I filter this false delete messages?



来源:https://stackoverflow.com/questions/22435714/jgit-show-remote-changes-like-git-diff-name-status-master-origin-master

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!