如何在jgit中“cat”文件?

bvpmtnay  于 2021-07-07  发布在  Java
关注(0)|答案(7)|浏览(258)

不久前,我在寻找一个用java实现的可嵌入分布式版本控制系统,我想我在jgit中找到了它,它是git的纯java实现。但是,没有太多的示例代码或教程。
如何使用jgit检索某个文件的头版本(就像 svn cat 或者 hg cat 你能做什么?
我想这涉及到一些rev树遍历,我正在寻找一个代码示例。

rggaifut

rggaifut1#

我按照@thilo和@morisil的答案得到了与jgit1.2.0兼容的结果:

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

我没有测试java版本,但它应该可以工作。它是从

(.getBytes (.open repo (.getObjectId (TreeWalk/forPath repo "b/test.txt" tree) 0)))

在clojure中(遵循与顶部相同的设置),它确实起作用。

jucafojl

jucafojl2#

您可以按如下方式读取给定文件路径的内容。请注意,如果在给定的树中找不到路径,则treewalk可以为null。所以它需要一些特殊的处理。

public String readFile(RevCommit commit, String filepath) throws IOException {
    try (TreeWalk walk = TreeWalk.forPath(repo, filepath, commit.getTree())) {
        if (walk != null) {
            byte[] bytes = repo.open(walk.getObjectId(0)).getBytes();
            return new String(bytes, StandardCharsets.UTF_8);
        } else {
            throw new IllegalArgumentException("No path found.");
        }
    }
}

例如:

ObjectId head = repo.resolve(Constants.HEAD);
RevCommit last = repo.parseCommit(head);
readFile(last, "docs/README.md")

这个答案是用JGIT4.8.0编写的。

bwleehnv

bwleehnv3#

jgit教程中有一些信息(但这也没有真正的帮助,也不完整,而且可能已经过时了,因为他们切换到eclipse,那里还没有可用的文档)。

mnemlml8

mnemlml84#

我自己想出来的。api的级别相当低,但也不算太差:

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

izj3ouym5#

不幸的是,thilo的答案不适用于最新的jgitapi。以下是我找到的解决方案:

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)

我希望能简单一点。

zu0ti5jz

zu0ti5jz6#

我已经开始编写一个名为gitective的库,其中包含许多使用jgit处理blob、提交和树的帮助程序,并且是mit授权的,可以在github上使用。
获取头提交中的文件内容

Repository repo = new FileRepository("/repos/project/.git");
String content = BlobUtils.getHeadContent(repo, "src/Buffer.java");

获取分支上文件的内容

Repository repo = new FileRepository("/repos/project/.git");
String content = BlobUtils.getContent(repo, "master", "src/Buffer.java");

区分两个文件

Repository repo = new FileRepository("/repos/project/.git");
ObjectId current = BlobUtils.getId(repo, "master", "Main.java");
ObjectId previous = BlobUtils.getId(repo, "master~1", "Main.java");
Collection<Edit> edit = BlobUtils.diff(repo, previous, current);

自述文件中详细介绍了提供的更多实用程序示例。

ymzxtsji

ymzxtsji7#

下面是@morisil的答案的一个简单版本,使用了@directed laugh的一些概念,并用jgit2.2.0进行了测试:

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` 是在其他答案中创建的存储库对象。

相关问题