从裸仓库获取子模块哈希 [英] Get submodule hash from bare repository

查看:64
本文介绍了从裸仓库获取子模块哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有客户定制应用程序的gitolite服务器.

We have a gitolite server with our customers custom apps.

每个应用程序都有一个子模块存储库/核心",该子模块引用了我们的基本应用程序.

Every app has a submodule "repository/core" which refers to our base app.

我们现在想创建一个仪表板,以显示我们所有客户的应用程序以及其核心版本.

We would now like to create a dashboard which shows all our customers apps and at which revision there core is at.

gitolite将所有内容存储在磁盘上的裸存储库中,并且仪表板应用程序可以直接访问存储库/或使用ssh键(如果更容易的话).

gitolite stores everything in bare repositories on disk and the dashboard app has direct access to the repos / or using ssh keys if that's easier.

我的问题是,我如何从一个裸仓库中找出子模块的修订版本,以及谁提交了该修订版本?

My question is how would I from a bare repository find out what revision a submodule is at, and who committed it?

推荐答案

JGit的SubmoduleStatusCommand列出了所有已知的子模块.请注意,该命令仅在非裸存储库上有效.

JGit has a SubmoduleStatusCommand that lists all known submodules. Note that this command works only on non-bare repositories.

Git git = Git.open( new File( "/path/to/repo/.git" ) );
Map<String,SubmoduleStatus> submodules = git.submoduleStatus().call();
SubmoduleStatus status = submodules.get( "repository/core" );
ObjectId headId = status.getHeadId();

如您所见,该命令返回子模块名称的映射以及它们各自的SubmoduleStatus,其中包括HEAD提交的SHA-1.

As you can see, the command returns a map of submodules-names along with their respective SubmoduleStatus which includes the SHA-1 of the HEAD commit.

还有一个我不久前写的有关JGit的子模块API的文章有更多详细信息.

There is also an article about JGit's submodule API that I wrote some time ago that has some more details.

对于裸存储库,您必须像这样直接从存储库的对象数据库中读取子模块的HEAD ID:

For a bare repository you would have to read the the submodule's HEAD ID directly from the repository's object database like so:

try( RevWalk revWalk = new RevWalk( repository ) ) {
  RevCommit headCommit = revWalk.parseCommit( repository.resolve( Constants.HEAD ) );
}
try( TreeWalk treeWalk = TreeWalk.forPath( repository, "repository/core", headCommit.getTree() ) ) {
  ObjectId blobId = treeWalk.getObjectId( 0 );
  ObjectLoader objectLoader = repository.open( blobId, Constants.OBJ_BLOB );
  try( InputStream inputStream = objectLoader.openStream() ) {
    // read contents from the input stream
  }
}

这篇关于从裸仓库获取子模块哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆