0

我想用 jgit 创建一个 git 存储库浏览器。但我不知道如何获取文件的最后修改日期和最后提交消息。这是我当前的浏览器代码:

File directory = new File("/Users/sdorra/.scm/repositories/git/scm-git");
Repository repository =
  RepositoryCache.open(RepositoryCache.FileKey.lenient(directory,
    FS.DETECTED), true);

try
{
  ObjectId revId = repository.resolve(Constants.HEAD);
  DirCache cache = new DirCache(directory, FS.DETECTED);
  TreeWalk treeWalk = new TreeWalk(repository);

  treeWalk.addTree(new RevWalk(repository).parseTree(revId));
  treeWalk.addTree(new DirCacheIterator(cache));

  while (treeWalk.next())
  {
    System.out.println("---------------------------");
    System.out.append("name: ").println(treeWalk.getNameString());
    System.out.append("path: ").println(treeWalk.getPathString());

    ObjectLoader loader = repository.open(treeWalk.getObjectId(0));

    System.out.append("directory: ").println(loader.getType()
                      == Constants.OBJ_TREE);
    System.out.append("size: ").println(loader.getSize());
    // ???
    System.out.append("last modified: ").println("???");
    System.out.append("message: ").println("???");
  }
}
finally
{
  if (repository != null)
  {
    repository.close();
  }
}

有可能获得文件的最后一次提交吗?

注意:我的 git 存储库是一个没有工作副本的裸存储库。

4

1 回答 1

1

您使用的是较低级别的 JGit API,为什么不通过 org.eclipse.jgit.api 包使用 LogCommand?然后使用 addPath(...), call()...

之后,您应该获得指定路径的 RevCommit 列表。

于 2011-06-19T12:56:51.277 回答