获取已在git中执行特定提交的操作

时间:2016-05-03 09:02:37

标签: eclipse git jgit git-status

In TortoiseGit GUI,if we show log for a commit it is displaying the status as added since this file is added newly at that commit[![In this ,as i modified the file ,in status column it is showing as modified

在我的本地git存储库中,我有一个以上的文件提交。我如何获得状态/动作已经在该提交上执行。我的意思是,我怎么能知道特定的提交,是新添加的文件还是已被修改或被标记为..我想要正确的eclipse API到得到上述细节。

1 个答案:

答案 0 :(得分:0)

使用JGit获取git文件状态

        // find the HEAD
        ObjectId lastCommitId = existingRepo.resolve(Constants.HEAD);

        // a RevWalk allows to walk over commits based on some filtering that is defined
        try (RevWalk revWalk = new RevWalk(existingRepo)) {
            RevCommit commit = revWalk.parseCommit(lastCommitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();

            // now try to find a specific file
            try (TreeWalk treeWalk = new TreeWalk(existingRepo)) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create("TestProject/test1"));
                if (!treeWalk.next()) {
                    throw new IllegalStateException("Did not find expected file");
                }

                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = existingRepo.open(objectId);

                // and then one can the loader to read the file
                loader.copyTo(System.out);

                // Get status
                Git git = new Git(existingRepo);
                StatusCommand status = git.status().addPath(treeWalk.getPathString());
                System.out.println("Not Modified : " + status.call().getModified().isEmpty());
            }