I have a git repository which contains a sub-module and it is loaded to my local sandbox.
After updating the sub-module to the latest commit to which the remote repository is pointing too, I would like to find its latest commit SHA1 and the sub-module branch name.
While trying to fetch the branchName of the loaded sub-module the code always returns its SHA1 instead of the name.
// Method to find the localHeadCommit
public RevCommit fetchLocalHeadCommit(final String gitSubmoduleRootFolderPath) throws IOException {
try (Git gitSubmodule = Git.open(new File(resolveRootFolderPath(gitSubmoduleRootFolderPath)))) {
Repository repository = gitSubmodule.getRepository();
ObjectId localHeadObjId = repository.resolve("HEAD");
try (RevWalk revWalk = new RevWalk(repository, 0)) {
return revWalk.parseCommit(localHeadObjId);
}
}
// Method to find the submoduleBranchName
public String fetchBranchNameOfSubmodule(final String gitRootFolderPath) throws IOException {
return fetchLocalHeadCommit(gitRootFolderPath).getName();
}
I expect the output of fetchBranchNameOfSubmodule() to be
feature/SubmodulebrachName , but the actual output is
4f8664429af63c0309c0f063436b911845e3d3a43
1) Do we have any other option to find the branchName of the locally loaded submodule ?
2) Also, How to find the branchName if multiple branches are pointing to the same commitId?