File repoDir = new File("test-git/.git");
// open the repository
Repository repo = new Repository(repoDir);
// find the HEAD
Commit head = repo.mapCommit(Constants.HEAD);
// retrieve the tree in HEAD
Tree tree = head.getTree();
// 1.2.0 api version here
// find a file (as a TreeEntry, which contains the blob object id)
TreeWalk treewalk = TreeWalk.forPath(repo, "b/test.txt", tree);
// use the blob id to read the file's data
byte[] data = repo.open(treewalk.getObjectId(0)).getBytes();
File repoDir = new File("test-git/.git");
// open the repository
Repository repo = new Repository(repoDir);
// find the HEAD
Commit head = repo.mapCommit(Constants.HEAD);
// retrieve the tree in HEAD
Tree tree = head.getTree();
// find a file (as a TreeEntry, which contains the blob object id)
TreeEntry entry = tree.findBlobMember("b/test.txt");
// use the blob id to read the file's data
byte[] data = repo.openBlob(entry.getId()).getBytes();
File repoDir = new File("test-git");
// open the repository
Repository repository = new Repository(repoDir);
// find the HEAD
ObjectId lastCommitId = repository.resolve(Constants.HEAD);
// now we have to get the commit
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(lastCommitId);
// and using commit's tree find the path
RevTree 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 either
InputStream in = loader.openStream()
// or
loader.copyTo(out)
private String fetchBlob(String revSpec, String path) throws MissingObjectException, IncorrectObjectTypeException,
IOException {
// Resolve the revision specification
final ObjectId id = this.repo.resolve(revSpec);
// Makes it simpler to release the allocated resources in one go
ObjectReader reader = this.repo.newObjectReader();
try {
// Get the commit object for that revision
RevWalk walk = new RevWalk(reader);
RevCommit commit = walk.parseCommit(id);
// Get the revision's file tree
RevTree tree = commit.getTree();
// .. and narrow it down to the single file's path
TreeWalk treewalk = TreeWalk.forPath(reader, path, tree);
if (treewalk != null) {
// use the blob id to read the file's data
byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
return new String(data, "utf-8");
} else {
return "";
}
} finally {
reader.close();
}
}
``` `repo` 是在其他答案中创建的存储库对象。
7条答案
按热度按时间rggaifut1#
我按照@thilo和@morisil的答案得到了与jgit1.2.0兼容的结果:
我没有测试java版本,但它应该可以工作。它是从
在clojure中(遵循与顶部相同的设置),它确实起作用。
jucafojl2#
您可以按如下方式读取给定文件路径的内容。请注意,如果在给定的树中找不到路径,则treewalk可以为null。所以它需要一些特殊的处理。
例如:
这个答案是用JGIT4.8.0编写的。
bwleehnv3#
jgit教程中有一些信息(但这也没有真正的帮助,也不完整,而且可能已经过时了,因为他们切换到eclipse,那里还没有可用的文档)。
mnemlml84#
我自己想出来的。api的级别相当低,但也不算太差:
izj3ouym5#
不幸的是,thilo的答案不适用于最新的jgitapi。以下是我找到的解决方案:
我希望能简单一点。
zu0ti5jz6#
我已经开始编写一个名为gitective的库,其中包含许多使用jgit处理blob、提交和树的帮助程序,并且是mit授权的,可以在github上使用。
获取头提交中的文件内容
获取分支上文件的内容
区分两个文件
自述文件中详细介绍了提供的更多实用程序示例。
ymzxtsji7#
下面是@morisil的答案的一个简单版本,使用了@directed laugh的一些概念,并用jgit2.2.0进行了测试: