不幸的是,Thilo的答案不适用于最新的JGit API。这是我找到的解决方案:
File repoDir = new File("test-git");// open the repositoryRepository repository = new Repository(repoDir);// find the HEADObjectId lastCommitId = repository.resolve(Constants.HEAD);// now we have to get the commitRevWalk revWalk = new RevWalk(repository);RevCommit commit = revWalk.parseCommit(lastCommitId);// and using commit's tree find the pathRevTree tree = commit.getTree();TreeWalk treeWalk = new TreeWalk(repository);treeWalk.addTree(tree);treeWalk.setRecursive(true);treeWalk.setFilter(PathFilter.create(path));if (!treeWalk.next()) { return null;}ObjectId objectId = treeWalk.getObjectId(0);ObjectLoader loader = repository.open(objectId);// and then one can use eitherInputStream in = loader.openStream()// orloader.copyTo(out)
我希望它更简单。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)