java - jgit 存储库浏览器

标签 java git repository jgit

我想使用 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 列表。

关于java - jgit 存储库浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6395123/

相关文章:

java - 如何保护自己免受动态查询中的 SQL 注入(inject)攻击?

java - 反序列化时的 xstream 转换异常

Java cURL 与 HttpURLConnection 的替代方案

Git 告诉我我的分支没有跟踪信息,但是远程上有我的分支的旧版本

git - 来自 git 状态的 "fatal: Not a git repository (or any of the parent directories)"

java - 通过 ftp 发送数据库 - 获取不同的文件

git: 查找 `mine` 中不存在的最旧提交 `theirs`

ruby-on-rails - Github 和 heroku 上的应用程序 : Ignore file on one but not the other

repository - 无法在 Debian 上获取 go 语言存储库

git - 如何将gitorious迁移到gitlab仓库