Get submodule hash from bare repository

筅森魡賤 提交于 2020-01-24 11:28:50

问题


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 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?


回答1:


I just had to implement this for a CI server. The tricky part is remaining with a bare checkout.

My solution was as follows:

1) git show HEAD:.gitmodules can be used to get a list of paths which are submodules.

2) for each path =, the third field of this can be used to determine the SHA the submodule is at:

git ls-tree -z -d HEAD -- <submodule path>




回答2:


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();

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.

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

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
  }
}



回答3:


You can list the .gitmodules to find out which paths are deemed to be modules, and then go through them and find out what the <module>.git/HEAD/ reference is pointing to (probably a symbolic reference, like ref: refs/heads/master, which you'd then have to chain to find the value of refs/heads/master). That will then give you the most recent commit hash of that module.



来源:https://stackoverflow.com/questions/26018979/get-submodule-hash-from-bare-repository

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!