如何在JGit中"捕捉"文件?

Thi*_*ilo 27 java git jgit

前段时间我一直在寻找一个用Java编写的嵌入式分布式版本控制系统,我想我已经在JGit中找到了它,它是git的纯Java实现.但是,示例代码或教程的方式并不多.

如何使用JGit检索某个文件的HEAD版本(就像svn cat或者应该hg cat这样做)?

我想这涉及到一些转树走路,我正在寻找代码示例.

mor*_*sil 19

不幸的是,Thilo的答案不适用于最新的JGit API.这是我找到的解决方案:

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)
Run Code Online (Sandbox Code Playgroud)

我希望它更简单.

  • 谁在世界上设计了这个API? (35认同)
  • 你知道 treeWalk.getObjectId(nth) 的第 n 个值是做什么的吗?(即我们将值传递给 treeWalk.getObjectId 大于 0 的情况是什么?) (2认同)

cre*_*nig 16

这里是@ morisil答案的简单版本,使用@directed笑的一些概念并使用JGit 2.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.release();
    }
}
Run Code Online (Sandbox Code Playgroud)

repo 是在其他答案中创建的Repository对象.


dir*_*ugh 12

我跟着@ Thilo和@ morisil的回答得到了这个,与JGit 1.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();
Run Code Online (Sandbox Code Playgroud)

我没有测试Java版本,但它应该工作.它翻译自

(.getBytes (.open repo (.getObjectId (TreeWalk/forPath repo "b/test.txt" tree) 0)))
Run Code Online (Sandbox Code Playgroud)

在clojure中(遵循与顶部相同的设置),这确实有效.


Thi*_*ilo 5

自己弄清楚了.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();
Run Code Online (Sandbox Code Playgroud)

  • 这似乎是当前JGit版本的过时示例.API有所改变,请注意. (2认同)