Reading submodule status information in git

I have a script that checks out a repo, checks out submodules, and then prints some information about the submodules so the user knows which tags / branches they are dealing with.

The output on my workstation looks like this:

        Entering 'SubModuleA', 
        * feature/some_feature_branch,
        Branch is, 
        feature/some_feature_branch,
        Tag is, 
        undefined, 
        Entering 'SubModuleB'
        * (detached from 1.1.9)
        Branch is
        HEAD
        Tag is
        1.1.9^0

      

In this example, the master repo points to the tag branch of submodule A and the tag of submodule B.

But when I use the ability to run this code on other workstations or virtual machines, the result looks different:

        Entering 'SubModuleA', 
        * (detached from 1234abcd), 
        Branch is, 
        HEAD, 
        Tag is, 
        undefined, 
        Entering 'SubModuleB'
        * (detached from a1b23c4d)
        Branch is
        HEAD
        Tag is
        1.1.9^0

      

The code looks like this:

 git submodule foreach 'git branch | grep \* ; echo Branch is && git rev-parse --abbrev-ref HEAD ;  echo Tag is && git name-rev --tags --name-only $(git rev-parse HEAD) '

      

It's a bit of a jumble of other examples I've found to get this kind of information, but I'm having a hard time finding something that is consistent.

The command that is immediately before checking the status in the player,

git submodule init
git submodule update

      

I'm not sure why I have different results. Is there anything that will reliably print the branch name and submodule tag name sequentially?

+3


source to share


1 answer


I think the source of your confusion is that the only git information held regarding the state of a submodule is the commit id. Consider an existing repository with multiple branches, say this one:

Clone it:

$ git https://github.com/githubtraining/hellogitworld.git
$ cd hellogitworld

      

Now get the commit id of the named branch (say "master"):

$ git rev-parse master
ef7bebf8bdb1919d947afe46ab4b2fb4278039b3

      

Now, check that the commit ID is:

$ git checkout ef7bebf8bdb1919d947afe46ab4b2fb4278039b3

      



And run git status

:

$ git status
HEAD detached at ef7bebf
nothing to commit, working directory clean

      

As you can see, even if you checked out the commit ID that matches the named branch, git

doesn't know this (because you are passing the commit ID directly, rather than looking at it through the title).

This is exactly what happens if you clone a repository that contains submodules. Submodules are checked out with a commit id, not any named branch. This is actually a critical feature: it ensures that you always get the same message, even if a branch is promoted to a linked repository.

You can use the command git describe

to get the name of the branch in this case. For example:

$ git describe --all
heads/master

      

The flag --all

is required to get git describe

up to consider branch names as well as tags.

+2


source







All Articles