繁体   English   中英

jgit存储库浏览器

[英]jgit repository browser

我想用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仓库是一个没有工作副本的裸仓库。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM